Waveform filter programs

◆ main()

int main ( int  iargc,
char *  argv[] 
)

Definition at line 93 of file noisymize.cc.

References Options::inputformat, Options::noiselength, NOISYMIZE_VERSION, Options::noscaling, Options::offexp, Options::overwrite, Options::reportfactors, Filenames::Rin, Filenames::Rout, Options::setnoiselength, Options::verbose, Filenames::Zin, and Filenames::Zout.

94 {
95 
96  // define usage information
97  char usage_text[]=
98  {
100  "usage: noisymize Zin Rin Zout Rout" "\n"
101  " [-v] [-type t] [-n n] [-o] [-rf] [-s e]" "\n"
102  " [-fixed]" "\n"
103  " or: noisymize --help|-h" "\n"
104  };
105 
106  // define full help text
107  char help_text[]=
108  {
109  "Zin vertical component input data" "\n"
110  "Rin radial component input data" "\n"
111  "Zout vertical component output data" "\n"
112  "Rout radial component output data" "\n"
113  "\n"
114  "-v be verbose" "\n"
115  "-type t input file type" "\n"
116  "-n n number of noise samples to use" "\n"
117  "-o overwrite output" "\n"
118  "-rf report factors" "\n"
119  "-s e scale seismograms with offset" "\n"
120  " the scaling factor is r**e, where r is the offset" "\n"
121  "-fixed do not scale each offset with a randomly" "\n"
122  " chosen amplitude" "\n"
123  "\n"
124  "This program was specifically created to test procedures for\n"
125  "MHVSR (microseismic H/V spectral ratio) analysis by application\n"
126  "to synthetic data. Synthetic seismograms for vertical and radial\n"
127  "component of the receiver calculated for a transient source signal\n"
128  "have to be convolved with the same random noise in this case\n"
129  "\n"
130  "Description of the procedure:" "\n"
131  "- the program reads pairs of seismograms from Zin and Rin" "\n"
132  "- each of the input files may contain multiple traces" "\n"
133  "- for each trace in Zin there must be a corresponding trace" "\n"
134  " in Rin at the same offset" "\n"
135  "- for each offset the program does the following:" "\n"
136  " 1. the Z-trace and the R-trace are convolved with the same" "\n"
137  " stochastic time series (random normaly distributed white noise)" "\n"
138  " a new stochastic time series is calculated for each offset" "\n"
139  " through the libgsl random number generator" "\n"
140  " 2. the Z-trace and the R-trace are scaled by a random value" "\n"
141  " (this function can be switched off through option -fixed)" "\n"
142  " 3. the Z-trace and the R-trace are scaled by an offset" "\n"
143  " dependend value (selected through option -s e)" "\n"
144  " 4. the R-trace is scaled by 0.707 to simulate a uniform" "\n"
145  " distribution of sources of all azimuths; as a consequence" "\n"
146  " the R result my be used as N- and E-component as well" "\n"
147  "- the results for all offsets are stacked; the resulting Z-" "\n"
148  " and R-series are written to files Zout and Rout," "\n"
149  " respectively"
150  };
151 
152  // define commandline options
153  using namespace tfxx::cmdline;
154  static Declare options[]=
155  {
156  // 0: print help
157  {"help",arg_no,"-"},
158  // 1: verbose mode
159  {"v",arg_no,"-"},
160  // 2: input format
161  {"type",arg_yes,"sff"},
162  // 3: number of noise samples
163  {"n",arg_yes,"1"},
164  // 4: overwrite output file
165  {"o",arg_no,"-"},
166  // 5: report factors
167  {"rf",arg_no,"-"},
168  // 6: report factors
169  {"s",arg_yes,"0."},
170  // 7: report factors
171  {"fixed",arg_no,"-"},
172  {NULL}
173  };
174 
175  // no arguments? print usage...
176  if (iargc<2)
177  {
178  cerr << usage_text << endl;
179  exit(0);
180  }
181 
182  // collect options from commandline
183  Commandline cmdline(iargc, argv, options);
184 
185  // help requested? print full help text...
186  if (cmdline.optset(0))
187  {
188  cerr << usage_text << endl;
189  cerr << help_text << endl;
190  exit(0);
191  }
192 
193 
194  /*----------------------------------------------------------------------*/
195  // read command line settings
196 
197  Options opt;
198  opt.verbose=cmdline.optset(1);
199  opt.inputformat=cmdline.string_arg(2);
200  opt.setnoiselength=cmdline.optset(3);
201  opt.noiselength=cmdline.int_arg(3);
202  opt.overwrite=cmdline.optset(4);
203  opt.reportfactors=cmdline.optset(5);
204  opt.offexp=cmdline.double_arg(6);
205  opt.noscaling=cmdline.optset(7);
206 
207  Filenames filename;
208  TFXX_assert(cmdline.extra(), "ERROR: missing filename Zin!");
209  filename.Zin=cmdline.next();
210  TFXX_assert(cmdline.extra(), "ERROR: missing filename Rin!");
211  filename.Rin=cmdline.next();
212  TFXX_assert(cmdline.extra(), "ERROR: missing filename Zout!");
213  filename.Zout=cmdline.next();
214  TFXX_assert(cmdline.extra(), "ERROR: missing filename Rout!");
215  filename.Rout=cmdline.next();
216 
217  /*----------------------------------------------------------------------*/
218  // read vertical component input data
219  if (opt.verbose)
220  { cout << "read input file " << filename.Zin << endl; }
221  Tfile Zindata;
222  {
223  std::ifstream ifs(filename.Zin.c_str());
224  datrw::ianystream is(ifs, opt.inputformat);
225  Zindata.read(is.idatstream(), opt.verbose);
226  }
227  // read radial component input data
228  if (opt.verbose)
229  { cout << "read input file " << filename.Rin << endl; }
230  Tfile Rindata;
231  {
232  std::ifstream ifs(filename.Rin.c_str());
233  datrw::ianystream is(ifs, opt.inputformat);
234  Rindata.read(is.idatstream(), opt.verbose);
235  }
236 
237  /*----------------------------------------------------------------------*/
238  // consistency checks
239  // ------------------
240  //
241  if (opt.verbose) { cout << "consistency checks:" << endl; }
242 
243  // check number of traces
244  TFXX_assert(Zindata.size()==Rindata.size(),
245  "ERROR: inconsitent number of traces");
246  if (opt.verbose)
247  { cout << "input files both have " << Zindata.size() << " traces." << endl; }
248 
249  // check trace headers
250  Tfile::Tbase::const_iterator Zit=Zindata.begin();
251  Tfile::Tbase::const_iterator Rit=Rindata.begin();
252  sff::WID2compare compare(sff::Fnsamples | sff::Fdt);
253  while((Zit!=Zindata.end()) && (Rit!=Rindata.end()))
254  {
255  TFXX_assert(compare(Zit->header.wid2(), Rit->header.wid2()),
256  "ERROR: inconsitent trace headers");
257  if (Zit->header.hasinfo())
258  {
259  sff::INFO Zinfo=Zit->header.info();
260  sff::INFO Rinfo=Rit->header.info();
261  TFXX_assert(Zinfo==Rinfo,
262  "ERROR: inconsitent coordinates");
263  }
264  ++Zit;
265  ++Rit;
266  }
267  if (opt.verbose)
268  { cout << "trace headers and coordinates are consistent" << endl; }
269 
270  /*----------------------------------------------------------------------*/
271  // go and noisymize data
272  // ---------------------
273  //
274  Zit=Zindata.begin();
275  Rit=Rindata.begin();
276  // source coordinates
277  sff::SRCE Zsrce=Zindata.fileheader.srce();
278  sff::SRCE Rsrce=Rindata.fileheader.srce();
279  // results
280  Tseries Zseries, Rseries;
281  // scaling factors
282  Tseries fac=ts::rnd::dugauss(Zindata.size());
283  if (opt.noscaling) { fac=1.; }
284  Tseries offfac(Zindata.size());
285  Tseries traceoffset(Zindata.size());
286  //
287  if (!opt.setnoiselength)
288  {
289  opt.noiselength=Zit->size();
290  }
291  if (opt.verbose)
292  {
293  cout << "convolve " << Zit->size()
294  << " data samples (each trace) with" << endl
295  << opt.noiselength << " samples of uniform gaussian noise"
296  << " and stack." << endl;
297  }
298  bool initialize_result=true;
299  unsigned int itrace=0;
300  while((Zit!=Zindata.end()) && (Rit!=Rindata.end()))
301  {
302  // calculate offset and offset dependend scaling
303  sff::INFO Zinfo=Zit->header.info();
304  sff::INFO Rinfo=Rit->header.info();
305  double Zoffset=sff::offset(Zsrce, Zinfo);
306  double Roffset=sff::offset(Rsrce, Rinfo);
307  double offerr=(Zoffset/Roffset)-1.;
308  if (offerr < 0.) { offerr=-offerr; }
309  TFXX_assert((offerr < 1.e-4),
310  "ERROR: offset of R- and Z-component are inconsistent!");
311  traceoffset(itrace)=Zoffset;
312  offfac(itrace)=std::pow(Zoffset, opt.offexp);
313  // scaling factor for radial component, since we see only the projection
314  // of the radial component of all sources on the respective horizontal
315  // component
316  // 7.9.2007: scaling factor changed appropriately for signal power (see
317  // help output of gresynoise)
318  // Hfactor = sqrt( (int_0^{2*pi} cos(phi)**2 d phi) / (2 pi) )
319  // formerly: const double Hfactor=2./M_PI;
320  const double Hfactor=0.707; // = 1/sqrt(2)
321  // go
322  double scalefac=fac(itrace)*offfac(itrace);
323  if (opt.verbose) { cout << "*"; cout.flush(); }
324  Tseries noise=ts::rnd::dugauss(opt.noiselength);
325  if (opt.verbose) { cout << "+"; cout.flush(); }
326  if (initialize_result)
327  { Zseries = scalefac*ts::convolve(*Zit, noise); }
328  else
329  { Zseries += scalefac*ts::convolve(*Zit, noise); }
330  if (opt.verbose) { cout << "+"; cout.flush(); }
331  if (initialize_result)
332  { Rseries = Hfactor*scalefac*ts::convolve(*Rit, noise); }
333  else
334  { Rseries += Hfactor*scalefac*ts::convolve(*Rit, noise); }
335  initialize_result=false;
336  ++Zit;
337  ++Rit;
338  ++itrace;
339  }
340  if (opt.verbose) { cout << endl; }
341  if (opt.reportfactors)
342  {
343  cout << "factors used for scaling:" << endl;
344  cout.width(5);
345  cout << "#";
346  cout.width(12);
347  cout << "f1";
348  cout.width(12);
349  cout << "f2";
350  cout.width(14);
351  cout << "offset" << endl;
352  for (itrace=0; itrace<Zindata.size(); itrace++)
353  {
354  cout.width(5); cout.precision(3);
355  cout << itrace;
356  cout.width(12); cout.precision(4);
357  cout << fac(itrace);
358  cout.width(12); cout.precision(4);
359  cout << offfac(itrace);
360  cout.width(12); cout.precision(4);
361  cout << traceoffset(itrace)*1.e-3 << "km";
362  cout << endl;
363  }
364  cout << "traces are scale by f1*f2" << endl;
365  }
366 
367  /*----------------------------------------------------------------------*/
368  // write result
369  if (opt.verbose)
370  { cout << "prepare output data" << endl; }
371  typedef ts::sff::SFFTimeSeries<Tseries> Tsffseries;
372  Tsffseries::Theader Zheader(Zindata[0].header);
373  Tsffseries::Theader Rheader(Rindata[0].header);
374 
375  sff::WID2 wid2line=Zheader.wid2();
376  wid2line.nsamples=Zseries.size();
377  wid2line.channel="Z";
378  Zheader.wid2(wid2line);
379  Tsffseries Zoutdata(Zseries, Zheader);;
380 
381  wid2line=Rheader.wid2();
382  wid2line.nsamples=Rseries.size();
383  wid2line.channel="R";
384  Rheader.wid2(wid2line);
385  Tsffseries Routdata(Rseries, Rheader);;
386 
387  {
388  if (opt.verbose) { cout << "open output file " << filename.Zout << endl; }
389  // check if output file exists and open
390  if (!opt.overwrite)
391  {
392  std::ifstream file(filename.Zout.c_str(),std::ios_base::in);
393  TFXX_assert((!file.good()),"ERROR: output file exists!");
394  }
395  std::ofstream ofs(filename.Zout.c_str());
396  datrw::osffstream os(ofs);
397 
398  // prepare file FREE block
399  sff::FREE filefree;
400  filefree.append(NOISYMIZE_VERSION);
401 
402  os << Zoutdata;
403  }
404 
405  {
406  if (opt.verbose) { cout << "open output file " << filename.Rout << endl; }
407  // check if output file exists and open
408  if (!opt.overwrite)
409  {
410  std::ifstream file(filename.Rout.c_str(),std::ios_base::in);
411  TFXX_assert((!file.good()),"ERROR: output file exists!");
412  }
413  std::ofstream ofs(filename.Rout.c_str());
414  datrw::osffstream os(ofs);
415 
416  // prepare file FREE block
417  sff::FREE filefree;
418  filefree.append(NOISYMIZE_VERSION);
419 
420  os << Routdata;
421  }
422 }
std::string Rout
Definition: noisymize.cc:81
#define NOISYMIZE_VERSION
Definition: noisymize.cc:44
bool setnoiselength
Definition: noisymize.cc:69
std::string inputformat
Definition: cross.cc:75
std::string Rin
Definition: noisymize.cc:79
std::string Zout
Definition: noisymize.cc:80
bool overwrite
Definition: autocorr.cc:61
bool noscaling
Definition: noisymize.cc:70
int noiselength
Definition: noisymize.cc:71
bool reportfactors
Definition: noisymize.cc:69
bool verbose
Definition: autocorr.cc:61
std::string Zin
Definition: noisymize.cc:78
double offexp
Definition: noisymize.cc:72
aff::Series< double > Tseries
Definition: cross.cc:69
ts::sff::File< Tseries > Tfile
Definition: foutra.cc:131