diff --git a/nx-class-documentation/html/examples/code_napi.html b/nx-class-documentation/html/examples/code_napi.html index 7586d0c17..f3f34f62b 100644 --- a/nx-class-documentation/html/examples/code_napi.html +++ b/nx-class-documentation/html/examples/code_napi.html @@ -244,30 +244,30 @@
The output from that code is given in the @@ -480,40 +482,42 @@
BasicWriter.py: Write a NeXus HDF5 file using Python with h5py
1#!/usr/bin/env python
- 2'''Writes a NeXus HDF5 file using h5py and numpy'''
+ 2"""Writes a NeXus HDF5 file using h5py and numpy"""
3
- 4import h5py # HDF5 support
+ 4import h5py # HDF5 support
5import numpy
6
7print("Write a NeXus HDF5 file")
@@ -211,47 +211,49 @@ 2.1.2.1. Writing the simplest data using
11# load data from two column format
12data = numpy.loadtxt("input.dat").T
13mr_arr = data[0]
-14i00_arr = numpy.asarray(data[1],'int32')
+14i00_arr = numpy.asarray(data[1], "int32")
15
16# create the HDF5 NeXus file
17f = h5py.File(fileName, "w")
18# point to the default data to be plotted
-19f.attrs['default'] = 'entry'
+19f.attrs["default"] = "entry"
20# give the HDF5 root some more attributes
-21f.attrs['file_name'] = fileName
-22f.attrs['file_time'] = timestamp
-23f.attrs['instrument'] = 'APS USAXS at 32ID-B'
-24f.attrs['creator'] = 'BasicWriter.py'
-25f.attrs['NeXus_version'] = '4.3.0'
-26f.attrs['HDF5_Version'] = h5py.version.hdf5_version
-27f.attrs['h5py_version'] = h5py.version.version
+21f.attrs["file_name"] = fileName
+22f.attrs["file_time"] = timestamp
+23f.attrs["instrument"] = "APS USAXS at 32ID-B"
+24f.attrs["creator"] = "BasicWriter.py"
+25f.attrs["NeXus_version"] = "4.3.0"
+26f.attrs["HDF5_Version"] = h5py.version.hdf5_version
+27f.attrs["h5py_version"] = h5py.version.version
28
29# create the NXentry group
-30nxentry = f.create_group('entry')
-31nxentry.attrs['NX_class'] = 'NXentry'
-32nxentry.attrs['default'] = 'mr_scan'
-33nxentry.create_dataset('title', data='1-D scan of I00 v. mr')
+30nxentry = f.create_group("entry")
+31nxentry.attrs["NX_class"] = "NXentry"
+32nxentry.attrs["default"] = "mr_scan"
+33nxentry.create_dataset("title", data="1-D scan of I00 v. mr")
34
35# create the NXentry group
-36nxdata = nxentry.create_group('mr_scan')
-37nxdata.attrs['NX_class'] = 'NXdata'
-38nxdata.attrs['signal'] = 'I00' # Y axis of default plot
-39nxdata.attrs['axes'] = 'mr' # X axis of default plot
-40nxdata.attrs['mr_indices'] = [0,] # use "mr" as the first dimension of I00
-41
-42# X axis data
-43ds = nxdata.create_dataset('mr', data=mr_arr)
-44ds.attrs['units'] = 'degrees'
-45ds.attrs['long_name'] = 'USAXS mr (degrees)' # suggested X axis plot label
-46
-47# Y axis data
-48ds = nxdata.create_dataset('I00', data=i00_arr)
-49ds.attrs['units'] = 'counts'
-50ds.attrs['long_name'] = 'USAXS I00 (counts)' # suggested Y axis plot label
-51
-52f.close() # be CERTAIN to close the file
+36nxdata = nxentry.create_group("mr_scan")
+37nxdata.attrs["NX_class"] = "NXdata"
+38nxdata.attrs["signal"] = "I00" # Y axis of default plot
+39nxdata.attrs["axes"] = "mr" # X axis of default plot
+40nxdata.attrs["mr_indices"] = [
+41 0,
+42] # use "mr" as the first dimension of I00
+43
+44# X axis data
+45ds = nxdata.create_dataset("mr", data=mr_arr)
+46ds.attrs["units"] = "degrees"
+47ds.attrs["long_name"] = "USAXS mr (degrees)" # suggested X axis plot label
+48
+49# Y axis data
+50ds = nxdata.create_dataset("I00", data=i00_arr)
+51ds.attrs["units"] = "counts"
+52ds.attrs["long_name"] = "USAXS I00 (counts)" # suggested Y axis plot label
53
-54print("wrote file:", fileName)
+54f.close() # be CERTAIN to close the file
+55
+56print("wrote file:", fileName)
BasicReader.py: Read a NeXus HDF5 file using Python with h5py
1#!/usr/bin/env python
- 2'''Reads NeXus HDF5 files using h5py and prints the contents'''
+ 2"""Reads NeXus HDF5 files using h5py and prints the contents"""
3
- 4import h5py # HDF5 support
+ 4import h5py # HDF5 support
5
6fileName = "prj_test.nexus.hdf5"
- 7f = h5py.File(fileName, "r")
+ 7f = h5py.File(fileName, "r")
8for item in f.attrs.keys():
9 print(item + ":", f.attrs[item])
-10mr = f['/entry/mr_scan/mr']
-11i00 = f['/entry/mr_scan/I00']
+10mr = f["/entry/mr_scan/mr"]
+11i00 = f["/entry/mr_scan/I00"]
12print("%s\t%s\t%s" % ("#", "mr", "I00"))
13for i in range(len(mr)):
14 print("%d\t%g\t%d" % (i, mr[i], i00[i]))
@@ -351,32 +353,31 @@ 2.1.2.1. Writing the simplest data using
subsection Version 3, for the details.)
reader_attributes_trail.py: Read a NeXus HDF5 file using Python with h5py
- 1
- 2import h5py
- 3
- 4with h5py.File("prj_test.nexus.hdf5", "r") as nx:
- 5 # find the default NXentry group
- 6 nx_entry = nx[nx.attrs["default"]]
- 7 # find the default NXdata group
- 8 nx_data = nx_entry[nx_entry.attrs["default"]]
- 9 # find the signal field
-10 signal = nx_data[nx_data.attrs["signal"]]
-11 # find the axes field(s)
-12 attr_axes = nx_data.attrs["axes"]
-13 if isinstance(attr_axes, (set, tuple, list)):
-14 # but check that attr_axes only describes 1-D data
-15 if len(attr_axes) == 1:
-16 attr_axes = attr_axes[0]
-17 else:
-18 raise ValueError(f"expected 1-D data but @axes={attr_axes}")
-19 axes = nx_data[attr_axes]
-20
-21 print(f"file: {nx.filename}")
-22 print(f"signal: {signal.name}")
-23 print(f"axes: {axes.name}")
-24 print(f"{axes.name} {signal.name}")
-25 for x, y in zip(axes, signal):
-26 print(x, y)
+ 1import h5py
+ 2
+ 3with h5py.File("prj_test.nexus.hdf5", "r") as nx:
+ 4 # find the default NXentry group
+ 5 nx_entry = nx[nx.attrs["default"]]
+ 6 # find the default NXdata group
+ 7 nx_data = nx_entry[nx_entry.attrs["default"]]
+ 8 # find the signal field
+ 9 signal = nx_data[nx_data.attrs["signal"]]
+10 # find the axes field(s)
+11 attr_axes = nx_data.attrs["axes"]
+12 if isinstance(attr_axes, (set, tuple, list)):
+13 # but check that attr_axes only describes 1-D data
+14 if len(attr_axes) == 1:
+15 attr_axes = attr_axes[0]
+16 else:
+17 raise ValueError(f"expected 1-D data but @axes={attr_axes}")
+18 axes = nx_data[attr_axes]
+19
+20 print(f"file: {nx.filename}")
+21 print(f"signal: {signal.name}")
+22 print(f"axes: {axes.name}")
+23 print(f"{axes.name} {signal.name}")
+24 for x, y in zip(axes, signal):
+25 print(x, y)
@@ -540,11 +541,11 @@ source code: externalExample.py
externalExample.py: Write using HDF5 external links
1#!/usr/bin/env python
- 2'''
+ 2"""
3Writes a NeXus HDF5 file using h5py with links to data in other HDF5 files.
4
5This example is based on ``writer_2_1``.
- 6'''
+ 6"""
7
8import h5py
9import numpy
@@ -553,21 +554,21 @@ source code: externalExample.py12FILE_HDF5_ANGLES = u"external_angles.hdf5"
13FILE_HDF5_COUNTS = u"external_counts.hdf5"
14
-15#---------------------------
+15# ---------------------------
16
17# get some data
18buffer = numpy.loadtxt("input.dat").T
-19tthData = buffer[0] # float[]
-20countsData = numpy.asarray(buffer[1],'int32') # int[]
+19tthData = buffer[0] # float[]
+20countsData = numpy.asarray(buffer[1], "int32") # int[]
21
22# put the angle data in an external (non-NeXus) HDF5 data file
23f = h5py.File(FILE_HDF5_ANGLES, "w")
24ds = f.create_dataset(u"angles", data=tthData)
25ds.attrs[u"units"] = u"degrees"
-26f.close() # be CERTAIN to close the file
+26f.close() # be CERTAIN to close the file
27
28
-29# put the detector counts in an external HDF5 data file
+29# put the detector counts in an external HDF5 data file
30# with *incomplete* NeXus structure (no NXdata group)
31f = h5py.File(FILE_HDF5_COUNTS, "w")
32nxentry = f.create_group(u"entry")
@@ -587,13 +588,13 @@ source code: externalExample.py46f = h5py.File(FILE_HDF5_MASTER, "w")
47f.attrs[u"default"] = u"entry"
48nxentry = f.create_group(u"entry")
-49nxentry.attrs[u"NX_class"] =u"NXentry"
+49nxentry.attrs[u"NX_class"] = u"NXentry"
50nxentry.attrs[u"default"] = u"data"
51nxdata = nxentry.create_group(u"data")
52nxdata.attrs[u"NX_class"] = u"NXdata"
53
54# link in the signal data
-55local_addr = '/entry/data/counts'
+55local_addr = "/entry/data/counts"
56external_addr = u"/entry/instrument/detector/counts"
57f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, external_addr)
58nxdata.attrs[u"signal"] = u"counts"
@@ -602,12 +603,14 @@ source code: externalExample.py61local_addr = u"/entry/data/two_theta"
62f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, u"/angles")
63nxdata.attrs[u"axes"] = u"two_theta"
-64nxdata.attrs[u"two_theta_indices"] = [0,]
-65
-66local_addr = u"/entry/instrument"
-67f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, u"/entry/instrument")
-68
-69f.close()
+64nxdata.attrs[u"two_theta_indices"] = [
+65 0,
+66]
+67
+68local_addr = u"/entry/instrument"
+69f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, u"/entry/instrument")
+70
+71f.close()
diff --git a/nx-class-documentation/html/examples/h5py/writer_1_3.html b/nx-class-documentation/html/examples/h5py/writer_1_3.html
index d8524dfa6..c90fc7fff 100644
--- a/nx-class-documentation/html/examples/h5py/writer_1_3.html
+++ b/nx-class-documentation/html/examples/h5py/writer_1_3.html
@@ -87,20 +87,20 @@ Navigation
command line (and there are no problems), the writer_1_3_h5py.hdf5
file is generated.
1#!/usr/bin/env python
- 2'''
+ 2"""
3Writes the simplest NeXus HDF5 file using h5py
4
5Uses method accepted at 2014NIAC
6according to the example from Figure 1.3
7in the Introduction chapter
- 8'''
+ 8"""
9
10import h5py
11import numpy
12
13buffer = numpy.loadtxt("input.dat").T
-14tthData = buffer[0] # float[]
-15countsData = numpy.asarray(buffer[1],'int32') # int[]
+14tthData = buffer[0] # float[]
+15countsData = numpy.asarray(buffer[1], "int32") # int[]
16
17f = h5py.File("writer_1_3.hdf5", "w") # create the HDF5 NeXus file
18# since this is a simple example, no attributes are used at this point
@@ -112,15 +112,17 @@ Navigation
24nxdata.attrs["NX_class"] = u"NXdata"
25nxdata.attrs[u"signal"] = u"counts"
26nxdata.attrs[u"axes"] = u"two_theta"
-27nxdata.attrs[u"two_theta_indices"] = [0,]
-28
-29tth = nxdata.create_dataset(u"two_theta", data=tthData)
-30tth.attrs[u"units"] = u"degrees"
-31
-32counts = nxdata.create_dataset(u"counts", data=countsData)
-33counts.attrs[u"units"] = u"counts"
-34
-35f.close() # be CERTAIN to close the file
+27nxdata.attrs[u"two_theta_indices"] = [
+28 0,
+29]
+30
+31tth = nxdata.create_dataset(u"two_theta", data=tthData)
+32tth.attrs[u"units"] = u"degrees"
+33
+34counts = nxdata.create_dataset(u"counts", data=countsData)
+35counts.attrs[u"units"] = u"counts"
+36
+37f.close() # be CERTAIN to close the file
One of the tools provided with the HDF5 support libraries is
diff --git a/nx-class-documentation/html/examples/h5py/writer_2_1.html b/nx-class-documentation/html/examples/h5py/writer_2_1.html
index 4ed7905f7..0603e77b5 100644
--- a/nx-class-documentation/html/examples/h5py/writer_2_1.html
+++ b/nx-class-documentation/html/examples/h5py/writer_2_1.html
@@ -81,17 +81,17 @@
Navigation
The Python code to build an HDF5 data file with that structure (using
numerical data from the previous example) is shown below.
1#!/usr/bin/env python
- 2'''
+ 2"""
3Writes a simple NeXus HDF5 file using h5py with links
4according to the example from Figure 2.1 in the Design chapter
- 5'''
+ 5"""
6
7import h5py
8import numpy
9
10buffer = numpy.loadtxt("input.dat").T
-11tthData = buffer[0] # float[]
-12countsData = numpy.asarray(buffer[1],'int32') # int[]
+11tthData = buffer[0] # float[]
+12countsData = numpy.asarray(buffer[1], "int32") # int[]
13
14f = h5py.File("writer_2_1.hdf5", "w") # create the HDF5 NeXus file
15f.attrs[u"default"] = u"entry"
@@ -117,21 +117,23 @@ Navigation
35nxdata.attrs[u"NX_class"] = u"NXdata"
36nxdata.attrs[u"signal"] = u"counts"
37nxdata.attrs[u"axes"] = u"two_theta"
-38nxdata.attrs[u"two_theta_indices"] = [0,]
-39
-40source_addr = u"/entry/instrument/detector/two_theta" # existing data
-41target_addr = u"two_theta" # new location
-42ds_tth.attrs[u"target"] = source_addr # a NeXus API convention for links
-43nxdata[target_addr] = f[source_addr] # hard link
-44# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)
-45
-46source_addr = u"/entry/instrument/detector/counts" # existing data
-47target_addr = u"counts" # new location
-48ds_counts.attrs[u"target"] = source_addr # a NeXus API convention for links
-49nxdata[target_addr] = f[source_addr] # hard link
-50# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)
-51
-52f.close() # be CERTAIN to close the file
+38nxdata.attrs[u"two_theta_indices"] = [
+39 0,
+40]
+41
+42source_addr = u"/entry/instrument/detector/two_theta" # existing data
+43target_addr = u"two_theta" # new location
+44ds_tth.attrs[u"target"] = source_addr # a NeXus API convention for links
+45nxdata[target_addr] = f[source_addr] # hard link
+46# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)
+47
+48source_addr = u"/entry/instrument/detector/counts" # existing data
+49target_addr = u"counts" # new location
+50ds_counts.attrs[u"target"] = source_addr # a NeXus API convention for links
+51nxdata[target_addr] = f[source_addr] # hard link
+52# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)
+53
+54f.close() # be CERTAIN to close the file
It is interesting to compare the output of the h5dump
diff --git a/nx-class-documentation/html/introduction.html b/nx-class-documentation/html/introduction.html
index 468c97375..862b71447 100644
--- a/nx-class-documentation/html/introduction.html
+++ b/nx-class-documentation/html/introduction.html
@@ -298,39 +298,69 @@
Navigation
Using Python to write a very simple NeXus HDF5 Data file
1#!/usr/bin/env python
- 2'''uses h5py to build the verysimple.nx5 data file'''
+ 2"""uses h5py to build the verysimple.nx5 data file"""
3
4import h5py
5
- 6angle = [18.9094, 18.9096, 18.9098, 18.91, 18.9102,
- 7 18.9104, 18.9106, 18.9108, 18.911, 18.9112,
- 8 18.9114, 18.9116, 18.9118, 18.912, 18.9122]
- 9diode = [1193, 4474, 53220, 274310, 515430, 827880,
-10 1227100, 1434640, 1330280, 1037070, 598720,
-11 316460, 56677, 1000, 1000]
-12
-13f = h5py.File('verysimple.nx5', 'w')
-14f.attrs['default'] = 'entry'
-15
-16nxentry = f.create_group('entry')
-17nxentry.attrs["NX_class"] = 'NXentry'
-18nxentry.attrs['default'] = 'data'
-19
-20nxdata = nxentry.create_group('data')
-21nxdata.attrs["NX_class"] = 'NXdata'
-22nxdata.attrs['signal'] = 'counts'
-23nxdata.attrs['axes'] = 'two_theta'
-24nxdata.attrs['two_theta_indices'] = [0,]
-25
-26tth = nxdata.create_dataset('two_theta', data=angle)
-27tth.attrs['units'] = 'degrees'
-28tth.attrs['long_name'] = 'two_theta (degrees)'
-29
-30counts = nxdata.create_dataset('counts', data=diode)
-31counts.attrs['units'] = 'counts'
-32counts.attrs['long_name'] = 'photodiode counts'
-33
-34f.close()
+ 6angle = [
+ 7 18.9094,
+ 8 18.9096,
+ 9 18.9098,
+10 18.91,
+11 18.9102,
+12 18.9104,
+13 18.9106,
+14 18.9108,
+15 18.911,
+16 18.9112,
+17 18.9114,
+18 18.9116,
+19 18.9118,
+20 18.912,
+21 18.9122,
+22]
+23diode = [
+24 1193,
+25 4474,
+26 53220,
+27 274310,
+28 515430,
+29 827880,
+30 1227100,
+31 1434640,
+32 1330280,
+33 1037070,
+34 598720,
+35 316460,
+36 56677,
+37 1000,
+38 1000,
+39]
+40
+41f = h5py.File("verysimple.nx5", "w")
+42f.attrs["default"] = "entry"
+43
+44nxentry = f.create_group("entry")
+45nxentry.attrs["NX_class"] = "NXentry"
+46nxentry.attrs["default"] = "data"
+47
+48nxdata = nxentry.create_group("data")
+49nxdata.attrs["NX_class"] = "NXdata"
+50nxdata.attrs["signal"] = "counts"
+51nxdata.attrs["axes"] = "two_theta"
+52nxdata.attrs["two_theta_indices"] = [
+53 0,
+54]
+55
+56tth = nxdata.create_dataset("two_theta", data=angle)
+57tth.attrs["units"] = "degrees"
+58tth.attrs["long_name"] = "two_theta (degrees)"
+59
+60counts = nxdata.create_dataset("counts", data=diode)
+61counts.attrs["units"] = "counts"
+62counts.attrs["long_name"] = "photodiode counts"
+63
+64f.close()