conv/many suite: convert (m)any seismic data format(s)
anyindex.cc
Go to the documentation of this file.
1 
43 #define ANYINDEX_VERSION \
44  "ANYINDEX V1.6 create index of data files"
45 
46 #include <fstream>
47 #include <iostream>
48 #include <vector>
49 #include <string>
50 #include <tfxx/commandline.h>
51 #include <tfxx/error.h>
52 #include <tfxx/stringfunc.h>
53 #include <tfxx/seitosh.h>
54 #include <datrwxx/readany.h>
55 #include <datrwxx/debug.h>
56 #include <sffostream.h>
57 
58 using std::cout;
59 using std::cerr;
60 using std::endl;
61 
62 struct Options {
64  std::string format;
65 }; // struct Options
66 
67 typedef std::vector<std::string> Tvecofstrings;
68 
69 typedef datrw::Tdseries Tseries;
70 
71 int main(int iargc, char* argv[])
72 {
73 
74  // define usage information
75  char usage_text[]=
76  {
77  ANYINDEX_VERSION "\n"
78  "usage: anyindex [-v] [-a] [-o] [-t type] [-D] [-r]" "\n"
79  " [-Fbonjer] [-Fsff] [-Fpdas] [-Fhpmo]" "\n"
80  " file [file ...] outfile" "\n"
81  " or: anyindex --help|-h" "\n"
82  " or: anyindex --fhelp type" "\n"
83  };
84 
85  // define full help text
86  char help_text[]=
87  {
88  "-fhelp type print online help for file format \"type\"\n"
89  "\n"
90  "-v be verbose" "\n"
91  "-a append to outfile" "\n"
92  "-o overwrite outfile" "\n"
93  "-r explicitely read all samples (default: skip)" "\n"
94  "-t format select data format or use one of the shortcuts below" "\n"
95  "-Fsff input format is SFF" "\n"
96  "-Fhpmo input format is multiplexed ASCII format" "\n"
97  " defined by WG for BFO HP-MO data acquisition system" "\n"
98  "-Fpdas input format is PDAS100 (i.e. DaDisp)" "\n"
99  "-Fbonjer input format is defined by K. Bonjer for" "\n"
100  " K2 ASCII data" "\n"
101  "-D produce debugging output" "\n"
102  "file ... file(s) to read data from" "\n"
103  "outfile out file (index)" "\n"
104  };
105 
106  // define commandline options
107  using namespace tfxx::cmdline;
108  static Declare options[]=
109  {
110  // 0: print help
111  {"help",arg_no,"-"},
112  // 1: verbose mode
113  {"v",arg_no,"-"},
114  // 2: append mode
115  {"a",arg_no,"-"},
116  // 3: overwrite mode
117  {"o",arg_no,"-"},
118  // 4: original SFF data
119  {"Fsff",arg_no,"-"},
120  // 5: WG's multiplexed ASCII data (BFO format)
121  {"Fhpmo",arg_no,"-"},
122  // 6: PDAS 100 data (aka DaDisp)
123  {"Fpdas",arg_no,"-"},
124  // 7: K. Bonjer's ASCII data
125  {"Fbonjer",arg_no,"-"},
126  // 8: select any data type
127  {"t",arg_yes,"-"},
128  // 9: debugging output
129  {"DEBUG",arg_no,"-"},
130  // 10: read samples
131  {"read",arg_no,"-"},
132  // 11: format specific online help
133  {"fhelp",arg_yes,"sff"},
134  {NULL}
135  };
136 
137  // no arguments? print usage...
138  if (iargc<2)
139  {
140  cerr << usage_text << endl;
141  cerr << tfxx::seitosh::repository_reference << endl;
142  exit(0);
143  }
144 
145  // collect options from commandline
146  Commandline cmdline(iargc, argv, options);
147 
148  // help requested? print full help text...
149  if (cmdline.optset(0) || cmdline.optset(11))
150  {
151  cerr << usage_text << endl;
152  cerr << help_text << endl << endl;
153  if (cmdline.optset(11))
154  {
155  datrw::online_help(cmdline.string_arg(11), cerr, true);
156  }
157  else
158  {
159  datrw::supported_data_types(cerr);
160  }
161  cerr << endl << tfxx::seitosh::repository_reference << endl;
162  exit(0);
163  }
164 
165  // options
166  Options opt;
167 
168  opt.verbose=cmdline.optset(1);
169  opt.append=cmdline.optset(2);
170  opt.overwrite=cmdline.optset(3);
171  opt.format=datrw::anyID(datrw::Fsff);
172  if (cmdline.optset(5)) { opt.format=datrw::anyID(datrw::Fhpmo); }
173  if (cmdline.optset(6)) { opt.format=datrw::anyID(datrw::Fpdas); }
174  if (cmdline.optset(7)) { opt.format=datrw::anyID(datrw::Fbonjer); }
175  if (cmdline.optset(8)) { opt.format=cmdline.string_arg(8); }
176  opt.debug=cmdline.optset(9);
177  opt.readsamples=cmdline.optset(10);
178 
179  tfxx::string::trimws(opt.format);
180  if (opt.verbose)
181  { cout << "selected data type: " << opt.format << endl; }
182 
183  TFXX_assert(opt.format.find(" ")==std::string::npos,
184  "ERROR: using white space in format modifiers will fail "
185  "when reading index file");
186 
187  // setup vector of input filenames and read command line
188  Tvecofstrings infiles;
189  std::string outfile;
190  TFXX_assert(cmdline.extra(), "ERROR: missing input file!");
191  outfile=cmdline.next();
192  // collect input files
193  while (cmdline.extra())
194  {
195  infiles.push_back(outfile);
196  outfile=cmdline.next();
197  }
198  TFXX_assert((infiles.size()>0), "ERROR: missing output file!");
199 
200  // open output file
201  std::ios_base::openmode ofopenmode;
202  if (opt.append)
203  {
204  ofopenmode=std::ios_base::out|std::ios_base::app|std::ios_base::app;
205  }
206  else
207  {
208  if (!opt.overwrite)
209  {
210  std::ifstream file(outfile.c_str(),std::ios_base::in);
211  TFXX_assert((!file.good()),"ERROR: output file exists!");
212  }
213  ofopenmode=std::ios_base::out;
214  }
215  std::ofstream os(outfile.c_str(), ofopenmode);
216 
217  Tvecofstrings::const_iterator infile=infiles.begin();
218  while( infile!=infiles.end())
219  {
220  if (opt.verbose)
221  { cout << "** open next file: " << *infile << endl; }
222  std::ifstream ifs(infile->c_str(),
223  datrw::ianystream::openmode(opt.format));
224  datrw::ianystream is(ifs, opt.format, opt.debug);
225 
226  int ntrace=0;
227  if (is.last() && opt.debug) { cout << "LAST!" << endl; }
228  while(is.good())
229  {
230  ntrace++;
231  if (opt.verbose)
232  { cout << "**** read next trace: #" << ntrace << endl; }
233  if (opt.readsamples)
234  {
235  datrw::Tdseries series;
236  is >> series;
237  }
238  else
239  {
240  is.skipseries();
241  }
242  sff::WID2 wid2;
243  is >> wid2;
244  os << *infile << " " << ntrace << " " <<
245  opt.format << endl;
246  DATRW_debug(opt.debug, "anyindex main()",
247  "prepare WIDX line");
248  if (opt.debug) { sff::verbose(std::cerr, wid2); }
249  std::string widx=sff::WIDXline(wid2, opt.debug);
250  DATRW_debug(opt.debug, "anyindex main()",
251  widx);
252  os << widx << endl;
253  os.flush();
254  }
255  ++infile;
256  }
257 }
258 
259 /* ----- END OF anyindex.cc ----- */
bool append
Definition: anyindex.cc:63
datrw::Tdseries Tseries
Definition: anyindex.cc:69
std::vector< std::string > Tvecofstrings
Definition: anyindex.cc:67
bool overwrite
Definition: any2any.cc:57
bool debug
Definition: any2any.cc:57
bool readsamples
Definition: anyindex.cc:63
bool verbose
Definition: any2any.cc:57
int main(int iargc, char *argv[])
Definition: anyindex.cc:71
std::string format
Definition: any2sff.cc:59
std::vector< std::string > Tvecofstrings
Definition: any2sff.cc:62
#define ANYINDEX_VERSION
Definition: anyindex.cc:43