cooset.cc 8.12 KB
Newer Older
1 2 3 4 5
/*! \file cooset.cc
 * \brief set coordinates
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: cooset.cc,v 1.4 2007-07-05 16:26:35 tforb Exp $
7 8 9 10 11 12
 * \author Thomas Forbriger
 * \date 08/09/2004
 * 
 * set coordinates
 * 
 * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach) 
thomas.forbriger's avatar
thomas.forbriger committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * ----
 * 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
 * ----
29 30 31 32 33 34 35 36 37
 * 
 * REVISIONS and CHANGES 
 *  - 08/09/2004   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define COOSET_VERSION \
  "COOSET   V1.0   set coordinates"
#define COOSET_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
38
  "$Id: cooset.cc,v 1.4 2007-07-05 16:26:35 tforb Exp $"
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 217 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

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <sffxx.h>
#include <sffostream.h>
#include <aff/series.h>
#include <aff/subarray.h>
#include <libtime++.h>
#include <tfxx/commandline.h>
#include <tfxx/error.h>

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

struct Options {
  bool verbose, overwrite, dostations, debug;
  std::string stationsystem, stationfile, sourcetime, sourcecoo;
  bool sesot, sesoc;
}; // struct Options

// structure to hold station coordinates
struct Station {
  std::string id;
  double x,y,z;
}; // struct Station

// vector of stations
typedef std::vector<Station> Tvecofstations;

// container to handle samples
typedef aff::Series<double> Tseries;

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

  // define usage information
  char usage_text[]=
  {
    COOSET_VERSION "\n"
    "usage: cooset [-stc s] [-stf file] [-soc \"C x y z\"] [-sot date]" "\n"
    "              [-v] [-o] infile outfile" "\n"
    "   or: cooset --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
    "set source and station coordinates" "\n"
    "\n"
    "-stc s       station coordinates system" "\n"
    "-stf file    station coordinates file" "\n"
    "-soc C,x,y,z source coordinates" "\n"
    "-sot date    source time" "\n"
    "-v           be verbose" "\n"
    "-o           overwrite" "\n"
    COOSET_CVSID
  };

  // define commandline options
  using namespace tfxx::cmdline;
  static Declare options[]= 
  {
    // 0: print help
    {"help",arg_no,"-"},
    // 1: verbose mode
    {"v",arg_no,"-"},
    // 2: overwrite mode
    {"o",arg_no,"-"},
    // 3: station coordinates system
    {"stc",arg_yes,"S"},
    // 4: station coordinates file
    {"stf",arg_yes,"-"},
    // 5: source coordinates
    {"soc",arg_yes,"-"},
    // 6: source time
    {"sot",arg_yes,"-"},
    // 7: debug mode
    {"D",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;
    exit(0);
  }

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

  // read command line
  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.overwrite=cmdline.optset(2);
  opt.stationsystem=cmdline.string_arg(3);
  opt.stationfile=cmdline.string_arg(4);
  opt.dostations=cmdline.optset(4);
  opt.sourcecoo=cmdline.string_arg(5);
  opt.sesoc=cmdline.optset(5);
  opt.sourcetime=cmdline.string_arg(6);
  opt.sesot=cmdline.optset(6);
  opt.debug=cmdline.optset(7);

  TFXX_assert(cmdline.extra(),"ERROR: missing input file name!");
  std::string infile=cmdline.next();
  TFXX_assert(cmdline.extra(),"ERROR: missing output file name!");
  std::string outfile=cmdline.next();

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

  // read station file if needed
  Tvecofstations station;
  if (opt.dostations)
  {
    if (opt.verbose) { cout << "read station list" << endl;}
    std::ifstream is(opt.stationfile.c_str());
    Station nextstation;
    is >> nextstation.id;
    while(is.good())
    {
      is >> nextstation.x >> nextstation.y >> nextstation.z;
      station.push_back(nextstation);
      if (opt.verbose)
      {
        cout << nextstation.id << " "
             << nextstation.x << " "
             << nextstation.y << " "
             << nextstation.z << endl; 
      }
      is >> nextstation.id;
    }
  }

  /*----------------------------------------------------------------------*/
  
  // open files
  if (opt.verbose) { cout << "open input file" << endl; }
  std::ifstream is(infile.c_str());
  TFXX_assert(is.good(),"ERROR: opening input file");

  if (!opt.overwrite)
  {
    std::ifstream check(outfile.c_str());
    TFXX_assert(!check,"ERROR: output file exists");
  }
  std::ofstream ofs(outfile.c_str());
  TFXX_assert(ofs.good(),"ERROR: opening output file");
  sff::SFFostream<Tseries> os(ofs, opt.debug);

  /*----------------------------------------------------------------------*/
  
  // modify file header
  sff::FileHeader fileheader(is);
  fileheader.appendfree(COOSET_VERSION);
  fileheader.appendfree(COOSET_CVSID);
  sff::SRCE filesrce=fileheader.srce();
  if (fileheader.hassrce())
  { fileheader.appendfree("input file has SRCE line"); }
  if (opt.sesot)
  {
    fileheader.appendfree("set source time");
    libtime::TAbsoluteTime newsot(opt.sourcetime);
    filesrce.date=newsot;
    fileheader.setsrce(filesrce);
  }
  if (opt.sesoc)
  {
    fileheader.appendfree("set source coordinates");
    std::istringstream socospec(opt.sourcecoo);
    char syst;
    double x,y,z;
    socospec >> syst >> x >> y >> z;
    std::ostringstream message;
    message << "new coordinates: " << syst << ","
      << x << "," << y << "," << z;
    filesrce.cs=sff::coosysID(syst);
    filesrce.cx=x;
    filesrce.cy=y;
    filesrce.cz=z;
    fileheader.appendfree(message.str());
    fileheader.setsrce(filesrce);
  }
  if (opt.verbose) 
  { 
    cout << endl << "output file FREE block:" << endl;
    cout << fileheader.free(); 
  }
  os << fileheader;

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

  // handle traces
  bool last=false;
  while (!last)
  {
    if (opt.verbose) { cout << endl << "read next trace:" << endl; }
    sff::InputWaveform<Tseries> inputwaveform(is,opt.debug);
    sff::TraceHeader traceheader(inputwaveform.header());
    last=traceheader.last();
 
    sff::INFO newinfo(traceheader.info());
    const sff::WID2& wid2(traceheader.wid2());
    if (opt.verbose) { cout << "station: " << wid2.station << endl; }
    bool infoset=false;
    if (opt.dostations)
    {
      Station thestation;
      for (unsigned int i=0; i<station.size(); ++i)
      {
        thestation=station[i];
        if (thestation.id == wid2.station)
        { 
          infoset=true; 
          char syst=opt.stationsystem[0];
          std::ostringstream message;
          message << "new coordinates: " << syst << ","
            << thestation.x << "," << thestation.y << "," << thestation.z;
          newinfo.cs=sff::coosysID(syst);
          newinfo.cx=thestation.x;
          newinfo.cy=thestation.y;
          newinfo.cz=thestation.z;
          traceheader.appendfree(message.str());
          i=station.size();
        }
      }
      if (infoset) 
      {
        if (opt.verbose)
        {
          cout << traceheader.free() << endl;
        }
      }
      else
      { 
        cerr << "could not find station " << wid2.station << endl; 
        traceheader.appendfree("could not find station!");
      }
    }
291 292
    // NOTICE: samples must be written first!
    os << inputwaveform.series();
293 294 295 296 297 298
    os << traceheader;
    if (infoset) { os << newinfo; }
  }
}

/* ----- END OF cooset.cc ----- */