2.2.1.4. HDF5 in C with NAPI¶
Code examples are provided in this section that write 2-D data to a NeXus HDF5 file in the C language using the NAPI: NeXus Application Programmer Interface (frozen).
The following code reads a two-dimensional set counts
with dimension scales of t
and phi
using
local routines, and then writes a NeXus file containing a single
NXentry
group and a single NXdata
group.
This is the simplest data file that conforms to the NeXus standard.
2.2.1.4.1. NAPI C Example: write simple NeXus file¶
Note
This example uses the signal/axes attributes applied to the data field, as described in Associating plottable data by name using the axes attribute. New code should use the method described in Associating plottable data using attributes applied to the NXdata group.
1#include "napi.h"
2
3int main()
4{
5 int counts[50][1000], n_t=1000, n_p=50, dims[2], i;
6 float t[1000], phi[50];
7 NXhandle file_id;
8/*
9 * Read in data using local routines to populate phi and counts
10 *
11 * for example you may create a getdata() function and call
12 *
13 * getdata (n_t, t, n_p, phi, counts);
14 */
15/* Open output file and output global attributes */
16 NXopen ("NXfile.nxs", NXACC_CREATE5, &file_id);
17 NXputattr (file_id, "user_name", "Joe Bloggs", 10, NX_CHAR);
18/* Open top-level NXentry group */
19 NXmakegroup (file_id, "Entry1", "NXentry");
20 NXopengroup (file_id, "Entry1", "NXentry");
21/* Open NXdata group within NXentry group */
22 NXmakegroup (file_id, "Data1", "NXdata");
23 NXopengroup (file_id, "Data1", "NXdata");
24/* Output time channels */
25 NXmakedata (file_id, "time_of_flight", NX_FLOAT32, 1, &n_t);
26 NXopendata (file_id, "time_of_flight");
27 NXputdata (file_id, t);
28 NXputattr (file_id, "units", "microseconds", 12, NX_CHAR);
29 NXclosedata (file_id);
30/* Output detector angles */
31 NXmakedata (file_id, "polar_angle", NX_FLOAT32, 1, &n_p);
32 NXopendata (file_id, "polar_angle");
33 NXputdata (file_id, phi);
34 NXputattr (file_id, "units", "degrees", 7, NX_CHAR);
35 NXclosedata (file_id);
36/* Output data */
37 dims[0] = n_t;
38 dims[1] = n_p;
39 NXmakedata (file_id, "counts", NX_INT32, 2, dims);
40 NXopendata (file_id, "counts");
41 NXputdata (file_id, counts);
42 i = 1;
43 NXputattr (file_id, "signal", &i, 1, NX_INT32);
44 NXputattr (file_id, "axes", "polar_angle:time_of_flight", 26, NX_CHAR);
45 NXclosedata (file_id);
46/* Close NXentry and NXdata groups and close file */
47 NXclosegroup (file_id);
48 NXclosegroup (file_id);
49 NXclose (&file_id);
50 return;
51}