croposp.cc 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*! \file croposp.cc
 * \brief Cross power spectral density
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
 * \date 25/12/2018
 * 
 * Cross power spectral density
 * 
 * Copyright (c) 2018 by Thomas Forbriger (BFO Schiltach) 
12
13
14
15
16
17
 *
 * ----
 * 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. 
18
 * 
19
20
21
22
23
24
25
26
27
 * 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, see <http://www.gnu.org/licenses/>.
 * ----
 *
28
29
30
31
32
33
34
35
36
 * REVISIONS and CHANGES 
 *  - 25/12/2018   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define CROPOSP_VERSION \
  "CROPOSP   V1.0   Cross power spectral density"

#include <iostream>
37
38
39
#include <vector>
#include <string>
#include <sstream>
40
#include <tfxx/commandline.h>
41
#include <tfxx/error.h>
42
#include <tfxx/misc.h>
43
#include <tfxx/stringfunc.h>
44
#include <tsioxx/cmdlinefiles.h>
45
#include <tsxx/tscollection.h>
46
47
48
49
50

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

51
struct Options {
52
  bool verbose, trim, debug;
53
  std::string inputformat, labelpattern;
54
  double datetolerance;
55
56
}; // struct Options

57
58
59
60
61
62
63
// a struct to hold meta data
struct Meta {
  tfxx::cmdline::Filename filearguments;
  std::string label;
}; // struct Meta

// type of time series collection to hold input data
64
typedef ts::TimeSeriesCollection<double> TCollection;
65
66
// a vector to gold meta data for each time series
typedef std::vector<Meta> TMetaVector;
67
68
69

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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
std::string patsubst(const std::string& templatestring,
                     const std::string& pattern,
                     const std::string& content)
{
  std::string retval;
  retval=tfxx::string::patsubst(templatestring, pattern,
                                tfxx::string::trimws(content));
  return(retval);
} // std::string patsubst(const std::string& templatestring,
  //                      const std::string& pattern,
  //                      const std::string& content)

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

84
void report_collection(const TCollection& collection,
85
                       const TMetaVector& metadata,
86
                       const bool& debug=false)
87
{
88
89
  TFXX_assert(collection.size() == metadata.size(),
              "data inconsitency; report this as a program bug");
90
  TCollection::const_iterator i_series=collection.begin();
91
92
93
  TMetaVector::const_iterator i_meta=metadata.begin();
  while (i_series != collection.end() &&
         i_meta != metadata.end())
94
95
96
97
98
99
100
101
102
  {
    cout << "  "
      << i_series->header.station << " "
      << i_series->header.channel << " "
      << i_series->header.auxid << " "
      << "dt=" << i_series->header.dt << "s "
      << "begin: " << i_series->header.date.timestring() << " "
      << "n: " << i_series->header.nsamples 
      << endl;
103
    cout << "  " << i_meta->label << endl;
104
105
106
107
108
109
    TFXX_debug(debug, "report_collection",
               TFXX_value(i_series->f())
               << " " <<
               TFXX_value(i_series->l())
               << " " <<
               TFXX_value(i_series->size()));
110
    ++i_series;
111
    ++i_meta;
112
113
114
115
116
  }
} // void report_collection(const Tcollection& collection)

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

117
118
119
120
121
122
123
int main(int iargc, char* argv[])
{

  // define usage information
  char usage_text[]=
  {
    CROPOSP_VERSION "\n"
124
125
126
    "usage: croposp [-verbose] [-itype f] [-trim] [-datetolerance t]" "\n"
    "               [-label p]" "\n"
    "       file [t:sel] [f:format] [n:label] [file [t:s] [f:f] [n:l]] [...]\n"
127
128
129
130
131
132
    "   or: croposp --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
133
    "-verbose           be verbose\n"
134
    "-DEBUG             produce debugging output\n"
135
136
137
138
139
    "-itype f           set default input file format (this value can\n"
    "                   be overwritten by the format key at a file name)\n"
    "-trim              trim time series to identical begin and length\n"
    "-datetolerance t   set tolerance as a fraction of sampling interval\n"
    "                   when comparing time series header date values\n"
140
141
142
143
144
145
146
147
    "-label p           set pattern for time series label\n"
    "                   %C: channel (WID2 header field)\n"
    "                   %S: station (WID2 header field)\n"
    "                   %I: instype (WID2 header field)\n"
    "                   %A: auxid (WID2 header field)\n"
    "                   %N: label set for file name\n"
    "                   %F: file name\n"
    "                   %NT: number of trace in file\n"
148
    "\n"
149
    "keys to be appended to file names if required:\n"
150
151
    "t:sel        trace selection\n"
    "f:format     file format\n"
152
    "n:label      label to be used in plot legend\n"
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
162
163
164
    {"verbose",arg_no,"-"},
    // 2: default input file format
    {"itype",arg_yes,"sff"},
165
166
167
168
    // 3: trim time series
    {"trim",arg_no,"-"},
    // 4: default input file format
    {"datetolerance",arg_yes,"0."},
169
170
    // 5: debug mode
    {"DEBUG",arg_no,"-"},
171
172
    // 6: pattern for trace label
    {"pattern",arg_yes,"%S:%C:%A:%I"},
173
174
175
176
177
    {NULL}
  };

  // define command line keys for input files
  static const char tracekey[]="t";
178
  static const char formatkey[]="f";
179
  static const char labelkey[]="n";
180
181

  // define commandline argument modifier keys
182
  static const char* cmdlinekeys[]={tracekey, formatkey, labelkey, 0};
183

184
185
186
187
188
  /* ====================================================================== */
  /* analyze command line
   * --------------------
   */

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  // 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);
  }
206
207
208
209

  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.inputformat=cmdline.string_arg(2);
210
211
  opt.trim=cmdline.optset(3);
  opt.datetolerance=cmdline.double_arg(4);
212
  opt.debug=cmdline.optset(5);
213
  opt.labelpattern=cmdline.string_arg(6);
214
215
216

  TFXX_assert(((opt.datetolerance >= 0.) && (opt.datetolerance <=1.)),
              "datetolerance is out of accepted range");
217
218
219
220
221
  
  // extract commandline arguments
  TFXX_assert(cmdline.extra(), "missing input file");
  tfxx::cmdline::Tparsed arguments=parse_cmdline(cmdline, cmdlinekeys);

222
223
224
225
226
  /* ====================================================================== */
  /* read data, prepare labels and trim time series if requested
   * -----------------------------------------------------------
   */

227
228
229
230
231
  // read data files
  if (opt.verbose) 
  {
    cout << "Read time series data" << endl;
  }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  ts::sff::TDFileList input_file_list;
  tfxx::cmdline::Tparsed::const_iterator file=arguments.begin();
  while (file != arguments.end())
  {
    std::string format=opt.inputformat;
    if (file->haskey(formatkey))
    {
      format=file->value(formatkey);
    }
    input_file_list.push_back(ts::sff::readDSFF(*file, opt.verbose, 
                                                tracekey, format));
    ++file;
  }

246
  // collect traces
247
  TCollection collection_of_series;
248
  TMetaVector vector_of_metadata;
249
250
251
252
253
254
255
256
  typedef ts::sff::DFile::Tfile Tfile;
  typedef Tfile::Ttracevector Ttracevector;
  ts::sff::TDFileList::const_iterator i_file=input_file_list.begin();
  while (i_file != input_file_list.end())
  {
    Ttracevector::const_iterator i_trace=i_file->data.begin();
    while (i_trace != i_file->data.end())
    {
257
      collection_of_series.push_back(*i_trace);
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
      Meta metadata;
      metadata.filearguments=i_file->arguments;
      metadata.label=opt.labelpattern;
      metadata.label=patsubst(metadata.label, "%C", 
                              i_trace->header.wid2().channel);
      metadata.label=patsubst(metadata.label, "%S", 
                              i_trace->header.wid2().station);
      metadata.label=patsubst(metadata.label, "%A", 
                              i_trace->header.wid2().auxid);
      metadata.label=patsubst(metadata.label, "%I", 
                              i_trace->header.wid2().instype);
      metadata.label=patsubst(metadata.label, "%F", 
                              metadata.filearguments.name);
      std::ostringstream oss;
      oss << i_trace->traceindex();
      metadata.label=patsubst(metadata.label, "%NT", oss.str());
      if (metadata.filearguments.haskey(labelkey))
      {
        metadata.label=patsubst(metadata.label, "%N", 
                                metadata.filearguments.value(labelkey));
      }
      vector_of_metadata.push_back(metadata);
280
281
282
283
284
285
286
287
288
      ++i_trace;
    }
    ++i_file;
  }

  // report traces
  if (opt.verbose)
  {
    cout << "Time series to be analyzed:" << endl;
289
290
    report_collection(collection_of_series,
                      vector_of_metadata);
291
292
  }

293
294
295
  // trim time series
  if (opt.trim)
  {
296
297
    TFXX_assert(collection_of_series.overlap(),
                "time series do not overlap");
298
299
    collection_of_series.trim_to_date();
    collection_of_series.trim_to_nsamples();
300
301
302
303
304

    // report traces
    if (opt.verbose)
    {
      cout << "Time series after trimming:" << endl;
305
306
      report_collection(collection_of_series,
                        vector_of_metadata, opt.debug);
307
    }
308
309
310
311
312
313
314
315
  }

  // check consistency
  ::sff::WID2compare comparer(::sff::Fdt | ::sff::Fnsamples | ::sff::Fdate);
  comparer.setdatetolerance(opt.datetolerance);
  TFXX_assert(collection_of_series.are_consistent(comparer),
              "time series are inconsistent");

316
317
  /* ====================================================================== */

318
319
320
} // main()

/* ----- END OF croposp.cc ----- */