DATRW++ library: seismic data I/O with multiple formats
seifetest.cc
Go to the documentation of this file.
1 /*! \file seifetest.cc
2  * \brief test seife format I/O functions
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 02/09/2011
8  *
9  * test seife format I/O functions
10  *
11  * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach)
12  *
13  * ----
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27  * ----
28  *
29  * REVISIONS and CHANGES
30  * - 02/09/2011 V1.0 Thomas Forbriger
31  *
32  * ============================================================================
33  */
34 #define SEIFETEST_VERSION \
35  "SEIFETEST V1.0 test seife format I/O functions"
36 
37 #include <fstream>
38 #include <iostream>
39 #include <sstream>
40 #include <tfxx/commandline.h>
41 #include <datrwxx/seifeio.h>
42 #include <datrwxx/util.h>
43 #include <datrwxx/formats.h>
44 #include <datrwxx/datread.h>
45 #include <datrwxx/seife.h>
46 
47 using std::cout;
48 using std::cerr;
49 using std::endl;
50 
51 /*----------------------------------------------------------------------*/
52 
53 struct Options {
57  int nprint, nsamples;
58  double frequency;
59  std::string modifiers;
60 }; // struct Options
61 
62 /*----------------------------------------------------------------------*/
63 
64 int main(int iargc, char* argv[])
65 {
66 
67  // define usage information
68  char usage_text[]=
69  {
71  "usage: seifetest [-pl file] [-read file] [-DEBUG] [-stream file]" "\n"
72  " [-cmmntlim] [-verbose] [-countonly] [-nprint n]\n"
73  " [-sine file] [-freq f] [-nsamples n] [-moretraces]\n"
74  " [-modifiers s]\n"
75  " or: seifetest --help|-h" "\n"
76  " or: seifetest --xhelp" "\n"
77  };
78 
79  // define full help text
80  char help_text[]=
81  {
82  "\n"
83  "-pl file read parameter line from file" "\n"
84  "-read file read seife data file" "\n"
85  "-stream file read seife data file through stream interface" "\n"
86  "-DEBUG produce debug output" "\n"
87  "-xhelp print complete online help available" "\n"
88  "-cmmntlim test automatic limiting of comment lines" "\n"
89  "-verbose be verbose" "\n"
90  "-countonly just count traces in stream mode (test skiptrace)" "\n"
91  "-nprint n print n samples in stream mode when verbose" "\n"
92  "-sine file write sine wave to file" "\n"
93  "-freq f produce sine wave with frequency f" "\n"
94  "-nsamples n write n samples for sine wave" "\n"
95  "-moretraces try to write more than one trace" "\n"
96  "-modifiers s pass string as format modifiers to the I/O stream" "\n"
97  };
98 
99  // define commandline options
100  using namespace tfxx::cmdline;
101  static Declare options[]=
102  {
103  // 0: print help
104  {"help",arg_no,"-"},
105  // 1: verbose mode
106  {"verbose",arg_no,"-"},
107  // 2: parameter line
108  {"pl",arg_yes,"-"},
109  // 3: debug output
110  {"DEBUG",arg_no,"-"},
111  // 4: parameter line
112  {"read",arg_yes,"-"},
113  // 5: detailed help
114  {"xhelp",arg_no,"-"},
115  // 6: test limiting of comment lines
116  {"cmmntlim",arg_no,"-"},
117  // 7: read through stream
118  {"stream",arg_yes,"-"},
119  // 8: count only
120  {"countonly",arg_no,"-"},
121  // 9: print n samples in stream mode
122  {"nprint",arg_yes,"10"},
123  // 10: nsamples for sine wave
124  {"nsamples",arg_yes,"1000"},
125  // 11: frequency of sine wave
126  {"frequency",arg_yes,"25."},
127  // 12: write sine wave
128  {"sine",arg_yes,"-"},
129  // 13: try to write more than one trace
130  {"moretraces",arg_no,"-"},
131  // 14: pass format modifiers
132  {"modifiers",arg_yes,""},
133  {NULL}
134  };
135 
136  // no arguments? print usage...
137  if (iargc<2)
138  {
139  cerr << usage_text << endl;
140  exit(0);
141  }
142 
143  // collect options from commandline
144  Commandline cmdline(iargc, argv, options);
145 
146  // help requested? print full help text...
147  if (cmdline.optset(0) || cmdline.optset(5))
148  {
149  cerr << usage_text << endl;
150  cerr << help_text << endl;
151  if (cmdline.optset(5))
152  {
153  cerr << "\n";
154  cerr << "help functions:" << endl;
156  cerr << "\n";
157  datrw::online_help(cerr);
158  }
159  exit(0);
160  }
161 
162  Options opt;
163 
164  opt.verbose=cmdline.optset(1);
165  opt.parameterline=cmdline.optset(2);
166  opt.parameterlinefile=cmdline.string_arg(2);
167  opt.debug=cmdline.optset(3);
168  opt.datafile=cmdline.optset(4);
169  opt.datafilename=cmdline.string_arg(4);
170  opt.testcommentlimit=cmdline.optset(6);
171  opt.streamread=cmdline.optset(7);
172  opt.streamreadfile=cmdline.string_arg(7);
173  opt.countonly=cmdline.optset(8);
174  opt.nprint=cmdline.int_arg(9);
175  opt.nsamples=cmdline.int_arg(10);
176  opt.frequency=cmdline.double_arg(11);
177  opt.writesine=cmdline.optset(12);
178  opt.sinefilename=cmdline.string_arg(12);
179  opt.moretraces=cmdline.optset(13);
180  opt.modifiers=cmdline.string_arg(14);
181 
182  /*======================================================================*/
183  // go
184  //
185 
186  if ( opt.parameterline )
187  {
188  cout << "read parameter line from file " << opt.parameterlinefile << endl;
189  std::ifstream ifs(opt.parameterlinefile.c_str());
190  std::string line;
191  getline(ifs, line);
192  cout << "read parameter line is:" << endl;
193  cout << line << endl;
194  datrw::seife::ParameterLine paraline(line, opt.debug);
195  cout << "reproduced as:" << endl;
196  cout << paraline.line() << endl;
197  cout << "123456789012345678901234567890123456789012345678901234567890" <<
198  endl;
199  cout << "0 1 2 3 4 5 6" <<
200  endl;
201  } // if ( opt.parameterline )
202 
203  /*----------------------------------------------------------------------*/
204 
205  if ( opt.datafile )
206  {
207  cout << "read data file " << opt.datafilename << endl;
208  std::ifstream ifs(opt.datafilename.c_str());
209  datrw::seife::Header header(ifs);
210  datrw::seife::ParameterLine parameters=header.parameters();
211  ::sff::FREE free=header.comments();
212  datrw::Tdseries series=
213  datrw::util::readasciidouble<datrw::Tdseries>(ifs, parameters.nsamples(),
214  "seifetest");
215  cout << "the file is reproduced as:" << std::endl;
217  header.set(parameters);
218  free.append(SEIFETEST_VERSION);
219  header.set(free);
220  header.write(cout);
221  datrw::seife::write_series(cout, series);
222  } // if ( opt.datafile )
223 
224  /*----------------------------------------------------------------------*/
225 
226  if (opt.testcommentlimit)
227  {
228  cout << "test automatic limiting of comment lines" << endl;
229  ::sff::FREE comments;
230  std::ostringstream oss;
231  for (unsigned int i=0; i<60; ++i)
232  {
233  if (oss.str().length()>60) { oss.str(""); }
234  oss << i << " ";
235  comments.append(oss.str());
236  }
237  cout << "FREE block prepared is:" << endl;
238  comments.write(cout);
239  datrw::seife::Header header;
240  header.set(comments);
241  cout << "seife header produced from this:" << endl;
242  header.write(cout);
243  } // if (opt.testcommentlimit)
244 
245  /*----------------------------------------------------------------------*/
246 
247  if (opt.streamread)
248  {
249 
250  std::ifstream ifs(opt.streamreadfile.c_str());
251  datrw::iseifestream is(ifs, opt.modifiers);
252  if (is.hasfree())
253  {
254  if (opt.verbose)
255  {
256  cout << "file FREE block:" << endl;
257  is.free().write(std::cout);
258  }
259  else
260  {
261  cout << "file has FREE block" << endl;
262  }
263  }
264  else
265  {
266  cout << "file has no FREE block" << endl;
267  }
268  if (is.hassrce())
269  {
270  if (opt.verbose)
271  {
272  cout << "file SRCE line:" << endl;
273  cout << is.srce().line() << endl;;
274  }
275  else
276  {
277  cout << "file has SRCE line" << endl;
278  }
279  }
280  else
281  {
282  cout << "file has no SRCE line" << endl;
283  }
284  datrw::Tfseries fseries;
285 
286  if (opt.countonly)
287  {
288  is.skipseries();
289  }
290  else
291  {
292  is >> fseries;
293  }
294  if (is.hasfree())
295  {
296  if (opt.verbose)
297  {
298  cout << "trace FREE block:" << endl;
299  is.free().write(std::cout);
300  }
301  else
302  {
303  cout << "trace has FREE block" << endl;
304  }
305  }
306  else
307  {
308  cout << "trace has no FREE block" << endl;
309  }
310  if (is.hasinfo())
311  {
312  if (opt.verbose)
313  {
314  cout << "trace INFO line:" << endl;
315  cout << is.info().line() << endl;;
316  }
317  else
318  {
319  cout << "trace has INFO line" << endl;
320  }
321  }
322  else
323  {
324  cout << "trace has no INFO line" << endl;
325  }
326  std::cout << is.wid2().line() << std::endl;
327  if ((!opt.countonly) && (opt.verbose))
328  {
329  int npr=
330  opt.nprint < int(fseries.size()/2) ?
331  opt.nprint : fseries.size()/2;
332  for (int i=0; i<npr; ++i)
333  { std::cout << i << " " << fseries(i) << std::endl; }
334  std::cout << " ... " << std::endl;
335  for (int i=fseries.size()-npr; i<int(fseries.size()); ++i)
336  { std::cout << i << " " << fseries(i) << std::endl; }
337  }
338  } // if (opt.streamread)
339 
340  /*----------------------------------------------------------------------*/
341 
342  if (opt.writesine)
343  {
344  cout << "write sine wave to file " << opt.sinefilename << endl;
345  std::ofstream ofs(opt.sinefilename.c_str(), datrw::oseifestream::openmode);
346  datrw::oseifestream os(ofs, opt.modifiers, opt.debug);
347 
348  cout << "file data is stored in ";
349  // report output data format
350  switch (os.seriestype()) {
351  case datrw::Fint:
352  cout << "integer";
353  break;
354  case datrw::Ffloat:
355  cout << "single precision floating point";
356  break;
357  case datrw::Fdouble:
358  cout << "double precision floating point";
359  break;
360  case datrw::Fall:
361  cout << "any desired";
362  break;
363  default:
364  TFXX_abort("output stream uses unknown variable type!");
365  } // switch (os.seriestype())
366  cout << " variable type" << endl;
367 
368  cout << "file FREE data can ";
369  if (!os.handlesfilefree()) { cout << "NOT "; }
370  cout << "be handled" << endl;
371 
372  cout << "SRCE data can ";
373  if (!os.handlessrce()) { cout << "NOT "; }
374  cout << "be handled" << endl;
375 
376  int ntraces=1;
377  if (opt.moretraces) { ntraces=10; }
378  for (int itrace=0; itrace<ntraces; ++itrace)
379  {
380  cout << "write trace #" << itrace+1 << endl;
381  aff::Series<double> sine(opt.nsamples);
382  const double dt=2.e-3*(itrace+4);
383  for (int isample=0; isample<opt.nsamples; ++isample)
384  {
385  sine(sine.f()+isample)=(itrace+2)*sin(2.*3.141592653589793115998
386  *opt.frequency*dt*isample);
387  }
388 
389  cout << "trace FREE data can ";
390  if (!os.handlestracefree()) { cout << "NOT "; }
391  cout << "be handled" << endl;
392 
393  cout << "INFO data can ";
394  if (!os.handlesinfo()) { cout << "NOT "; }
395  cout << "be handled" << endl;
396 
397  ::sff::FREE free;
398  free.append(SEIFETEST_VERSION);
399  ::sff::WID2 wid2;
400  wid2.dt=dt;
401  wid2.nsamples=sine.size();
402  wid2.date=libtime::now();
403  os << wid2;
404  if (os.handlestracefree()) { os << free; }
405  os << sine;
406  }
407  } // if (opt.writesine)
408 
409 }
410 
411 /* ----- END OF seifetest.cc ----- */
seife format header parameter line
Definition: seifeio.h:69
int main(int iargc, char *argv[])
Definition: seifetest.cc:64
class to read seife data
Definition: seife.h:64
bool countonly
Definition: seifetest.cc:55
unsigned int nsamples() const
return number of samples
Definition: seifeio.h:92
seife reading and writing module (prototypes)
const char *const seife_standard_format
Definition: seifeio.cc:47
static const std::ios_base::openmode openmode
Definition: seife.h:96
aff::Series< float > Tfseries
Definition: types.h:46
bool streamread
Definition: seifetest.cc:54
libtime::TRelativeTime dt()
return sampling interval of HPMO data acquisition (i.e. 5 sec)
Definition: readhpmo.cc:83
aff::Series< double > Tdseries
Definition: types.h:45
void online_help(const std::string &format, std::ostream &os, const bool &modifierhelp)
Definition: formats.cc:120
bool writesine
Definition: seifetest.cc:55
void set(const ::sff::FREE &free)
set comments
Definition: seifeio.h:149
int nprint
Definition: pdastest.cc:52
bool moretraces
Definition: seifetest.cc:55
std::string sinefilename
Definition: seifetest.cc:56
bool debug
Definition: asciitest.cc:52
utilities used by more than one type of data reader (prototypes)
void write_series(std::ostream &os, const Tseries::Tcoc &s)
write samples to file
Definition: seifeio.cc:189
std::string format() const
return Fortran data format
Definition: seifeio.h:94
void supported_data_types(std::ostream &os)
Definition: formats.cc:290
bool verbose
Definition: asciitest.cc:51
int nsamples
Definition: asciitest.cc:54
std::string datafilename
Definition: seifetest.cc:56
void write(std::ostream &os) const
! write header to stream
Definition: seifeio.cc:163
#define SEIFETEST_VERSION
Definition: seifetest.cc:34
std::string streamreadfile
Definition: seifetest.cc:56
class to hold complete seife header
Definition: seifeio.h:132
class to write seife data
Definition: seife.h:88
double frequency
Definition: asciitest.cc:55
std::string parameterlinefile
Definition: seifetest.cc:56
bool datafile
Definition: seifetest.cc:54
std::string modifiers
Definition: seifetest.cc:59
bool testcommentlimit
Definition: seifetest.cc:54
common description of formats (prototypes)
std::string line() const
create line for output from values
Definition: seifeio.cc:112
bool parameterline
Definition: seifetest.cc:54