DATRW++ library: seismic data I/O with multiple formats
binarytest.cc
Go to the documentation of this file.
1 /*! \file binarytest.cc
2  * \brief test binary format I/O functions
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 02/09/2011
8  *
9  * test binary 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  * - 19/02/2014 thof: include datrwxx/binary.h
32  *
33  * ============================================================================
34  */
35 #define BINARYTEST_VERSION \
36  "BINARYTEST V1.0 test binary format I/O functions"
37 
38 #include <iostream>
39 #include <fstream>
40 #include <string>
41 #include <tfxx/commandline.h>
42 #include <aff/seriesoperators.h>
43 #include <aff/dump.h>
44 #include <datrwxx/binary.h>
45 #include <datrwxx/ibinstream.h>
46 #include <datrwxx/obinstream.h>
47 
48 /*----------------------------------------------------------------------*/
49 
50 using std::cout;
51 using std::cerr;
52 using std::endl;
53 
54 /*----------------------------------------------------------------------*/
55 
56 struct Options {
58  std::string iofile;
59 }; // struct Options
60 
61 /*----------------------------------------------------------------------*/
62 
63 void values(const char& out, const char& in)
64 {
65  cout << "written to file: " << int(out) << endl;
66  cout << "read from file: " << int(in) << endl;
67  if (out&::datrw::binary::Ffree)
68  { cout << "* flag set: free" << endl; }
69  if (out&::datrw::binary::Fsrce)
70  { cout << "* flag set: srce" << endl; }
71  if (out&::datrw::binary::Finfo)
72  { cout << "* flag set: info" << endl; }
73  if (out&::datrw::binary::Fdouble)
74  { cout << "* flag set: double" << endl; }
75  if (out&::datrw::binary::Ffloat)
76  { cout << "* flag set: float" << endl; }
77  if (out&::datrw::binary::Fint)
78  { cout << "* flag set: int" << endl; }
79 }
80 
81 /*----------------------------------------------------------------------*/
82 
83 template<class C>
84 void reportsff(const C& out, const C& in)
85 {
86  cout << "written to file:" << endl;
87  ::sff::verbose(cout, out);
88  cout << "read from file:" << endl;
89  ::sff::verbose(cout, in);
90 }
91 
92 /*----------------------------------------------------------------------*/
93 
94 template<class C>
95 void reportseries(const C& out, const C& in)
96 {
97  cout << "written to file:" << endl;
98  ::aff::dump(out);
99  cout << "read from file:" << endl;
100  ::aff::dump(in);
101  cout << "residual:" << endl;
102  ::aff::dump(out-in);
103 }
104 
105 /*----------------------------------------------------------------------*/
106 
107 int main(int iargc, char* argv[])
108 {
109 
110  // define usage information
111  char usage_text[]=
112  {
113  BINARYTEST_VERSION "\n"
114  "usage: binarytest [-v] [-iotest file] [-skip]" "\n"
115  " or: binarytest --help|-h" "\n"
116  };
117 
118  // define full help text
119  char help_text[]=
120  {
121  "\n"
122  "-v be verbose\n"
123  "-iotest file execute fixed I/O test with file \"file\"\n"
124  "-skip skip samples of series, just count\n"
125  };
126 
127  // define commandline options
128  using namespace tfxx::cmdline;
129  static Declare options[]=
130  {
131  // 0: print help
132  {"help",arg_no,"-"},
133  // 1: verbose mode
134  {"v",arg_no,"-"},
135  // 2: skip samples
136  {"skip",arg_no,"-"},
137  // 3: verbose mode
138  {"iotest",arg_yes,"-"},
139  {NULL}
140  };
141 
142  // no arguments? print usage...
143  if (iargc<2)
144  {
145  cerr << usage_text << endl;
146  exit(0);
147  }
148 
149  // collect options from commandline
150  Commandline cmdline(iargc, argv, options);
151 
152  // help requested? print full help text...
153  if (cmdline.optset(0))
154  {
155  cerr << usage_text << endl;
156  cerr << help_text << endl;
157  exit(0);
158  }
159 
160  Options opt;
161  opt.verbose=cmdline.optset(1);
162  opt.skipsamples=cmdline.optset(2);
163  opt.iotest=cmdline.optset(3);
164  opt.iofile=cmdline.string_arg(3);
165 
166  /*
167  // dummy operation: print option settings
168  for (int iopt=0; iopt<2; iopt++)
169  {
170  cout << "option: '" << options[iopt].opt_string << "'" << endl;
171  if (cmdline.optset(iopt)) { cout << " option was set"; }
172  else { cout << "option was not set"; }
173  cout << endl;
174  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
175  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
176  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
177  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
178  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
179  cout << " argument (bool): '";
180  if (cmdline.bool_arg(iopt))
181  { cout << "true"; } else { cout << "false"; }
182  cout << "'" << endl;
183  }
184  while (cmdline.extra()) { cout << cmdline.next() << endl; }
185 
186  // dummy operation: print rest of command line
187  while (cmdline.extra()) { cout << cmdline.next() << endl; }
188  */
189 
190  /*----------------------------------------------------------------------*/
191 
192  if (opt.iotest)
193  {
194  // prepare test data
195  datrw::Tiseries iseries(-10,10);
196  datrw::Tdseries dseries(15);
197  datrw::Tfseries fseries(20);
198  for (int i=iseries.f(); i<=iseries.l(); ++i)
199  { iseries(i)=2*i; }
200  for (int i=dseries.f(); i<=dseries.l(); ++i)
201  { dseries(i)=double(i)*i; }
202  for (int i=fseries.f(); i<=fseries.l(); ++i)
203  { fseries(i)=sin((i-fseries.f())*2.
204  *3.141592653589793115998/(fseries.size()-1)); }
205  ::sff::WID2 wid2;
206  ::sff::INFO info;
207  ::sff::SRCE srce;
208  ::sff::FREE free;
209 
210  wid2.nsamples=50;
211  wid2.dt=1.e-4;
212  wid2.date=libtime::TAbsoluteTime(2010,5,6,18,23,12,13,14);
213  wid2.station="STAT";
214  wid2.channel="CHA";
215  wid2.auxid="AUX";
216  wid2.instype="my seismometer type";
217 
218  info.cx=1.;
219  info.cy=2.;
220  info.cz=3.;
221  info.nstacks=49;
222 
223  srce.cx=-1.;
224  srce.cy=-2.;
225  srce.cz=-3.;
226  srce.type="my source type for this test";
227  srce.date=libtime::TAbsoluteTime(2006,1,1,9,15,12,345,678);
228 
229  free.append(BINARYTEST_VERSION);
230  free.append("my free comment line");
231 
232  char fflags=
235  char tflags=
237 
238  const char* const magic="B123";
239  // write data
240  {
241  std::ofstream ofs(opt.iofile.c_str(),
242  std::ios_base::out|std::ios_base::binary);
244 
245  os << wid2;
246  os << free;
247  os << fseries;
248  os << info;
249  os << dseries;
250  os << srce;
251  os << char(fflags);
252  os << char(tflags);
253  os << iseries;
254  }
255 
256  // reread file
257  {
258  std::ifstream ifs(opt.iofile.c_str(),
259  std::ios_base::in|std::ios_base::binary);
261 
262  ::sff::WID2 inwid2;
263  is >> inwid2;
264  reportsff(wid2, inwid2);
265 
266  ::sff::FREE infree;
267  is >> infree;
268  reportsff(free, infree);
269 
270  if (opt.skipsamples)
271  {
272  cout << "skipped " << is.skipfseries() << " samples" << endl;
273  }
274  else
275  {
276  datrw::Tfseries infseries;
277  is >> infseries;
278  reportseries(fseries, infseries);
279  }
280 
281  ::sff::INFO ininfo;
282  is >> ininfo;
283  reportsff(info, ininfo);
284 
285  if (opt.skipsamples)
286  {
287  cout << "skipped " << is.skipdseries() << " samples" << endl;
288  }
289  else
290  {
291  datrw::Tdseries indseries;
292  is >> indseries;
293  reportseries(dseries, indseries);
294  }
295 
296  ::sff::SRCE insrce;
297  is >> insrce;
298  reportsff(srce, insrce);
299 
300  char inflags;
301  is >> inflags;
302  values(char(fflags), inflags);
303  is >> inflags;
304  values(char(tflags), inflags);
305 
306  if (opt.skipsamples)
307  {
308  cout << "skipped " << is.skipiseries() << " samples" << endl;
309  }
310  else
311  {
312  datrw::Tiseries iniseries;
313  is >> iniseries;
314  reportseries(iseries, iniseries);
315  }
316  }
317 
318  }
319 }
320 
321 /* ----- END OF binarytest.cc ----- */
std::string iofile
Definition: binarytest.cc:58
aff::Series< float > Tfseries
Definition: types.h:46
void values(const char &out, const char &in)
Definition: binarytest.cc:63
trace has INFO header
Definition: binary.h:81
bool skipsamples
Definition: binarytest.cc:57
write raw binary data (prototypes)
const char *const magic
magic number to identify file type and bytesex
Definition: binary.cc:53
aff::Series< double > Tdseries
Definition: types.h:45
binary output for basic types and classes
Definition: obinstream.h:56
bool iotest
Definition: binarytest.cc:57
trace has int data
Definition: binary.h:84
trace has SRCE header
Definition: binary.h:79
trace has float data
Definition: binary.h:83
int main(int iargc, char *argv[])
Definition: binarytest.cc:107
trace has double data
Definition: binary.h:82
bool verbose
Definition: asciitest.cc:51
aff::Series< int > Tiseries
Definition: types.h:47
stream like class for binary input of basic types and classes
Definition: ibinstream.h:56
void dump(std::ostream &os, const SampleBlock &block)
dump one block of samples
Definition: readhpmo.cc:161
#define BINARYTEST_VERSION
Definition: binarytest.cc:35
trace has FREE header
Definition: binary.h:80
void reportseries(const C &out, const C &in)
Definition: binarytest.cc:95
void reportsff(const C &out, const C &in)
Definition: binarytest.cc:84