From 15cc64d09c29f7c8e4ace550c115ff5a1bce4cc3 Mon Sep 17 00:00:00 2001 From: cow-bot Date: Fri, 5 Aug 2022 13:44:37 +0000 Subject: [PATCH] Update NeXus HTML documentation --- .../html/examples/code_napi.html | 20 +- .../html/examples/epics/index.html | 228 +++++++++--------- .../html/examples/h5py/index.html | 161 +++++++------ .../html/examples/h5py/writer_1_3.html | 28 ++- .../html/examples/h5py/writer_2_1.html | 40 +-- nx-class-documentation/html/introduction.html | 90 ++++--- 6 files changed, 304 insertions(+), 263 deletions(-) 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 @@

NAPI Python Example: write simple NeXus file 4import nxs 5import numpy 6 - 7a = numpy.zeros((2,3,4),dtype=numpy.int) + 7a = numpy.zeros((2, 3, 4), dtype=numpy.int) 8val = 0 9for i in range(2): 10 for j in range(3): 11 for k in range(4): -12 a[i,j,k] = val +12 a[i, j, k] = val 13 val = val + 1 14 15nf = nxs.open("simple3D.h5", "w5") 16 -17nf.makegroup("entry","NXentry") -18nf.opengroup("entry","NXentry") +17nf.makegroup("entry", "NXentry") +18nf.opengroup("entry", "NXentry") 19 -20nf.makegroup("data","NXdata") -21nf.opengroup("data","NXdata") -22nf.putattr("signal","test") +20nf.makegroup("data", "NXdata") +21nf.opengroup("data", "NXdata") +22nf.putattr("signal", "test") 23 -24nf.makedata("test",'int32',[2,3,4]) +24nf.makedata("test", "int32", [2, 3, 4]) 25nf.opendata("test") 26nf.putdata(a) 27nf.closedata() 28 -29nf.closegroup() # NXdata -30nf.closegroup() # NXentry +29nf.closegroup() # NXdata +30nf.closegroup() # NXentry 31 32nf.close() 33 diff --git a/nx-class-documentation/html/examples/epics/index.html b/nx-class-documentation/html/examples/epics/index.html index f796192fb..dded32abd 100644 --- a/nx-class-documentation/html/examples/epics/index.html +++ b/nx-class-documentation/html/examples/epics/index.html @@ -326,84 +326,86 @@

using the h5py package 2import h5py 3import datetime 4 - 5def write_nexus_file(fname, image, md={}): - 6 """ - 7 write the image to a NeXus HDF5 data file - 8 - 9 Parameters -10 ---------- -11 fname : str -12 name of the file (relative or absolute) to be written -13 image : numpy array -14 the image data -15 md : dictionary -16 key: value where value is something that can be written by h5py -17 (such as str, int, float, numpy array, ...) -18 """ -19 nexus = h5py.File(fname, "w") -20 nexus.attrs["filename"] = fname -21 nexus.attrs["file_time"] = str(datetime.datetime.now()) -22 nexus.attrs["creator"] = "write_nexus_file()" -23 nexus.attrs["H5PY_VERSION"] = h5py.__version__ -24 -25 # /entry -26 nxentry = nexus.create_group("entry") -27 nxentry.attrs["NX_class"] = "NXentry" -28 nexus.attrs["default"] = nxentry.name -29 -30 # /entry/instrument -31 nxinstrument = nxentry.create_group("instrument") -32 nxinstrument.attrs["NX_class"] = "NXinstrument" -33 -34 # /entry/instrument/detector -35 nxdetector = nxinstrument.create_group("detector") -36 nxdetector.attrs["NX_class"] = "NXdetector" -37 -38 # /entry/instrument/detector/image -39 ds = nxdetector.create_dataset("image", data=image, compression="gzip") -40 ds.attrs["units"] = "counts" -41 ds.attrs["target"] = "/entry/instrument/detector/image" -42 -43 # /entry/data -44 nxdata = nxentry.create_group("data") -45 nxdata.attrs["NX_class"] = "NXdata" -46 nxentry.attrs["default"] = nxdata.name -47 -48 # /entry/data/data --> /entry/instrument/detector/image -49 nxdata["data"] = nexus["/entry/instrument/detector/image"] -50 nxdata.attrs["signal"] = "data" -51 -52 if len(md) > 0: -53 # /entry/instrument/metadata (optional, for metadata) -54 metadata = nxinstrument.create_group("metadata") -55 metadata.attrs["NX_class"] = "NXcollection" -56 for k, v in md.items(): -57 try: -58 metadata.create_dataset(k, data=v) -59 except Exception: -60 metadata.create_dataset(k, data=str(v)) -61 -62 nexus.close() -63 -64 -65if __name__ == "__main__": -66 """demonstrate how to use this code""" -67 import epics -68 prefix = "13SIM1:" -69 img = epics.caget(prefix+"image1:ArrayData") -70 size_x = epics.caget(prefix+"cam1:ArraySizeX_RBV") -71 size_y = epics.caget(prefix+"cam1:ArraySizeY_RBV") -72 # edit the full image for just the binned data -73 img = img[:size_x*size_y].reshape((size_x, size_y)) -74 -75 extra_information = dict( -76 unique_id = epics.caget(prefix+"image1:UniqueId_RBV"), -77 size_x = size_x, -78 size_y = size_y, -79 detector_state = epics.caget(prefix+"cam1:DetectorState_RBV"), -80 bitcoin_value="15000", -81 ) -82 write_nexus_file("example.h5", img, md=extra_information) + 5 + 6def write_nexus_file(fname, image, md={}): + 7 """ + 8 write the image to a NeXus HDF5 data file + 9 +10 Parameters +11 ---------- +12 fname : str +13 name of the file (relative or absolute) to be written +14 image : numpy array +15 the image data +16 md : dictionary +17 key: value where value is something that can be written by h5py +18 (such as str, int, float, numpy array, ...) +19 """ +20 nexus = h5py.File(fname, "w") +21 nexus.attrs["filename"] = fname +22 nexus.attrs["file_time"] = str(datetime.datetime.now()) +23 nexus.attrs["creator"] = "write_nexus_file()" +24 nexus.attrs["H5PY_VERSION"] = h5py.__version__ +25 +26 # /entry +27 nxentry = nexus.create_group("entry") +28 nxentry.attrs["NX_class"] = "NXentry" +29 nexus.attrs["default"] = nxentry.name +30 +31 # /entry/instrument +32 nxinstrument = nxentry.create_group("instrument") +33 nxinstrument.attrs["NX_class"] = "NXinstrument" +34 +35 # /entry/instrument/detector +36 nxdetector = nxinstrument.create_group("detector") +37 nxdetector.attrs["NX_class"] = "NXdetector" +38 +39 # /entry/instrument/detector/image +40 ds = nxdetector.create_dataset("image", data=image, compression="gzip") +41 ds.attrs["units"] = "counts" +42 ds.attrs["target"] = "/entry/instrument/detector/image" +43 +44 # /entry/data +45 nxdata = nxentry.create_group("data") +46 nxdata.attrs["NX_class"] = "NXdata" +47 nxentry.attrs["default"] = nxdata.name +48 +49 # /entry/data/data --> /entry/instrument/detector/image +50 nxdata["data"] = nexus["/entry/instrument/detector/image"] +51 nxdata.attrs["signal"] = "data" +52 +53 if len(md) > 0: +54 # /entry/instrument/metadata (optional, for metadata) +55 metadata = nxinstrument.create_group("metadata") +56 metadata.attrs["NX_class"] = "NXcollection" +57 for k, v in md.items(): +58 try: +59 metadata.create_dataset(k, data=v) +60 except Exception: +61 metadata.create_dataset(k, data=str(v)) +62 +63 nexus.close() +64 +65 +66if __name__ == "__main__": +67 """demonstrate how to use this code""" +68 import epics +69 +70 prefix = "13SIM1:" +71 img = epics.caget(prefix + "image1:ArrayData") +72 size_x = epics.caget(prefix + "cam1:ArraySizeX_RBV") +73 size_y = epics.caget(prefix + "cam1:ArraySizeY_RBV") +74 # edit the full image for just the binned data +75 img = img[: size_x * size_y].reshape((size_x, size_y)) +76 +77 extra_information = dict( +78 unique_id=epics.caget(prefix + "image1:UniqueId_RBV"), +79 size_x=size_x, +80 size_y=size_y, +81 detector_state=epics.caget(prefix + "cam1:DetectorState_RBV"), +82 bitcoin_value="15000", +83 ) +84 write_nexus_file("example.h5", img, md=extra_information)

The output from that code is given in the @@ -480,40 +482,42 @@

using the nexusformat package17 (such as str, int, float, numpy array, ...) 18 """ 19 nx = NXroot() -20 nx['/entry'] = NXentry(NXinstrument(NXdetector())) -21 nx['entry/instrument/detector/image'] = NXfield(image, units='counts', -22 compression='gzip') -23 nx['entry/data'] = NXdata() -24 nx['entry/data'].makelink(nx['entry/instrument/detector/image']) -25 nx['entry/data'].nxsignal = nx['entry/data/image'] -26 -27 if len(md) > 0: -28 # /entry/instrument/metadata (optional, for metadata) -29 metadata = nx['/entry/instrument/metadata'] = NXcollection() -30 for k, v in md.items(): -31 metadata[k] = v -32 -33 nx.save(fname, 'w') -34 -35 -36if __name__ == "__main__": -37 """demonstrate how to use this code""" -38 import epics -39 prefix = "13SIM1:" -40 img = epics.caget(prefix+"image1:ArrayData") -41 size_x = epics.caget(prefix+"cam1:ArraySizeX_RBV") -42 size_y = epics.caget(prefix+"cam1:ArraySizeY_RBV") -43 # edit the full image for just the binned data -44 img = img[:size_x*size_y].reshape((size_x, size_y)) -45 -46 extra_information = dict( -47 unique_id = epics.caget(prefix+"image1:UniqueId_RBV"), -48 size_x = size_x, -49 size_y = size_y, -50 detector_state = epics.caget(prefix+"cam1:DetectorState_RBV"), -51 bitcoin_value="15000", -52 ) -53 write_nexus_file("example.h5", img, md=extra_information) +20 nx["/entry"] = NXentry(NXinstrument(NXdetector())) +21 nx["entry/instrument/detector/image"] = NXfield( +22 image, units="counts", compression="gzip" +23 ) +24 nx["entry/data"] = NXdata() +25 nx["entry/data"].makelink(nx["entry/instrument/detector/image"]) +26 nx["entry/data"].nxsignal = nx["entry/data/image"] +27 +28 if len(md) > 0: +29 # /entry/instrument/metadata (optional, for metadata) +30 metadata = nx["/entry/instrument/metadata"] = NXcollection() +31 for k, v in md.items(): +32 metadata[k] = v +33 +34 nx.save(fname, "w") +35 +36 +37if __name__ == "__main__": +38 """demonstrate how to use this code""" +39 import epics +40 +41 prefix = "13SIM1:" +42 img = epics.caget(prefix + "image1:ArrayData") +43 size_x = epics.caget(prefix + "cam1:ArraySizeX_RBV") +44 size_y = epics.caget(prefix + "cam1:ArraySizeY_RBV") +45 # edit the full image for just the binned data +46 img = img[: size_x * size_y].reshape((size_x, size_y)) +47 +48 extra_information = dict( +49 unique_id=epics.caget(prefix + "image1:UniqueId_RBV"), +50 size_x=size_x, +51 size_y=size_y, +52 detector_state=epics.caget(prefix + "cam1:DetectorState_RBV"), +53 bitcoin_value="15000", +54 ) +55 write_nexus_file("example.h5", img, md=extra_information) diff --git a/nx-class-documentation/html/examples/h5py/index.html b/nx-class-documentation/html/examples/h5py/index.html index 65fdd233b..c16af53a0 100644 --- a/nx-class-documentation/html/examples/h5py/index.html +++ b/nx-class-documentation/html/examples/h5py/index.html @@ -199,9 +199,9 @@

2.1.2.1. Writing the simplest data using

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)

@@ -275,16 +277,16 @@

2.1.2.1. Writing the simplest data using

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()