soutifu.cc 5.02 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*! \file soutifu.cc
 * \brief inversion for source time function
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id$
 * \author Thomas Forbriger
 * \date 06/05/2011
 * 
 * inversion for source time function
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 * 
 * REVISIONS and CHANGES 
 *  - 06/05/2011   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define SOUTIFU_VERSION \
  "SOUTIFU   V1.0   inversion for source time function"
#define SOUTIFU_CVSID \
  "$Id$"

#include <iostream>
25 26
#include <fstream>
#include <tfxx/error.h>
thomas.forbriger's avatar
thomas.forbriger committed
27
#include <tfxx/commandline.h>
28 29 30 31 32
#include <tsxx/sffheaders.h>
#include <datrwxx/readany.h>
#include <stfinv/stfinvany.h>

/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
33 34 35 36 37

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

38 39 40 41 42 43 44 45 46 47 48
/*----------------------------------------------------------------------*/

struct Options {
  bool verbose, debug, overwrite;
  bool writestf, writeconvolved;
  std::string stffilename, convolvedfilename;
  datrw::Eformat datafileformat;
}; // struct Options

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

thomas.forbriger's avatar
thomas.forbriger committed
49 50 51 52 53 54 55
int main(int iargc, char* argv[])
{

  // define usage information
  char usage_text[]=
  {
    SOUTIFU_VERSION "\n"
56 57
    "usage: soutifu [-v] [-o] [-wc f] [-ws f] [--type f] [--para p]\n"
    "               parameters data synthetics" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
58
    "   or: soutifu --help|-h" "\n"
59
    "   or: soutifu --xhelp" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
60 61 62 63 64
  };

  // define full help text
  char help_text[]=
  {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    SOUTIFU_CVSID "\n"
    "\n"
    "Calculate optimized source time function to minimize misfit\n"
    "between recorded data and synthetic waveforms.\n"
    "\n"
    "data         name of file containing recorded data\n"
    "synthetics   name of file containing synthetic data\n"
    "parameters   parameterstring to select STF engine\n"
    "             The string consists of an ID selecting one of the\n"
    "             available engines and a set of options an parameters\n"
    "             to be passed to this engine.\n"
    "\n"
    "-v           be verbose\n"
    "-o           overwrite existing output files\n"
    "--type f     use file format type \"f\" for file I/O\n"
    "-wc f        write convolved synthetics to file \"f\"\n"
    "-ws f        write source time function to file \"f\"\n"
thomas.forbriger's avatar
thomas.forbriger committed
82 83 84 85 86 87 88 89 90 91
  };

  // define commandline options
  using namespace tfxx::cmdline;
  static Declare options[]= 
  {
    // 0: print help
    {"help",arg_no,"-"},
    // 1: verbose mode
    {"v",arg_no,"-"},
92 93 94 95 96 97 98 99 100 101
    // 2: overwrite mode
    {"o",arg_no,"-"},
    // 3: file format
    {"type",arg_yes,"sff"},
    // 4: name of convolved synthetics
    {"wc",arg_yes,"-"},
    // 5: name of source time function
    {"ws",arg_yes,"-"},
    // 6: present full details
    {"xhelp",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
    {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...
116
  if (cmdline.optset(0) || cmdline.optset(6))
thomas.forbriger's avatar
thomas.forbriger committed
117 118 119
  {
    cerr << usage_text << endl;
    cerr << help_text << endl;
120 121 122 123 124 125
    datrw::supported_input_data_types(cerr);
    if (cmdline.optset(6)) { datrw::online_help(cerr); }
    cerr << endl;
    stfinv::STFEngine::help(cerr);
    cerr << endl;
    if (cmdline.optset(6)) { stfinv::help(cerr); }
thomas.forbriger's avatar
thomas.forbriger committed
126 127 128
    exit(0);
  }

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
  /*----------------------------------------------------------------------*/
  // read options and parameters
  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.overwrite=cmdline.optset(2);
  opt.datafileformat=datrw::anyID(cmdline.string_arg(3));
  opt.writeconvolved=cmdline.optset(4);
  opt.convolvedfilename=cmdline.string_arg(4);
  opt.writestf=cmdline.optset(5);
  opt.stffilename=cmdline.string_arg(5);

  TFXX_assert(cmdline.extra(), "missing parameter string");
  std::string parameters=cmdline.next();
  TFXX_assert(cmdline.extra(), "missing recorded data file name");
  std::string datafilename=cmdline.next();
  TFXX_assert(cmdline.extra(), "missing synthetics data file name");
  std::string syntheticsfilename=cmdline.next();

  /*----------------------------------------------------------------------*/
  // go
  // --
 
  // read input data
  // ---------------
  typedef aff::Series<double> Tseries;
  typedef ts::sff::File<Tseries> Tfile;
  Tfile recordeddata;
  Tfile syntheticdata;

  std::ios_base::openmode iopenmode
    =datrw::ianystream::openmode(opt.datafileformat);
  if (opt.verbose) { cout << "open input file " << datafilename << endl; }
thomas.forbriger's avatar
thomas.forbriger committed
161
  {
162 163 164
    std::ifstream ifs(datafilename.c_str(), iopenmode);
    datrw::ianystream is(ifs, opt.datafileformat);
    recordeddata.read(is.idatstream(), opt.verbose);
thomas.forbriger's avatar
thomas.forbriger committed
165 166
  }

167 168 169 170 171 172
  if (opt.verbose) { cout << "open input file " << syntheticsfilename << endl; }
  {
    std::ifstream ifs(syntheticsfilename.c_str(), iopenmode);
    datrw::ianystream is(ifs, opt.datafileformat);
    syntheticdata.read(is.idatstream(), opt.verbose);
  }
thomas.forbriger's avatar
thomas.forbriger committed
173 174 175
}

/* ----- END OF soutifu.cc ----- */