Library Reference

From Madagascar
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

If you are programming with Madagascar, please use the utilities described below instead of creating your own equivalents. The RSF library is written in C, and all facilities offered by it are available directly to the C API via inclusion of header files and linking with the library. Other APIs only have access to a limited set of functions from the library. The ultimate guide to what the libraries provide is the file RSFSRC/filt/lib/SConstruct, which contains the instructions for building the RSF library in C and the interfaces to it for the various APIs.

C API

Macros

The following useful macros are accessible through the C and C++ APIs:

Functions
Name Value Meaning
SF_ABS(a) (a) >= 0  ? (a) : (-(a)) Absolute value
SF_MAX(a,b) (a) < (b) ? (b) : (a) Maximum of two values
SF_MIN(a,b) (a) < (b) ? (a) : (b) Minimum of two values
SF_SIG(a) (a) >= 0  ? 1  : -1 Sign function
General constants
Name Value Meaning
SF_EOL '\014' End-of-line ASCII character
SF_EOT '\004' End-of-transmission ASCII character
SF_EPS FLT_EPSILON The smallest X of type float such that 1.0 + X != 1.0. From float.h
SF_HUGE FLT_MAX The largest finite representable value of type float. From float.h
SF_MAX_DIM 9 Maximum number of dimensions in a RSF file
SF_PI 3.141592653589793 The number pi
SEG-Y-related constants
Name Value Meaning
SF_SEGY_FORMAT 24 Byte position in SEG-Y binary reel header, where info on encoding of data samples is kept (format parameter in sfsegyread)
SF_SEGY_NS 20 Byte position in SEG-Y binary reel header, where info on nr of samples in one trace is kept (ns parameter in sfsegyread)
SF_SEGY_DT 16 Byte position in SEG-Y binary reel header, where info on time sampling is kept
SF_EBCBYTES 3200 Bytes in the SEG-Y EBCDIC reel header
SF_BNYBYTES 400 Bytes in the SEG-Y binary reel header
SF_HDRBYTES 240 Bytes in each individual SEG-Y trace header
SF_NKEYS 71 Number of mandated header fields
SF_BHKEYS 27 Number of mandated binary fields

These macros were found with: <bash>grep '# define SF_' $RSFROOT/include/rsf.h</bash>

Data types

See the Data Types section of the Programming Reference Manual

Functions

See the Programming Reference Manual.

Function wrappers

These programs make the respective function callable from the command line, so that it can be used directly with files/other programs through "metaprograms" (shell/Python scripts). This allows the user to get the best of all worlds -- fast development in Python, all of Madagascar's visualization tools for debugging, and the speed of code written in C. This allows fast prototyping, followed by writing a production version in C calling the functions used.

Library function Program
sf_filedims filedims
sf_leftsize leftsize
sf_quantile quantile

C++ API

Macros

Same as for the C API.

Fortran 77 API

Fortran 90 API

The interface is accessed by including a use rsf statement in your program. The source code for the rsf module can be viewed in the repository. A (G) means that the entity was featured in the Guide to API.

Constants

For use with gettype and settype:

  • sf_char=1
  • sf_complex=4
  • sf_float=3 (G)
  • sf_int=2
  • sf_short=5
  • sf_uchar=0

For use with sf_seek:

  • sf_seek_set=0
  • sf_seek_cur=1
  • sf_seek_end=2

Data types

All procedures in Madagascar's F90 API act upon or take as input one of the following data types:

  • axa: Holds some info about a hypercube axis (integer::n; real::o,d)
  • file: Simply an interface to the sf_file type described in the C library.

Functions

Data types are indicated in italics and intent in bold italics. In the Wrapper for column, procedure names with no other description belong to the C library. The G? heading means "Demo-ed in the Guide to the F90 API?".

Name Arguments Result What it does Wrapper for G?
axisname integer in i, optional character in mystring character mystringi Given a string mystring and a positive number i, this function will return the concatenation of the string and the number converted to string (mystringi). In practice, mystring will probably be "n", "o", "d", "label" or "unit", in preparation for writing to a header file. Default value for mystring is "n". Verbose string operations in F90 N
dimension file in tag, integer out dims integer n Returns number of dimensions of already-open file handle tag in n, and the dimension values in dims sf_filedims N
filesize file in tag, optional integer in n integer nr_elements Computes number of n-dimensional subcubes in file hypercube. When n is absent, default is zero and the number of elements in file. sf_filesize, sf_leftsize. Y
gettype file in tag integer type Reads the binary data type from the already-open file handle tag. The type constant should be then compared in the user-written code with one of the six type constants provided by the F90 interface sf_gettype Y
rsf_input optional character in filename file tag Opens filename for reading and returns file handle tag. The default is filename="in", which corresponds to the standard input stream sf_input Y
rsf_output optional character in filename file tag Opens filename for writing and returns file handle tag. The default is filename="out", which corresponds to the standard output stream sf_input Y

Subroutines

Because subroutine names are preceded by a call statement, it is easy to find occurrences of them being used by typing: <bash>grep "call subroutine_name" {filt,user}/*/*.f90 user/*/*/*.f90</bash> in RSFSRC.

Below, data types are indicated in italics and intent in bold italics. The type multi means that multiple data types can be used (subroutine is overloaded). In the Wrapper for column, procedure names with no other description belong to the C library. The G? heading means "Demo-ed in the Guide to the F90 API?".

Name Arguments What it does Wrapper for G?
from_either file in tag, character in name, multi out value, optional multi in default This subroutine looks for the real or integer variable name first in the history file, then in the parameter table. If it it is not found and default is present, it assigns it to value, otherwise it terminates the program. sf_histint, sf_getint N
from_par optional file in tag, character in name, multi out value, optional multi in default If already-opened file handle tag is supplied, then it reads into value a parameter designated by name from a history file. Else, it reads it from the parameter table. If the parameter is not found and a default is present, then that is used, else ends with failure. It can read from both history files and parameter tables the following types: integer, integer array, real. Strings can be read only from the history file, and not from the parameter table. Real arrays, logicals and logical arrays can be read only from the parameter table. sf_histint, sf_getint, sf_histints, sf_getints, sf_histfloat, sf_getfloat, sf_histstring, sf_getfloats, sf_getbool, sf_getbools Y
iaxa file in tag, axa out axisname, integer in axisnr Read axis with number axisnr as axis axisname from the already-open file header indicated by tag Multiple calls to F90 API's from_par N
oaxa file in tag, axa in axisname, integer in axisnr Write axis axisname (i.e. its n, o, d) as axis with number axisnr to the already-open file header indicated by tag Multiple calls to F90 API's from_par N
raxa axa in axisname unformatted write to I/O unit nr. 0 (usually stdout, but system-dependent) of the contents of the axisname structure (n, o, d) F90 write statement N
rsf_read file in tag, multi out array, optional integer in n If n is specified, the real or complex array must be 1-D and n elements will be read into it from an already-opened file specified by tag. If n is not specified, reads enough to fill the array, and the array can be 1-D, 2-D, 3-D, 4-D or 5-D. sf_floatread, sf_complexread Y
rsf_write file in tag, multi in array, optional integer in n If n is specified, real or complex array must be 1-D and n elements will be written to an already-opened file specified by tag. If n is not specified, writes to file the entire contents of the array, and the array can be 1-D, 2-D, 3-D, 4-D or 5-D sf_floatwrite, sf_complexwrite Y
settype file in tag, integer in file_type Write a type for the binary data to the already-open file header indicated by tag. Data type constants are also provided by the F90 API. sf_settype N
to_par file in tag, character in name, multi in value Writes name=value to already-opened file specified by tag. Value can be integer, integer array, real or character. sf_putint, sf_putints, sf_putfloat, sf_putstring N

Functions from the C library

Functions from the C library for which an interface has been defined at the end of RSFSRC/api/f90/fortran.c can be called directly from Madagascar F90 programs. The programmer must know, however, that an argument type mismatch will result in a silent failure, with no informative errors. There may be other sources of problems as well. Caution therefore must be advised when calling these functions. The reason for these issues is that if a FORTRAN90 routine needs to pass a scalar parameter to a C routine, there is no standard way to ensure that a value is passed rather than an address. The success rate of calling C from Fortran is compiler-dependent and special types may have to be defined for portability. If you do have to call a C function from F90, follow the examples of the functions below. Use the C library reference to find argument and output types. A C-to-F90 data type dictionary follows:

C F90
RSFFILE type(file)
INT integer
OFFSETT integer(kind=OFFKIND)

Void-output functions callable as subroutines

These can be treated as subroutines, and accessed with a call statement. Follow

  • sf_error (G)
  • sf_init (G) (Can it also be called in subroutines if we want to isolate I/O operations there?)
  • sf_seek

Non-void-output functions

It is a good idea to declare in your program the function output type and use the keyword external, i.e.:

real, external :: sf_dosomething

Alternately, you may define a F90 interface block for the procedure(s).

Several examples can be found in RSFSRC/filt/lib/rsf.f90, but they are not enumerated here because F90 wrappers exist for them and they should be used instead of the direct calls to the C library functions.

Matlab API

Data types are indicated in italics. In the Wrapper for column, procedure names with no other description belong to the C library. The G? heading means: "Demo-ed in the Guide to the Matlab API?"

Name Arguments What it does Wrapper for G?
rsf_create Arg 1: string (row vector) new_file_name;
Arg 2: string (row vector) old_file_name, or double (column vector) in new_file_cube_dims
Writes to disk RSF header of native_float hypercube of desired dimensions. Takes as input the name of the new file and either the name of another file with a cube of the same size, or a column with cube dimensions sf_fileflush, or sf_putint and sf_setformat Y
rsf_dim Arg1: string (row vector) filename Returns a 9-element vector [n1, n2, ... n9] with the dimensions of the hypercube as described in the RSF header filename. Missing dimensions are given the value 1 sf_filedims Y
rsf_par Arg 1: string (row vector) filename;
Arg 2: string (row vector) par_name;
Arg 3: string (row vector) par_type: i[nt], l[ogical], f[loat];
Arg 4: double (scalar) default_val
Returns a scalar parameter read from a RSF header sf_histint, sf_histbool, sf_histfloat N
rsf_read Arg 1: double data_array;
Arg 2: string (row vector) filename;
Arg 3 (optional): string (row vector) same. If Arg 3 is given, value should be "same"
Reads data_array from filename. Input data should be of SF_FLOAT, SF_INT or SF_COMPLEX type. The same parameter is a way to indicate if the data is read from the same file as before or from a newly opened file. If same is not given, the data will be read from the beginning of the file. sf_floatread, sf_intread, sf_complexread Y
rsf_write Arg 1: double data_array;
Arg 2: string (row vector) filename;
Arg 3 (optional): string (row vector) same. If Arg 3 is given, value should be "same"
Writes data_array to filename. Data to be written can be integer, float, double or complex. It will get cast to RSF's native_float or native_complex encoding. sf_floatwrite, sf_complexwrite Y

Octave API

Java API

There are two APIs available for Java. New programs should be written to use the SWIG API. The Mines JTK API is provided here only for reference for those who may still be using it.

SWIG API

The SWIG API includes three classes. The methods that each class exposes are as follows:

RSF

<java> public RSF(String[] args); // Constructor, pass the command line arguments public float getFloat(String key, float default); // Gets the command line argument associated with this key. public int getInt(String key, int default); // Gets an int from the command line associated with this key. public boolean getBool(String key, boolean default); // Gets a boolean from the command line associated with this key. public String getString(String key, String default); // Gets a string corresponding to this key. </java>

Input

<java> public Input(String name); // Constructor, pass a filename. "in" defaults to stdin public int getN(int index); // Gets the number of elements in the file for that index. Counting starts at 1. Returns 1 if not found. public float getDelta(int index); // Gets the delta for that index. Returns 0.0 if not found. public float getOrigin(int index); // Gets the origin for that file. Returns 0.0 if not found. public String getLabel(int index); // Gets the label name for the index. Returns "" if not found. public String getUnit(int index); // Gets the unit for the index. Returns "" if not found. public void close(); // Close the file. public void read(float[] array); // Read data from the open RSF file into the given array. Reads the length of the array only. public void read(float[][] array); // The following methods all read data from the RSF file in the given shape of the array. public void read(float[][][] array); public void read(float[][][][] array); public void read(float[][][][][] array); public void read(float[][][][][][] array); public void read(float[][][][][][][] array); public void read(float[][][][][][][][] array); public void read(float[][][][][][][][][] array); </java>

Output

<java> public Output(String name); // Constructor, pass a filename. "out" points to standard out. public void setN(int index, int n); // Gets the number of elements in the file for that index. Counting starts at 1. Returns 1 if not found. public void setDelta(int index, float delta); // Gets the delta for that index. Returns 0.0 if not found. public void setOrigin(int index, float origin); // Gets the origin for that file. Returns 0.0 if not found. public void setLabel(int index, String label); // Gets the label name for the index. Returns "" if not found. public void setUnit(int index, String unit); // Gets the unit for the index. Returns "" if not found. public void close(); // Close the file. public void write(float[] array); // Writes data to the RSF file from the given array. Writes the shape of the array only. public void write(float[][] array); // The following methods all write data using the given shape of the array. public void write(float[][][] array); public void write(float[][][][] array); public void write(float[][][][][] array); public void write(float[][][][][][] array); public void write(float[][][][][][][] array); public void write(float[][][][][][][][] array); public void write(float[][][][][][][][][] array); </java>

Additional notes:

Java does not support complex numbers by default. To use complex numbers, you must convert complex valued RSF files to floating point numbers using sfdd .


MINES JTK API

THIS API IS DEPRECATED. PLEASE DEVELOP YOUR PROGRAMS TO BE COMPATIBLE WITH THE SWIG API.

The following methods are exposed directly at this time:

Par Class

<java> public float getFloat(String key, float default); // Gets the command line argument associated with this key. public int getInt(String key, int default); // Gets an int from the command line associated with this key. public boolean getBool(String key, boolean default); // Gets a boolean from the command line associated with this key. public String getString(String key, String default); // Gets a string corresponding to this key. </java>

Header Class

<java> public void setN(int dim, int n); // Used to set the number of elements in a dimension. For dimension 1, use an index value of 1. public int getN(int dim); public void setDelta(int dim, float delta); // Used to set the delta for a dimension. For dimension 1, use an index value of 1, not 0. public float getDelta(int dim); public void setOrigin(int dim, float origin); // Used to set the origin for a dimension. public float getOrigin(int dim); public void setLabel(int dim, String label); // Sets the label for a dimension. public String getLabel(int dim); public void setUnit(int dim, String unit); // Sets the unit for a dimension. public String getUnit(int dim); public void setFormat(String format); // Sets the output format information, has no effect on how Java treats the data. public String getFormat(); public void setPath(String path); /* Sets the location of the binary file that corresponds to this header file.

                                                Not used for writing information to an RSF file. */

public String getPath(); public void setName(String name); /* Change the file name that this header corresponds to. When writing this info to an RSF file,

                                                the API automatically will determine the binary file that goes along with this filename. */

public String getName(); public String toString(); // Can be used to print out information about the header file. </java>

Writer Class

When writing out RSF files, the header file is output in the same directory as the java program was executed. The binary file is placed according to the DATAPATH environment variable. saveName is the filename that you want to save your header to. Writing to standard out is not supported. All files must be saved via a name.

<java> public static void writeRSF(RSFHeader header, float[] data, String saveName); // Writes a 1D array to an RSF Header and binary file public static void writeRSF(RSFHeader header, float[][] data, String saveName); // Writes a 2D array to an RSF header and binary file. public static void writeRSF(RSFHeader header, float[][][] data, String saveName); // Writes a 3D array to an RSF header and binary file. </java>

Reader Class

<java> public static Header readHeader(String headerFileName); // Used to parse the RSF Header file for information. public static float[] readBinary(RSFHeader header); // Reads the RSF binary data file into a single array. public static float[][] readBinary2D(RSFHeader header); // Reads the RSF binary data file into a 2D array. public static float[][][] readBinary3D(RSFHeader header); // Reads the RSF binary data file into a 3D array. </java>

Python API

To use the python api, you need to import the rsf package via: import rsf.api .

NOTE: Madagascar now has automatic documentation, currently with Epydoc, and more to come. The information in the table below will be merged into it.

The rsf package has the following members:

Overview of the Python RSF API
Class Use Constructor Example Available Methods
File
Filter
Movie
Input Provides a basic way to read rsf files. file = rsf.Input(name) input = rsf.Input(name); n1 = input.int('n1') read(data), data is a numpy array to be read into.
close()
float(key, default=None), key is a string to be extracted, e.g. 'n1'.
int(key, default=None), key is a string to be extracted.
ints(key, num, default=None), key is a string, num is the number of ints.
put(key, val), key is a string to set the value for, val is a numerical value (int or float).
... and more, type help(rsf.Input) on a python interactive session for more information.
Output Provides a way to output numpy arrays to rsf files. file = rsf.Output(name) output=rsf.Output(name); output.write(data)
OverUnder
Overlay
Par Provides a method for accessing rsf parameters passed from the command line. par = rsf.Par() par = rsf.Par(); velname = par.string('vel')
SideBySide
Temp
Vplot View vplot graphics files from within python. plot = rsf.Vplot(name) plot = rsf.Vplot(name); plot.show()
Vppen

Calling standalone madagascar programs from Python

On systems with recent versions of Python (at least 2.4), and with Numpy and Swig installed, use the m8r module.

On old systems where dependencies cannot be easily installed (i.e. industrial clusters), use the sf module (i.e. import rsf.user.sf as sf).

SCons custom methods

Custom builders

Name File implemented in Function What it does
RSF_Include RSFSRC/framework/bldutil.py Header
RSF_Place RSFSRC/framework/bldutil.py Place
RSF_Pycompile RSFSRC/framework/bldutil.py Pycompile
RSF_Docmerge RSFSRC/framework/bldutil.py Docmerge

Other entities can be found in RSFSRC/framework/rsf/proj.py and tex.py