Editing
Guide to madagascar API
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==C++ interface== The C++ clip function is listed below. <syntaxhighlight lang="cpp"> /* 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); } </syntaxhighlight> Let us examine it line by line. <syntaxhighlight lang="cpp"> #include <rsf.hh> </syntaxhighlight> Including "<tt>rsf.hh</tt>" is required for accessing the RSF C++ interface. <syntaxhighlight lang="cpp"> sf_init(argc,argv); // Initialize RSF </syntaxhighlight> A call to <tt>sf_init</tt> is required to initialize the internally stored table of command-line arguments. <syntaxhighlight lang="cpp"> iRSF par(0), in; // input parameter, file oRSF out; // output file </syntaxhighlight> Two classes: <tt>iRSF</tt> and <tt>oRSF</tt> are used to define input and output files. For simplicity, the command-line parameters are also handled as an <tt>iRSF</tt> object, initialized with zero. <syntaxhighlight lang="cpp"> in.get("n1",n1); n2=in.size(1); </syntaxhighlight> Next, we read the data dimensions from the input RSF file object called <tt>in</tt>: the trace length is a parameter called "<tt>n1</tt>" and the number of traces is the size of <tt>in</tt> remaining after excluding the first dimension. It is extracted with the <tt>size</tt> method. <syntaxhighlight lang="cpp"> par.get("clip",clip); // parameter from the command line </syntaxhighlight> The clip parameter should be specified on the command line, for example, as <tt>clip=10</tt>. It is extracted with the <tt>get</tt> method of <tt>iRSF</tt> class from the <tt>par</tt> object. <syntaxhighlight lang="cpp"> std::valarray<float> trace(n1); </syntaxhighlight> The trace object has the single-precision floating-point type and is a 1-D array of length <tt>n1</tt>. It is declared and allocated using the <tt>valarray</tt> template class from the standard C++ library. <syntaxhighlight lang="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 } </syntaxhighlight> Next, we loop through the traces, read each trace from <tt>in</tt>, clip it and write the output to <tt>out</tt>. ===Compiling=== To compile the C++ program, run <pre> c++ clip.cc -I$RSFROOT/include -L$RSFROOT/lib -lrsf++ -lrsf -lm </pre> Change <tt>c++</tt> to the C++ compiler appropriate for your system and include additional compiler flags if necessary. The flags that RSF typically uses are in <tt>$RSFROOT/share/madagascar/etc/config.py</tt>.
Summary:
Please note that all contributions to Madagascar are considered to be released under the GNU Free Documentation License 1.3 or later (see
My wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
English
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Getting Madagascar
download
Installation
GitHub repository
SEGTeX
Introduction
Package overview
Tutorial
Hands-on tour
Reproducible documents
Hall of Fame
User Documentation
List of programs
Common programs
Popular programs
The RSF file format
Reproducibility with SCons
Developer documentation
Adding programs
Contributing programs
API demo: clipping data
API demo: explicit finite differences
Community
Conferences
User mailing list
Developer mailing list
GitHub organization
LinkedIn group
Development blog
Twitter
Slack
Tools
What links here
Related changes
Special pages
Page information