DM_odm2numpy.py
## @package python.DM_odm2numpy
## @brief unkown python script prefix=autobuild.swdvlp64.opals.distro.demo
#
# converting an odm including all attributes into numpy objects
#
# The script uses the NumpyConverter functionality of pyDM. Coordinates and attributes are converted as separate numpy array
# and stored in a single dictionary object
# (script works in python 2 and 3)
#
from __future__ import print_function
from opals import pyDM
import sys
odm = "fullwave.odm" # odm test file
# pyDM.Datamanager.load parameter: filename(string), readOnly(bool) threadSafety(bool)
dm = pyDM.Datamanager.load(odm, True, False)
if not dm:
print("Unable to open ODM '" + odm + "'")
sys.exit(1)
#query attribute layout that is stored within the odm
stat = dm.getAddInfoStatistics()
dmLayout = stat.layout()
#output odm attribute layout
print("Attribute list of the ODM:")
for i in range(dmLayout.columns()):
name = dmLayout.name(i)
print(i,"\t", dmLayout.name(i)," "*(25-len(name)), dmLayout.type(i))
# build layout for attributes of interest (subset of odm attributes)
lf = pyDM.AddInfoLayoutFactory()
type, inDM = lf.addColumn(dm, "GPSTime", True); assert inDM == True
type, inDM = lf.addColumn(dm, "Amplitude", True); assert inDM == True
type, inDM = lf.addColumn(dm, "EchoWidth", True); assert inDM == True
layout = lf.getLayout()
print("Get odm points as numpy object...")
#create dictionary of numpy objects
numpyDict = pyDM.NumpyConverter.createNumpyDict(dm.sizePoint(),layout,True)
pointindex = dm.getPointIndex()
#fill numpy dictionary with all leafs of the ODM point index
rowIdx = 0
count = float(pointindex.sizeLeaf())
for idx,leaf in enumerate(pointindex.leafs()):
print("%5.1f%% finished" % (idx/count*100.) )
rowIdx += pyDM.NumpyConverter.fillNumpyDict(numpyDict,rowIdx,leaf)
print("100.0% finished.",rowIdx,"points have been converted")
# list all values
print("Output converted numpy arrays")
if sys.version_info >= (3, 0):
for attr, value in numpyDict.items():
print(attr,"->", value)
else:
for attr, value in numpyDict.iteritems():
print(attr,"->", value)
