User and Programmers Guide to the Neutron Ray-Tracing Package McStas, version 3.8.6

B.2  Reading a data file into a vector/matrix (Table input, read_table-lib)

The read_table-lib provides functionalities for reading text (and binary) data files. To use this library, add a %include "read_table-lib" in your component definition DECLARE or SHARE section. Tables are structures of type t_Table (see read_table-lib.h file for details):

1/* t_Table structure (most important members) */ 
2double *data;     /* Use Table_Index(Table, i j) to get element [i,j] */ 
3long    rows;     /* number of rows */ 
4long    columns;  /* number of columns */ 
5char   *header;   /* the header with comments */ 
6char   *filename; /* file name or title */ 
7double  min_x;    /* minimum value of 1st column/vector */ 
8double  max_x;    /* maximum value of 1st column/vector */

Available functions to read a single vector/matrix are:

Available functions to read an array of vectors/matrices in a text file are:

The format of text files is free. Lines starting by ’# ; % /’ characters are considered to be comments, and stored in \(Table.header\). Data blocks are vectors and matrices. Block numbers are counted starting from 1, and changing when a comment is found, or the column number changes. For instance, the file ’MCSTAS/data/BeO.trm’ (Transmission of a Berylium filter) looks like:

1  # BeO transmission, as measured on IN12 
2  # Thickness: 0.05 [m] 
3  # [ k(Angs-1) Transmission (0-1) ] 
4  # wavevector multiply 
5  1.0500  0.74441 
6  1.0750  0.76727 
7  1.1000  0.80680 
8  ...

Binary files should be of type "float" (i.e. REAL*32) and "double" (i.e. REAL*64), and should not contain text header lines. These files are platform dependent (little or big endian).

The \(filename\) is first searched into the current directory (and all user additional locations specified using the -I option, see the ’Running McStas’ chapter in the User Manual), and if not found, in the data sub-directory of the MCSTAS library location. This way, you do not need to have local copies of the McStas Library Data files (see table 7.1).

A usage example for this library part may be:

1  t_Table Table;       // declare a t_Table structure 
2  char file[]="BeO.trm";  // a file name 
3  double x,y; 
4 
5  Table_Read(&Table, file, 1);  // initialize and read the first numerical block 
6  Table_Info(Table);           // display table informations 
7  ... 
8  x = Table_Index(Table, 2,5);  // read the 3rd row, 6th column element 
9                               // of the table. Indexes start at zero in C. 
10  y = Table_Value(Table, 1.45,1);  // look for value 1.45 in 1st column (x axis) 
11                               // and extract 2nd column value of that row 
12  Table_Free(&Table);          // free allocated memory for table

Additionally, if the block number (3rd) argument of Table_Read is 0, all blocks will be concatenated. The Table_Value function assumes that the ’x’ axis is the first column (index 0). Other functions are used the same way with a few additional parameters, e.g. specifying an offset for reading files, or reading binary data.

This other example for text files shows how to read many data blocks:

1  t_Table *Table;       // declare a t_Table structure array 
2  long     n; 
3  double y; 
4 
5  Table = Table_Read_Array("file.dat", &n); // initialize and read the all numerical block 
6  n = Table_Info_Array(Table);     // display informations for all blocks (also returns n) 
7 
8  y = Table_Index(Table[0], 2,5);  // read in 1st block the 3rd row, 6th column element 
9                                  // ONLY use Table[i] with i < n ! 
10  Table_Free_Array(Table);         // free allocated memory for Table

You may look into, for instance, the source files for Monochromator_curved or Virtual_input for other implementation examples.