Skip to content

Commit ced895b

Browse files
committed
Add Python example for custom hierarchies
1 parent 74f642c commit ced895b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/14_custom_hierarchy.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import openpmd_api as io
3+
4+
5+
def main():
6+
s = io.Series("../samples/custom_hierarchy.bp", io.Access.create)
7+
it = s.write_iterations()[100]
8+
9+
# write openPMD part
10+
temp = it.meshes["temperature"]
11+
temp.axis_labels = ["x", "y"]
12+
temp.unit_dimension = {io.Unit_Dimension.T: 1}
13+
temp.position = [0.5, 0.5]
14+
temp.grid_spacing = [1, 1]
15+
temp.grid_global_offset = [0, 0]
16+
temp.reset_dataset(io.Dataset(np.dtype("double"), [5, 5]))
17+
temp[()] = np.zeros((5, 5))
18+
19+
# write NeXus part
20+
nxentry = it["Scan:NXentry"]
21+
nxentry.set_attribute("NX_class", "NXentry")
22+
nxentry.set_attribute("default", "data")
23+
24+
data = nxentry["data:Nxdata"]
25+
data.set_attribute("NX_class", "NXdata")
26+
data.set_attribute("signal", "counts")
27+
data.set_attribute("axes", ["two_theta"])
28+
data.set_attribute("two_theta_indices", [0])
29+
30+
counts = data.datasets()["counts"]
31+
counts.set_attribute("units", "counts")
32+
counts.set_attribute("long_name", "photodiode counts")
33+
counts.reset_dataset(io.Dataset(np.dtype("int"), [15]))
34+
counts[()] = np.zeros(15, dtype=np.dtype("int"))
35+
36+
two_theta = data.datasets()["two_theta"]
37+
two_theta.set_attribute("units", "degrees")
38+
two_theta.set_attribute("long_name", "two_theta (degrees)")
39+
two_theta.reset_dataset(io.Dataset(np.dtype("double"), [15]))
40+
two_theta[()] = np.zeros(15)
41+
42+
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)