DM_odm2numpy.py
1 ## @package python.demo.DM_odm2numpy
2 #
3 # converting an odm including all attributes into numpy objects
4 #
5 # The script uses the NumpyConverter functionality of pyDM. Coordinates and attributes are converted as separate numpy array
6 # and stored in a single dictionary object
7 # (script works in python 2 and 3)
8 #
9 from __future__ import print_function
10 
11 from opals import pyDM
12 import sys
13 
14 odm = "fullwave.odm" # odm test file
15 
16 # pyDM.Datamanager.load parameter: filename(string), readOnly(bool) threadSafety(bool)
17 dm = pyDM.Datamanager.load(odm, True, False)
18 if not dm:
19  print("Unable to open ODM '" + odm + "'")
20  sys.exit(1)
21 
22 #query attribute layout that is stored within the odm
23 stat = dm.getAddInfoStatistics()
24 dmLayout = stat.layout()
25 
26 #output odm attribute layout
27 print("Attribute list of the ODM:")
28 for i in range(dmLayout.columns()):
29  name = dmLayout.name(i)
30  print(i,"\t", dmLayout.name(i)," "*(25-len(name)), dmLayout.type(i))
31 
32 # build layout for attributes of interest (subset of odm attributes)
33 lf = pyDM.AddInfoLayoutFactory()
34 type, inDM = lf.addColumn(dm, "GPSTime", True); assert inDM == True
35 type, inDM = lf.addColumn(dm, "Amplitude", True); assert inDM == True
36 type, inDM = lf.addColumn(dm, "EchoWidth", True); assert inDM == True
37 layout = lf.getLayout()
38 
39 
40 print("Get odm points as numpy object...")
41 #create dictionary of numpy objects
42 numpyDict = pyDM.NumpyConverter.createNumpyDict(dm.sizePoint(),layout,True)
43 pointindex = dm.getPointIndex()
44 
45 #fill numpy dictionary with all leafs of the ODM point index
46 rowIdx = 0
47 count = float(pointindex.sizeLeaf())
48 for idx,leaf in enumerate(pointindex.leafs()):
49  print("%5.1f%% finished" % (idx/count*100.) )
50  rowIdx += pyDM.NumpyConverter.fillNumpyDict(numpyDict,rowIdx,leaf)
51 
52 print("100.0% finished.",rowIdx,"points have been converted")
53 
54 # list all values
55 print("Output converted numpy arrays")
56 if sys.version_info >= (3, 0):
57  for attr, value in numpyDict.items():
58  print(attr,"->", value)
59 else:
60  for attr, value in numpyDict.iteritems():
61  print(attr,"->", value)