noisymize.cc 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*! \file noisymize.cc
 * \brief program reads a set of SFF traces, convolves them with white noise and stacks
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
 * \date 27/06/2006
 * 
 * program reads a set of SFF traces, convolves them with white noise and stacks
 * 
 * Copyright (c) 2006 by Thomas Forbriger (BFO Schiltach) 
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
28 29 30
 * 
 * REVISIONS and CHANGES 
 *  - 27/06/2006   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
31
 *  - 11/07/2006   V1.1   keep result of full convolution length
thomas.forbriger's avatar
thomas.forbriger committed
32
 *  - 26/02/2007   V1.2   report factors
thomas.forbriger's avatar
thomas.forbriger committed
33
 *  - 02/03/2007   V1.3   scale with offset
thomas.forbriger's avatar
thomas.forbriger committed
34
 *                 V1.4   special scaling for horizontal component
thomas.forbriger's avatar
thomas.forbriger committed
35 36 37 38
 *  - 08/03/2007   V1.5   
 *                        - allow switching of offset specific scaling
 *                        - added description in help section
 *                        - improved out format of list of factors
thomas.forbriger's avatar
thomas.forbriger committed
39
 *  - 07/09/2007   V1.6   corrected horizontal component scaling factor
40
 *  - 02/04/2019          use new header file interface to libtsioxx
41 42 43 44
 * 
 * ============================================================================
 */
#define NOISYMIZE_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
45
  "NOISYMIZE   V1.6   program reads a set of SFF traces,\n" \
thomas.forbriger's avatar
thomas.forbriger committed
46
  "                   convolves them with white noise and stacks"
47 48 49 50

#include <fstream>
#include <iostream>
#include <string>
thomas.forbriger's avatar
thomas.forbriger committed
51
#include <cmath>
52 53 54
#include <tfxx/commandline.h>
#include <tfxx/error.h>
#include <aff/series.h>
thomas.forbriger's avatar
thomas.forbriger committed
55
#include <aff/seriesoperators.h>
56 57
#include <tsxx/tsxx.h>
#include <tsxx/convolve.h>
58
#include <tsioxx/tsioxx.h>
thomas.forbriger's avatar
thomas.forbriger committed
59
#include <tsxx/random.h>
60
#include <datrwxx/readany.h>
61
#include <datrwxx/sff.h>
62 63 64 65 66 67 68

using std::cout;
using std::cerr;
using std::endl;

// put all commandline settings into a struct
struct Options {
thomas.forbriger's avatar
thomas.forbriger committed
69
  bool verbose, setnoiselength, overwrite, reportfactors;
thomas.forbriger's avatar
thomas.forbriger committed
70
  bool noscaling;
thomas.forbriger's avatar
thomas.forbriger committed
71
  int noiselength;
thomas.forbriger's avatar
thomas.forbriger committed
72
  double offexp;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  std::string inputformat;
}; // struct Options

// put all filenames in one struct
struct Filenames {
  std::string Zin;
  std::string Rin;
  std::string Zout;
  std::string Rout;
}; // struct Filenames

// values type to be used for samples
typedef double Tvalue;

// time series
typedef aff::Series<Tvalue> Tseries;

// full featured time series file
typedef ts::sff::File<Tseries> Tfile;

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

  // define usage information
  char usage_text[]=
  {
    NOISYMIZE_VERSION "\n"
    "usage: noisymize Zin Rin Zout Rout" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
101
    "                 [-v] [-type t] [-n n] [-o] [-rf] [-s e]" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
102
    "                 [-fixed]" "\n"
103 104 105 106 107 108 109 110 111 112 113 114 115
    "   or: noisymize --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
    "Zin          vertical component input data" "\n"
    "Rin          radial component input data" "\n"
    "Zout         vertical component output data" "\n"
    "Rout         radial component output data" "\n"
    "\n"
    "-v           be verbose" "\n"
    "-type t      input file type" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
116
    "-n n         number of noise samples to use" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
117
    "-o           overwrite output" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
118
    "-rf          report factors" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
119 120
    "-s e         scale seismograms with offset" "\n"
    "             the scaling factor is r**e, where r is the offset" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
121 122 123
    "-fixed       do not scale each offset with a randomly" "\n"
    "             chosen amplitude" "\n"
    "\n"
124 125 126 127 128 129
    "This program was specifically created to test procedures for\n"
    "MHVSR (microseismic H/V spectral ratio) analysis by application\n"
    "to synthetic data. Synthetic seismograms for vertical and radial\n"
    "component of the receiver calculated for a transient source signal\n"
    "have to be convolved with the same random noise in this case\n"
    "\n"
thomas.forbriger's avatar
thomas.forbriger committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143
    "Description of the procedure:" "\n"
    "- the program reads pairs of seismograms from Zin and Rin" "\n"
    "- each of the input files may contain multiple traces" "\n"
    "- for each trace in Zin there must be a corresponding trace" "\n"
    "  in Rin at the same offset" "\n"
    "- for each offset the program does the following:" "\n"
    "  1. the Z-trace and the R-trace are convolved with the same" "\n"
    "     stochastic time series (random normaly distributed white noise)" "\n"
    "     a new stochastic time series is calculated for each offset" "\n"
    "     through the libgsl random number generator" "\n"
    "  2. the Z-trace and the R-trace are scaled by a random value" "\n"
    "     (this function can be switched off through option -fixed)" "\n"
    "  3. the Z-trace and the R-trace are scaled by an offset" "\n"
    "     dependend value (selected through option -s e)" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
144
    "  4. the R-trace is scaled by 0.707 to simulate a uniform" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
145 146 147 148 149
    "     distribution of sources of all azimuths; as a consequence" "\n"
    "     the R result my be used as N- and E-component as well" "\n"
    "- the results for all offsets are stacked; the resulting Z-" "\n"
    "  and R-series are written to files Zout and Rout," "\n"
    "  respectively"
150 151 152 153 154 155 156 157 158 159 160 161
  };

  // define commandline options
  using namespace tfxx::cmdline;
  static Declare options[]= 
  {
    // 0: print help
    {"help",arg_no,"-"},
    // 1: verbose mode
    {"v",arg_no,"-"},
    // 2: input format
    {"type",arg_yes,"sff"},
thomas.forbriger's avatar
thomas.forbriger committed
162 163
    // 3: number of noise samples
    {"n",arg_yes,"1"},
thomas.forbriger's avatar
thomas.forbriger committed
164
    // 4: overwrite output file
thomas.forbriger's avatar
thomas.forbriger committed
165
    {"o",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
166 167
    // 5: report factors
    {"rf",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
168 169
    // 6: report factors
    {"s",arg_yes,"0."},
thomas.forbriger's avatar
thomas.forbriger committed
170 171
    // 7: report factors
    {"fixed",arg_no,"-"},
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    {NULL}
  };

  // no arguments? print usage...
  if (iargc<2) 
  {
    cerr << usage_text << endl;
    exit(0);
  }

  // collect options from commandline
  Commandline cmdline(iargc, argv, options);

  // help requested? print full help text...
  if (cmdline.optset(0))
  {
    cerr << usage_text << endl;
    cerr << help_text << endl;
    exit(0);
  }

 
  /*----------------------------------------------------------------------*/
  // read command line settings

  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.inputformat=cmdline.string_arg(2);
thomas.forbriger's avatar
thomas.forbriger committed
200 201
  opt.setnoiselength=cmdline.optset(3);
  opt.noiselength=cmdline.int_arg(3);
thomas.forbriger's avatar
thomas.forbriger committed
202
  opt.overwrite=cmdline.optset(4);
thomas.forbriger's avatar
thomas.forbriger committed
203
  opt.reportfactors=cmdline.optset(5);
thomas.forbriger's avatar
thomas.forbriger committed
204
  opt.offexp=cmdline.double_arg(6);
thomas.forbriger's avatar
thomas.forbriger committed
205
  opt.noscaling=cmdline.optset(7);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

  Filenames filename;
  TFXX_assert(cmdline.extra(), "ERROR: missing filename Zin!");
  filename.Zin=cmdline.next();
  TFXX_assert(cmdline.extra(), "ERROR: missing filename Rin!");
  filename.Rin=cmdline.next();
  TFXX_assert(cmdline.extra(), "ERROR: missing filename Zout!");
  filename.Zout=cmdline.next();
  TFXX_assert(cmdline.extra(), "ERROR: missing filename Rout!");
  filename.Rout=cmdline.next();

  /*----------------------------------------------------------------------*/
  // read vertical component input data
  if (opt.verbose)
  { cout << "read input file " << filename.Zin << endl; }
  Tfile Zindata;
  {
    std::ifstream ifs(filename.Zin.c_str());
224
    datrw::ianystream is(ifs, opt.inputformat);
225 226 227 228 229 230 231 232
    Zindata.read(is.idatstream(), opt.verbose); 
  }
  // read radial component input data
  if (opt.verbose)
  { cout << "read input file " << filename.Rin << endl; }
  Tfile Rindata;
  {
    std::ifstream ifs(filename.Rin.c_str());
233
    datrw::ianystream is(ifs, opt.inputformat);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    Rindata.read(is.idatstream(), opt.verbose); 
  }

  /*----------------------------------------------------------------------*/
  // consistency checks
  // ------------------
  //
  if (opt.verbose) { cout << "consistency checks:" << endl; }

  // check number of traces
  TFXX_assert(Zindata.size()==Rindata.size(),
              "ERROR: inconsitent number of traces");
  if (opt.verbose) 
  { cout << "input files both have " << Zindata.size() << " traces." << endl; }

  // check trace headers
  Tfile::Tbase::const_iterator Zit=Zindata.begin();
  Tfile::Tbase::const_iterator Rit=Rindata.begin();
  sff::WID2compare compare(sff::Fnsamples | sff::Fdt);
  while((Zit!=Zindata.end()) && (Rit!=Rindata.end()))
  {
    TFXX_assert(compare(Zit->header.wid2(), Rit->header.wid2()),
                "ERROR: inconsitent trace headers");
    if (Zit->header.hasinfo())
    {
      sff::INFO Zinfo=Zit->header.info();
      sff::INFO Rinfo=Rit->header.info();
      TFXX_assert(Zinfo==Rinfo,
                  "ERROR: inconsitent coordinates");
    }
    ++Zit;
    ++Rit;
  }
  if (opt.verbose) 
  { cout << "trace headers and coordinates are consistent" << endl; }

  /*----------------------------------------------------------------------*/
  // go and noisymize data
  // ---------------------
thomas.forbriger's avatar
thomas.forbriger committed
273
  //
thomas.forbriger's avatar
thomas.forbriger committed
274 275
  Zit=Zindata.begin();
  Rit=Rindata.begin();
thomas.forbriger's avatar
thomas.forbriger committed
276 277 278
  // source coordinates
  sff::SRCE Zsrce=Zindata.fileheader.srce();
  sff::SRCE Rsrce=Rindata.fileheader.srce();
thomas.forbriger's avatar
thomas.forbriger committed
279 280
  // results
  Tseries Zseries, Rseries;
thomas.forbriger's avatar
thomas.forbriger committed
281 282
  // scaling factors
  Tseries fac=ts::rnd::dugauss(Zindata.size());
thomas.forbriger's avatar
thomas.forbriger committed
283
  if (opt.noscaling) { fac=1.; }
thomas.forbriger's avatar
thomas.forbriger committed
284 285
  Tseries offfac(Zindata.size());
  Tseries traceoffset(Zindata.size());
thomas.forbriger's avatar
thomas.forbriger committed
286
  //
thomas.forbriger's avatar
thomas.forbriger committed
287 288 289 290 291 292
  if (!opt.setnoiselength)
  {
    opt.noiselength=Zit->size();
  }
  if (opt.verbose) 
  { 
thomas.forbriger's avatar
thomas.forbriger committed
293 294
    cout << "convolve " << Zit->size() 
      << " data samples (each trace) with" << endl
thomas.forbriger's avatar
thomas.forbriger committed
295 296 297
      << opt.noiselength << " samples of uniform gaussian noise" 
      << " and stack." << endl; 
  }
thomas.forbriger's avatar
thomas.forbriger committed
298
  bool initialize_result=true;
thomas.forbriger's avatar
thomas.forbriger committed
299
  unsigned int itrace=0;
thomas.forbriger's avatar
thomas.forbriger committed
300 301
  while((Zit!=Zindata.end()) && (Rit!=Rindata.end()))
  {
thomas.forbriger's avatar
thomas.forbriger committed
302 303 304 305 306 307 308 309 310
    // calculate offset and offset dependend scaling
    sff::INFO Zinfo=Zit->header.info();
    sff::INFO Rinfo=Rit->header.info();
    double Zoffset=sff::offset(Zsrce, Zinfo);
    double Roffset=sff::offset(Rsrce, Rinfo);
    double offerr=(Zoffset/Roffset)-1.;
    if (offerr < 0.) { offerr=-offerr; }
    TFXX_assert((offerr < 1.e-4), 
                "ERROR: offset of R- and Z-component are inconsistent!");
thomas.forbriger's avatar
thomas.forbriger committed
311 312
    traceoffset(itrace)=Zoffset;
    offfac(itrace)=std::pow(Zoffset, opt.offexp);
thomas.forbriger's avatar
thomas.forbriger committed
313
    // scaling factor for radial component, since we see only the projection
thomas.forbriger's avatar
thomas.forbriger committed
314
    // of the radial component of all sources on the respective horizontal
thomas.forbriger's avatar
thomas.forbriger committed
315
    // component
thomas.forbriger's avatar
thomas.forbriger committed
316 317 318 319 320
    // 7.9.2007: scaling factor changed appropriately for signal power (see
    //           help output of gresynoise)
    // Hfactor = sqrt( (int_0^{2*pi} cos(phi)**2 d phi) / (2 pi) )
    // formerly: const double Hfactor=2./M_PI;
    const double Hfactor=0.707; // = 1/sqrt(2)
thomas.forbriger's avatar
thomas.forbriger committed
321
    // go
thomas.forbriger's avatar
thomas.forbriger committed
322
    double scalefac=fac(itrace)*offfac(itrace);
thomas.forbriger's avatar
thomas.forbriger committed
323
    if (opt.verbose) { cout << "*"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
324
    Tseries noise=ts::rnd::dugauss(opt.noiselength);
thomas.forbriger's avatar
thomas.forbriger committed
325
    if (opt.verbose) { cout << "+"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
326
    if (initialize_result)
thomas.forbriger's avatar
thomas.forbriger committed
327
    { Zseries = scalefac*ts::convolve(*Zit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
328
    else
thomas.forbriger's avatar
thomas.forbriger committed
329
    { Zseries += scalefac*ts::convolve(*Zit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
330
    if (opt.verbose) { cout << "+"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
331
    if (initialize_result)
thomas.forbriger's avatar
thomas.forbriger committed
332
    { Rseries = Hfactor*scalefac*ts::convolve(*Rit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
333
    else
thomas.forbriger's avatar
thomas.forbriger committed
334
    { Rseries += Hfactor*scalefac*ts::convolve(*Rit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
335
    initialize_result=false;
thomas.forbriger's avatar
thomas.forbriger committed
336 337
    ++Zit;
    ++Rit;
thomas.forbriger's avatar
thomas.forbriger committed
338
    ++itrace;
thomas.forbriger's avatar
thomas.forbriger committed
339
  }
thomas.forbriger's avatar
thomas.forbriger committed
340
  if (opt.verbose) { cout << endl; }
thomas.forbriger's avatar
thomas.forbriger committed
341 342 343
  if (opt.reportfactors)
  {
    cout << "factors used for scaling:" << endl;
thomas.forbriger's avatar
thomas.forbriger committed
344 345 346 347 348 349 350 351
    cout.width(5);
    cout << "#";
    cout.width(12);
    cout << "f1";
    cout.width(12);
    cout << "f2";
    cout.width(14);
    cout << "offset" << endl;
thomas.forbriger's avatar
thomas.forbriger committed
352 353
    for (itrace=0; itrace<Zindata.size(); itrace++)
    {
thomas.forbriger's avatar
thomas.forbriger committed
354 355 356 357 358 359 360 361 362
      cout.width(5); cout.precision(3);
      cout << itrace;
      cout.width(12); cout.precision(4);
      cout << fac(itrace);
      cout.width(12); cout.precision(4);
      cout << offfac(itrace);
      cout.width(12); cout.precision(4);
      cout << traceoffset(itrace)*1.e-3 << "km";
      cout << endl;
thomas.forbriger's avatar
thomas.forbriger committed
363
    }
thomas.forbriger's avatar
thomas.forbriger committed
364
    cout << "traces are scale by f1*f2" << endl;
thomas.forbriger's avatar
thomas.forbriger committed
365
  }
thomas.forbriger's avatar
thomas.forbriger committed
366 367 368

  /*----------------------------------------------------------------------*/
  // write result
thomas.forbriger's avatar
thomas.forbriger committed
369 370 371 372 373 374 375
  if (opt.verbose) 
  { cout << "prepare output data" << endl; }
  typedef ts::sff::SFFTimeSeries<Tseries> Tsffseries;
  Tsffseries::Theader Zheader(Zindata[0].header);
  Tsffseries::Theader Rheader(Rindata[0].header);

  sff::WID2 wid2line=Zheader.wid2();
376
  wid2line.nsamples=Zseries.size();
thomas.forbriger's avatar
thomas.forbriger committed
377 378
  wid2line.channel="Z";
  Zheader.wid2(wid2line);
379
  Tsffseries Zoutdata(Zseries, Zheader);;
thomas.forbriger's avatar
thomas.forbriger committed
380 381

  wid2line=Rheader.wid2();
382
  wid2line.nsamples=Rseries.size();
thomas.forbriger's avatar
thomas.forbriger committed
383 384
  wid2line.channel="R";
  Rheader.wid2(wid2line);
385
  Tsffseries Routdata(Rseries, Rheader);;
thomas.forbriger's avatar
thomas.forbriger committed
386 387 388 389 390 391 392 393 394 395

  {
    if (opt.verbose) { cout << "open output file " << filename.Zout << endl; }
    // check if output file exists and open
    if (!opt.overwrite)
    {
      std::ifstream file(filename.Zout.c_str(),std::ios_base::in);
      TFXX_assert((!file.good()),"ERROR: output file exists!");
    }
    std::ofstream ofs(filename.Zout.c_str());
396
    datrw::osffstream os(ofs);
397

thomas.forbriger's avatar
thomas.forbriger committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    // prepare file FREE block
    sff::FREE filefree;
    filefree.append(NOISYMIZE_VERSION);

    os << Zoutdata;
  }

  {
    if (opt.verbose) { cout << "open output file " << filename.Rout << endl; }
    // check if output file exists and open
    if (!opt.overwrite)
    {
      std::ifstream file(filename.Rout.c_str(),std::ios_base::in);
      TFXX_assert((!file.good()),"ERROR: output file exists!");
    }
    std::ofstream ofs(filename.Rout.c_str());
414
    datrw::osffstream os(ofs);
thomas.forbriger's avatar
thomas.forbriger committed
415 416 417 418 419 420 421

    // prepare file FREE block
    sff::FREE filefree;
    filefree.append(NOISYMIZE_VERSION);

    os << Routdata;
  }
422 423 424
}

/* ----- END OF noisymize.cc ----- */