next up previous [pdf]

Next: Compiling Up: Fomel: RSF API Previous: Compiling

C++ interface

The C++ clip function is listed below.

/* Clip the data. */

#include <valarray>
#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);
}
Let us examine it line by line.

#include <rsf.hh>
Including ``rsf.hh'' is required for accessing the RSF C++ interface.

    sf_init(argc,argv); // Initialize RSF
A call to sf_init is required to initialize the internally stored table of command-line arguments.

    iRSF par(0), in; // input parameter, file
    oRSF out;        // output file
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.

    in.get("n1",n1);
    n2=in.size(1);
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.

    par.get("clip",clip); // parameter from the command line
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.

    std::valarray<float> trace(n1);
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.

    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
    }
Next, we loop through the traces, read each trace from in, clip it and write the output to out.



Subsections
next up previous [pdf]

Next: Compiling Up: Fomel: RSF API Previous: Compiling

2013-04-08