cxxfftwartest.cc 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*! \file cxxfftwartest.cc
 * \brief test fftw3 array engine
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
 * \date 16/05/2011
 * 
 * test fftw3 array engine
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
14
 * libfourier is free software; you can redistribute it and/or modify
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
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
101
102
103
104
105
106
107
108
109
110
111
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
164
165
166
167
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
 * 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
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 16/05/2011   V1.0   Thomas Forbriger
 *  - 27/05/2011   V1.1   test copy constructor and default constructor
 *  - 08/09/2011   V1.2   avoid deprecated ?anystream constructors
 * 
 * ============================================================================
 */
#define CXXFFTWARTEST_VERSION \
  "CXXFFTWARTEST   V1.2   test fftw3 array engine"

// use the input/output facilities from the standard library
#include <iostream>
#include <fstream>

// use output string streams to create strings
#include <sstream>

// use the vector container from the STL (standard template library) to store
// collections of traces and files
#include <vector>

// use command line argument reader from libtfxx
#include <tfxx/commandline.h>
#include <tfxx/xcmdline.h>

// use the error handling and debugging facilities from libtfxx
#include <tfxx/error.h>
#include <tfxx/misc.h>

// use the facility in libtfxx to handle lists of trace number ranges
#include <tfxx/rangestring.h>
#include <tfxx/rangelist.h>

// use series container from libaff
#include <aff/series.h>

// use data file reading and writing facility from libdatrwxx
#include <datrwxx/readany.h>
#include <datrwxx/writeany.h>
  
// use operators defined for series
#include <aff/seriesoperators.h>
// use operators defined for array
#include <aff/arrayoperators.h>

// use fft modules
#include <fourier/fftwaff.h>
#include <fourier/fftwaffar.h>

/*----------------------------------------------------------------------*/

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

/*----------------------------------------------------------------------*/

// a struct to store values of command line arguments
struct Options {
  // be verbose
  bool verbose, verbsize, exchangeengines;
  // file format to be used
  std::string fileformat;
  // overwrite output files in case they already exist
  bool overwrite;
}; // struct Options

/*----------------------------------------------------------------------*/

// type of actual time series samples
typedef double Tvalue;

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

/*----------------------------------------------------------------------*/

// a struct to store the contents of one data trace
struct Trace {
  // the time series
  Tseries series;
  // number of trace in file
  int tracenumber;
  // the waveform header (sampling rate, etc)
  sff::WID2 wid2;
  // coordinates of receiver
  sff::INFO info;
}; // struct Trace

// several traces (e.g. the traces of one file) are collected in a vector
// of the STL (standard template library)
typedef std::vector<Trace> Tvecoftraces;

/*======================================================================*/

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

  // define usage information
  char usage_text[]=
  {
    CXXFFTWARTEST_VERSION "\n"
    "usage: cxxfftwartest input output [-s] [-v] [-o] [-x] [-type f]" "\n"
    "   or: cxxfftwartest --help|-h" "\n"
    "   or: cxxfftwartest --xhelp" "\n"
  };

  // define full help text
  char help_text[]=
  {
    "\n"
    "input        input file name\n"
    "output       output file name\n"
    "\n"
    "-v           verbose mode\n"
    "-s           report container sizes\n"
    "-o           overwrite output file\n"
    "-x           reverse order of DRFFTWAFF and DRFFTWAFFArrayEngine\n"
    "             (see below)\n"
    "-type f      file format\n"
    "\n"
    "Fourier coefficients for input time series are obtained from\n"
    "DRFFTWAFFArrayEngine. Then time series are constructed from these\n"
    "coefficients by application of DRFFTWAFF. If the -x option is\n"
    "selected, both transformation tools are exchanged in the\n"
    "application order.\n"
  };

  // define commandline options
  using namespace tfxx::cmdline;
  static Declare options[]= 
  {
    // 0: print help
    {"help",arg_no,"-"},
    // 1: print detailed online help
    {"xhelp",arg_no,"-"},
    // 2: verbose mode
    {"v",arg_no,"-"},
    // 3: overwrite mode
    {"o",arg_no,"-"},
    // 4: file type
    {"type",arg_no,"sff"},
    // 5: report container sizes
    {"s",arg_no,"-"},
    // 6: exchange engines
    {"x",arg_no,"-"},
    {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;
    datrw::supported_data_types(cerr);
    exit(0);
  }

  // help on file format details requested? 
  if (cmdline.optset(1))
  {
    cerr << usage_text << endl;
    cerr << endl;
    datrw::online_help(cerr);
    exit(0);
  }

  Options opt;

  opt.verbose=cmdline.optset(2);
  opt.overwrite=cmdline.optset(3);
  opt.fileformat=cmdline.string_arg(4);
  opt.verbsize=cmdline.optset(5);
  opt.exchangeengines=cmdline.optset(6);

  // report program version if in verbose mode
  if (opt.verbose)
217
  { cout << CXXFFTWARTEST_VERSION << endl; }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

  // extract commandline arguments
  // here the rest of the command line is parsed; i.e. the names of input
  // files together with file specific options
  TFXX_assert(cmdline.extra(), "missing input file");
  std::string infilename=cmdline.next();
  TFXX_assert(cmdline.extra(), "missing output file");
  std::string outfilename=cmdline.next();

  /*----------------------------------------------------------------------*/

  // here we read the input file
  // ---------------------------

  // open input file
  // ---------------
  if (opt.verbose) { cout << "open input file " << infilename << endl; }
  // open the input file with appropriate input mode
  std::ios_base::openmode iopenmode
    =datrw::ianystream::openmode(opt.fileformat);
  // open file stream
  std::ifstream ifs(infilename.c_str(), iopenmode);
  // open seismic time series stream
  datrw::ianystream is(ifs, opt.fileformat);

  // read file specific header information
  sff::SRCE srce;
  is >> srce;

  /*----------------------------------------------------------------------*/

  // read all traces from this file
  // ------------------------------

  Tvecoftraces vecoftraces;
  // count traces
  unsigned int itrace=0;

  unsigned int nsamples=0;

  // read next trace, while input stream is good
  while (is.good())
  {
    // count traces
    ++itrace;

    // create a place to store trace data
    Trace trace;

    // remember number of trace
    trace.tracenumber=itrace;

    // read trace data
    is >> trace.series;
    is >> trace.wid2;
    is >> trace.info;

    nsamples=nsamples > trace.series.size() ? nsamples : trace.series.size();

    // trace data is read; add to collection
    vecoftraces.push_back(trace);

  } // while (is.good())

  /*----------------------------------------------------------------------*/

  unsigned int ntraces=vecoftraces.size();

  /*----------------------------------------------------------------------*/

  if (opt.verbose)
  {
    cout << "input files are read..." << endl;
  }

  if (opt.verbose)
  {
    cout << "number of traces: " << ntraces << endl;
    cout << "number of samples in largest trace: " << nsamples << endl;
  }

  /*----------------------------------------------------------------------*/

  if (opt.exchangeengines)
  {
    // prepare array engine
    if (opt.verbose) { cout << "create FFT array engine" << endl; }
    fourier::fft::DRFFTWAFFArrayEngine primaryengine;
    primaryengine=fourier::fft::DRFFTWAFFArrayEngine(ntraces, nsamples);
    fourier::fft::DRFFTWAFFArrayEngine engine(primaryengine);

    if (opt.verbose) 
    {
      cout << "fill FFT array engine with Fourier coefficients" << endl; 
    }
    engine.spectrum()=0;

    fourier::fft::DRFFTWAFF fft;
    fft.size(nsamples);

    for (int i=0; i<engine.nseries(); ++i)
    {
      fourier::fft::DRFFTWAFF::Tspectrum
        spectrum=fft(vecoftraces[i].series,vecoftraces[i].wid2.dt);
      engine.spectrum(i).copyin(spectrum);
    }

    if (opt.verbose) { cout << "execute FFT array engine" << endl; }
    engine.c2r();

    if (opt.verbose) 
    {
      cout << "read time series samples from FFT array engine" << endl; 
    }
    for (int i=0; i<engine.nseries(); ++i)
    {
      Trace& trace=vecoftraces[i];
      Tseries& series=trace.series;
      series.copyin(engine.series(i)*engine.scale_series(trace.wid2.dt));
    }
  }
  else // if (opt.exchangeengines)
  {
    // prepare array engine
    if (opt.verbose) { cout << "create FFT array engine" << endl; }
    fourier::fft::DRFFTWAFFArrayEngine engine(ntraces, nsamples);

    if (opt.verbose) { cout << "fill FFT array engine" << endl; }
    engine.series()=0;
    // fill sample array
    for (int i=0; i<engine.nseries(); ++i)
    {
      engine.series(i).copyin(vecoftraces[i].series);
      if (opt.verbsize)
      {
        cout << "engine series #" << i << " has " << engine.series(i).size()
          << " samples" << endl;
      }
    }

    if (opt.verbose) { cout << "execute FFT array engine" << endl; }
    engine.r2c();

    if (opt.verbose) 
    {
      cout << "read coefficients and transform to time domain" << endl; 
    }
    fourier::fft::DRFFTWAFF fft;
    fft.size(nsamples);
    for (int i=0; i<engine.nseries(); ++i)
    {
      Trace& trace=vecoftraces[i];
      Tseries& series=trace.series;
      fourier::fft::DRFFTWAFFArrayEngine::TAspectrum coeff
        =engine.spectrum(i)*engine.scale_spectrum(trace.wid2.dt);
      fourier::fft::DRFFTWAFF::Tspectrum spectrum(coeff.size());
      spectrum.copyin(coeff);
      series=fft(spectrum,trace.wid2.dt);
      if (opt.verbsize)
      {
        cout << "after return from engine for trace #" << i << endl;
        cout << "  size of engine spectrum: " << engine.spectrum(i).size() 
          << endl;
        cout << "  size of copy of spectrum: " << spectrum.size()
          << endl;
        cout << "  returned series size: " << series.size() << endl;
      }
    }
  } // if (opt.exchangeengines)

  /*----------------------------------------------------------------------*/

  // write back transformed traces
  // -----------------------------

  if (opt.verbose) { cout << "write transformed traces" << endl; }
  if (opt.verbose) 
  {
    cout << "write file " << outfilename << endl;
  }
    
  // check if output file exists and open
  if (!opt.overwrite)
  {
    std::ifstream file(outfilename.c_str(),std::ios_base::in);
    TFXX_assert((!file.good()),"ERROR: output file exists!");
  }

  // create output stream of desired file format
  std::ios_base::openmode oopenmode
    =datrw::oanystream::openmode(opt.fileformat);
  std::ofstream ofs(outfilename.c_str(), oopenmode);
  datrw::oanystream os(ofs, opt.fileformat);

  // write srce data and file free block if supported
  if (os.handlessrce()) { os << srce; }

  // write trace data
  for (unsigned int j=0; j<vecoftraces.size(); ++j)
  {
    if (opt.verbose)
    {
      cout << "  write trace #" << j+1 << endl;
    }
    Trace& currenttrace=vecoftraces[j];

    os << currenttrace.wid2;
    if (os.handlesinfo()) { os << currenttrace.info; }
    os << currenttrace.series;
  } // for (unsigned int j=0; j<currentfile.traces.size(); ++j)
}

/* ----- END OF cxxfftwartest.cc ----- */