2.1.5. HDF5 in Fortran with NAPI

Code examples are provided in this section that write 2-D data to a NeXus HDF5 file in F77, and F90 languages 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.1.5.1. NAPI F77 Example: write simple NeXus file

Note

The F77 interface is no longer being developed.

 1       program WRITEDATA
 2       
 3       include 'NAPIF.INC'
 4       integer*4 status, file_id(NXHANDLESIZE), counts(1000,50), n_p, n_t, dims(2)
 5       real*4 t(1000), phi(50)
 6 
 7 !Read in data using local routines
 8       call getdata (n_t, t, n_p, phi, counts)
 9 !Open output file
10       status = NXopen ('NXFILE.NXS', NXACC_CREATE, file_id)
11         status = NXputcharattr 
12      +         (file_id, 'user', 'Joe Bloggs', 10, NX_CHAR)
13 !Open top-level NXentry group
14         status = NXmakegroup (file_id, 'Entry1', 'NXentry')
15         status = NXopengroup (file_id, 'Entry1', 'NXentry')
16 !Open NXdata group within NXentry group
17           status = NXmakegroup (file_id, 'Data1', 'NXdata')
18           status = NXopengroup (file_id, 'Data1', 'NXdata')
19 !Output time channels
20             status = NXmakedata 
21      +         (file_id, 'time_of_flight', NX_FLOAT32, 1, n_t)
22             status = NXopendata (file_id, 'time_of_flight')
23               status = NXputdata (file_id, t)
24               status = NXputcharattr 
25      +         (file_id, 'units', 'microseconds', 12, NX_CHAR)
26             status = NXclosedata (file_id)
27 !Output detector angles
28             status = NXmakedata (file_id, 'polar_angle', NX_FLOAT32, 1, n_p)
29             status = NXopendata (file_id, 'polar_angle')
30               status = NXputdata (file_id, phi)
31               status = NXputcharattr (file_id, 'units', 'degrees', 7, NX_CHAR)
32             status = NXclosedata (file_id)
33 !Output data
34             dims(1) = n_t
35             dims(2) = n_p
36             status = NXmakedata (file_id, 'counts', NX_INT32, 2, dims)
37             status = NXopendata (file_id, 'counts')
38               status = NXputdata (file_id, counts)
39               status = NXputattr (file_id, 'signal', 1, 1, NX_INT32)
40               status = NXputattr
41      +          (file_id, 'axes', 'polar_angle:time_of_flight', 26, NX_CHAR)
42             status = NXclosedata (file_id)
43 !Close NXdata and NXentry groups and close file
44           status = NXclosegroup (file_id)
45         status = NXclosegroup (file_id)
46       status = NXclose (file_id)
47 
48       stop
49       end

2.1.5.2. NAPI F90 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 program WRITEDATA
 2       
 3    use NXUmodule
 4 
 5    type(NXhandle) :: file_id
 6    integer, pointer :: counts(:,:)
 7    real, pointer :: t(:), phi(:)
 8 
 9 !Use local routines to allocate pointers and fill in data
10    call getlocaldata (t, phi, counts)
11 !Open output file
12    if (NXopen ("NXfile.nxs", NXACC_CREATE, file_id) /= NX_OK) stop
13    if (NXUwriteglobals (file_id, user="Joe Bloggs") /= NX_OK) stop
14 !Set compression parameters
15    if (NXUsetcompress (file_id, NX_COMP_LZW, 1000) /= NX_OK) stop
16 !Open top-level NXentry group
17    if (NXUwritegroup (file_id, "Entry1", "NXentry") /= NX_OK) stop
18    !Open NXdata group within NXentry group
19       if (NXUwritegroup (file_id, "Data1", "NXdata") /= NX_OK) stop
20    !Output time channels
21          if (NXUwritedata (file_id, "time_of_flight", t, "microseconds") /= NX_OK) stop
22    !Output detector angles
23          if (NXUwritedata (file_id, "polar_angle", phi, "degrees") /= NX_OK) stop
24    !Output data
25          if (NXUwritedata (file_id, "counts", counts, "counts") /= NX_OK) stop
26             if (NXputattr (file_id, "signal", 1) /= NX_OK) stop
27             if (NXputattr (file_id, "axes", "polar_angle:time_of_flight") /= NX_OK) stop
28    !Close NXdata group
29       if (NXclosegroup (file_id) /= NX_OK) stop
30 !Close NXentry group
31    if (NXclosegroup (file_id) /= NX_OK) stop
32 !Close NeXus file
33    if (NXclose (file_id) /= NX_OK) stop
34 
35 end program WRITEDATA