添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
悲伤的熊猫  ·  Write a NeXus HDF5 ...·  6 天前    · 
奔跑的书包  ·  博士申请 | ...·  1 年前    · 
previous |

Write a NeXus HDF5 file with plottable data

Building on the previous example, we wish to identify our measured data with the detector on the instrument where it was generated. In this hypothetical case, since the detector was positioned at some angle two_theta , we choose to store both datasets, two_theta and counts , in a NeXus group. One appropriate NeXus group is NXdetector . This group is placed in a NXinstrument group which is placed in a NXentry group. To support a default plot, we provide a NXdata group. Rather than duplicate the same data already placed in the detector group, we choose to link to those datasets from the NXdata group. (Compare the next figure with Linking in a NeXus file in the NeXus Design chapter of the NeXus User Manual.) The NeXus Design chapter provides a figure ( Linking in a NeXus file ) with a small variation from our previous example, placing the measured data within the /entry/instrument/detector group. Links are made from that data to the /entry/data group.

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"""
 3Writes a simple NeXus HDF5 file using h5py with links
 4according to the example from Figure 2.1 in the Design chapter
 5"""
 7from pathlib import Path
 9import numpy
11from nexusformat.nexus import (NXdata, NXdetector, NXentry, NXfield,
12                               NXinstrument, NXlink, nxopen)
14filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat")
15buffer = numpy.loadtxt(filename).T
16tthData = buffer[0]  # float[]
17countsData = numpy.asarray(buffer[1], "int32")  # int[]
19with nxopen("simple_example_write2.hdf5", "w") as f:  # create the HDF5 NeXus file
20    f["entry"] = NXentry()
21    f["entry/instrument"] = NXinstrument()
22    f["entry/instrument/detector"] = NXdetector()
24    # store the data in the NXdetector group
25    f["entry/instrument/detector/two_theta"] = NXfield(tthData, units="degrees")
26    f["entry/instrument/detector/counts"] = NXfield(countsData, units="counts")
28    f["entry/data"] = NXdata(NXlink("/entry/instrument/detector/counts"),
29                             NXlink("/entry/instrument/detector/two_theta"))
30    f["entry/data"].set_default()