noisymize.cc 9.93 KB
Newer Older
1
2
3
4
5
/*! \file noisymize.cc
 * \brief program reads a set of SFF traces, convolves them with white noise and stacks
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: noisymize.cc,v 1.6 2007-02-26 10:38:59 tforb Exp $
7
8
9
10
11
12
13
14
15
 * \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) 
 * 
 * REVISIONS and CHANGES 
 *  - 27/06/2006   V1.0   Thomas Forbriger
thomas.forbriger's avatar
thomas.forbriger committed
16
 *  - 11/07/2006   V1.1   keep result of full convolution length
thomas.forbriger's avatar
thomas.forbriger committed
17
 *  - 26/02/2007   V1.2   report factors
18
19
20
21
 * 
 * ============================================================================
 */
#define NOISYMIZE_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
22
  "NOISYMIZE   V1.2   program reads a set of SFF traces, convolves them with white noise and stacks"
23
#define NOISYMIZE_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
24
  "$Id: noisymize.cc,v 1.6 2007-02-26 10:38:59 tforb Exp $"
25
26
27
28
29
30
31

#include <fstream>
#include <iostream>
#include <string>
#include <tfxx/commandline.h>
#include <tfxx/error.h>
#include <aff/series.h>
thomas.forbriger's avatar
thomas.forbriger committed
32
#include <aff/seriesoperators.h>
33
34
35
#include <tsxx/tsxx.h>
#include <tsxx/convolve.h>
#include <tsxx/sffheaders.h>
thomas.forbriger's avatar
thomas.forbriger committed
36
#include <tsxx/random.h>
37
38
39
40
41
42
43
44
#include <datreadxx/readany.h>

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
45
  bool verbose, setnoiselength, overwrite, reportfactors;
thomas.forbriger's avatar
thomas.forbriger committed
46
  int noiselength;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  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
75
    "                 [-v] [-type t] [-n n] [-o] [-rf]" "\n"
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    "   or: noisymize --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
    NOISYMIZE_CVSID "\n"
    "\n"
    "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
91
    "-n n         number of noise samples to use" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
92
    "-o           overwrite output" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
93
    "-rf          report factors" "\n"
94
95
96
97
98
99
100
101
102
103
104
105
  };

  // 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
106
107
    // 3: number of noise samples
    {"n",arg_yes,"1"},
thomas.forbriger's avatar
thomas.forbriger committed
108
    // 4: overwrite output file
thomas.forbriger's avatar
thomas.forbriger committed
109
    {"o",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
110
111
    // 5: report factors
    {"rf",arg_no,"-"},
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    {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);
  }

  /*
  // dummy operation: print option settings
  for (int iopt=0; iopt<2; iopt++)
  {
    cout << "option: '" << options[iopt].opt_string << "'" << endl;
    if (cmdline.optset(iopt)) {  cout << "  option was set"; }
    else { cout << "option was not set"; }
    cout << endl;
    cout << "  argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
    cout << "     argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
    cout << "    argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
    cout << "   argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
    cout << "  argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
    cout << "    argument (bool): '";
    if (cmdline.bool_arg(iopt))
    { cout << "true"; } else { cout << "false"; }
    cout << "'" << endl;
  }
  while (cmdline.extra()) { cout << cmdline.next() << endl; }

  // dummy operation: print rest of command line
  while (cmdline.extra()) { cout << cmdline.next() << endl; }
  */

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

  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.inputformat=cmdline.string_arg(2);
thomas.forbriger's avatar
thomas.forbriger committed
164
165
  opt.setnoiselength=cmdline.optset(3);
  opt.noiselength=cmdline.int_arg(3);
thomas.forbriger's avatar
thomas.forbriger committed
166
  opt.overwrite=cmdline.optset(4);
thomas.forbriger's avatar
thomas.forbriger committed
167
  opt.reportfactors=cmdline.optset(5);
168
169
170
171
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

  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());
    datread::ianystream is(ifs, datread::anyID(opt.inputformat));
    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());
    datread::ianystream is(ifs, datread::anyID(opt.inputformat));
    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
235
  //
thomas.forbriger's avatar
thomas.forbriger committed
236
237
  Zit=Zindata.begin();
  Rit=Rindata.begin();
thomas.forbriger's avatar
thomas.forbriger committed
238
239
  // results
  Tseries Zseries, Rseries;
thomas.forbriger's avatar
thomas.forbriger committed
240
241
242
  // scaling factors
  Tseries fac=ts::rnd::dugauss(Zindata.size());
  //
thomas.forbriger's avatar
thomas.forbriger committed
243
244
245
246
247
248
  if (!opt.setnoiselength)
  {
    opt.noiselength=Zit->size();
  }
  if (opt.verbose) 
  { 
thomas.forbriger's avatar
thomas.forbriger committed
249
250
    cout << "convolve " << Zit->size() 
      << " data samples (each trace) with" << endl
thomas.forbriger's avatar
thomas.forbriger committed
251
252
253
      << opt.noiselength << " samples of uniform gaussian noise" 
      << " and stack." << endl; 
  }
thomas.forbriger's avatar
thomas.forbriger committed
254
  bool initialize_result=true;
thomas.forbriger's avatar
thomas.forbriger committed
255
  unsigned int itrace=0;
thomas.forbriger's avatar
thomas.forbriger committed
256
257
  while((Zit!=Zindata.end()) && (Rit!=Rindata.end()))
  {
thomas.forbriger's avatar
thomas.forbriger committed
258
    if (opt.verbose) { cout << "*"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
259
    Tseries noise=ts::rnd::dugauss(opt.noiselength);
thomas.forbriger's avatar
thomas.forbriger committed
260
    if (opt.verbose) { cout << "+"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
261
    if (initialize_result)
thomas.forbriger's avatar
thomas.forbriger committed
262
    { Zseries = fac(itrace)*ts::convolve(*Zit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
263
    else
thomas.forbriger's avatar
thomas.forbriger committed
264
    { Zseries += fac(itrace)*ts::convolve(*Zit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
265
    if (opt.verbose) { cout << "+"; cout.flush(); }
thomas.forbriger's avatar
thomas.forbriger committed
266
    if (initialize_result)
thomas.forbriger's avatar
thomas.forbriger committed
267
    { Rseries = fac(itrace)*ts::convolve(*Rit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
268
    else
thomas.forbriger's avatar
thomas.forbriger committed
269
    { Rseries += fac(itrace)*ts::convolve(*Rit, noise); }
thomas.forbriger's avatar
thomas.forbriger committed
270
    initialize_result=false;
thomas.forbriger's avatar
thomas.forbriger committed
271
272
    ++Zit;
    ++Rit;
thomas.forbriger's avatar
thomas.forbriger committed
273
    ++itrace;
thomas.forbriger's avatar
thomas.forbriger committed
274
  }
thomas.forbriger's avatar
thomas.forbriger committed
275
  if (opt.verbose) { cout << endl; }
thomas.forbriger's avatar
thomas.forbriger committed
276
277
278
279
280
281
282
283
  if (opt.reportfactors)
  {
    cout << "factors used for scaling:" << endl;
    for (itrace=0; itrace<Zindata.size(); itrace++)
    {
      cout << "trace #" << itrace << ": " << fac(itrace) << endl;
    }
  }
thomas.forbriger's avatar
thomas.forbriger committed
284
285
286

  /*----------------------------------------------------------------------*/
  // write result
thomas.forbriger's avatar
thomas.forbriger committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  if (opt.verbose) 
  { cout << "prepare output data" << endl; }
  typedef ts::sff::SFFTimeSeries<Tseries> Tsffseries;
  Tsffseries Zoutdata(Zseries), Routdata(Rseries);
  Tsffseries::Theader Zheader(Zindata[0].header);
  Tsffseries::Theader Rheader(Rindata[0].header);

  sff::WID2 wid2line=Zheader.wid2();
  wid2line.nsamples=Zoutdata.size();
  wid2line.channel="Z";
  Zheader.wid2(wid2line);
  Zoutdata.header=Zheader;

  wid2line=Rheader.wid2();
  wid2line.nsamples=Routdata.size();
  wid2line.channel="R";
  Rheader.wid2(wid2line);
  Routdata.header=Rheader;

  {
    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());
    sff::SFFostream<Tseries> os(ofs);
316

thomas.forbriger's avatar
thomas.forbriger committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    // 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());
    sff::SFFostream<Tseries> os(ofs);

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

    os << Routdata;
  }
341
342
343
}

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