Editing
Guide to madagascar programs
(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!
====Implementation: [https://github.com/ahay/src/blob/master/system/main/add.c system/main/add.c]==== The first input file is either in the list or in the standard input. <syntaxhighlight lang="c"> /* find number of input files */ if (isatty(fileno(stdin))) { /* no input file in stdin */ nin=0; } else { filename[0] = "in"; nin=1; } </syntaxhighlight> Collect input files in the <tt>in</tt> array from all command-line parameters that don't contain an "<tt>=</tt>" sign. The total number of input files is <tt>nin</tt>. <syntaxhighlight lang="c"> for (i=1; i< argc; i++) { /* collect inputs */ if (NULL != strchr(argv[i],'=')) continue; /* not a file */ filename[nin] = argv[i]; nin++; } if (0==nin) sf_error ("no input"); /* nin = no of input files*/ </syntaxhighlight> A helper function <tt>check_compat</tt> checks the compatibility of input files. <syntaxhighlight lang="c"> static void check_compat (sf_datatype type /* data type */, size_t nin /* number of files */, sf_file* in /* input files [nin] */, int dim /* file dimensionality */, const int* n /* dimensions [dim] */) /* Check that the input files are compatible. Issue error for type mismatch or size mismatch. Issue warning for grid parameters mismatch. */ { int ni, id; size_t i; float d, di, o, oi; char key[3]; const float tol=1.e-5; /* tolerance for comparison */ for (i=1; i < nin; i++) { if (sf_gettype(in[i]) != type) sf_error ("type mismatch: need %d",type); for (id=1; id <= dim; id++) { (void) snprintf(key,3,"n%d",id); if (!sf_histint(in[i],key,&ni) || ni != n[id-1]) sf_error("%s mismatch: need %d",key,n[id-1]); (void) snprintf(key,3,"d%d",id); if (sf_histfloat(in[0],key,&d)) { if (!sf_histfloat(in[i],key,&di) || (fabsf(di-d) > tol*fabsf(d))) sf_warning("%s mismatch: need %g",key,d); } else { d = 1.; } (void) snprintf(key,3,"o%d",id); if (sf_histfloat(in[0],key,&o) && (!sf_histfloat(in[i],key,&oi) || (fabsf(oi-o) > tol*fabsf(d)))) sf_warning("%s mismatch: need %g",key,o); } } } </syntaxhighlight> Finally, we enter the main loop, where the input data are getting read buffer by buffer and combined in the total product depending on the data type. <syntaxhighlight lang="c"> for (nbuf /= sf_esize(in[0]); nsiz > 0; nsiz -= nbuf) { if (nbuf > nsiz) nbuf=nsiz; for (j=0; j < nin; j++) { collect = (bool) (j != 0); switch(type) { case SF_FLOAT: sf_floatread((float*) bufi, nbuf, in[j]); add_float(collect, nbuf, (float*) buf, (const float*) bufi, cmode, scale[j], add[j], abs_flag[j], log_flag[j], sqrt_flag[j], exp_flag[j]); break; </syntaxhighlight> The data combination program for floating point numbers is <tt>add_float</tt>. <syntaxhighlight lang="c"> static void add_float (bool collect, /* if collect */ size_t nbuf, /* buffer size */ float* buf, /* output [nbuf] */ const float* bufi, /* input [nbuf] */ char cmode, /* operation */ float scale, /* scale factor */ float add, /* add factor */ bool abs_flag, /* if abs */ bool log_flag, /* if log */ bool sqrt_flag, /* if sqrt */ bool exp_flag /* if exp */) /* Add floating point numbers */ { size_t j; float f; for (j=0; j < nbuf; j++) { f = bufi[j]; if (abs_flag) f = fabsf(f); f += add; if (log_flag) f = logf(f); if (sqrt_flag) f = sqrtf(f); if (1. != scale) f *= scale; if (exp_flag) f = expf(f); if (collect) { switch (cmode) { case 'p': /* product */ case 'm': /* multiply */ buf[j] *= f; break; case 'd': /* delete */ if (f != 0.) buf[j] /= f; break; default: /* add */ buf[j] += f; break; } } else { buf[j] = f; } } } </syntaxhighlight>
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