DM_append_attribute.py
1 ## @package python.demo.DM_append_attribute
2 #
3 # example script showing how to append attribute to an existing
4 # OPALS datamanager using the Kernel/Processor concept
5 # (script works in python 2 and 3)
6 #
7 
8 from __future__ import print_function # print function syntax as in python 3
9 import sys
10 
11 from opals import pyDM
12 
13 
14 ##
15 # callback object computing an user-defined attribute '_dummy'
16 class Kernel(pyDM.KernelPoint):
17 
18  def __init__(self):
19  # initialize the base class (mandatory!)
20  if sys.version_info >= (3, 0):
21  super().__init__()
22  else:
23  super(Kernel, self).__init__()
24  return
25 
26  def tileFilter(self):
27  return None
28 
29  ##
30  # callback notifies about a changed leaf
31  def leafChanged(self, leaf):
32  print("leafChanged called")
33  return
34 
35  ##
36  # callback for processing a point
37  def process(self, pt):
38  # formula _dummy = Amplitude * EchoWidth
39  pt.info().set(0, pt.info().get(1) * pt.info().get(2))
40  # return True if the point was changed otherwise False
41  # this internally marks the current point leaf as changed (=needs to be written to disk)
42  return True
43 
44 
45 def DM_append_attribute(filename, maxOutput=1000):
46  # pyDM.Datamanager.load parameter: filename(string), readOnly(bool) threadSafety(bool)
47  dm = pyDM.Datamanager.load(filename, False, False)
48  if not dm:
49  print("Unable to open ODM '" + odm + "'")
50  sys.exit(1)
51 
52  # creates the appropriate attribute layout for processing
53  lf = pyDM.AddInfoLayoutFactory()
54  lf.addColumn(pyDM.ColumnType.float_, "_dummy") # column 0
55  lf.addColumn(pyDM.ColumnSemantic.Amplitude) # column 1
56  lf.addColumn(pyDM.ColumnSemantic.EchoWidth) # column 2
57  layout = lf.getLayout()
58 
59  print("Ready to append attribute '_dummy' to 'fullwave.odm'")
60 
61  if sys.version_info >= (3, 0):
62  input("Press Enter to continue...")
63  else:
64  raw_input("Press Enter to continue...")
65 
66  # create kernel object
67  k = Kernel()
68  # create processor
69  # pyDM.Processor parameter: dm(datamnager), layout, readOnlyView(bool)
70  processor = pyDM.Processor(dm, layout, False)
71  processor.run(k) # perform computation
72 
73  # save manager object
74  print("Save manager")
75  dm.save()
76 
77  print("done")
78 
79 if len(sys.argv) == 1:
80  print("ODM parameter missing")
81  sys.exit(-1)
82 
83 DM_append_attribute(sys.argv[1])