DATRW++ library: seismic data I/O with multiple formats
thiesdl1test.cc
Go to the documentation of this file.
1 /*! \file thiesdl1test.cc
2  * \brief Test Thies DL1 data reading module
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 13/09/2011
8  *
9  * Test Thies DL1 data reading module
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  * - 13/09/2011 V1.0 Thomas Forbriger
31  *
32  * ============================================================================
33  */
34 #define THIESDL1TEST_VERSION \
35  "THIESDL1TEST V1.0 Test Thies DL1 data reading module"
36 
37 #include <iostream>
38 #include <fstream>
39 #include <tfxx/commandline.h>
40 #include <datrwxx/thiesdl1.h>
41 #include <datrwxx/thiesdl1file.h>
42 #include <aff/dump.h>
43 
44 using std::cout;
45 using std::cerr;
46 using std::endl;
47 
48 /*----------------------------------------------------------------------*/
49 
51 {
52  cout << "file structure is ";
53  if (!f.isproperlyfilled()) { cout << "NOT "; }
54  cout << "properly filled" << endl;
55 } // void reportfilled(const datrw::thiesdl1::file& f)
56 
57 /*----------------------------------------------------------------------*/
58 
60 {
61  std::cout << "File Header contents:\n"
62  << "---------------------" << endl;
63  std::cout << " earliest: " <<
64  header.earliestdate.timestring() << std::endl;
65  std::cout << " latest: " <<
66  header.latestdate.timestring() << std::endl;
67  std::cout << " creation: " <<
68  header.creationdate.timestring() << std::endl;
69  std::cout << " expected initial: >>" <<
70  header.expectedinitialdataline << "<<" << std::endl;
71  std::cout << " expected final: >>" <<
72  header.expectedfinaldataline << "<<" << std::endl;
73  std::cout << " initial: >>" <<
74  header.initialdataline << "<<" << std::endl;
75  ::sff::verbose(cout, header.lines);
76  std::cout << "end of File Header" << endl;
77 } // void reportheader(const datrw::thiesdl1::Header& header)
78 
79 /*----------------------------------------------------------------------*/
80 
81 struct Options {
84  bool streamread, verbose;
86  int nprint;
87  std::string modifiers;
88 };
89 
90 /*----------------------------------------------------------------------*/
91 
92 int main(int iargc, char* argv[])
93 {
94 
95  // define usage information
96  char usage_text[]=
97  {
99  "usage: thiesdl1test [-v] [-h] [-f] [-trs] [-twt] [-stream] [-count]\n"
100  " [-int] [-np n] [-modifiers m]\n"
101  " file [file ...]" "\n"
102  " or: thiesdl1test --help|-h" "\n"
103  };
104 
105  // define full help text
106  char help_text[]=
107  {
108  "\n"
109  "-v be verbose\n"
110  "-h read file header and report entries\n"
111  "-f read complete file and report entries\n"
112  "-trs tolerate redundant samples\n"
113  "-twt tolerate wrong time\n"
114  "-stream use stream interface\n"
115  "-count only count samples in stream mode\n"
116  "-int use integer reading in stream mode\n"
117  "-np n display n samples in stream mode\n"
118  "-modifiers m use modifiers m in stream mode\n"
119  };
120 
121  // define commandline options
122  using namespace tfxx::cmdline;
123  static Declare options[]=
124  {
125  // 0: print help
126  {"help",arg_no,"-"},
127  // 1: verbose mode
128  {"v",arg_no,"-"},
129  // 2: read file header
130  {"h",arg_no,"-"},
131  // 3: read complete file
132  {"f",arg_no,"-"},
133  // 4: tolerate redundant samples
134  {"trs",arg_no,"-"},
135  // 5: tolerate wrong time
136  {"twt",arg_no,"-"},
137  // 6: use stream reading
138  {"stream",arg_no,"-"},
139  // 7: count samples
140  {"count",arg_no,"-"},
141  // 8: use integer input
142  {"int",arg_no,"-"},
143  // 9: use integer input
144  {"np",arg_yes,"10"},
145  // 10: use integer input
146  {"modifiers",arg_yes,""},
147  {NULL}
148  };
149 
150  // no arguments? print usage...
151  if (iargc<2)
152  {
153  cerr << usage_text << endl;
154  exit(0);
155  }
156 
157  // collect options from commandline
158  Commandline cmdline(iargc, argv, options);
159 
160  // help requested? print full help text...
161  if (cmdline.optset(0))
162  {
163  cerr << usage_text << endl;
164  cerr << help_text << endl;
165  exit(0);
166  }
167 
168  Options opt;
169  opt.verbose=cmdline.optset(1);
170  opt.readheader=cmdline.optset(2);
171  opt.readfile=cmdline.optset(3);
172  opt.tolerateredundant=cmdline.optset(4);
173  opt.toleratewrongtime=cmdline.optset(5);
174  opt.streamread=cmdline.optset(6);
175  opt.countonly=cmdline.optset(7);
176  opt.integer=cmdline.optset(8);
177  opt.nprint=cmdline.int_arg(9);
178  opt.modifiers=cmdline.string_arg(10);
179 
180  /*
181  // dummy operation: print option settings
182  for (int iopt=0; iopt<2; iopt++)
183  {
184  cout << "option: '" << options[iopt].opt_string << "'" << endl;
185  if (cmdline.optset(iopt)) { cout << " option was set"; }
186  else { cout << "option was not set"; }
187  cout << endl;
188  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
189  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
190  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
191  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
192  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
193  cout << " argument (bool): '";
194  if (cmdline.bool_arg(iopt))
195  { cout << "true"; } else { cout << "false"; }
196  cout << "'" << endl;
197  }
198  while (cmdline.extra()) { cout << cmdline.next() << endl; }
199  */
200 
201  // cycle through all file names
202  while (cmdline.extra())
203  {
204  std::string filename=cmdline.next();
205  cout << "process file " << filename << endl;
206 
207  /*----------------------------------------------------------------------*/
208 
209  if (opt.readheader)
210  {
211  cout << " read header\n"
212  << " -----------" << endl;
213  std::ifstream ifs(filename.c_str(), datrw::ithiesdl1stream::openmode);
216  reportheader(header);
217  } // if (opt.readheader)
218 
219  /*----------------------------------------------------------------------*/
220 
221  if (opt.readfile)
222  {
223  cout << " read file\n"
224  << " ---------" << endl;
225  std::ifstream ifs(filename.c_str(), datrw::ithiesdl1stream::openmode);
228  file.toleratewrongtime(opt.toleratewrongtime);
229  reportfilled(file);
230  file.readwithheader(ifs);
231  reportfilled(file);
232  reportheader(file.header());
233  datrw::Tdseries series=file.dseries();
234  aff::dump(series);
235  } // if (opt.readheader)
236 
237  /*----------------------------------------------------------------------*/
238 
239  if (opt.streamread)
240  {
241 
242  std::ifstream ifs(filename.c_str());
243  datrw::ithiesdl1stream is(ifs, opt.modifiers);
244  if (is.hasfree())
245  {
246  if (opt.verbose)
247  {
248  cout << "file FREE block:" << endl;
249  is.free().write(std::cout);
250  }
251  else
252  {
253  cout << "file has FREE block" << endl;
254  }
255  }
256  else
257  {
258  cout << "file has no FREE block" << endl;
259  }
260  if (is.hassrce())
261  {
262  if (opt.verbose)
263  {
264  cout << "file SRCE line:" << endl;
265  cout << is.srce().line() << endl;;
266  }
267  else
268  {
269  cout << "file has SRCE line" << endl;
270  }
271  }
272  else
273  {
274  cout << "file has no SRCE line" << endl;
275  }
276  datrw::Tfseries fseries;
277  datrw::Tiseries iseries;
278 
279  if (opt.countonly)
280  {
281  is.skipseries();
282  }
283  else
284  {
285  if (opt.integer)
286  {
287  is >> iseries;
288  }
289  else
290  {
291  is >> fseries;
292  }
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  if (opt.integer)
330  {
331  int npr=
332  opt.nprint < int(iseries.size()/2) ?
333  opt.nprint : iseries.size()/2;
334  for (int i=0; i<npr; ++i)
335  { std::cout << i << " " << iseries(i) << std::endl; }
336  std::cout << " ... " << std::endl;
337  for (int i=iseries.size()-npr; i<int(iseries.size()); ++i)
338  { std::cout << i << " " << iseries(i) << std::endl; }
339  }
340  else
341  {
342  int npr=
343  opt.nprint < int(fseries.size()/2) ?
344  opt.nprint : fseries.size()/2;
345  for (int i=0; i<npr; ++i)
346  { std::cout << i << " " << fseries(i) << std::endl; }
347  std::cout << " ... " << std::endl;
348  for (int i=fseries.size()-npr; i<int(fseries.size()); ++i)
349  { std::cout << i << " " << fseries(i) << std::endl; }
350  }
351  }
352  DATRW_assert(is.last(), "last flag was not set");
353  } // if (opt.streamread)
354 
355  } // while (cmdline.extra())
356 }
357 
358 /* ----- END OF thiesdl1test.cc ----- */
void reportheader(const datrw::thiesdl1::FileHeader &header)
Definition: thiesdl1test.cc:59
bool countonly
Definition: seifetest.cc:55
bool tolerateredundant
Definition: thiesdl1test.cc:83
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
FileHeader readheader(std::istream &is)
Read and parse a file header.
aff::Series< float > Tfseries
Definition: types.h:46
bool streamread
Definition: seifetest.cc:54
class to read Thies DL1 data
Definition: thiesdl1.h:64
void tolerateredundant(const bool flag=true)
set tolerance for redundant samples
Definition: thiesdl1file.h:167
aff::Series< double > Tdseries
Definition: types.h:45
Store a full data file.
Definition: thiesdl1file.h:125
int nprint
Definition: pdastest.cc:52
libtime::TAbsoluteTime latestdate
latest date
Definition: thiesdl1file.h:102
::sff::FREE lines
A collection of header lines with hash sign stripped off.
Definition: thiesdl1file.h:92
std::string initialdataline
Initial data line.
Definition: thiesdl1file.h:98
bool isproperlyfilled(const bool &throwerrors=false) const
libtime::TAbsoluteTime creationdate
creation date
Definition: thiesdl1file.h:104
void reportfilled(const datrw::thiesdl1::File &f)
Definition: thiesdl1test.cc:50
#define THIESDL1TEST_VERSION
Definition: thiesdl1test.cc:34
bool toleratewrongtime
Definition: thiesdl1test.cc:83
bool integer
Definition: thiesdl1test.cc:85
Store the header of a data file.
Definition: thiesdl1file.h:87
bool readfile
Definition: thiesdl1test.cc:82
bool verbose
Definition: asciitest.cc:51
module to read ThiesDL1 data files (prototypes)
int main(int iargc, char *argv[])
Definition: thiesdl1test.cc:92
aff::Series< int > Tiseries
Definition: types.h:47
std::string expectedfinaldataline
Expected final data line (as announced in header lines)
Definition: thiesdl1file.h:96
libtime::TAbsoluteTime earliestdate
earliest date
Definition: thiesdl1file.h:100
void dump(std::ostream &os, const SampleBlock &block)
dump one block of samples
Definition: readhpmo.cc:161
bool readheader
Definition: thiesdl1test.cc:82
std::string modifiers
Definition: seifetest.cc:59
static const std::ios_base::openmode openmode
Definition: thiesdl1.h:76
std::string expectedinitialdataline
Expected initial data line (as announced in header lines)
Definition: thiesdl1file.h:94