Guide to madagascar API

From Madagascar
Jump to navigation Jump to search
This page was created from the LaTeX source in book/rsf/rsf/api.tex using latex2wiki

This guide explains the RSF programming interface. See the Library Reference for more information on how to use the particular APIs.

Introduction

To work with RSF files in your own programs, you may need to use an appropriate programming interface. We will demonstrate the interface in different languages using a simple example. The example is a clipping program. It reads and writes RSF files and accesses parameters both from the input file and the command line. The input is processed trace by trace. This is not necessarily the most efficient approach[1] but it suffices for a simple demonstration.

Installation

Only the C interface is installed by default. To install other APIs, use API= parameter in the RSF configuration. For example, to install C++ and Fortran-90 API bindings in addition to the basic package, run

./configure API=c++,fortran-90
scons install

The configuration parameters are stored in $RSFROOT/lib/rsfconfig.py.

C interface

The C clip function is listed below. <c> /* Clip the data. */

  1. include <rsf.h>

int main(int argc, char* argv[]) {

   int n1, n2, i1, i2;
   float clip, *trace=NULL;
   sf_file in=NULL, out=NULL; /* Input and output files */
   /* Initialize RSF */
   sf_init(argc,argv);
   /* standard input */
   in = sf_input("in");
   /* standard output */
   out = sf_output("out");
   /* check that the input is float */
   if (SF_FLOAT != sf_gettype(in)) 

sf_error("Need float input");

   /* n1 is the fastest dimension (trace length) */
   if (!sf_histint(in,"n1",&n1)) 

sf_error("No n1= in input");

   /* leftsize gets n2*n3*n4*... (the number of traces) */
   n2 = sf_leftsize(in,1);
   /* parameter from the command line (i.e. clip=1.5 ) */
   if (!sf_getfloat("clip",&clip)) sf_error("Need clip=");
   /* allocate floating point array */
   trace = sf_floatalloc (n1);
   /* loop over traces */
   for (i2=0; i2 < n2; i2++) {

/*read a trace */ sf_floatread(trace,n1,in);

/* loop over samples */ for (i1=0; i1 < n1; i1++) { if (trace[i1] > clip) trace[i1]= clip; else if (trace[i1] < -clip) trace[i1]=-clip; }

/* write a trace */ sf_floatwrite(trace,n1,out);

   }
   sf_close();
   exit(0);

} </c>

Let us examine it in detail. <c>

  1. include <rsf.h>

</c>

The include preprocessing directive is required to access the RSF interface. <c>

   sf_file in=NULL, out=NULL; /* Input and output files */

</c>

RSF data files are defined with an abstract sf_file data type. An abstract data type means that the contents of it are not publicly declared, and all operations on sf_file objects should be performed with library functions. This is analogous to FILE * data type used in stdio.h and as close as C gets to an object-oriented style of programming (Roberts, 1998[2]). <c>

   /* Initialize RSF */
   sf_init(argc,argv);

</c>

Before using any of the other functions, you must call sf_init. This function parses the command line and initializes an internally stored table of command-line parameters. <c>

   /* standard input */
   in = sf_input("in");
   /* standard output */
   out = sf_output("out");

</c>

The input and output RSF file objects are created with sf_input and sf_output constructor functions. Both these functions take a string argument. The string may refer to a file name or a file tag. For example, if the command line contains vel=velocity.rsf, then both sf_input("velocity.rsf") and sf_input("vel") are acceptable. Two tags are special: "in" refers to the file in the standard input and "out" refers to the file in the standard output. <c>

   /* check that the input is float */
   if (SF_FLOAT != sf_gettype(in)) 

sf_error("Need float input"); </c>

RSF files can store data of different types (character, integer, floating point, complex). We extract the data type of the input file with the library sf_gettype function and check if it represents floating point numbers. If not, the program is aborted with an error message, using the sf_error function. It is generally a good idea to check the input for user errors and, if they cannot be corrected, to take a safe exit. <c>

   /* n1 is the fastest dimension (trace length) */
   if (!sf_histint(in,"n1",&n1)) 

sf_error("No n1= in input");

   /* leftsize gets n2*n3*n4*... (the number of traces) */
   n2 = sf_leftsize(in,1);

</c>

Conceptually, the RSF data model is a multidimensional hypercube. By convention, the dimensions of the cube are stored in n1=, n2=, etc. parameters. The n1 parameter refers to the fastest axis. If the input dataset is a collection of traces, n1 refers to the trace length. We extract it using the sf_histint function (integer parameter from history) and abort if no value for n1 is found. We could proceed in a similar fashion, extracting n2, n3, etc. If we are interested in the total number of traces, like in the clip example, a shortcut is to use the sf_leftsize function. Calling sf_leftsize(in,0) returns the total number of elements in the hypercube (the product of n1, n2, etc.), calling sf_leftsize(in,1) returns the number of traces (the product of n2, n3, etc.), calling sf_leftsize(in,2) returns the product of n3, n4, etc. By calling sf_leftsize, we avoid the need to extract additional parameters for the hypercube dimensions that we are not interested in. <c>

   /* parameter from the command line (i.e. clip=1.5 ) */
   if (!sf_getfloat("clip",&clip)) sf_error("Need clip=");

</c>

The clip parameter is read from the command line, where it can be specified, for example, as clip=10. The parameter has the float type, therefore we read it with the sf_getfloat function. If no clip= parameter is found among the command line arguments, the program is aborted with an error message using the sf_error function. <c>

   /* allocate floating point array */
   trace = sf_floatalloc (n1);

</c>

Next, we allocate an array of floating-point numbers to store a trace with the library sf_floatalloc function. Unlike the standard malloc the RSF allocation function checks for errors and either terminates the program or returns a valid pointer. <c>

   /* loop over traces */
   for (i2=0; i2 < n2; i2++) {

/*read a trace */ sf_floatread(trace,n1,in);

/* loop over samples */ for (i1=0; i1 < n1; i1++) { if (trace[i1] > clip) trace[i1]= clip; else if (trace[i1] < -clip) trace[i1]=-clip; }

/* write a trace */ sf_floatwrite(trace,n1,out);

   }

</c>

The rest of the program is straightforward. We loop over all available traces, read each trace, clip it and right the output out. The syntax of sf_floatread and sf_floatwrite functions is similar to the syntax of the C standard fread and fwrite function except that the type of the element is specified explicitly in the function name and that the input and output files have the RSF type sf_file.

<c> sf_close(); exit(0) </c>

We explicitly close the input file to avoid leaving a stale temporary file in $DATAPATH if the program is called in a pipe sequence. Then, we close the program by sending the shell the Unix code that tells it no errors were encountered.

Compiling

To compile the clip program, run

cc clip.c -I$RSFROOT/include -L$RSFROOT/lib -lrsf -lm

Change cc to the C compiler appropriate for your system and include additional compiler flags if necessary. The flags that RSF typically uses are in $RSFROOT/lib/rsfconfig.py.

C++ interface

The C++ clip function is listed below. <cpp> /* Clip the data. */

  1. include <valarray>
  2. include <rsf.hh>

int main(int argc, char* argv[]) {

   sf_init(argc,argv); // Initialize RSF
   
   iRSF par(0), in; // input parameter, file
   oRSF out;        // output file
   int n1, n2;      // trace length, number of traces
   float clip;
   
   in.get("n1",n1);
   n2=in.size(1);
   par.get("clip",clip); // parameter from the command line
   std::valarray<float> trace(n1);
   for (int i2=0; i2 < n2; i2++) { // loop over traces

in >> trace; // read a trace

for (int i1=0; i1 < n1; i1++) { // loop over samples if (trace[i1] > clip) trace[i1]=clip; else if (trace[i1] < -clip) trace[i1]=-clip; }

out << trace; // write a trace

   }
   exit(0);

} </cpp>

Let us examine it line by line. <cpp>

  1. include <rsf.hh>

</cpp>

Including "rsf.hh" is required for accessing the RSF C++ interface. <cpp>

   sf_init(argc,argv); // Initialize RSF

</cpp>

A call to sf_init is required to initialize the internally stored table of command-line arguments. <cpp>

   iRSF par(0), in; // input parameter, file
   oRSF out;        // output file

</cpp>

Two classes: iRSF and oRSF are used to define input and output files. For simplicity, the command-line parameters are also handled as an iRSF object, initialized with zero. <cpp>

   in.get("n1",n1);
   n2=in.size(1);

</cpp>

Next, we read the data dimensions from the input RSF file object called in: the trace length is a parameter called "n1" and the number of traces is the size of in remaining after excluding the first dimension. It is extracted with the size method. <cpp>

   par.get("clip",clip); // parameter from the command line

</cpp>

The clip parameter should be specified on the command line, for example, as clip=10. It is extracted with the get method of iRSF class from the par object. <cpp>

   std::valarray<float> trace(n1);

</cpp>

The trace object has the single-precision floating-point type and is a 1-D array of length n1. It is declared and allocated using the valarray template class from the standard C++ library. <cpp>

   for (int i2=0; i2 < n2; i2++) { // loop over traces

in >> trace; // read a trace

for (int i1=0; i1 < n1; i1++) { // loop over samples if (trace[i1] > clip) trace[i1]=clip; else if (trace[i1] < -clip) trace[i1]=-clip; }

out << trace; // write a trace

   }

</cpp>

Next, we loop through the traces, read each trace from in, clip it and write the output to out.

Compiling

To compile the C++ program, run

c++ clip.cc -I$RSFROOT/include -L$RSFROOT/lib -lrsf++ -lrsf -lm

Change c++ to the C++ compiler appropriate for your system and include additional compiler flags if necessary. The flags that RSF typically uses are in $RSFROOT/lib/rsfconfig.py.

Fortran-77 interface

The Fortran-77 clip function is listed below. <fortran> program Clipit implicit none integer n1, n2, i1, i2, in, out integer sf_input, sf_output, sf_leftsize, sf_gettype logical sf_getfloat, sf_histint real clip, trace(1000)

call sf_init() in = sf_input("in") out = sf_output("out")

if (3 .ne. sf_gettype(in))

    &  call sf_error("Need float input")

if (.not. sf_histint(in,"n1",n1)) then call sf_error("No n1= in input") else if (n1 > 1000) then call sf_error("n1 is too long") end if n2 = sf_leftsize(in,1)

if (.not. sf_getfloat("clip",clip))

    &  call sf_error("Need clip=")

do 10 i2=1, n2 call sf_floatread(trace,n1,in)

do 20 i1=1, n1 if (trace(i1) > clip) then trace(i1)=clip else if (trace(i1) < -clip) then trace(i1)=-clip end if

20	   continue

call sf_floatwrite(trace,n1,out)

10	continue

stop end </fortran>

Let us examine it in detail. <fortran> call sf_init() </fortran>

The program starts with a call to sf_init, which initializes the command-line interface. <fortran> in = sf_input("in") out = sf_output("out") </fortran>

The input and output files are created with calls to sf_input and sf_output. Because of the absence of derived types in Fortran-77, we use simple integer pointers to represent RSF files. Both sf_input and sf_output accept a character string, which may refer to a file name or a file tag. For example, if the command line contains vel=velocity.rsf, then both sf_input("velocity.rsf") and sf_input("vel") are acceptable. Two tags are special: "in" refers to the file in the standard input and "out" refers to the file in the standard output. <fortran> if (3 .ne. sf_gettype(in))

    &  call sf_error("Need float input")

</fortran>

RSF files can store data of different types (character, integer, floating point, complex). The function sf_gettype checks the type of data stored in the RSF file. We make sure that the type corresponds to floating-point numbers. If not, the program is aborted with an error message, using the sf_error function. It is generally a good idea to check the input for user errors and, if they cannot be corrected, to take a safe exit. <fortran> if (.not. sf_histint(in,"n1",n1)) then call sf_error("No n1= in input") else if (n1 > 1000) then call sf_error("n1 is too long") end if n2 = sf_leftsize(in,1) </fortran>

Conceptually, the RSF data model is a multidimensional hypercube. By convention, the dimensions of the cube are stored in n1=, n2=, etc. parameters. The n1 parameter refers to the fastest axis. If the input dataset is a collection of traces, n1 refers to the trace length. We extract it using the sf_histint function (integer parameter from history) and abort if no value for n1 is found. Since Fortran-77 cannot easily handle dynamic allocation, we also need to check that n1 is not larger than the size of the statically allocated array. We could proceed in a similar fashion, extracting n2, n3, etc. If we are interested in the total number of traces, like in the clip example, a shortcut is to use the sf_leftsize function. Calling sf_leftsize(in,0) returns the total number of elements in the hypercube (the product of n1, n2, etc.), calling sf_leftsize(in,1) returns the number of traces (the product of n2, n3, etc.), calling sf_leftsize(in,2) returns the product of n3, n4, etc. By calling sf_leftsize, we avoid the need to extract additional parameters for the hypercube dimensions that we are not interested in. <fortran> if (.not. sf_getfloat("clip",clip))

    &  call sf_error("Need clip=")

</fortran>

The clip parameter is read from the command line, where it can be specified, for example, as clip=10. The parameter has the float type, therefore we read it with the sf_getfloat function. If no clip= parameter is found among the command line arguments, the program is aborted with an error message using the sf_error function. <fortran> do 10 i2=1, n2 call sf_floatread(trace,n1,in)

do 20 i1=1, n1 if (trace(i1) > clip) then trace(i1)=clip else if (trace(i1) < -clip) then trace(i1)=-clip end if

20	   continue

call sf_floatwrite(trace,n1,out)

10	continue

</fortran>

Finally, we do the actual work: loop over input traces, reading, clipping, and writing out each trace.

Compiling

To compile the Fortran-77 program, run

f77 clip.f -L$RSFROOT/lib -lrsff -lrsf -lm

Change f77 to the Fortran compiler appropriate for your system and include additional compiler flags if necessary. The flags that RSF typically uses are in $RSFROOT/lib/rsfconfig.py.

Fortran-90 interface

The Fortran-90 clip function is listed below. <fortran> program Clipit

 use rsf
 implicit none
 type (file)                      :: in, out
 integer                          :: n1, n2, i1, i2
 real                             :: clip
 real, dimension (:), allocatable :: trace
 call sf_init()            ! initialize RSF
 in = rsf_input()
 out = rsf_output()
 if (sf_float /= gettype(in)) call sf_error("Need float type")
 call from_par(in,"n1",n1)
 n2 = filesize(in,1)
 call from_par("clip",clip) ! command-line parameter 
 allocate (trace (n1))
 do i2=1, n2                ! loop over traces
    call rsf_read(in,trace)
    
    where (trace >  clip) trace =  clip
    where (trace < -clip) trace = -clip
    call rsf_write(out,trace)
 end do

end program Clipit </fortran>

Let us examine it in detail. <fortran>

 use rsf

</fortran>

The program starts with importing the rsf module. <fortran>

 call sf_init()            ! initialize RSF

</fortran>

A call to sf_init is needed to initialize the command-line interface. <fortran>

 in = rsf_input()
 out = rsf_output()

</fortran>

The standard input and output files are initialized with rsf_input and rsf_output functions. Both functions accept optional arguments. For example, if the command line contains vel=velocity.rsf, then both rsf_input("velocity.rsf") and rsf_input("vel") are acceptable. <fortran>

 if (sf_float /= gettype(in)) call sf_error("Need float type")

</fortran>

A call to from_par extracts the "n1" parameter from the input file. Conceptually, the RSF data model is a multidimensional hypercube. The n1 parameter refers to the fastest axis. If the input dataset is a collection of traces, n1 corresponds to the trace length. We could proceed in a similar fashion, extracting n2, n3, etc. If we are interested in the total number of traces, like in the clip example, a shortcut is to use the filesize function. Calling filesize(in) returns the total number of elements in the hypercube (the product of n1, n2, etc.), calling filesize(in,1) returns the number of traces (the product of n2, n3, etc.), calling filesize(in,2) returns the product of n3, n4, etc. By calling filesize, we avoid the need to extract additional parameters for the hypercube dimensions that we are not interested in. <fortran>

 n2 = filesize(in,1)

</fortran>

The clip parameter is read from the command line, where it can be specified, for example, as clip=10. If we knew a good default value for clip, we could specify it with an optional argument, i.e. call~from_par("clip",clip,default). <fortran>

 allocate (trace (n1))
 do i2=1, n2                ! loop over traces
    call rsf_read(in,trace)
    
    where (trace >  clip) trace =  clip
    where (trace < -clip) trace = -clip

</fortran>

Finally, we do the actual work: loop over input traces, reading, clipping, and writing out each trace.

Compiling

To compile the Fortran-90 program, run

f90 clip.f90 -I$RSFROOT/include -L$RSFROOT/lib -lrsff90 -lrsf -lm

Change f90 to the Fortran-90 compiler appropriate for your system and include additional compiler flags if necessary. The flags that RSF typically uses are in $RSFROOT/share/madagasacar/etc/config.py.

The complete specification for the F90 API can be found on the Library Reference page.

Python interface

The Python clip script is listed below. <python>

  1. !/usr/bin/env python

import numpy import rsf.api as rsf

par = rsf.Par() input = rsf.Input() output = rsf.Output() assert 'float' == input.type

n1 = input.int("n1") n2 = input.size(1) assert n1

clip = par.float("clip") assert clip

trace = numpy.zeros(n1,'f')

for i2 in xrange(n2): # loop over traces

   input.read(trace)
   trace = numpy.clip(trace,-clip,clip)
   output.write(trace)

</python>

Let us examine it in detail. <python> import numpy import rsf.api as rsf </python>

The script starts with importing the numpy module and the rsf API. <python> par = rsf.Par() input = rsf.Input() output = rsf.Output() assert 'float' == input.type </python>

Next, we initialize the command line interface and the standard input and output files. We also make sure that the input file type is floating point. <python> n1 = input.int("n1") n2 = input.size(1) assert n1 </python>

We extract the "n1" parameter from the input file. Conceptually, the RSF data model is a multidimensional hypercube. The n1 parameter refers to the fastest axis. If the input dataset is a collection of traces, n1 corresponds to the trace length. We could proceed in a similar fashion, extracting n2, n3, etc. If we are interested in the total number of traces, like in the clip example, a shortcut is to use the size method of the Input class1. Calling size(0) returns the total number of elements in the hypercube (the product of n1, n2, etc.), calling size(1) returns the number of traces (the product of n2, n3, etc.), calling size(2) returns the product of n3, n4, etc. <python> clip = par.float("clip") assert clip </python>

The clip parameter is read from the command line, where it can be specified, for example, as clip=10. <python> for i2 in xrange(n2): # loop over traces

   input.read(trace)
   trace = numpy.clip(trace,-clip,clip)
   output.write(trace)

</python>

Finally, we do the actual work: loop over input traces, reading, clipping, and writing out each trace.

Compiling

The python script does not require compilation. Simply make sure that $RSFROOT/lib is in PYTHONPATH and LD_LIBRARY_PATH.

Interactive mode usage without graphics

Madagascar's Python API can be used interactively too. Create an input dataset with

sfmath n1=10 n2=10 output=x1+x2 > test.rsf

Then, start the python interpreter and paste the following to its command line:

<python> import numpy, rsf.api

input = rsf.api.Input('test.rsf') n1 = input.int("n1") n2 = input.int("n2")

data = numpy.zeros((n2,n1),'f') input.read(data) data = data.transpose() # Example of numpy in action

print data </python>

You will get

[[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.]
 [  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]
 [  2.   3.   4.   5.   6.   7.   8.   9.  10.  11.]
 [  3.   4.   5.   6.   7.   8.   9.  10.  11.  12.]
 [  4.   5.   6.   7.   8.   9.  10.  11.  12.  13.]
 [  5.   6.   7.   8.   9.  10.  11.  12.  13.  14.]
 [  6.   7.   8.   9.  10.  11.  12.  13.  14.  15.]
 [  7.   8.   9.  10.  11.  12.  13.  14.  15.  16.]
 [  8.   9.  10.  11.  12.  13.  14.  15.  16.  17.]
 [  9.  10.  11.  12.  13.  14.  15.  16.  17.  18.]]

This code will also work in batch mode in a Python script, not only pasted to the interpreter's command line.

Graphics with Matplotlib

Python can plot arrays directly from memory, without having to write a file to disk first. Matplotlib is one of the several packages that accomplish this. To create a figure, execute the code in the previous section, followed by:

<python> from pylab import * imshow(data) xlabel('X (m)') ylabel('Y (m)') title('Matplotlib example') </python>

If you want to pop up a figure in an interactive session, after pasting to a Python command line the code shown before, also paste:

<python> show() </python>

You will get Figure 1. The figure will pop up if you run the code in a script too, and the script will stop until the figure is manually closed. You must press the floppy disk button in order to save it. To have the image written to disk automatically, instead of show() use:

<python> savefig('myfile.png') </python>

Putting it all together, here is a sample script reading a RSF file from stdin and printing out a figure:

<python>

  1. !/usr/bin/env python

import rsf.api, numpy, sys, pylab

input = rsf.api.Input('test.rsf') n1 = input.int("n1") n2 = input.int("n2")

data = numpy.zeros((n2,n1),'f') input.read(data)

pylab.imshow(data) pylab.savefig('out.png') </python>

MATLAB interface

The MATLAB clip function is listed below. <matlab> function clip(in,out,clip) %CLIP Clip the data

dims = rsf_dim(in); n1 = dims(1);  % trace length n2 = prod(dims(2:end)); % number of traces trace = 1:n1;  % allocate trace rsf_create(out,in)  % create an output file

for i2 = 1:n2  % loop over traces

   rsf_read(trace,in,'same');
   trace(trace >   clip) =  clip;
   trace(trace < - clip) = -clip;
   rsf_write(trace,out,'same');

end

</matlab>

Let us examine it in detail. <matlab> dims = rsf_dim(in); </matlab>

We start by figuring out the input file dimensions. <matlab> n1 = dims(1);  % trace length n2 = prod(dims(2:end)); % number of traces </matlab>

The first dimension is the trace length, the product of all other dimensions correspond to the number of traces. <matlab> trace = 1:n1;  % allocate trace rsf_create(out,in)  % create an output file </matlab>

Next, we allocate the trace array and create an output file. <matlab> for i2 = 1:n2  % loop over traces

   rsf_read(trace,in,'same');
   trace(trace >   clip) =  clip;
   trace(trace < - clip) = -clip;
   rsf_write(trace,out,'same');

end </matlab>

Finally, we do the actual work: loop over input traces, reading, clipping, and writing out each trace.

Available functions

Only some of the functions in the rsf library have received a MATLAB interface. These functions are rsf_par, rsf_dim, rsf_read, rsf_write and rsf_create. All these functions except rsf_par have been illustrated in the example above.

Compiling

The MATLAB script does not require compilation. Simply make sure that $RSFROOT/lib is in MATLABPATH and LD_LIBRARY_PATH.

Java Interface

There are two interfaces to Java that are available. New codes should be written to use the SWIG interface ONLY. The older interface (using the MINES JTK) is deprecated, and is only provided while users migrate their code to the new interface.

SWIG

To install the SWIG interface:

1 - Download the Java Standard Development Kit (JDK). Installation varies by platform.

2 - Create the JAVA_HOME environment variable for your shell. This should point to the directory where the JDK was installed. Example (for Ubuntu 10.04): <bash> export JAVA_HOME=/usr/lib/jvm/java-6-opensdk </bash> 3 - Reconfigure Madagascar: <bash> ./configure API=java </bash> 4 - Reinstall Madagascar (Ignore the compilation warnings for Java files): <bash> scons; scons install </bash>

The installation creates two files: $RSFROOT/lib/libjrsf.so and $RSFROOT/lib/rsf.jar .

Make sure that you set your LD_LIBRARY_PATH to include $RSFROOT/lib.


A short demonstration of the interface follows:

<java> import rsf.RSF; import rsf.Input; import rsf.Output;

/* A simple Java program to clip a dataset. */

public class Clip {

   static {
       System.loadLibrary("jrsf");
   }
   public static void main(String[] args){
       // Initialize command line argument passing
        RSF par = new RSF(args);
        // Get the input file name.
        Input input = new Input("in");
        Output output = new Output("out");
       // Get the value to clip to.
        float clip = par.getFloat("clip",0.0f);
       // Read our input header
       int n3 = input.getN(3);
       int n2 = input.getN(2);
       int n1 = input.getN(1);
       //Perform clipping operation on a single trace and write out.
       float[] data = new float[n1];
        for(int i = 0; i < n3; ++i){
           for(int j = 0; j < n2; ++j){
               input.read(data);
               for(int k = 0; k < n1; ++k){
                   if (data[k] > clip) data[k] = clip;
                   else if (data[k] < -clip) data[k] = -clip;
               }
               output.write(data);
           }
        }
        output.setN(1,n1);
        output.setN(2,n2);
        output.setN(3,n3);
        input.close();
        output.close();
   }

} </java>

There are only three classes in the interface:

1 -- RSF - The command line argument parser, and the initializer for the native interface. An RSF object MUST be instantiated BEFORE instantiating an Input or Output object.

2 -- Input - An object that provides read access to an RSF file.

3 -- Output - An object that provides write access to an RSF file.

Additionally, the shared library (libjrsf.so or libjrsf.dll) must be included via the System.loadLibrary("jrsf"); as the first command in the file.

To compile on the command line: <bash> javac -cp $RSFROOT/lib Clip.java </bash>

To include this as part of a SCons script (SConstruct): <python> from rsf.proj import *

  1. Compiles Clip.class

project.Java('.','Clip.java')

Flow('dat',None,'spike n1=1000 n2=100 n3=10 nsp=1 k1=500 l1=1000') Flow('clipd','dat Clip.class',

   
   %s Clip clip=0.5
    % WhereIs('java'))

Flow('test.attr','clipd','sfattr')

</python>

Note, that we compile Clip.java using the SCons Java builder. To execute the command, we have to locate the "java" command, which we do using WhereIs('java'). Then we execute the class name, and pass any command line arguments as usual. The files are read from standard in for this program and written to standard out.

Please see the Library Reference for more details on the functions available in the SWIG API.

Note: Additional Java packages can included in SCons automatically by setting the CLASSPATH environment variable to point to the JAR files that they are contained in.

Mines JTK

THIS INTERFACE IS DEPRECATED AND ONLY PROVIDED AS A REFERENCE. THIS INTERFACE IS NO LONGER SUPPORTED AND WILL BE REMOVED

This interface may still be compiled by specifying the MINESJTK environment variable.

The interface to Java is less full featured than others. Presently, it only allows you to read RSF files with fewer than 4-dimensions into Java, and then export RSF files. The Java interface does not support reading from standard in, or writing from standard out. Therefore, the Java Interface does not support piping either. The Java interface at present treats all values as floats, and does not have support for complex numbers. Java programs are not parsed for self-doc information like other programs are either. Using javadoc may be a viable alternative to creating a parsing engine for Java programs.

Once the build is complete, the JAR file rsf.jar will be added to your $RSFROOT/lib folder.

There are two ways to access the API:

1 - From the command line: point the classpath at BOTH compile and runtime to this location on the command line. <bash> javac -cp $MINESJTK:$RSFROOT/lib/rsf.jar:. Test.java java -cp $MINESJTK:$RSFROOT/lib/rsf.jar:. Test arg1=... arg2=... arg3=... </bash>

2 - From within a SConstruct script: BOTH the path for the API ($RSFROOT/lib) and the path to the Mines JTK ($MINESJTK) are automatically added to the environment variable CLASSPATH and JAVACLASSPATH for executions made within an SConstruct. Additionally, any additional classes that are already in the CLASSPATH environmental variable in the shell that you launch from will be added to your classpath. This allows you to include additional libraries automatically within your Java programs. The local directory (.) is also included in the CLASSPATH.

<python> from rsf.proj import *

  1. Compiles Clip.class

project.Java('.','Clip.java')

Flow('dat',None,'spike n1=1000 n2=100 n3=10 nsp=1 k1=500') Flow('clipd','Clip.class dat',

   
   %s ${SOURCES[0].filebase} clip=0.5 
   in=${SOURCES[1]} out=$TARGET 
    % WhereIs('java'),stdin=0,stdout=-1)

Flow('test.attr','clipd','sfattr')

End() </python>

The interface itself is fairly straightforward. More details on the methods exposed by the API can be found at the Library Reference.

Data clipping, argument parsing, and IO example:

<java> import rsf.Par; import rsf.Reader; import rsf.Writer; import rsf.Header;

/* A simple Java program to clip a dataset.

Presently, there is no automatic self-documentation generation for use with sfdoc. Javadoc may be a better way to generate self-doc for Java programs.

  • /

public class Clip {

   public static void main(String[] args){
       // Initialize command line argument passing
        Par par = new Par(args);
        // Get the input file name.
        String input = par.getString("in","");
        // If the input file name is nothing, then quit!
        if (input.equals("")){
               System.out.println("Did not find input file!");
               System.exit(1);
        }
        //If the output file name is nothing, then quit!
        String output = par.getString("out","");
        if (output.equals("")){
               System.out.println("Did not find output file!");
               System.exit(1);
        }
       // Get the value to clip to.
        float clip = par.getFloat("clip",0.0f);
       //Read our header file.
        Header header = Reader.readHeader(input);
       // Read our binary data.
        float[][][] data = Reader.readBinary3D(header);
       //Initialize our array values.
        int n3 = header.getN(3);
        int n2 = header.getN(2);
        int n1 = header.getN(1);
       //Perform clipping operation.
        for(int i = 0; i < n3; ++i){
           for(int j = 0; j < n2; ++j){
               for(int k = 0; k < n1; ++k){
                   float trace = data[i][j][k];
                   if (trace > clip) data[i][j][k] = clip;
                   else if (trace < -clip) data[i][j][k] = -clip;
               }
           }
        }
       //Write our data out, using the same header values to the file
       //located at: output.
        Writer.writeRSF(header,data,output);
   }

}

</java>

How to read a file:

<java>

import rsf.Header; import rsf.Reader; import rsf.Writer;

public class Test {

     public static void main(String[] args){
               Header header = Reader.readHeader("junk.rsf"); //To read the header file, just feed in the path relative to the execution directory
               System.out.println(header); //The header file will print out if you ask it to, this is good for debugging
               float[][] data = Reader.readBinary2D(header); //Now I can manipulate my data
      }

}

</java>

How to write a file:

<java> import rsf.Header; import rsf.Reader; import rsf.Writer;

public class Test {

     public static void main(String[] args){
               
               Header header = Reader.readHeader("test.rsf"); //To read the header file, just feed in the path relative to the execution directory
               System.out.println(header); //The header file will print out if you ask it to, this is good for debugging
               float[][] data = Reader.readBinary2D(header); //Now I can manipulate my data
               //...Do something
               Writer.writeRSF(header,data,"test2.rsf"); //Write out my data!
      }

}

</java>

If you want to create a dataset from scratch from within Java, then you can create an array of data, modify the values, create a header, and then write it out to rsf:

<java> import rsf.Header; import rsf.Reader; import rsf.Writer;

public class Test {

     public static void main(String[] args){
               int n1 = 20;
               int n2 = 40;
               float[][] data = new float[n2][n1]; 
               //The order for dimensions is reversed, because RSF stores them as column-major arrays (see Python API).
               //...Do something
               Header header = new Header();
               header.setN(1,n1);
               /* We set the values using the proper RSF number for the dimension, instead of the Java array index.
                  Example:  RSF Dimension 1, corresponds to array index 0 in Java.
                            However, we set the values using index 1.  The mapping is handled behind the scenes.
                */
               header.setN(2,n2); 
               header.setDelta(1,0.0);
               header.setLabel(1,"time");
               header.setUnits(1,"s");
               Writer.writeRSF(header,data,"junk2.rsf"); //Write out my data!
      }

}

</java>

References

  1. Compare with the library clip program.
  2. Roberts, E. S., 1998, Programming abstractions in C: Addison-Wesley.