DATRW++ library: seismic data I/O with multiple formats
writetest.cc
Go to the documentation of this file.
1 /*! \file writetest.cc
2  * \brief test writing a file
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 20/02/2010
8  *
9  * test writing a file
10  *
11  * Copyright (c) 2010 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  * - 20/02/2010 V1.0 Thomas Forbriger
31  *
32  * ============================================================================
33  *
34  * \example writetest.cc
35  * \brief This program is an example on writing data with libdatrwxx.
36  */
37 #define WRITETEST_VERSION \
38  "WRITETEST V1.0 test writing a file"
39 
40 #include <iostream>
41 #include <fstream>
42 #include <tfxx/commandline.h>
43 #include <datrwxx/sff.h>
44 #include <datrwxx/writeany.h>
45 #include <datrwxx/util.h>
46 
47 using std::cout;
48 using std::cerr;
49 using std::endl;
50 
51 struct Options {
52  bool verbose, debug;
53  bool sfftest;
54  std::string sfffilename;
55  bool gsetest;
56  std::string gsefilename;
57  bool anytest;
58  std::string anyfilename;
59  std::string format;
60  double amplitude;
61  bool overwrite;
62  bool testdigits;
64 }; // struct Options
65 
66 int main(int iargc, char* argv[])
67 {
68 
69  // define usage information
70  char usage_text[]=
71  {
73  "usage: writetest [-v] [-D] [-sff n] [-gse n] [-any n]" "\n"
74  " [-t type] [-A amp] [-o] [-digits v]" "\n"
75  " or: writetest --help|-h" "\n"
76  " or: writetest --xhelp" "\n"
77  };
78 
79  // define full help text
80  char help_text[]=
81  {
82  "\n"
83  "-v be verbose" "\n"
84  "-D enter debug mode" "\n"
85  "-sff n write to SFF file \"n\"" "\n"
86  "-gse n write to GSE file \"n\"" "\n"
87  "-any n write to file \"n\"" "\n"
88  "\n"
89  "options to define \"any\" test:" "\n"
90  "-t type select output format type for any" "\n"
91  "-A amp select signal amplitude" "\n"
92  "-o overwrite existing output file" "\n"
93  "-digits v test digits functions" "\n"
94  };
95 
96  // define commandline options
97  using namespace tfxx::cmdline;
98  static Declare options[]=
99  {
100  // 0: print help
101  {"help",arg_no,"-"},
102  // 1: verbose mode
103  {"v",arg_no,"-"},
104  // 2: debug mode
105  {"D",arg_no,"-"},
106  // 3: write SFF data
107  {"sff",arg_yes,"-"},
108  // 4: write GSE data
109  {"gse",arg_yes,"-"},
110  // 5: write any format
111  {"any",arg_yes,"-"},
112  // 6: write any format
113  {"t",arg_yes,"sff"},
114  // 7: write any format
115  {"A",arg_yes,"1."},
116  // 8: write any format
117  {"o",arg_no,"-"},
118  // 9: write any format
119  {"digits",arg_yes,"1."},
120  // 10: write any format
121  {"xhelp",arg_no,"-"},
122  {NULL}
123  };
124 
125  // no arguments? print usage...
126  if (iargc<2)
127  {
128  cerr << usage_text << endl;
129  exit(0);
130  }
131 
132  // collect options from commandline
133  Commandline cmdline(iargc, argv, options);
134 
135  // help requested? print full help text...
136  if (cmdline.optset(0) || cmdline.optset(10))
137  {
138  cerr << usage_text << endl;
139  cerr << help_text << endl;
141  if (cmdline.optset(10))
142  {
143  datrw::online_help(cerr);
144  }
145  exit(0);
146  }
147 
148  /*
149  // dummy operation: print option settings
150  for (int iopt=0; iopt<2; iopt++)
151  {
152  cout << "option: '" << options[iopt].opt_string << "'" << endl;
153  if (cmdline.optset(iopt)) { cout << " option was set"; }
154  else { cout << "option was not set"; }
155  cout << endl;
156  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
157  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
158  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
159  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
160  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
161  cout << " argument (bool): '";
162  if (cmdline.bool_arg(iopt))
163  { cout << "true"; } else { cout << "false"; }
164  cout << "'" << endl;
165  }
166  while (cmdline.extra()) { cout << cmdline.next() << endl; }
167 
168  // dummy operation: print rest of command line
169  while (cmdline.extra()) { cout << cmdline.next() << endl; }
170  */
171 
172  Options opt;
173  opt.verbose=cmdline.optset(1);
174  opt.debug=cmdline.optset(2);
175  opt.sfftest=cmdline.optset(3);
176  opt.sfffilename=cmdline.string_arg(3);
177  opt.gsetest=cmdline.optset(4);
178  opt.gsefilename=cmdline.string_arg(4);
179  opt.anytest=cmdline.optset(5);
180  opt.anyfilename=cmdline.string_arg(5);
181  opt.format=cmdline.string_arg(6);
182  opt.amplitude=cmdline.double_arg(7);
183  opt.overwrite=cmdline.optset(8);
184  opt.testdigits=cmdline.optset(9);
185  opt.digitstestvalue=cmdline.double_arg(9);
186 
187  /*======================================================================*/
188 
189  if (opt.sfftest)
190  {
191  if (opt.verbose)
192  { cout << "SFF test: write to file " << opt.sfffilename << endl; }
193  std::ofstream ofs(opt.sfffilename.c_str());
194  datrw::osffstream os(ofs, "", opt.debug);
195  sff::FREE free;
196  free.append("my file comment");
197  sff::SRCE srce;
198  os << free;
199  os << srce;
200  {
201  sff::WID2 wid2;
202  wid2.dt=10;
203  wid2.channel="LIN";
204  wid2.station="THOF";
205  {
206  datrw::Tiseries s(10);
207  sff::FREE tracefree;
208  tracefree.append("linear integer trace");
209  sff::INFO info;
210  for (int i=s.f(); i<=s.l(); ++i)
211  {
212  s(i)=i;
213  }
214  os << wid2;
215  os << tracefree;
216  os << info;
217  os << s;
218  }
219  {
220  datrw::Tdseries s(100);
221  sff::FREE tracefree;
222  tracefree.append("double sine trace");
223  for (int i=s.f(); i<=s.l(); ++i)
224  {
225  s(i)=5.e4*sin(2*3.14159265358979311599*i/(s.size()-1));
226  }
227  wid2.dt=1.;
228  wid2.channel="SIN";
229  os << wid2;
230  os << tracefree;
231  os << s;
232  }
233  {
234  datrw::Tfseries s(1000);
235  sff::FREE tracefree;
236  tracefree.append("float cosine trace");
237  for (int i=s.f(); i<=s.l(); ++i)
238  {
239  s(i)=6.e-12*cos(2*3.14159265358979311599*i/(s.size()-1));
240  }
241  wid2.dt=.1;
242  wid2.channel="COS";
243  os << wid2;
244  os << tracefree;
245  os << s;
246  }
247  }
248  }
249 
250  /*======================================================================*/
251 
252  if (opt.gsetest)
253  {
254  if (opt.verbose)
255  { cout << "GSE test: write to file " << opt.gsefilename << endl; }
256  std::ofstream ofs(opt.gsefilename.c_str());
257  datrw::ogsestream os(ofs, opt.debug);
258  sff::FREE free;
259  free.append("my file comment");
260  sff::SRCE srce;
261  os << free;
262  os << srce;
263  {
264  sff::WID2 wid2;
265  wid2.dt=10;
266  wid2.channel="LIN";
267  wid2.station="THOF";
268  {
269  datrw::Tiseries s(100);
270  sff::FREE tracefree;
271  tracefree.append("linear integer trace");
272  sff::INFO info;
273  for (int i=s.f(); i<=s.l(); ++i)
274  {
275  s(i)=i/5-10;
276  }
277  os << wid2;
278  os << tracefree;
279  os << info;
280  os << s;
281  }
282  {
283  datrw::Tdseries s(100);
284  sff::FREE tracefree;
285  tracefree.append("double sine trace");
286  for (int i=s.f(); i<=s.l(); ++i)
287  {
288  s(i)=30.*sin(2*3.14159265358979311599*i/(s.size()-1));
289  }
290  wid2.dt=1.;
291  wid2.channel="SIN";
292  os << wid2;
293  os << tracefree;
294  os << s;
295  }
296  {
297  datrw::Tfseries s(1000);
298  sff::FREE tracefree;
299  tracefree.append("float cosine trace");
300  for (int i=s.f(); i<=s.l(); ++i)
301  {
302  s(i)=6.*cos(2*3.14159265358979311599*i/(s.size()-1));
303  }
304  wid2.dt=.1;
305  wid2.channel="COS";
306  os << wid2;
307  os << tracefree;
308  os << s;
309  }
310  }
311  }
312 
313  /*======================================================================*/
314 
315  if (opt.anytest)
316  {
317  if (opt.verbose)
318  {
319  cout << "ANY test: write to file " << opt.anyfilename << endl;
320  cout << "using " << opt.format << endl;
321  }
322  if (!opt.overwrite) { datrw::abort_if_exists(opt.anyfilename); }
323  std::ofstream ofs(opt.anyfilename.c_str());
324  datrw::oanystream os(ofs, opt.format, opt.debug);
325  sff::FREE free;
326  free.append("my file comment");
327  sff::SRCE srce;
328  os << free;
329  os << srce;
330  sff::WID2 wid2;
331  wid2.dt=0.01;
332  wid2.channel="SIN";
333  wid2.station="THOF";
334  datrw::Tdseries s(100);
335  sff::FREE tracefree;
336  tracefree.append("double sine trace");
337  for (int i=s.f(); i<=s.l(); ++i)
338  {
339  s(i)=opt.amplitude*sin(2*3.14159265358979311599*i/(s.size()-1));
340  }
341  os << wid2;
342  os << tracefree;
343  os << s;
344  tracefree.append("smaller sampling interval");
345  wid2.dt=0.002;
346  wid2.channel="SIN";
347  os << wid2;
348  os << tracefree;
349  os << s;
350  }
351 
352  /*======================================================================*/
353 
354  if (opt.testdigits)
355  {
356  cout << "test digits functions with value " << opt.digitstestvalue <<
357  endl;
358  cout << "number of siginificant digits "
360  opt.debug) << endl;
361  cout << "number of trailing digits "
363  opt.debug) << endl;
364  }
365 
366 }
367 
368 /* ----- END OF writetest.cc ----- */
void abort_if_exists(const std::string &filename)
Definition: datwrite.cc:49
double digitstestvalue
Definition: writetest.cc:63
aff::Series< float > Tfseries
Definition: types.h:46
std::string anyfilename
Definition: writetest.cc:58
read sff data (prototypes)
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
class to write SFF data
Definition: sff.h:98
std::string sfffilename
Definition: writetest.cc:54
bool overwrite
Definition: asciitest.cc:51
int main(int iargc, char *argv[])
Definition: writetest.cc:66
bool anytest
Definition: writetest.cc:57
bool debug
Definition: asciitest.cc:52
utilities used by more than one type of data reader (prototypes)
bool gsetest
Definition: writetest.cc:55
bool testdigits
Definition: writetest.cc:62
common interface for all data types (prototypes)
void supported_data_types(std::ostream &os)
Definition: formats.cc:290
bool verbose
Definition: asciitest.cc:51
int ntrailingdigits(double v, const bool &debug)
return number of trailing digits (after decimal point)
Definition: util.cc:97
int nsignificantdigits(double v, const bool &debug)
return number of significant digits
Definition: util.cc:49
aff::Series< int > Tiseries
Definition: types.h:47
Class to write any type of data file.
Definition: writeany.h:75
std::string format
Definition: writetest.cc:59
bool sfftest
Definition: writetest.cc:53
double amplitude
Definition: writetest.cc:60
#define WRITETEST_VERSION
Definition: writetest.cc:37
class to write GSE data
Definition: sff.h:136
std::string gsefilename
Definition: writetest.cc:56