Skip to content

Commit

Permalink
deploy: 76b5242
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Nov 7, 2024
0 parents commit 4ceed2d
Show file tree
Hide file tree
Showing 7,953 changed files with 4,903,268 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Empty file added .nojekyll
Empty file.
Empty file added docs/.nojekyll
Empty file.
1 change: 1 addition & 0 deletions docs/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manual.nexusformat.org
1 change: 1 addition & 0 deletions docs/SPM_domain_app_def/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manual.nexusformat.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import numpy as np
from nexusformat.nexus import *


def write_nexus_file(fname, image, md={}):
"""
write the image to a NeXus HDF5 data file
Parameters
----------
fname : str
name of the file (relative or absolute) to be written
image : numpy array
the image data
md : dictionary
key: value where value is something that can be written by h5py
(such as str, int, float, numpy array, ...)
"""
nx = NXroot()
nx['/entry'] = NXentry(NXinstrument(NXdetector()))
nx['entry/instrument/detector/image'] = NXfield(image, units='counts',
compression='gzip')
nx['entry/data'] = NXdata()
nx['entry/data'].makelink(nx['entry/instrument/detector/image'])
nx['entry/data'].nxsignal = nx['entry/data/image']

if len(md) > 0:
# /entry/instrument/metadata (optional, for metadata)
metadata = nx['/entry/instrument/metadata'] = NXcollection()
for k, v in md.items():
metadata[k] = v

nx.save(fname, 'w')


if __name__ == "__main__":
"""demonstrate how to use this code"""
import epics
prefix = "13SIM1:"
img = epics.caget(prefix+"image1:ArrayData")
size_x = epics.caget(prefix+"cam1:ArraySizeX_RBV")
size_y = epics.caget(prefix+"cam1:ArraySizeY_RBV")
# edit the full image for just the binned data
img = img[:size_x*size_y].reshape((size_x, size_y))

extra_information = dict(
unique_id = epics.caget(prefix+"image1:UniqueId_RBV"),
size_x = size_x,
size_y = size_y,
detector_state = epics.caget(prefix+"cam1:DetectorState_RBV"),
bitcoin_value="15000",
)
write_nexus_file("example.h5", img, md=extra_information)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
entry:NXentry
@NX_class = "NXentry"
instrument:NXinstrument
@NX_class = "NXinstrument"
detector:NXdetector
@NX_class = "NXdetector"
counts:NX_INT32[31] = [1037, 1318, 1704, '...', 1321]
@units = "counts"
two_theta:NX_FLOAT64[31] = [17.92608, 17.92591, 17.92575, '...', 17.92108]
@units = "degrees"
@file = "external_angles.hdf5"
@path = "/angles"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://github.com/areaDetector/ADCore/blob/master/iocBoot/NDAttributes.xsd"
>
<Attribute name="AcquireTime" type="EPICS_PV" source="13SIM1:cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/>
<Attribute name="ImageCounter" type="PARAM" source="ARRAY_COUNTER" datatype="INT" description="Image counter"/>
<Attribute name="calc1_val" type="EPICS_PV" source="prj:userCalc1.VAL" datatype="DBR_NATIVE" description="some calculation result"/>
<Attribute name="calc2_val" type="EPICS_PV" source="prj:userCalc2.VAL" datatype="DBR_NATIVE" description="another calculation result"/>
<Attribute name="MaxSizeX" type="PARAM" source="MAX_SIZE_X" datatype="INT" description="Detector X size"/>
<Attribute name="MaxSizeY" type="PARAM" source="MAX_SIZE_Y" datatype="INT" description="Detector Y size"/>
<Attribute name="CameraModel" type="PARAM" source="MODEL" datatype="STRING" description="Camera model"/>
<Attribute name="CameraManufacturer" type="PARAM" source="MANUFACTURER" datatype="STRING" description="Camera manufacturer"/>
</Attributes>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
"""
Writes the simplest NeXus HDF5 file using h5py
Uses method accepted at 2014NIAC
according to the example from Figure 1.3
in the Introduction chapter
"""

from pathlib import Path

import numpy

from nexusformat.nexus import NXdata, NXentry, NXfield, nxopen

filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0]
countsData = numpy.asarray(buffer[1], "int32")

with nxopen("simple_example_write1.hdf5", "w") as f: # create the NeXus file
f["Scan"] = NXentry()
tth = NXfield(tthData, name="two_theta", units="degrees")
counts = NXfield(countsData, name="counts", units="counts")
f["Scan/data"] = NXdata(counts, tth)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
"""
Writes the simplest NeXus HDF5 file using h5py
Uses method accepted at 2014NIAC
according to the example from Figure 1.3
in the Introduction chapter
"""

from pathlib import Path
import h5py
import numpy

filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0] # float[]
countsData = numpy.asarray(buffer[1], "int32") # int[]

with h5py.File("simple_example_write1.hdf5", "w") as f: # create the HDF5 NeXus file
# since this is a simple example, no attributes are used at this point

nxentry = f.create_group("Scan")
nxentry.attrs["NX_class"] = "NXentry"

nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "counts"
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]

tth = nxdata.create_dataset("two_theta", data=tthData)
tth.attrs["units"] = "degrees"

counts = nxdata.create_dataset("counts", data=countsData)
counts.attrs["units"] = "counts"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function h5link(filename, from, to)
%H5LINK Create link to an HDF5 dataset.
% H5LINK(FILENAME,SOURCE,TARGET) creates an HDF5 link from the
% dataset at location SOURCE to a dataset at location TARGET. All
% intermediate groups in the path to target are created.
%
% Example: create a link from /hello/world to /goodbye/world
% h5create('myfile.h5','/hello/world',[100 200]);
% h5link('myfile.h5','/hello/world','/goodbye/world');
% hgdisp('myfile.h5');
%
% See also: h5create, h5read, h5write, h5info, h5disp

% split from and to into group/dataset
idx = strfind(from,'/');
from_path = from(1:idx(end)-1);
from_data = from(idx(end)+1:end);
idx = strfind(to,'/');
to_path = to(1:idx(end)-1);
to_data = to(idx(end)+1:end);

% open the HDF file
fid = H5F.open(filename,'H5F_ACC_RDWR','H5P_DEFAULT');

% create target group if it doesn't already exist
create_intermediate = H5P.create('H5P_LINK_CREATE');
H5P.set_create_intermediate_group(create_intermediate, 1);
try
H5G.create(fid,to_path,create_intermediate,'H5P_DEFAULT','H5P_DEFAULT');
catch
end
H5P.close(create_intermediate);

% open groups and create link
from_id = H5G.open(fid, from_path);
to_id = H5G.open(fid, to_path);
H5L.create_hard(from_id, from_data, to_id, to_data, 'H5P_DEFAULT','H5P_DEFAULT');

% close all
H5G.close(from_id);
H5G.close(to_id);
H5F.close(fid);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Scan:NXentry
@NX_class = "NXentry"
data:NXdata
@NX_class = "NXdata"
@axes = "two_theta"
@signal = "counts"
@two_theta_indices = [0]
counts:NX_INT32[31] = [1037, 1318, 1704, '...', 1321]
@units = "counts"
two_theta:NX_FLOAT64[31] = [17.92608, 17.92591, 17.92575, '...', 17.92108]
@units = "degrees"
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
"""
Writes a simple NeXus HDF5 file using h5py with links
according to the example from Figure 2.1 in the Design chapter
"""

from pathlib import Path
import h5py
import numpy

filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0] # float[]
countsData = numpy.asarray(buffer[1], "int32") # int[]

with h5py.File("simple_example_write2.hdf5", "w") as f: # create the HDF5 NeXus file
f.attrs["default"] = "entry"

nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "data"

nxinstrument = nxentry.create_group("instrument")
nxinstrument.attrs["NX_class"] = "NXinstrument"

nxdetector = nxinstrument.create_group("detector")
nxdetector.attrs["NX_class"] = "NXdetector"

# store the data in the NXdetector group
ds_tth = nxdetector.create_dataset("two_theta", data=tthData)
ds_tth.attrs["units"] = "degrees"
ds_counts = nxdetector.create_dataset("counts", data=countsData)
ds_counts.attrs["units"] = "counts"

# create the NXdata group to define the default plot
nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "counts"
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]

source_addr = "/entry/instrument/detector/two_theta" # existing data
target_addr = "two_theta" # new location
ds_tth.attrs["target"] = source_addr # a NeXus API convention for links
nxdata[target_addr] = f[source_addr] # hard link
# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)

source_addr = "/entry/instrument/detector/counts" # existing data
target_addr = "counts" # new location
ds_counts.attrs["target"] = source_addr # a NeXus API convention for links
nxdata[target_addr] = f[source_addr] # hard link
# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pathlib import Path
import h5py

filename = str(
Path(__file__).absolute().parent.parent
/ "simple_example_basic"
/ "simple_example_basic.nexus.hdf5"
)
with h5py.File(filename, "r") as nx:
# find the default NXentry group
nx_entry = nx[nx.attrs["default"]]
# find the default NXdata group
nx_data = nx_entry[nx_entry.attrs["default"]]
# find the signal field
signal = nx_data[nx_data.attrs["signal"]]
# find the axes field(s)
attr_axes = nx_data.attrs["axes"]
if isinstance(attr_axes, (set, tuple, list)):
# but check that attr_axes only describes 1-D data
if len(attr_axes) == 1:
attr_axes = attr_axes[0]
else:
raise ValueError(f"expected 1-D data but @axes={attr_axes}")
axes = nx_data[attr_axes]

print(f"file: {nx.filename}")
print(f"signal: {signal.name}")
print(f"axes: {axes.name}")
print(f"{axes.name} {signal.name}")
for x, y in zip(axes, signal):
print(x, y)
Loading

0 comments on commit 4ceed2d

Please sign in to comment.