1.2.3.3. Physical File format

This section describes how NeXus structures are mapped to features of the underlying physical file format. This is a guide for people who wish to create NeXus files without using the NeXus-API.

Choice of HDF as Underlying File Format

At its beginnings, the founders of NeXus identified the Hierarchical Data Format (HDF) as a capable and efficient multi-platform data storage format. HDF was designed for large data sets and already had a substantial user community. HDF was developed and maintained initially by the National Center for Supercomputing Applications (NCSA) at the University of Illinois at Urbana-Champaign (UIUC) and later spun off into its own group called The HDF Group (THG: http://www.hdfgroup.org/). Rather then developing its own unique physical file format, the NeXus group choose to build NeXus on top of HDF.

HDF (now HDF5) is provided with software to read and write data (this is the application-programmer interface, or API) using a large number of computing systems in common use for neutron and X-ray science. HDF is a binary data file format that supports compression and structured data.

Mapping NeXus into HDF

NeXus data structures map directly to HDF structures. NeXus groups are HDF5 groups and NeXus fields (or data sets) are HDF5 datasets. Attributes map directly to HDF group or dataset attributes. The NeXus class is stored as an attribute to the HDF5 group with the name NX_class with value of the NeXus class name. (For legacy NeXus data files using HDF4, groups are HDF4 vgroups and fields are HDF4 SDS (scientific data sets). HDF4 does not support group attributes. HDF4 supports a group class which is set with the Vsetclass() call and read with VGetclass().)

A NeXus link directly maps to the HDF hard link mechanisms.

Note

Examples are provided in the Examples of writing and reading NeXus data files chapter. These examples include software to write and read NeXus data files using the NAPI, as well as other software examples that use native (non-NAPI) libraries. In some cases the examples show the content of the NeXus data files that are produced. Here are links to some of the examples:

Perhaps the easiest way to view the implementation of NeXus in HDF5 is to view how the data structures look. For this, we use the h5dump command-line utility provided with the HDF5 support libraries. Short examples are provided for the basic NeXus data components:

  • group: created in C NAPI by:

    NXmakegroup (fileID, "entry", "NXentry");
    
  • field: created in C NAPI by:

    NXmakedata (fileID, "two_theta", NX_FLOAT32, 1, &n);
      NXopendata (fileID, "two_theta");
    NXputdata (fileID, tth);
    
  • attribute: created in C NAPI by:

    NXputattr (fileID, "units", "degrees", 7, NX_CHAR);
    
  • link created in C NAPI by:

    # --tba--
    # TODO: write some text about HDF5 hard links
    # until then, see the h5dump example below
    

See the sections NAPI Simple 2-D Write Example (C, F77, F90) and NAPI Python Simple 3-D Write Example in the Examples of writing and reading NeXus data files chapter for examples that use the native HDF5 calls to write NeXus data files.

h5dump of a NeXus NXentry group

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
GROUP "entry" {
  ATTRIBUTE "NX_class" {
     DATATYPE  H5T_STRING {
           STRSIZE 7;
           STRPAD H5T_STR_NULLPAD;
           CSET H5T_CSET_ASCII;
           CTYPE H5T_C_S1;
        }
     DATASPACE  SCALAR
     DATA {
     (0): "NXentry"
     }
  }
  # ... group contents
}

h5dump of a NeXus field (HDF5 dataset)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 DATASET "two_theta" {
    DATATYPE  H5T_IEEE_F64LE
    DATASPACE  SIMPLE { ( 31 ) / ( 31 ) }
    DATA {
    (0): 17.9261, 17.9259, 17.9258, 17.9256, 17.9254, 17.9252,
    (6): 17.9251, 17.9249, 17.9247, 17.9246, 17.9244, 17.9243,
    (12): 17.9241, 17.9239, 17.9237, 17.9236, 17.9234, 17.9232,
    (18): 17.9231, 17.9229, 17.9228, 17.9226, 17.9224, 17.9222,
    (24): 17.9221, 17.9219, 17.9217, 17.9216, 17.9214, 17.9213,
    (30): 17.9211
    }
    ATTRIBUTE "units" {
       DATATYPE  H5T_STRING {
             STRSIZE 7;
             STRPAD H5T_STR_NULLPAD;
             CSET H5T_CSET_ASCII;
             CTYPE H5T_C_S1;
          }
       DATASPACE  SCALAR
       DATA {
       (0): "degrees"
       }
    }
    # ... other attributes
 }

h5dump of a NeXus attribute

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ATTRIBUTE "axes" {
   DATATYPE  H5T_STRING {
         STRSIZE 9;
         STRPAD H5T_STR_NULLPAD;
         CSET H5T_CSET_ASCII;
         CTYPE H5T_C_S1;
      }
   DATASPACE  SCALAR
   DATA {
   (0): "two_theta"
   }
}

h5dump of a NeXus link