Usage Example for the NeXus API

Below is the NeXus code for writing three data items into a NeXus file using the base NeXus API.

#include "napi.h"
int main()
{
    int counts[1000][50], n_t, n_p, dims[2], i;
    float t[1000], phi[50];
    NXhandle file_id;
/* Read in data using local routines */
    getdata (n_t, t, n_p, phi, counts);
/* Open output file and output global attributes */
    NXopen ('OUTPUT.HDF', NXACC_CREATE, &file_id);
    NXputattr (file_id, "user_name", "Joe Bloggs", 10, NX_CHAR);
/* Open top-level NXentry group */
    NXmakegroup (file_id, "Entry1", "NXentry");
    NXopengroup (file_id, "Entry1", "NXentry");
/* Open NXdata group within NXentry group */
    NXmakegroup (file_id, "Data1", "NXdata");
    NXopengroup (file_id, "Data1", "NXdata");
/* Output time channels */
    NXmakedata (file_id, "time_of_flight", NX_FLOAT32, 1, &n_t);
    NXopendata (file_id, "time_of_flight")
    NXputdata (file_id, t);
    NXputattr (file_id, "units", "microseconds", 12, NX_CHAR);
    i = 1;
    NXputattr (file_id, "axis", &i, 1, NX_INT32);
/* Output detector angles */
    NXmakedata (file_id, "phi", NX_FLOAT32, 1, &n_p);
    NXopendata (file_id, "phi")
    NXputdata (file_id, phi);
    NXputattr (file_id, "units", "degrees", 7, NX_CHAR);
    i = 2;
    NXputattr (file_id, "axis", &i, 1, NX_INT32);
/* Output data */
    dims[0] = n_t;
    dims[1] = n_p;
    NXmakedata (file_id, "counts", NX_INT32, 2, dims);
    NXopendata (file_id, "counts")
    NXputdata (file_id, counts);
    i = 1;
    NXputattr (file_id, "signal", &i, 1, NX_INT32);
/* Close NXentry and NXdata groups and close file */
    NXclosegroup (file_id);
    NXclosegroup (file_id);
    NXclose (&file_id);
    return;
}


File translated from TEX by TTH, version 0.9.