SFF++ library: reading and writing SFF from C++
sfftest.cc
Go to the documentation of this file.
1 
35 #define SFFTEST_VERSION \
36  "SFFTEST V1.0 test library modules"
37 
38 #include <fstream>
39 #include <iostream>
40 #include <sffxx.h>
41 #include <aff/series.h>
42 #include <tfxx/commandline.h>
43 
44 using std::cout;
45 using std::cerr;
46 using std::endl;
47 
48 /*----------------------------------------------------------------------*/
49 
51 {
52  cout << "test internals\n"
53  << "--------------\n" << endl;
54  cout << "sizeof(sff::WID2): " << sizeof(sff::WID2) << endl;
55 } // void test_internals()
56 
57 /*----------------------------------------------------------------------*/
58 
60 {
61  cout << "test WIDX functions" << endl
62  << "-------------------" << endl;
63 
64  sff::WID2 mywid2;
65  sff::verbose(cout, mywid2);
66  mywid2.date += libtime::TRelativeTime(0,0,0,0,432,567);
67  mywid2.station="BFO";
68  mywid2.channel="UGZ";
69  mywid2.instype="ET-19";
70  mywid2.nsamples=2048;
71  mywid2.dt=60.;
72  mywid2.hang=98.3342;
73  mywid2.vang=-15.3;
74  sff::verbose(cout, mywid2);
75 
76  std::string Xline=sff::WIDXline(mywid2);
77  cout << Xline << std::endl;
78 
79  sff::WID2 readwid2=sff::WIDXline(Xline);
80  sff::verbose(cout, readwid2);
81 
82 } // test_extended_functions()
83 
84 /*----------------------------------------------------------------------*/
85 
87 {
88  cout << "test line functions" << endl
89  << "-------------------" << endl;
90 
91  sff::STAT mystat;
92  cout << mystat.line();
93  cout << endl;
94 
95  sff::SRCE mysrce;
96  cout << mysrce.line();
97  cout << endl;
98 
99  sff::DAST mydast;
100  cout << mydast.line();
101  cout << endl;
102 
103  sff::INFO myinfo;
104  cout << myinfo.line();
105  cout << endl;
106 
107  sff::WID2 mywid2;
108  cout << mywid2.line();
109  mywid2.station="BFO";
110  mywid2.channel="UGZ";
111  mywid2.instype="ET-19";
112  mywid2.nsamples=2048;
113  mywid2.dt=10.;
114  cout << mywid2.line();
115  cout << endl;
116 
117  sff::FREE myfree;
118  myfree.lines.push_back("Hi there!");
119  myfree.lines.push_back("A second free line");
120  myfree.write(cout);
121  cout << endl;
122 
123 } // test_line_functions()
124 
125 /*----------------------------------------------------------------------*/
126 
128 {
129  cout << "test verbose functions" << endl
130  << "----------------------" << endl;
131 
132  sff::STAT mystat;
133  sff::verbose(cout, mystat);
134 
135  sff::SRCE mysrce;
136  sff::verbose(cout, mysrce);
137 
138  sff::DAST mydast;
139  sff::verbose(cout, mydast);
140 
141  sff::INFO myinfo;
142  sff::verbose(cout, myinfo);
143 
144  sff::WID2 mywid2;
145  sff::verbose(cout, mywid2);
146  mywid2.station="BFO";
147  mywid2.channel="UGZ";
148  mywid2.instype="ET-19";
149  mywid2.nsamples=2048;
150  mywid2.dt=10.;
151  sff::verbose(cout, mywid2);
152 
153  sff::FREE myfree;
154  myfree.lines.push_back("Hi there!");
155  myfree.lines.push_back("A second free line");
156  sff::verbose(cout, myfree);
157 
158  sff::TraceHeader myth;
159  sff::verbose(cout, myth);
160 
161  sff::FileHeader myfh;
162  sff::verbose(cout, myfh);
163 
164 } // test_verbose_functions()
165 
166 /*----------------------------------------------------------------------*/
167 
169 {
170  cout << "test write wrappers" << endl
171  << "-------------------" << endl;
172 
173  sff::SRCE mysrce;
174  sff::INFO myinfo;
175  sff::FREE myfree;
176  myfree.lines.push_back("auch ein text");
177  sff::WID2 mywid2;
178  mywid2.station="BFO";
179  mywid2.channel="UGZ";
180  mywid2.instype="ET-19";
181  mywid2.nsamples=2048;
182  mywid2.dt=10.;
183 
184  { cout << sff::FileHeader(); }
185  cout << endl << "next:" << endl;
186  { cout << sff::FileHeader(mysrce); }
187  cout << endl << "next:" << endl;
188  { cout << sff::FileHeader(myfree); }
189  cout << endl << "next:" << endl;
190  { cout << sff::FileHeader(mysrce, myfree); }
191 
192  cout << endl << "next:" << endl;
193  { cout << sff::TraceHeader(mywid2); }
194  cout << endl << "next:" << endl;
195  { cout << sff::TraceHeader(mywid2, myinfo); }
196  cout << endl << "next:" << endl;
197  { cout << sff::TraceHeader(mywid2, myfree); }
198  cout << endl << "next:" << endl;
199  { cout << sff::TraceHeader(mywid2, myinfo, myfree); }
200  cout << endl << "next:" << endl;
201  { cout << sff::TraceHeader(mywid2, myinfo,true); }
202 }
203 
204 /*----------------------------------------------------------------------*/
205 
206 typedef aff::Series<double> Tseries;
207 void scale_series(Tseries& s, const double& f)
208 {
209  for (int i=s.first(); i<=s.last(); i++)
210  { s(i) *= f; }
211  cout << "scale by " << f << endl;
212 }
213 
214 void scaling(const sff::TraceHeader& hd)
215 {
216  if (hd.scale())
217  { cout << "will be scaled with ampfac "; }
218  else
219  { cout << "will not be scaled and ampfac is "; }
220  cout << hd.dast().ampfac << endl;
221 }
222 
224 {
225  cout << "test waveform normalizer" << endl
226  << "------------------------" << endl;
227 
228  cout << "limit: " << sff::WaveformNormalizer::limit << " ";
229  cout << "2^23: " << std::pow(2.,23.) << endl;
230 
231  const int msamp=100;
232  Tseries series(msamp);
233  for (int i=series.first(); i<=series.last(); i++)
234  { series(i)=sin(3.1415926*i*5/msamp); }
235 
236  sff::WID2 wid2;
237  sff::TraceHeader hd(wid2);
238  hd.scanseries(series);
239  scaling(hd);
240 
241  scale_series(series, 1.e10);
242  hd.scanseries(series);
243  scaling(hd);
244 
245  scale_series(series, 1.e-4);
246  hd.scanseries(series, sff::NM_ifneeded);
247  scaling(hd);
248 
249  scale_series(series, 1.e4);
250  hd.scanseries(series, sff::NM_ifneeded);
251  scaling(hd);
252 
253  scale_series(series, 1.e-4);
254  hd.scanseries(series, sff::NM_one);
255  scaling(hd);
256 
257 #ifdef ILLEGAL1
258  scale_series(series, 1.e4);
259  hd.scanseries(series, sff::NM_one);
260  scaling(hd);
261 #endif
262 }
263 
264 /*----------------------------------------------------------------------*/
265 
267 {
268  char filename[]="junk.sff";
269  cout << "test writing to file: " << filename << endl
270  << "---------------------" << endl;
271 
272  const int msamp=1000;
273  Tseries series(msamp);
274  for (int i=series.first(); i<=series.last(); i++)
275  { series(i)=10.*sin(3.1415926*i*5/msamp); }
276 
277  sff::SRCE mysrce;
278  mysrce.cx=1.;
279  mysrce.cy=2.;
280  mysrce.cz=3.;
281  mysrce.type="Quelle";
282 
283  sff::INFO myinfo;
284 
285  sff::FREE myfree;
286  myfree.lines.push_back("auch ein text");
287 
288  sff::WID2 mywid2;
289  mywid2.station="BFO";
290  mywid2.channel="UGZ";
291  mywid2.instype="ET-19";
292  mywid2.nsamples=2048;
293  mywid2.dt=10.;
294 
295  sff::FileHeader fhd(mysrce,myfree);
296  std::ofstream os(filename);
297  os << fhd;
298  {
299  sff::TraceHeader hd(mywid2,myinfo,myfree);
300  os << sff::OutputWaveform<Tseries>(series, hd, sff::NM_maxdyn);
301  }
302  {
303  myinfo.cx=10.;
304  sff::TraceHeader hd(mywid2,myinfo,myfree,true);
305  os << sff::OutputWaveform<Tseries>(series, hd, sff::NM_ifneeded);
306  }
307  cout << "you will find the data in " << filename << endl;
308 }
309 
310 /*----------------------------------------------------------------------*/
311 
312 void test_read_file(const bool& debug)
313 {
314  char infile[]="junk.sff";
315  char outfile[]="junk2.sff";
316  cout << "test reading from file: " << infile << endl
317  << "-----------------------" << endl;
318 
319  {
320  char c;
321  std::cerr << "manipulate " << infile
322  << " - if you like to..." << std::endl;
323  std::cin.get(c);
324  }
325 
326  std::ifstream is(infile);
327  sff::FileHeader fhd(is);
328 
329  std::ofstream os(outfile);
330  os << fhd;
331 
332  if (fhd.hasfree())
333  {
334  cout << "FREE: " << std::endl << fhd.free() << endl;
335  }
336  if (fhd.hassrce())
337  {
338  cout << "SRCEtime: " << fhd.srce().date.timestring() << endl;
339  }
340 
341  bool last=false;
342  while (!last)
343  {
344  sff::InputWaveform<Tseries> iwf(debug);
345  try {
346  is >> iwf;
347  } catch (GSE2::Terror) {
348  cerr << "UUpppsss" << endl;
349  sff::WID2 wid2line(iwf.header().wid2());
350  cerr << wid2line.line();
351  Tseries series(iwf.series());
352  cerr << series.size() << " samples" << endl;
353  throw;
354  }
355  cerr << iwf.header().wid2().line() << endl;
356  last=iwf.last();
357  os << sff::OutputWaveform<Tseries>(iwf.series(),
358  iwf.header(),
360  }
361  cout << "you will find the data in " << outfile << endl;
362 }
363 
364 /*----------------------------------------------------------------------*/
365 
367 {
368  char outfile[]="junk3.sff";
369  cout << "test kipping every second trace" << endl
370  << "-------------------------------" << endl;
371 
372  cout << "enter name of input file: ";
373  std::string infile;
374  std::cin >> infile;
375 
376  std::ifstream is(infile.c_str());
377  sff::FileHeader fhd(is);
378 
379  std::ofstream os(outfile);
380  os << fhd;
381 
382  if (fhd.hassrce())
383  {
384  cout << "SRCEtime: " << fhd.srce().date.timestring() << endl;
385  }
386 
387  bool last=false;
388  while (!last)
389  {
390  sff::SkipWaveform swf(is);
391  cout << swf.header().wid2().line() << endl;
392  last=swf.last();
393  if (!last)
394  {
396  last=iwf.last();
397  os << sff::OutputWaveform<Tseries>(iwf.series(),
398  iwf.header(),
400  }
401  else
402  {
403  Tseries dummy(5);
404  dummy=0.;
405  sff::TraceHeader ihd(swf.header());
406  sff::TraceHeader ohd(ihd.wid2(),true);
407  os << sff::OutputWaveform<Tseries>(dummy, ohd, sff::NM_one);
408  }
409  }
410  cout << "you will find the data in " << outfile << endl;
411 }
412 
413 /*----------------------------------------------------------------------*/
414 
415 struct Options {
417 }; // struct Options
418 
419 int main(int iargc, char* argv[])
420 {
421 
422  // define usage information
423  char usage_text[]=
424  {
425  "usage: sfftest [-D] [-v] [-x] [-i]" "\n"
426  " or: sfftest --help|-h" "\n"
427  };
428 
429  // define full help text
430  char help_text[]=
431  {
432  "-D produce debug output" "\n"
433  "notice: you have to pass at least one dummy argument to make" "\n"
434  "the program run" "\n"
435  "-v test verbose output" "\n"
436  "-x test extended WIDX format" "\n"
437  "-i test library internals" "\n"
438  };
439 
440  // define commandline options
441  using namespace tfxx::cmdline;
442  static Declare options[]=
443  {
444  // 0: print help
445  {"help",arg_no,"-"},
446  // 1: debug mode
447  {"D",arg_no,"-"},
448  // 2: test verbose functions
449  {"v",arg_no,"-"},
450  // 3: test WIDX
451  {"x",arg_no,"-"},
452  // 4: test internals
453  {"i",arg_no,"-"},
454  {NULL}
455  };
456 
457  // no arguments? print usage...
458  if (iargc<2)
459  {
460  cerr << usage_text << endl;
461  exit(0);
462  }
463 
464  // collect options from commandline
465  Commandline cmdline(iargc, argv, options);
466 
467  // help requested? print full help text...
468  if (cmdline.optset(0))
469  {
470  cerr << usage_text << endl;
471  cerr << help_text << endl;
472  exit(0);
473  }
474 
475  Options opt;
476  opt.debug=cmdline.optset(1);
477  opt.verbose=cmdline.optset(2);
478  opt.extended=cmdline.optset(3);
479  opt.internals=cmdline.optset(4);
480 
481  if (opt.verbose)
482  {
483  test_verbose_functions(); cout << endl;
484  }
485  else if (opt.extended)
486  {
487  test_extended_functions(); cout << endl;
488  }
489  else if (opt.internals)
490  {
491  test_internals(); cout << endl;
492  }
493  else
494  {
495  test_line_functions(); cout << endl;
496  test_write_wrappers(); cout << endl;
497  test_waveform_normalizer(); cout << endl;
498  test_write_file(); cout << endl;
499  test_read_file(opt.debug); cout << endl;
500  test_skip_trace(); cout << endl;
501  }
502 }
503 
504 /* ----- END OF sfftest.cc ----- */
void test_extended_functions()
Definition: sfftest.cc:59
void test_skip_trace()
Definition: sfftest.cc:366
static const int limit
the absolute maximum amplitude (one-sided) to which the time series will be normalized.
Definition: sffxx.h:270
SFF trace header elements.
Definition: sffxx.h:282
std::string line() const
Definition: sffxx.cc:493
aff::Series< double > Tseries
Definition: sfftest.cc:206
void write(std::ostream &os) const
Definition: sffxx.cc:565
std::string line() const
Definition: sffxx.cc:357
do not scale
Definition: sffxx.h:115
Tlines lines
Definition: sffxx.h:163
TraceHeader header() const
Definition: sffxx.h:371
std::string line() const
Definition: sffxx.cc:754
double cz
Definition: sffxx.h:176
void scanseries(const C &, const Enormmode &nm=NM_maxdyn)
Definition: sffxx.h:409
std::string line() const
Definition: sffxx.cc:657
void test_write_wrappers()
Definition: sfftest.cc:168
const bool & scale() const
Definition: sffxx.h:309
std::string station
Station code.
Definition: sffxx.h:216
const FREE & free() const
Definition: sffxx.h:250
const bool & last() const
Definition: sffxx.h:372
int main(int iargc, char *argv[])
Definition: sfftest.cc:419
std::string instype
instrument type
Definition: sffxx.h:223
double cx
Definition: sffxx.h:202
std::string channel
FDSN channel code.
Definition: sffxx.h:217
bool extended
Definition: sfftest.cc:416
int nsamples
number of samples
Definition: sffxx.h:219
libtime::TAbsoluteTime date
time of first sample
Definition: sffxx.h:215
void test_verbose_functions()
Definition: sfftest.cc:127
void test_line_functions()
Definition: sfftest.cc:86
std::string WIDXline(const sff::WID2 &wid2, const bool &debug=false)
write WID2 information in extended format
Definition: widXio.cc:115
std::string type
Definition: sffxx.h:173
void test_waveform_normalizer()
Definition: sfftest.cc:223
bool debug
Definition: sfftest.cc:416
Tcontainer series() const
Definition: sffxx.h:355
void test_read_file(const bool &debug)
Definition: sfftest.cc:312
scale if largest amplitude larger than limit
Definition: sffxx.h:117
bool verbose
Definition: sfftest.cc:416
Waveform Header.
Definition: sffxx.h:209
double vang
veritcal orientation
Definition: sffxx.h:225
void test_internals()
Definition: sfftest.cc:50
const WID2 & wid2() const
Definition: sffxx.h:305
double ampfac
Definition: sffxx.h:187
void scale_series(Tseries &s, const double &f)
Definition: sfftest.cc:207
double cx
Definition: sffxx.h:176
bool internals
Definition: sfftest.cc:416
void test_write_file()
Definition: sfftest.cc:266
double cy
Definition: sffxx.h:176
TraceHeader header() const
Definition: sffxx.h:356
double dt
sampling interval (sec)
Definition: sffxx.h:220
scale for maximum dynamic range
Definition: sffxx.h:116
std::string line() const
Definition: sffxx.cc:218
void scaling(const sff::TraceHeader &hd)
Definition: sfftest.cc:214
double hang
horizontal orientation
Definition: sffxx.h:224
SFF library (prototypes)
SFF file header elements.
Definition: sffxx.h:232
void verbose(std::ostream &os, const WID2 &wid2)
Definition: sffverbose.cc:93
const bool & last() const
Definition: sffxx.h:357
const DAST & dast() const
Definition: sffxx.h:306