DATRW++ library: seismic data I/O with multiple formats
tsofttest.cc
Go to the documentation of this file.
1 /*! \file tsofttest.cc
2  * \brief test tsoft reading
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 17/09/2009
8  *
9  * test tsoft reading
10  *
11  * Copyright (c) 2009 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  * - 17/09/2009 V1.0 Thomas Forbriger (thof)
31  * - 07/01/2013 V1.1 hunting a bug when reading single trace data
32  * - 19/02/2104 thof: comment iunits and count since they are only set but
33  * never used
34  *
35  * ============================================================================
36  */
37 #define TSOFTTEST_VERSION \
38  "TSOFTTEST V1.1 test tsoft reading"
39 
40 #include <iostream>
41 #include <fstream>
42 #include <sffxx.h>
43 #include <tfxx/commandline.h>
44 #include <datrwxx/tsoftdata.h>
45 #include <datrwxx/tsoftreader.h>
46 #include <datrwxx/channeltranslation.h>
47 #include <datrwxx/tsoft.h>
48 #include <datrwxx/error.h>
49 
50 using std::cout;
51 using std::cerr;
52 using std::endl;
53 
54 /*----------------------------------------------------------------------*/
55 
56 struct Options {
58 }; // struct Options
59 
60 /*----------------------------------------------------------------------*/
61 
62 std::string passtrimws(std::string line)
63 {
65  return(line);
66 }
67 
68 /*----------------------------------------------------------------------*/
69 
70 int main(int iargc, char* argv[])
71 {
72 
73  // define usage information
74  char usage_text[]=
75  {
77  "usage: tsofttest file [-t1] [-t2] [-t3] [-t4] [-t5] [-v] [-D]" "\n"
78  " or: tsofttest --help|-h" "\n"
79  };
80 
81  // define full help text
82  char help_text[]=
83  {
84  "\n"
85  "file TSOFT file" "\n"
86  "-v be verbose" "\n"
87  "-D debug mode" "\n"
88  "-t1 test individual components" "\n"
89  "-t2 test trimws" "\n"
90  "-t3 test class tsoftfile" "\n"
91  "-t4 test translation table" "\n"
92  "-t5 test itsoftstream" "\n"
93  };
94 
95  // define commandline options
96  using namespace tfxx::cmdline;
97  static Declare options[]=
98  {
99  // 0: print help
100  {"help",arg_no,"-"},
101  // 1: verbose mode
102  {"v",arg_no,"-"},
103  // 2: test basic classes
104  {"t1",arg_no,"-"},
105  // 3: test trimws
106  {"t2",arg_no,"-"},
107  // 4: test class tsoftfile
108  {"t3",arg_no,"-"},
109  // 5: debug mode
110  {"D",arg_no,"-"},
111  // 6: test translation table
112  {"t4",arg_no,"-"},
113  // 7: test translation table
114  {"t5",arg_no,"-"},
115  {NULL}
116  };
117 
118  // no arguments? print usage...
119  if (iargc<2)
120  {
121  cerr << usage_text << endl;
122  exit(0);
123  }
124 
125  // collect options from commandline
126  Commandline cmdline(iargc, argv, options);
127 
128  // help requested? print full help text...
129  if (cmdline.optset(0))
130  {
131  cerr << usage_text << endl;
132  cerr << help_text << endl;
133  exit(0);
134  }
135 
136  Options opt;
137  opt.verbose=cmdline.optset(1);
138  opt.test1=cmdline.optset(2);
139  opt.test2=cmdline.optset(3);
140  opt.test3=cmdline.optset(4);
141  opt.debug=cmdline.optset(5);
142  opt.ttest=cmdline.optset(6);
143  opt.stest=cmdline.optset(7);
144 
145  /*
146  // dummy operation: print option settings
147  for (int iopt=0; iopt<2; iopt++)
148  {
149  cout << "option: '" << options[iopt].opt_string << "'" << endl;
150  if (cmdline.optset(iopt)) { cout << " option was set"; }
151  else { cout << "option was not set"; }
152  cout << endl;
153  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
154  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
155  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
156  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
157  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
158  cout << " argument (bool): '";
159  if (cmdline.bool_arg(iopt))
160  { cout << "true"; } else { cout << "false"; }
161  cout << "'" << endl;
162  }
163  while (cmdline.extra()) { cout << cmdline.next() << endl; }
164 
165  // dummy operation: print rest of command line
166  while (cmdline.extra()) { cout << cmdline.next() << endl; }
167  */
168 
169  TFXX_assert(cmdline.extra(), "missing file name");
170  std::string filename=cmdline.next();
171  if (opt.verbose)
172  { cout << "test TSOFT reading on file " << filename << endl; }
173 
174 
175  /*======================================================================*/
176  // test 1
177  if (opt.test1)
178  {
179  if (opt.verbose)
180  { cout << "test 1: test individual components" << endl; }
181  std::ifstream is(filename.c_str());
182  std::string line;
183  bool indata=false;
184  bool inchannels=false;
185  // bool inunits=false;
186  // int count=0;
187  while (is.good())
188  {
189  line=datrw::tsoft::getDOSline(is);
190  if (is.good())
191  {
192  datrw::tsoft::Line theline(line);
193  cout << "<" << theline.theline() << ">" << endl;
194  if (theline.hastag())
195  {
196  indata=false;
197  inchannels=false;
198  // inunits=false;
199  // count=0;
200  cout << "tag: " << theline.thetag() << "; ";
201  if (theline.hascontent())
202  {
203  cout << "content: " << theline.thecontent() << endl;
204  }
205  else
206  {
207  cout << "no content" << endl;
208  }
209  if (theline.thetag() == datrw::tsoft::tagdata) { indata=true; }
210  if (theline.thetag() == datrw::tsoft::tagchannels)
211  { inchannels=true; }
212  // if (theline.thetag() == datrw::tsoft::tagunits) { inunits=true; }
213  }
214  else if (indata)
215  {
216  cout << line << endl;
217  datrw::tsoft::Dataline theline(line);
218  cout << theline.time().timestring() << endl;
219  cout << " ";
220  for (int i=0;i<theline.nsamples();++i)
221  {
222  cout << theline.sample(i) << "; ";
223  }
224  cout << endl;
225  }
226  else if (inchannels)
227  {
229  ci.setchannelinfo(line);
230  cout << ci.thelocation() << " * "
231  << ci.theinstrument() << " * "
232  << ci.thedatatype() << endl;
233  }
234  }
235  }
236  }
237  // end of test 1
238 
239  /*======================================================================*/
240  // test 2
241  if (opt.test2)
242  {
243  if (opt.verbose)
244  { cout << "test 2: test trimws" << endl; }
245  cout << "++++" << passtrimws(" hdj wed ") << "++++" << endl;
246  cout << "++++" << passtrimws(" hdj wed") << "++++" << endl;
247  cout << "++++" << passtrimws("hdj wed ") << "++++" << endl;
248  cout << "++++" << passtrimws(" ") << "++++" << endl;
249  cout << "++++" << passtrimws("") << "++++" << endl;
250  }
251  // end of test 2
252 
253  /*======================================================================*/
254  // test 3
255  if (opt.test3)
256  {
257  if (opt.verbose)
258  { cout << "test 3: test class tsoftfile" << endl; }
259  std::ifstream is(filename.c_str());
260  datrw::tsoft::TSOFTfile thefile(is, opt.debug);
261 
262  // report file free
263  sff::FREE filefree;
264  filefree.append(thefile.free());
265  cout << filefree;
266 
267  const datrw::tsoft::Datacontainer& dc=thefile.dc();
268  // report on channels
269  for (int ich=0; ich<dc.nchannels(); ++ich)
270  {
271  cout << "channel #" << ich << ":" << endl;
272  cout << "===========" << endl;
273  const datrw::tsoft::Channeldata& cd=dc.channel(ich, true);
274  const datrw::tsoft::Channelinfo& ci=cd.chinfo();
275  sff::FREE channelfree;
276  channelfree.append(datrw::tsoft::channelinfofree(ci));
277  cout << channelfree;
278  cout << "number of available sequences: " << cd.ntraces() << endl;
279  for (int iseq=0; iseq<cd.ntraces(); ++iseq)
280  {
281  const datrw::tsoft::Datatrace& ds=cd.trace(iseq);
282  cout << " sequence #" << iseq << ":" << endl;
283  cout << " =============" << endl;
284  cout << " start: " << ds.date().timestring() << endl;
285  cout << " dt: " << ds.interval().timestring() << endl;
286  datrw::Tdseries s=ds.series();
287  cout << " number of samples: " << s.size() << endl;
288  int nsmp=s.size() < 10 ? s.size() : 10;
289  int f=s.f();
290  for (int isa=0; isa<nsmp; ++isa)
291  {
292  cout << " " << s(f+isa) << endl;
293  }
294  }
295  }
296  }
297  // end of test 3
298 
299  /*======================================================================*/
300  // t test
301  if (opt.ttest)
302  {
303  if (opt.verbose)
304  { cout << "ttest: translation table" << endl; }
307  {
308  cout << "The translation table is unique" << endl;
309  }
310  else
311  {
312  cout << "ATTENTION: The translation table is ambiguous" << endl;
313  }
314  }
315  // end of t test
316 
317  /*======================================================================*/
318  // s test
319  if (opt.stest)
320  {
321  if (opt.verbose)
322  { cout << "stest: itsoftstream" << endl; }
323  std::ifstream is(filename.c_str());
324  datrw::itsoftstream its(is);
325  cout << its.free();
326  DATRW_assert(its.good(),
327  "stream is not good although no trace has been read");
328  while (its.good())
329  {
330  datrw::Tdseries series=its.dseries();
331  sff::WID2 wid2=its.wid2();
332  sff::FREE free=its.free();
333  cout << endl;
334  cout << "WID2 line of next trace:" << endl;
335  cout << wid2.line();
336  cout << "FREE block of next trace:" << endl;
337  cout << free;
338  cout << "some samples of of next trace:" << endl;
339  int nsa=series.size() < 10 ? series.size() : 10;
340  for (int isa=0; isa<nsa; ++isa)
341  {
342  cout << series(isa+series.f()) << endl;
343  }
344  }
345  }
346  // end of s test
347 }
348 
349 /* ----- END OF tsofttest.cc ----- */
contains channel info.
Definition: tsoftdata.h:173
std::string thetag() const
return tag
Definition: tsoftdata.h:148
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
int ntraces() const
number of data traces
Definition: tsoftdata.h:352
const char * tagdata
TSOFT data tag.
Definition: tsoftdata.cc:54
bool ttest
Definition: tsofttest.cc:57
bool test3
Definition: tsofttest.cc:57
trace of contiguous data.
Definition: tsoftdata.h:242
contains takes one line and splits it into tag and information.
Definition: tsoftdata.h:141
void reporttranslation(std::ostream &os)
report translation table
const std::string & thedatatype() const
return datatype
Definition: tsoftdata.h:190
libtime::TAbsoluteTime time() const
return time
Definition: tsoftdata.h:221
std::string passtrimws(std::string line)
Definition: tsofttest.cc:62
void series(const Tdseries &s)
set series
Definition: tsoftdata.h:249
double sample(const int &i) const
return specific sample
Definition: tsoftdata.cc:145
bool test2
Definition: tsofttest.cc:57
data container.
Definition: tsoftdata.h:385
int nsamples() const
return number of samples in line
Definition: tsoftdata.h:225
aff::Series< double > Tdseries
Definition: types.h:45
int nchannels() const
return number of channels
Definition: tsoftdata.h:421
const Datatrace & trace(const unsigned int &i) const
return data trace
Definition: tsoftdata.h:332
bool translationisunique(const bool &verbose)
check uniqueness of translation table
exception class declaration for libdatrwxx (prototypes)
void trimws(std::string &line)
helper function trimws
Definition: tsoftdata.cc:375
std::string thecontent() const
return content
Definition: tsoftdata.h:150
const std::string & theinstrument() const
return instrument
Definition: tsoftdata.h:188
read a complete file
Definition: tsoftreader.h:86
#define TSOFTTEST_VERSION
Definition: tsofttest.cc:37
bool debug
Definition: asciitest.cc:52
class to read TSOFT data
Definition: tsoft.h:63
void chinfo(const Channelinfo &ci)
set channel info
Definition: tsoftdata.h:346
Tlos channelinfofree(const Channelinfo &ci)
prepare free comment block from channel info
Definition: tsoftdata.cc:403
void setchannelinfo(const std::string &line)
set channel info
Definition: tsoftdata.cc:108
const std::string & thelocation() const
return location
Definition: tsoftdata.h:186
bool verbose
Definition: asciitest.cc:51
bool stest
Definition: tsofttest.cc:57
date for one channel.
Definition: tsoftdata.h:320
void date(const libtime::TAbsoluteTime d)
set time of first sample
Definition: tsoftdata.h:245
bool test1
Definition: tsofttest.cc:57
int main(int iargc, char *argv[])
Definition: tsofttest.cc:70
const char * tagchannels
TSOFT channels tag.
Definition: tsoftdata.cc:55
contains one data line.
Definition: tsoftdata.h:216
Channeldata & channel(const unsigned int &i, const bool &debug=false)
return data for specific channel
Definition: tsoftdata.h:397
classes for TSOFT in SFF (prototypes)
void interval(const libtime::TRelativeTime i)
set sampling interval
Definition: tsoftdata.h:247
bool hascontent() const
check whether this line has a content
Definition: tsoftdata.h:154
bool hastag() const
check whether this line has a tag
Definition: tsoftdata.h:152
std::string getDOSline(std::istream &is)
helper function getDOSline
Definition: tsoftdata.cc:423
std::string theline() const
return line
Definition: tsoftdata.h:146