Skip to content

Commit

Permalink
Restructure python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
woutdenolf committed Jun 15, 2022
1 parent 2bb99a4 commit 1b882ca
Show file tree
Hide file tree
Showing 46 changed files with 720 additions and 688 deletions.
15 changes: 0 additions & 15 deletions manual/source/examples/h5py/BasicReader.py

This file was deleted.

54 changes: 0 additions & 54 deletions manual/source/examples/h5py/BasicWriter.py

This file was deleted.

52 changes: 35 additions & 17 deletions manual/source/examples/h5py/create_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,44 @@

# (re)build all data files and text descriptions of data files

#python BasicReader.py
#python TestReader.py
#python TestWriter.py
ORIGINAL_WD=$(pwd)
SCRIPT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

python BasicWriter.py
punx tree prj_test.nexus.hdf5 > prj_test.nexus_structure.txt
h5dump prj_test.nexus.hdf5 > prj_test.nexus_h5dump.txt
PUNX_REMOVE_HEADER="+5"

python externalExample.py
punx tree external_angles.hdf5 > external_angles_structure.txt
ROOT=$SCRIPT_ROOT/simple_example_basic
cd $ROOT
python $ROOT/simple_example_basic_write.py
punx tree $ROOT/simple_example_basic.nexus.hdf5 | tail -n $PUNX_REMOVE_HEADER > $ROOT/simple_example_basic.nexus_structure.txt
h5dump $ROOT/simple_example_basic.nexus.hdf5 > $ROOT/simple_example_basic.nexus_h5dump.txt
python $ROOT/simple_example_basic_read.py

ROOT=$SCRIPT_ROOT/simple_example_test
cd $ROOT
python simple_example_test_write.py
python simple_example_test_read.py
rm simple_example_test.nexus.hdf5

ROOT=$SCRIPT_ROOT/simple_example_write1
cd $ROOT
python simple_example_write1.py
punx tree simple_example_write1.hdf5 | tail -n $PUNX_REMOVE_HEADER > simple_example_write1_structure.txt
h5dump simple_example_write1.hdf5 > simple_example_write1_h5dump.txt

ROOT=$SCRIPT_ROOT/simple_example_write2
cd $ROOT
python simple_example_write2.py
punx tree simple_example_write2.hdf5 | tail -n $PUNX_REMOVE_HEADER > simple_example_write2_structure.txt
h5dump simple_example_write2.hdf5 > simple_example_write2_h5dump.txt

ROOT=$SCRIPT_ROOT/external_example_write
cd $ROOT
python external_example_write.py
punx tree external_angles.hdf5 | tail -n $PUNX_REMOVE_HEADER > external_angles_structure.txt
h5dump external_angles.hdf5 > external_angles_h5dump.txt
punx tree external_counts.hdf5 > external_counts_structure.txt
punx tree external_counts.hdf5 | tail -n $PUNX_REMOVE_HEADER > external_counts_structure.txt
h5dump external_counts.hdf5 > external_counts_h5dump.txt
punx tree external_master.hdf5 > external_master_structure.txt
punx tree external_master.hdf5 | tail -n $PUNX_REMOVE_HEADER > external_master_structure.txt
h5dump external_master.hdf5 > external_master_h5dump.txt

python writer_1_3.py
punx tree writer_1_3.hdf5 > writer_1_3_structure.txt
h5dump writer_1_3.hdf5 > writer_1_3_h5dump.txt

python writer_2_1.py
punx tree writer_2_1.hdf5 > writer_2_1_structure.txt
h5dump writer_2_1.hdf5 > writer_2_1_h5dump.txt
cd $ORIGINAL_WD
69 changes: 0 additions & 69 deletions manual/source/examples/h5py/externalExample.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
"""
Writes a NeXus HDF5 file using h5py with links to data in other HDF5 files.
This example is based on ``writer_2_1``.
"""

import os
import h5py
import numpy

FILE_HDF5_MASTER = "external_master.hdf5"
FILE_HDF5_ANGLES = "external_angles.hdf5"
FILE_HDF5_COUNTS = "external_counts.hdf5"

# ---------------------------

# get some data
filename = os.path.join(os.path.dirname(__file__), "..", "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0] # float[]
countsData = numpy.asarray(buffer[1], "int32") # int[]

# put the angle data in an external (non-NeXus) HDF5 data file
f = h5py.File(FILE_HDF5_ANGLES, "w")
ds = f.create_dataset("angles", data=tthData)
ds.attrs["units"] = "degrees"
f.close() # be CERTAIN to close the file


# put the detector counts in an external HDF5 data file
# with *incomplete* NeXus structure (no NXdata group)
f = h5py.File(FILE_HDF5_COUNTS, "w")
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxinstrument = nxentry.create_group("instrument")
nxinstrument.attrs["NX_class"] = "NXinstrument"
nxdetector = nxinstrument.create_group("detector")
nxdetector.attrs["NX_class"] = "NXdetector"
ds = nxdetector.create_dataset("counts", data=countsData)
ds.attrs["units"] = "counts"
# link the "two_theta" data stored in separate file
local_addr = nxdetector.name + "/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")
f.close()

# create a master NeXus HDF5 file
f = h5py.File(FILE_HDF5_MASTER, "w")
f.attrs["default"] = "entry"
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "data"
nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"

# link in the signal data
local_addr = "/entry/data/counts"
external_addr = "/entry/instrument/detector/counts"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, external_addr)
nxdata.attrs["signal"] = "counts"

# link in the axes data
local_addr = "/entry/data/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]

local_addr = "/entry/instrument"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, "/entry/instrument")

f.close()
Loading

0 comments on commit 1b882ca

Please sign in to comment.