DM_create.py
1 ## @package python.demo.DM_create
2 #
3 # Small example script showing how to create points and fill them
4 # into a new manager
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 random, sys
10 
11 from opals import pyDM
12 
13 # raw_input("Press Enter to continue...") # python 2
14 # input("Press Enter to continue...") # python 3
15 
16 
17 # create an attribute layout for generating points with the corresponding attributes
18 lf = pyDM.AddInfoLayoutFactory()
19 lf.addColumn(pyDM.ColumnSemantic.GPSTime)
20 lf.addColumn(pyDM.ColumnSemantic.Amplitude)
21 lf.addColumn(pyDM.ColumnSemantic.EchoWidth)
22 layout = lf.getLayout()
23 
24 # now create 100 random points
25 print("Generate 100 random points")
26 pts = []
27 for i in range(100):
28  pt = pyDM.Point(layout)
29  pt.x = random.uniform(-10, 10)
30  pt.y = random.uniform(-10, 10)
31  pt.z = random.uniform(-10, 10)
32  pt.info().set(0, 132 + i / 1000.) # GPSTime
33  pt.info().set(1, random.uniform(1, 342.3)) # Amplitude
34  pt.info().set(2, random.uniform(0.5, 20)) # EchoWidth
35  pts.append(pt)
36 
37 odm = "test.odm" # odm test file
38 
39 # write points to pyDM 'test.pyDM'
40 print("Write points into manager '" + odm + "'")
41 
42 # pyDM.Datamanager.create parameter: file, threadSafety(bool)
43 dm = pyDM.Datamanager.create(odm, False)
44 if not dm:
45  print("Unable to create ODM '" + odm + "'")
46  sys.exit(1)
47 
48 for pt in pts:
49  dm.addPoint(pt)
50 dm.save()
51 
52 # change a point
53 for p in dm.points():
54  pt = p.clone()
55  pt.x = pt.x + 13.4
56  pt.y = pt.y - 10.3
57  dm.replacePoint(pt)
58 dm.save()
59 
60 for p in dm.points():
61  print(p.x, p.y, p.z)
62 
63 # output some pyDM statistics
64 limit = dm.getLimit()
65 print("ODM contains", dm.sizePoint(), "points")
66 print("2D-limit (%.3f," % limit.xmin, "%.3f) -" % limit.ymin, "(%.3f," % limit.xmax, "%.3f)" % limit.ymax)