This page is a reproduction of the content found at http://cmms.triumf.ca/mud/mud_friendly.html. Though this is mainly to test the formatting of the markdown-to-html conversion, I have made minor changes throughout the text to improve readability.
Routines begin with MUD_ for applications written in C and fMUD_ for applications written in Fortran. This document refers to all routines as MUD_*, but all descriptions apply equally (unless stated otherwise) to the Fortran equivalents, fMUD_*. The MUD file’s data structures can be accessed directly using routines in both C and Fortran, but this document is concerned with the higher level “friendly” interface.
Routines come generally under two categories: those to read a file (MUD_*Read, MUD_get*, see Reading a data file) and those to write a file (MUD_*Write, MUD_set*, see Writing a data file). Each routine for performing some aspect of reading the file has an equivalent for writing. There is a seperate routine for each item to be read/written. A file may be opened for reading, writing, or both (modification). If you are programming in C, you may also need to use the routine MUD_pack (see Miscellaneous routines).
Prototyle Delcarations
The prototype declarations listed in this file are intended to give guidance to programming applications. The prototypes are declared in the include files:
For C, the type UINT32 is also defined in mud.h. It is used to ensure a 32-bit (4-byte) unsigned integer across architectures. The type REAL64 declares 64-bit (8-byte) floating-point. (Other data types will undoubtedly be used in the future as new MUD sections are added.) Strings for the C routines are zero-terminated.
In the Fortran prototypes displayed below, the parameters are named according to their type:
Parameters of type ? indicate an array that may be of several different types. Parameters ending in (?) indicate an array of unknown size. It is left to the programmer to ensure that the type and size of the arrays passed to these routines is sensible. Fortran character strings are returned padded with spaces.
Common Parameters
All routines except MUD_open* and MUD_pack return a boolean status value (0 for failure, 1 for success). The MUD_open* routines return a file handle, or -1 for failure. MUD_pack returns the number of bytes in the resultant array.
The file handle, fh, is a small integer returned by MUD_open*, and must be passed to other routines.
The MUD_get-string functions (for C) take an integer parameter (strdim) specifying the maximum acceptable string length. The returned string will have no more than strdim - 1 significant characters (plus the null terminator). The Fortran routines have no need for an explicit length parameter (the dimensioned length is passed implicitly).
Many routines take an integer parameter (num) indicating which of many instances is to be dealt with. For example, which histogram.
Time
All measures of time (single values or arrays) are stored in 32 bit integers as the number of seconds since 00:00:00 GMT, January 1, 1970. This is, or was, the same as used by the standard C library function time and stored in the C type definition time_t, but on newer systems time_t is 64-bit. Programmers should beware that MUD files continue to use 32-bit time values regardless of the local system’s time_t type. MUD’s time will run out in the year 2038.
Supported Platforms
The MUD library of routines has been written so that data files may be used on all supported platforms. All non-portable issues (byte ordering, floating-point representation) are handled by the library. The library has been built on the following platforms:
Opening the file is the first thing that must be done when reading a file, and closing is the last. The open routine returns a small integer identifier (“file handle”) or -1 for failure. The file handle needs to be passed to all of the other routines (parameter fh) to specify the file and data to be acted on. The file type is passed back in pType. Valid file types include (defined in the include file):
The close routine returns a boolean status.
Note that, in the present implementation, the entire file is read into memory by the MUD_openRead (or MUD_openReadWrite) operation. Any subsequent “get” operation is a simple look-up in the MUD structure. The file handle is therefore an index selecting among loaded MUD structures. The MUD_closeRead operation closes the file and releases the memory used to hold the MUD structure.
C routines:
Fortran routines:
See below for a description of MUD_openReadWrite, which is almost identical to MUD_openRead.
Reading the Run Description
The routine MUD_getRunDesc returns the run description type in pType. Valid types are (defined in mud.h):
C routines:
Not in TI-MuSR:
TI-MuSR only:
Fortran routines:
Not in TI-MuSR:
TI-MuSR only:
Reading the Comments
MUD data files contain a section for comments, but to date the feature has been underutilized. The routine MUD_getComments returns the type of the comment group in pType and the number of comments in pNum. The only valid type currently defined in mud.h is MUD_GRP_CMT_ID, in which comments are plain text. Should comments start being used, an enhanced type could be defined to support various text encoding, mark-up style (such as HTML), and (graphical) attachments.
C routines:
Fortran routines:
Reading the Histograms
The routine MUD_getHists returns the type of the histogram group in pType and the number of histograms in pNumHists; it does not get the histograms themselves. Valid types are (defined in the include file mud.h):
The routine MUD_getHistType tells the type of a particular histogram, typically MUD_SEC_TRI_TD_HIST_ID or MUD_SEC_TRI_TI_HIST_ID which equal the corresponding MUD_GRP_... codes.
The routine MUD_getHistSecondsPerBin stands out because it does not retrieve a separate item of data, but duplicates the function of MUD_getHistFsPerBin. Cases have arisen where the histogram bin size was not exactly represented as an integer number of femtoseconds, so scaled or coded values were saved. MUD_getHistSecondsPerBin is intended to always return the correct time per bin (in seconds).
Note that the routine MUD_getHistData returns the entire (unpacked) array in pData. For C usage, it is possible to access the original packed data array using MUD_getHistpData, which returns a pointer to the data which can be unpacked later - see Packing Histograms below. Note that a value of 0 (zero) in pBytesPerBin indicates a packed array. Arrays that are returned already unpacked by MUD_getHistData always have 4 bytes per bin. Make sure that your array pData is large enough.
C routines:
Fortran routines:
Reading the Scalers
The routine MUD_getScalers returns the type of the scaler group in pType and the number of scalers in pNumScal. Valid types are (defined in the include file):
The function MUD_getScalerLabel gives the scaler label for a particular numbered scaler, and MUD_getScalerCounts gives an array of two 4-byte integers: the scaler total, and the most recent rate.
C routines:
Fortran routines:
Reading the Independent Variables
The routine MUD_getIndVars returns the type of the independent variable group in pType and the number of independent variables in pNumIV. Valid types are (defined in mud.h):
The type MUD_GRP_GEN_IND_VAR_ID has statistics data only. The type MUD_GRP_GEN_IND_VAR_ARR_ID has statistics data, history data and possibly time data (time that the data point was taken, in seconds since 00:00 Jan. 1, 1970).
For history data, the data type is returned in pDataType, with values of 1 for integer, 2 for real and 3 for string. The number of history data points is returned in pNumData. The element size pElemSize is in bytes per element. The boolean value pHasTime indicates whether or not there is time data. It might be desireable to receive a pointer to the array data, using MUD_getIndVarpData and MUD_getIndVarpTimeData. In this case, it is up to the programmer to unpack the data (if necessary). Note that time data is never packed.
C routines:
For array data in MUD_GRP_GEN_IND_VAR_ARR_ID groups:
Fortran routines:
For array data in MUD_GRP_GEN_IND_VAR_ARR_ID groups:
Writing a Data File
Opening and Closing
Writing a data file can be handled in three different ways, where the best choice depends on the origin of the data.
If the data does not come from a MUD file, but will be written to one, then the destination file should first be opened with MUD_openWrite, all the data should be inserted using the MUD_set... functions, and then the file should be closed by MUD_closeWrite. The open routine returns an integer file handle (or -1 for failure) which is passed to all of the other routines (parameter fh) to specify the file for writing. The file type is defined by type. Valid file types include (defined in the include file):
The close routine returns a boolean status. Note that, in the present implementation, the file on disk is indeed opened by MUD_openWrite, but nothing is written to it until MUD_closeWrite writes out the entire MUD structure.
To modify an existing MUD file, it should be opened with MUD_openReadWrite, which is the same as MUD_openRead except for the file-access mode. There is no corresponding MUD_closeReadWrite function! Use MUD_closeWrite to re-write the MUD file, or MUD_closeRead to close the file, abandoning any changes that were made.
To write modifications of an existing MUD file to a different file, access the original data beginning with MUD_openRead, modify the data (MUD_set...), then write the altered data using MUD_closeWriteFile specifying the new file name to write.
C routines:
Fortran routines:
Writing the Run Description
The routine MUD_setRunDesc initializes a run description section of type type. This must be done before specifying any of the other parts of the run description. Valid types are:
C routines:
Not in TI-MuSR:
TI-MuSR only:
Fortran routines:
Not in TI-MuSR:
TI-MuSR only:
Writing the Comments
The routine MUD_setComments initializes a comment group of type type with numCom comments. This must be done before defining the comments. The only valid type is (defined in the include file): MUD_GRP_CMT_ID.
C routines:
Fortran routines:
Writing the Histograms
The routine MUD_setHists initializes a histogram group of type type with numHists histograms. This must be done before defining the individual histograms. Valid types are (defined in mud.h):
Note that you pass an array of 32 bit integers to the routine MUD_setHistData. This routine will pack the array, if necessary, depending on the previous definition of bytesPerBin. Note that a value of 0 (zero) in bytesPerBin indicates a packed array.
For C usage, it might be desireable to pass a pointer to the array using MUD_setHistpData. In this case, it is left to the programmer to pack the array (if necessary).
C routines:
Fortran routines:
Writing the Scalers
The routine MUD_setScalers intializes a scaler group of type type with numScal scalers. Valid types are (defined in the include file):
Note that scaler counts are passed in an array of two four-byte unsigned integers. The first element is the scaler total, and the second is the most recent rate.
C routines:
Fortran routines:
Writing the Independent Variables
The routine MUD_setIndVars initializes a independent variable group of type type with numIV independent variables. Valid types are (defined in the include file):
The type MUD_GRP_GEN_IND_VAR_ID has statistics data only. The type MUD_GRP_GEN_IND_VAR_ARR_ID has statistics data, history data and possibly time data (time that the data point was taken, in seconds since 00:00:00 GMT, January 1, 1970).
For history data, the data type is specified in dataType, with values of 1 for integer, 2 for real and 3 for string. The number of history data points is set in numData. The element size elemSize is in bytes per element. It might be desireable to pass a pointer to the array data, using MUD_setIndVarpData and MUD_setIndVarpTimeData. In this case, it is up to the programmer to pack the data (if necessary). Note that time data is never packed.
C routines:
For MUD_GRP_GEN_IND_VAR_ARR_ID groups:
Fortran routines:
For MUD_GRP_GEN_IND_VAR_ARR_ID groups:
Miscellaneous Routines
Packing/Unpacking Integer Histograms
MUD_pack is used to pack or unpack integer histogram data. This routine is not necessary in typical use involving the routines MUD_getData, MUD_getIndVarData, or their MUD_set companions, which do the packing/unpacking internally if necessary. This is always the case for Fortran access. MUD_pack may be necessary when using the routines MUD_getpData, MUD_getIndVarpData, or their set equivalents. Valid bin sizes are: 0, 1, 2, 4 (bytes per bin), where a packed array is indicated by a bin size of 0 (zero), and numBins is the number of bins. You must make sure that outArray has enough space for the resultant array.