cooset.cc 14.9 KB
Newer Older
1 2 3 4 5 6
/*! \file cooset.cc
 * \brief set coordinates
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
7
 * \date 14/11/2016
8 9 10
 * 
 * set coordinates
 * 
11
 * Copyright (c) 2004, 2016 by Thomas Forbriger (BFO Schiltach) 
thomas.forbriger's avatar
thomas.forbriger committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * ----
 * 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
 * ----
28 29 30
 * 
 * REVISIONS and CHANGES 
 *  - 08/09/2004   V1.0   Thomas Forbriger
31
 *  - 14/11/2016   V1.1   support all libdatrwxx I/O formats
32
 *  - 01/12/2016   V1.2   create SRCE header, if not yet present
33 34 35 36
 *  - 18/08/2017   V1.3   new features:
 *                        - automatically convert comma to whitespace in
 *                          coordinate parameters
 *                        - support creation of info data
37 38 39 40
 * 
 * ============================================================================
 */
#define COOSET_VERSION \
41
  "COOSET   V1.3   set coordinates"
42 43 44 45 46

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
47
#include <algorithm>
48 49
#include <datrwxx/readany.h>
#include <datrwxx/writeany.h>
50 51 52 53 54
#include <aff/series.h>
#include <aff/subarray.h>
#include <libtime++.h>
#include <tfxx/commandline.h>
#include <tfxx/error.h>
55
#include <tfxx/stringfunc.h>
56
#include <sffxx.h>
57 58 59 60 61 62

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

struct Options {
63
  bool verbose, overwrite, dostations, debug, single, integer;
64
  std::string stationsystem, stationfile, sourcetime, sourcecoo;
65
  bool sesot, sesoc, createsrce, createinfo;
66
  std::string itype, otype;
67 68 69 70 71 72 73 74 75 76 77 78
}; // 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
typedef aff::Series<double> Tdseries;
typedef aff::Series<int>    Tiseries;
typedef aff::Series<float>  Tfseries;

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

std::ostream& operator<<(std::ostream& os, const sff::FREE& free)
{
  os << "contents of SFF FREE block:" << std::endl;
  sff::FREE::Tlines::const_iterator I=free.lines.begin();
  while (I != free.lines.end())
  {
    os << "  " << *I << std::endl;;
    ++I;
  }
94
  return(os);
95 96 97
} // std::ostream& operator<<(std::ostream& os, const sff::FREE& free)

/* ====================================================================== */
98 99 100 101 102 103 104 105 106

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"
107
    "              [-itype f] [-otype f] [-integer] [-float] [-screate]\n"
108 109
    "              [-v] [-o] infile outfile" "\n"
    "   or: cooset --help|-h" "\n"
110
    "   or: cooset --xhelp" "\n"
111 112 113 114 115 116 117
  };

  // define full help text
  char help_text[]=
  {
    "set source and station coordinates" "\n"
    "\n"
118 119
    "-xhelp       print I/O format details\n"
    "\n"
120 121 122 123
    "-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"
124 125 126
    "-screate     create source header, if not yet present\n"
    "             without this option, the program will only modify\n"
    "             existing source header data\n"
127 128 129
    "-icreate     create info trace header, if not yet present\n"
    "             without this option, the program will only modify\n"
    "             existing info trace header data\n"
130 131
    "-v           be verbose" "\n"
    "-o           overwrite" "\n"
132 133 134 135
    "-itype f     input file data format type\'f\'\n"
    "-otype f     output file data format type\'f\'\n"
    "-integer     use integer sample values rather than double\n"
    "-float       use single precision sample values rather than double\n"
136 137 138 139 140 141 142 143 144 145
    "\n"
    "station coordinate file:\n"
    "  Each line provides coordinates for one station. The coordinate\n"
    "  system to be used for all stations is selected by the argument\n"
    "  to option \"-stc\" on the command line. Each line has four columns:\n"
    "  1st column: name of station (will be matched against station name\n"
    "              found in trace header)\n"
    "  2nd column: value for x-coordinate or latitude field\n"
    "  3rd column: value for y-coordinate or longitude field\n"
    "  4th column: value for z-coordinate or height field\n"
146 147
    "\n"
    "Use program sehefixx to modify other header fields.\n"
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  };

  // 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,"-"},
170 171 172 173 174 175 176 177 178 179
    // 8: input file type
    {"itype",arg_yes,"sff"},
    // 9: output file type
    {"otype",arg_yes,"sff"},
    // 10: extra help
    {"xhelp",arg_no,"-"},
    // 11: integer values
    {"integer",arg_no,"-"},
    // 12: single precision values
    {"float",arg_no,"-"},
180 181
    // 13: create source header if not yet present
    {"screate",arg_no,"-"},
182 183
    // 14: create info trace header if not yet present
    {"icreate",arg_no,"-"},
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    {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;
202 203 204 205 206 207 208 209 210 211
    cerr << endl;
    datrw::supported_data_types(cerr);
    cerr << endl;
    cerr << libtime::usage_time_format_string;
    cerr << endl;
    if (cmdline.optset(10))
    {
      cerr << endl;
      datrw::online_help(cerr);
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    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);
229 230 231 232
  opt.itype=cmdline.string_arg(8);
  opt.otype=cmdline.string_arg(9);
  opt.integer=cmdline.optset(11);
  opt.single=cmdline.optset(12);
233
  opt.createsrce=cmdline.optset(13);
234
  opt.createinfo=cmdline.optset(14);
235 236 237 238 239 240

  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();

241 242
  std::replace(opt.sourcecoo.begin(), opt.sourcecoo.end(), ',', ' ');

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
  /*----------------------------------------------------------------------*/

  // 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
271 272 273 274 275 276 277 278
  if (opt.verbose) 
  { 
    cout << "open input file " << infile
      << " of format " << opt.itype << endl; 
  }
  std::ifstream ifs(infile.c_str());
  TFXX_assert(ifs.good(),"ERROR: opening input file");
  datrw::ianystream is(ifs, opt.itype, opt.debug);
279

280 281 282
  if (!opt.overwrite) { datrw::abort_if_exists(outfile); }

  if (opt.verbose)
283
  {
284 285
    cout << "open output file " << outfile 
      << " with format " << opt.otype << endl;
286 287 288
  }
  std::ofstream ofs(outfile.c_str());
  TFXX_assert(ofs.good(),"ERROR: opening output file");
289
  datrw::oanystream os(ofs, opt.otype, opt.debug);
290 291

  /*----------------------------------------------------------------------*/
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  // pass file header data

  sff::FREE filefree;
  // read file FREE block
  if (is.hasfree())
  {
    is >> filefree;
    if (!os.handlesfilefree())
    {
      if (opt.verbose)
      {
        cout << "  Output format cannot handle FREE block." << endl;
        cout << "  File FREE block will be discarded." << endl;
      }
    }
  } // if (is.hasfree())

  // leave a note
310
  filefree.append(COOSET_VERSION);
311 312 313 314

  // read and modify SRCE line
  sff::SRCE srceline;
  if (is.hassrce())
315
  {
316 317
    is >> srceline;
    // modify file header
318
    filefree.append("input file has SRCE line"); 
319 320 321
  } // if (is.hassrce())
  if (opt.createsrce || is.hassrce())
  {
322 323
    if (opt.sesot)
    {
324
      filefree.append("set source time");
325 326 327 328 329
      libtime::TAbsoluteTime newsot(opt.sourcetime);
      srceline.date=newsot;
    }
    if (opt.sesoc)
    {
330
      filefree.append("set source coordinates");
331
      std::istringstream socospec(opt.sourcecoo);
332
      cout << opt.sourcecoo << endl;
333 334 335 336
      char syst;
      double x,y,z;
      socospec >> syst >> x >> y >> z;
      std::ostringstream message;
337
      message << "new source coordinates: " << syst << ","
338 339 340 341 342
        << x << "," << y << "," << z;
      srceline.cs=sff::coosysID(syst);
      srceline.cx=x;
      srceline.cy=y;
      srceline.cz=z;
343
      filefree.append(message.str());
344
    }
345
    if (opt.verbose) { sff::verbose(cout, srceline); }
346
  } // if (opt.createsrce || is.hassrce())
347 348 349

  // write file FREE block
  if (os.handlesfilefree())
350
  {
351
    os << filefree;
352
  }
353 354

  // write SRCE line
355
  if (is.hassrce() || opt.createsrce)
356 357 358 359 360 361 362 363 364 365 366 367 368
  {
    if (os.handlessrce())
    {
      os << srceline;
    }
    else
    {
      if (opt.verbose)
      {
        cout << "  Output format cannot handle SRCE line." << endl;
        cout << "  SRCE line is discarded." << endl;
      }
    }
369
  } // if (is.hassrce() || opt.createsrce)
370 371
  
  // report
372 373 374
  if (opt.verbose) 
  { 
    cout << endl << "output file FREE block:" << endl;
375
    cout << filefree; 
376 377 378 379 380
  }

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

  // handle traces
381 382
  int itrace=0;
  while (is.good())
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    ++itrace;
    if (opt.verbose) { std::cout << "  edit trace #" << itrace << std::endl; }

    // check output data format
    switch (os.seriestype()) {
      case datrw::Fint:
        if (!opt.integer)
        {
          cout << "  WARNING: converting floating point data to integer!" 
            << endl;
        }
        break;
      case datrw::Ffloat:
        if (!(opt.single || opt.integer))
        {
          cout << "  WARNING: "
            "converting double precision to single precision data!" 
            << endl;
        }
        break;
      case datrw::Fdouble:
        // that's just fine
        break;
      case datrw::Fall:
        // that's just fine
        break;
      default:
        TFXX_abort("output stream uses unknown variable type!");
    } // switch (os.seriestype())

    datrw::Tiseries iseries;
    datrw::Tfseries fseries;
    datrw::Tdseries dseries;

    // read time series
    if (opt.integer) 
    {
      TFXX_assert(is.providesi(),
                  "ERROR: input data is not provided as integer values");
      is >> iseries;
    }
    else if (opt.single)
    {
      TFXX_assert(is.providesf(),
                  "ERROR: input data is not provided as "
                  "single precision floats");
      is >> fseries;
    }
    else
    {
      TFXX_assert(is.providesd(),
                  "ERROR: input data is not provided as "
                  "double precision floats");
      is >> dseries;
    }
      
    // pass WID2
    sff::WID2 wid2;
    is >> wid2;
    os << wid2;

    // read trace FRRE
    sff::FREE freeblock;
    if (is.hasfree())
    {
      is >> freeblock;
      freeblock.append("Edited with: ");
      freeblock.append(COOSET_VERSION);
    } // if (is.hasfree())
      
    // modify INFO
455
    if (opt.createinfo || is.hasinfo())
456
    {
457
      if (os.handlesinfo())
458
      {
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        sff::INFO infoline;
        is >> infoline;
        bool infoset=false;
        if (opt.dostations)
        {
          if (opt.verbose) { cout << "station: " << wid2.station << endl; }
          Station thestation;
          for (unsigned int i=0; i<station.size(); ++i)
          {
            thestation=station[i];
            if (opt.debug) { cout << "'" << thestation.id << "' '"
              << wid2.station << "'" << endl; }
            if (tfxx::string::trimws_end(thestation.id)
                == tfxx::string::trimws_end(wid2.station))
            { 
              infoset=true; 
              char syst=opt.stationsystem[0];
              std::ostringstream message;
              message << "new coordinates: " << syst << ","
                << thestation.x << "," << thestation.y << "," << thestation.z;
              infoline.cs=sff::coosysID(syst);
              infoline.cx=thestation.x;
              infoline.cy=thestation.y;
              infoline.cz=thestation.z;
483
              freeblock.append(message.str());
484 485 486 487 488 489 490
              i=station.size();
            }
          }
          if (infoset) 
          {
            if (opt.verbose)
            {
491
              cout << freeblock << endl;
492 493 494 495 496
            }
          }
          else
          { 
            cerr << "could not find station " << wid2.station << endl; 
497
            freeblock.append("could not find station!");
498 499 500
          }
        } // if (opt.dostations)
        os << infoline;
501
      }
502
      else
503 504 505
      {
        if (opt.verbose)
        {
506
          cout << "  INFO line is discarded." << endl;
507 508
        }
      }
509 510 511 512 513 514 515 516 517 518 519
    }

    ///////////////////
      
    // write trace FREE block
    if (is.hasfree())
    {
      if (os.handlestracefree())
      {
        os << freeblock;
      }
520
      else
521 522 523 524 525 526
      {
        if (opt.verbose)
        {
          cout << "  Output format cannot handle trace FREE block." << endl;
          cout << "  Trace FREE block is discarded." << endl;
        }
527
      }
528 529 530 531 532 533 534 535 536 537 538 539 540 541
    } // if (is.hasfree())

    // write time series
    if (opt.integer) 
    {
      os << iseries;
    }
    else if (opt.single)
    {
      os << fseries;
    }
    else
    {
      os << dseries;
542 543 544 545 546
    }
  }
}

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