DATRW++ library: seismic data I/O with multiple formats
asciitest.cc
Go to the documentation of this file.
1 /*! \file asciitest.cc
2  * \brief test ASCII format I/O functions
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 02/09/2011
8  *
9  * test ASCII 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  *
30  * REVISIONS and CHANGES
31  * - 02/09/2011 V1.0 Thomas Forbriger
32  *
33  * ============================================================================
34  */
35 #define ASCIITEST_VERSION \
36  "ASCIITEST V1.0 test ASCII format I/O functions"
37 
38 #include <iostream>
39 #include <fstream>
40 #include <string>
41 #include <tfxx/commandline.h>
42 #include <datrwxx/ascii.h>
43 
44 using std::cout;
45 using std::cerr;
46 using std::endl;
47 
48 /*----------------------------------------------------------------------*/
49 
50 struct Options {
53  std::string writefilename;
55  double frequency;
56 }; // struct Options
57 
58 /*----------------------------------------------------------------------*/
59 
60 int main(int iargc, char* argv[])
61 {
62 
63  // define usage information
64  char usage_text[]=
65  {
67  "usage: asciitest [-v] [-overwrite] [-write name] [-ntraces n]" "\n"
68  " [-nsamples n] [-frequency f] [-DEBUG]\n"
69  " or: asciitest --help|-h" "\n"
70  };
71 
72  // define full help text
73  char help_text[]=
74  {
75  "\n"
76  "-v verbose mode\n"
77  "-DEBUG debug mode\n"
78  "-overwrite overwrite existing output file\n"
79  "-write name write test traces to file \"name\"\n"
80  "-ntraces n write \"n\" traces\n"
81  "-nsamples n write \"n\" samples per trace\n"
82  "-frequency f create test signal of frequency \"f\"\n"
83  "\n"
84  "File reading can be tested with libdatrwxxtests.cc\n"
85  };
86 
87  // define commandline options
88  using namespace tfxx::cmdline;
89  static Declare options[]=
90  {
91  // 0: print help
92  {"help",arg_no,"-"},
93  // 1: verbose mode
94  {"v",arg_no,"-"},
95  // 2: write test data
96  {"write",arg_yes,"junk"},
97  // 3: overwrite existing file
98  {"overwrite",arg_no,"-"},
99  // 4: ntraces
100  {"ntraces",arg_yes,"1"},
101  // 5: nsamples
102  {"nsamples",arg_yes,"20"},
103  // 6: frequency
104  {"frequency",arg_yes,"10."},
105  // 7: debug mode
106  {"DEBUG",arg_no,"-"},
107  {NULL}
108  };
109 
110  // no arguments? print usage...
111  if (iargc<2)
112  {
113  cerr << usage_text << endl;
114  exit(0);
115  }
116 
117  // collect options from commandline
118  Commandline cmdline(iargc, argv, options);
119 
120  // help requested? print full help text...
121  if (cmdline.optset(0))
122  {
123  cerr << usage_text << endl;
124  cerr << help_text << endl;
125  cerr << endl;
128  exit(0);
129  }
130 
131  Options opt;
132  opt.verbose=cmdline.optset(1);
133  opt.writetest=cmdline.optset(2);
134  opt.writefilename=cmdline.string_arg(2);
135  opt.overwrite=cmdline.optset(3);
136  opt.ntraces=cmdline.int_arg(4);
137  opt.nsamples=cmdline.int_arg(5);
138  opt.frequency=cmdline.double_arg(6);
139  opt.debug=cmdline.optset(7);
140 
141  /*======================================================================*/
142 
143  if (opt.writetest)
144  {
145  cout << "test output module\n"
146  << "==================\n";
147  cout << "write to file " << opt.writefilename << endl;
149  std::ofstream ofs(opt.writefilename.c_str(), datrw::oasciistream::openmode);
150  datrw::oasciistream os(ofs, "", opt.debug);
151 
152  cout << "file data is stored in ";
153  // report output data format
154  switch (os.seriestype()) {
155  case datrw::Fint:
156  cout << "integer";
157  break;
158  case datrw::Ffloat:
159  cout << "single precision floating point";
160  break;
161  case datrw::Fdouble:
162  cout << "double precision floating point";
163  break;
164  case datrw::Fall:
165  cout << "any desired";
166  break;
167  default:
168  TFXX_abort("output stream uses unknown variable type!");
169  } // switch (os.seriestype())
170  cout << " variable type" << endl;
171 
172  cout << "file FREE data can ";
173  if (!os.handlesfilefree()) { cout << "NOT "; }
174  cout << "be handled" << endl;
175 
176  cout << "SRCE data can ";
177  if (!os.handlessrce()) { cout << "NOT "; }
178  cout << "be handled" << endl;
179 
180  ::sff::FREE filefree;
181  filefree.append("this is the FREE block for the file header");
182  filefree.append(ASCIITEST_VERSION);
183  filefree.append("output file name:");
184  filefree.append(opt.writefilename);
185 
186  ::sff::SRCE srce;
187  srce.date=libtime::now();
188  srce.type="test source";
189  srce.cx=-1.;
190  srce.cy=-2.;
191  srce.cz=-3.;
192  srce.cs=::sff::CS_cartesian;
193 
194  if (os.handlesfilefree()) { os << filefree; }
195  if (os.handlessrce()) { os << srce; }
196 
197  int ntraces=opt.ntraces;
198  for (int itrace=0; itrace<ntraces; ++itrace)
199  {
200  cout << "write trace #" << itrace+1 << endl;
201 
202  cout << "trace FREE data can ";
203  if (!os.handlestracefree()) { cout << "NOT "; }
204  cout << "be handled" << endl;
205 
206  cout << "INFO data can ";
207  if (!os.handlesinfo()) { cout << "NOT "; }
208  cout << "be handled" << endl;
209 
210  ::sff::FREE free;
211  free.append(ASCIITEST_VERSION);
212 
213  ::sff::INFO info;
214  info.nstacks=itrace;
215  info.cx=1.;
216  info.cy=2.;
217  info.cz=3.;
218  info.cs=::sff::CS_cartesian;
219 
220  const double dt=2.e-3*(itrace+4);
221  ::sff::WID2 wid2;
222  wid2.dt=dt;
223  wid2.nsamples=opt.nsamples;
224  wid2.date=libtime::now();
225  wid2.station="FEL";
226  wid2.auxid="AUX";
227  wid2.instype="SYNTH";
228 
229  int typeindex=itrace%3;
230  if (typeindex==0)
231  {
232  aff::Series<double> sine(opt.nsamples);
233  for (int isample=0; isample<opt.nsamples; ++isample)
234  {
235  sine(sine.f()+isample)=(itrace+2)*sin(2.*3.141592653589793115998
236  *opt.frequency*dt*isample);
237  }
238  free.append("create double type data");
239  wid2.channel="DBL";
240  os << wid2;
241  if (os.handlesinfo()) { os << info; }
242  if (os.handlestracefree()) { os << free; }
243  os << sine;
244  }
245  else if (typeindex==1)
246  {
247  aff::Series<float> sine(opt.nsamples);
248  for (int isample=0; isample<opt.nsamples; ++isample)
249  {
250  sine(sine.f()+isample)=(itrace+2)*sin(2.*3.141592653589793115998
251  *opt.frequency*dt*isample);
252  }
253  free.append("create float type data");
254  wid2.channel="FLT";
255  os << wid2;
256  if (os.handlesinfo()) { os << info; }
257  if (os.handlestracefree()) { os << free; }
258  os << sine;
259  }
260  else
261  {
262  aff::Series<int> sine(opt.nsamples);
263  for (int isample=0; isample<opt.nsamples; ++isample)
264  {
265  sine(isample)=isample%15;
266  }
267  wid2.channel="INT";
268  free.append("create int type data");
269  os << wid2;
270  if (os.handlesinfo()) { os << info; }
271  if (os.handlestracefree()) { os << free; }
272  os << sine;
273  }
274  }
275  }
276 
277 }
278 
279 /* ----- END OF asciitest.cc ----- */
void abort_if_exists(const std::string &filename)
Definition: datwrite.cc:49
libtime::TRelativeTime dt()
return sampling interval of HPMO data acquisition (i.e. 5 sec)
Definition: readhpmo.cc:83
static const std::ios_base::openmode openmode
Definition: ascii.h:113
static void help(std::ostream &os=std::cout)
Definition: oasciistream.cc:73
std::string writefilename
Definition: asciitest.cc:53
int main(int iargc, char *argv[])
Definition: asciitest.cc:60
bool overwrite
Definition: asciitest.cc:51
#define ASCIITEST_VERSION
Definition: asciitest.cc:35
bool debug
Definition: asciitest.cc:52
int ntraces
Definition: asciitest.cc:54
bool verbose
Definition: asciitest.cc:51
bool writetest
Definition: asciitest.cc:52
int nsamples
Definition: asciitest.cc:54
static void help(std::ostream &os=std::cout)
double frequency
Definition: asciitest.cc:55
class to write ascii data
Definition: ascii.h:105
interface to write ASCII data (prototypes)