conv/many suite: convert (m)any seismic data format(s)
any2ascii.cc
Go to the documentation of this file.
1 
45 #define ANY2ASCII_VERSION \
46  "ANY2ASCII V1.7 convert any input format to ASCII (obsolete)"
47 
48 #include <iostream>
49 #include <fstream>
50 #include <cstdio>
51 #include <map>
52 #include <tfxx/commandline.h>
53 #include <tfxx/error.h>
54 #include <tfxx/stringfunc.h>
55 #include <tfxx/seitosh.h>
56 #include <datrwxx/readany.h>
57 
58 using std::cout;
59 using std::cerr;
60 using std::endl;
61 
62 /*======================================================================*/
63 
64 
65 struct Options {
68  int precision;
69  std::string dataformat;
70  std::string headerfieldselection;
71 }; // struct Options
72 
73 struct Header {
75  sff::FREE tracefree, filefree;
76  sff::WID2 wid2;
77  sff::INFO info;
78  sff::SRCE srce;
79  int itrace;
80  std::string filename;
81 }; // struct Header
82 
83 /*======================================================================*/
84 
85 void writeheader(std::ostream& os, const Header& header)
86 {
87  os << ANY2ASCII_VERSION << endl;
88  os << "input file: " << header.filename << endl;
89  os << "input trace: " << header.itrace << endl << endl;
90  os << "file header: " << endl;
91  os << "============ " << endl;
92  if (header.hassrce) { sff::verbose(os, header.srce); }
93  else { os << "file header contains no SRCE line" << endl; }
94  if (header.hasfilefree) { sff::verbose(os, header.filefree); }
95  else { os << "file header contains no FREE block" << endl; }
96  os << endl;
97  os << "trace header: " << endl;
98  os << "============= " << endl;
99  sff::verbose(os, header.wid2);
100  if (header.hasinfo) { sff::verbose(os, header.info); }
101  else { os << "trace header contains no INFO line" << endl; }
102  if (header.hastracefree) { sff::verbose(os, header.tracefree); }
103  else { os << "trace header contains no FREE block" << endl; }
104  os << endl;
105 }
106 
107 void writeheaderhr(std::ostream& os, const Header& header)
108 {
109  os << ANY2ASCII_VERSION << endl;
110  os << "input file: " << header.filename << endl;
111  os << "input trace: " << header.itrace << endl << endl;
112  os << "file header: " << endl;
113  if (header.hasfilefree) { os << header.filefree << endl; }
114  if (header.hassrce) { os << header.srce.line() << endl << endl;; }
115  os << "trace header: " << endl;
116  os << header.wid2.line() << endl;
117  if (header.hastracefree) { os << header.tracefree << endl; }
118  if (header.hasinfo) { os << header.info.line() << endl << endl;; }
119 }
120 
121 /*======================================================================*/
122 
123 int main(int iargc, char* argv[])
124 {
125 
126  // define usage information
127  char usage_text[]=
128  {
129  ANY2ASCII_VERSION "\n"
130  "usage: any2ascii [-v] [-hf] [-all] [-type type] [-integer] [-hu]" "\n"
131  " [-precision p] [-tcol] [-namemod m]" "\n"
132  " infile outbase" "\n"
133  " or: any2ascii --help|-h" "\n"
134  };
135 
136  // define full help text
137  char help_text[]=
138  {
139  "read any input data and convert samples to ASCII" "\n"
140  "\n"
141  "Notice: With the introduction of the format 'ascii' in" "\n"
142  " libdatrwxx, this program has become obsolete. Use" "\n"
143  " any2any instead." "\n"
144  "\n"
145  "infile name of input file" "\n"
146  "outbase basename for output files" "\n"
147  "\n"
148  "-v be verbose" "\n"
149  "-hf write header information to separate file" "\n"
150  "-all extract all traces, not only first" "\n"
151  "-type type input file has data format \"type\" (default: sff)" "\n"
152  "-integer read and write integer data" "\n"
153  "-precision p set output precision for floating point output" "\n"
154  "-hu dump header in human readable form" "\n"
155  "-tcol print time of sample in first column" "\n"
156  "-namemod m modify filename with header fields" "\n"
157  " modifiers \"m\" can be:" "\n"
158  " d: date of first sample" "\n"
159  " t: time of first sample" "\n"
160  " u: time of first sample with parts of seconds" "\n"
161  " s: station name" "\n"
162  " c: channel name" "\n"
163  " a: auxilliary ID" "\n"
164  " i: instrument" "\n"
165  };
166 
167  // define commandline options
168  using namespace tfxx::cmdline;
169  static Declare options[]=
170  {
171  // 0: print help
172  {"help",arg_no,"-"},
173  // 1: verbose mode
174  {"verbose",arg_no,"-"},
175  // 2: header to separate file(s)
176  {"hf",arg_no,"-"},
177  // 3: extract all traces
178  {"all",arg_no,"-"},
179  // 4: extract all traces
180  {"type",arg_yes,"sff"},
181  // 5: extract all traces
182  {"integer",arg_no,"-"},
183  // 6: extract all traces
184  {"precision",arg_yes,"7"},
185  // 7: human readable header dump
186  {"hu",arg_no,"-"},
187  // 8: print time in first column
188  {"tcol",arg_no,"-"},
189  // 9: print time in first column
190  {"namemod",arg_yes,"-"},
191  {NULL}
192  };
193 
194  /* ---------------------------------------------------------------------- */
195 
196  cerr << ANY2ASCII_VERSION "\n"
197  "******************************************************************\n"
198  "THIS PROGRAM IS OBSOLETE\n"
199  "any2ascii is no longer maintained and may vanish in the future\n"
200  "consider to use any2any instead\n"
201  "******************************************************************\n";
202 
203  /* ---------------------------------------------------------------------- */
204 
205  // no arguments? print usage...
206  if (iargc<2)
207  {
208  cerr << usage_text << endl;
209  cerr << tfxx::seitosh::repository_reference << endl;
210  exit(0);
211  }
212 
213  // collect options from commandline
214  Commandline cmdline(iargc, argv, options);
215 
216  // help requested? print full help text...
217  if (cmdline.optset(0))
218  {
219  cerr << usage_text << endl;
220  cerr << help_text << endl << endl;
221  datrw::supported_data_types(cerr);
222  datrw::online_help(cerr);
223  cerr << endl << tfxx::seitosh::repository_reference << endl;
224  exit(0);
225  }
226 
227  Options opt;
228 
229  opt.verbose=cmdline.optset(1);
230  opt.headerfiles=cmdline.optset(2);
231  opt.alltraces=cmdline.optset(3);
232  opt.dataformat=cmdline.string_arg(4);
233  opt.integerdata=cmdline.optset(5);
234  opt.precision=cmdline.int_arg(6);
235  opt.hrdump=cmdline.optset(7);
236  opt.timecolumn=cmdline.optset(8);
237  opt.hfinfilename=cmdline.optset(9);
238  opt.headerfieldselection=cmdline.string_arg(9);
239 
240  TFXX_assert(cmdline.extra(), "ERROR: missing input filename!");
241  std::string infile=cmdline.next();
242  TFXX_assert(cmdline.extra(), "ERROR: missing output filename!");
243  std::string outfile=cmdline.next();
244 
245  std::ifstream ifs(infile.c_str(),
246  datrw::ianystream::openmode(opt.dataformat));
247  TFXX_assert(ifs.good(), "ERROR: cannot open input file!");
248  datrw::ianystream is(ifs, opt.dataformat);
249 
250  Header header;
251  header.filename=infile;
252 
253  // remember file FREE
254  header.hasfilefree=is.hasfree();
255  if (header.hasfilefree) { is >> header.filefree; }
256  // remember SRCE
257  header.hassrce=is.hassrce();
258  if (header.hassrce) { is >> header.srce; }
259 
260  // filename map to count traces
261  std::map<std::string, int> tracecounter;
262 
263  bool hot=is.good();
264  int itrace=0;
265  while (hot)
266  {
267  // read data
268  ++itrace;
269  if (opt.verbose)
270  {
271  cout << "trace #" << itrace << endl;
272  }
273 
274  header.itrace=itrace;
275 
276  datrw::Tdseries timeseries;
277  datrw::Tiseries itimeseries;
278  if (opt.integerdata)
279  { is >> itimeseries; }
280  else
281  { is >> timeseries; }
282  is >> header.wid2;
283  header.hastracefree=is.hasfree();
284  if (header.hastracefree) { is >> header.tracefree; }
285  header.hasinfo=is.hasinfo();
286  if (header.hasinfo) { is >> header.info; }
287 
288  /*----------------------------------------------------------------------*/
289  // create filename
290 
291  std::string outbase=outfile + ".";
292 
293  if (opt.hfinfilename)
294  {
295  for (std::string::const_iterator I=opt.headerfieldselection.begin();
296  I != opt.headerfieldselection.end(); ++I)
297  {
298  libtime::TAbsoluteTime date=header.wid2.date;
299  std::ostringstream oss;
300  if (*I == 'd')
301  {
302  oss.width(4);
303  oss.fill('0');
304  oss << date.year();
305  oss.width(2);
306  oss.fill('0');
307  oss << date.month();
308  oss.width(2);
309  oss.fill('0');
310  oss << date.day() << ".";
311  outbase += oss.str();
312  }
313  else if (*I == 't')
314  {
315  oss.width(2);
316  oss.fill('0');
317  oss << date.hour();
318  oss.width(2);
319  oss.fill('0');
320  oss << date.minute();
321  oss.width(2);
322  oss.fill('0');
323  oss << date.second() << ".";
324  outbase += oss.str();
325  }
326  else if (*I == 'u')
327  {
328  oss.width(2);
329  oss.fill('0');
330  oss << date.hour();
331  oss.width(2);
332  oss.fill('0');
333  oss << date.minute();
334  oss.width(2);
335  oss.fill('0');
336  oss << date.second() << ".";
337  oss.width(3);
338  oss.fill('0');
339  oss << date.milsec();
340  oss.width(3);
341  oss.fill('0');
342  oss << date.micsec() << ".";
343  outbase += oss.str();
344  }
345  else if (*I == 's')
346  {
347  outbase += tfxx::string::trimws(header.wid2.station) + ".";
348  }
349  else if (*I == 'c')
350  {
351  outbase += tfxx::string::trimws(header.wid2.channel) + ".";
352  }
353  else if (*I == 'a')
354  {
355  outbase += tfxx::string::trimws(header.wid2.auxid) + ".";
356  }
357  else if (*I == 'i')
358  {
359  outbase += tfxx::string::trimws(header.wid2.instype) + ".";
360  }
361  }
362  }
363 
364  if (opt.alltraces)
365  {
366  std::ostringstream oss;
367  oss.width(4);
368  oss.fill('0');
369  oss << ++tracecounter[outbase] << ".";
370  outbase += oss.str();
371  }
372 
373  if (opt.verbose)
374  {
375  cout << " basename for output: " << outbase << endl;
376  }
377 
378  std::string outputtracename=outbase+std::string("asc");
379  std::string outputheadername=outbase+std::string("hdr");
380 
381  /*----------------------------------------------------------------------*/
382  // write data
383 
384  std::ofstream tos(outputtracename.c_str());
385  if (opt.headerfiles)
386  {
387  std::ofstream hos(outputheadername.c_str());
388  if (opt.hrdump)
389  {
390  writeheaderhr(hos, header);
391  }
392  else
393  {
394  writeheader(hos, header);
395  }
396  }
397  else
398  {
399  if (opt.hrdump)
400  {
401  writeheaderhr(tos, header);
402  }
403  else
404  {
405  writeheader(tos, header);
406  }
407  tos << "data:" << endl;
408  }
409 
410  if (opt.integerdata)
411  {
412  for (int isample=itimeseries.first(); isample<=itimeseries.last();
413  ++isample)
414  {
415  if (opt.timecolumn)
416  {
417  tos.setf(std::ios_base::scientific,std::ios_base::floatfield);
418  tos.precision(opt.precision);
419  tos << (isample-itimeseries.f())*header.wid2.dt << " ";
420  }
421  tos << itimeseries(isample) << endl;
422  }
423  }
424  else
425  {
426  for (int isample=timeseries.first(); isample<=timeseries.last();
427  ++isample)
428  {
429  if (opt.timecolumn)
430  {
431  tos.setf(std::ios_base::scientific,std::ios_base::floatfield);
432  tos.precision(opt.precision);
433  tos << (isample-timeseries.f())*header.wid2.dt << " ";
434  }
435  tos.setf(std::ios_base::scientific,std::ios_base::floatfield);
436  tos.precision(opt.precision);
437  tos << timeseries(isample) << endl;
438  }
439  }
440 
441  hot=(!is.last());
442  if (!opt.alltraces) { hot=false; }
443  }
444 
445  if (opt.timecolumn)
446  {
447  cout << "DEBUG: time column" << endl;
448  }
449 }
450 
451 /* ----- END OF any2ascii.cc ----- */
sff::WID2 wid2
Definition: any2ascii.cc:76
bool hfinfilename
Definition: any2ascii.cc:67
sff::FREE tracefree
Definition: any2ascii.cc:75
bool headerfiles
Definition: any2ascii.cc:66
sff::FREE filefree
Definition: any2ascii.cc:75
void writeheaderhr(std::ostream &os, const Header &header)
Definition: any2ascii.cc:107
bool hasinfo
Definition: any2ascii.cc:74
bool hrdump
Definition: any2ascii.cc:66
bool hassrce
Definition: any2ascii.cc:74
std::string dataformat
Definition: any2ascii.cc:69
#define ANY2ASCII_VERSION
Definition: any2ascii.cc:45
sff::INFO info
Definition: any2ascii.cc:77
bool integerdata
Definition: any2ascii.cc:67
bool verbose
Definition: any2any.cc:57
datrw::Tiseries Tiseries
Definition: anyextract.cc:160
sff::SRCE srce
Definition: any2ascii.cc:78
bool alltraces
Definition: any2ascii.cc:66
int precision
Definition: any2ascii.cc:68
bool hasfilefree
Definition: any2ascii.cc:74
std::string headerfieldselection
Definition: any2ascii.cc:70
bool timecolumn
Definition: any2ascii.cc:67
std::string filename
Definition: any2ascii.cc:80
void writeheader(std::ostream &os, const Header &header)
Definition: any2ascii.cc:85
int main(int iargc, char *argv[])
Definition: any2ascii.cc:123
bool hastracefree
Definition: any2ascii.cc:74
int itrace
Definition: any2ascii.cc:79