cooset.cc 14.1 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
 * 
 * ============================================================================
 */
#define COOSET_VERSION \
37
  "COOSET   V1.2   set coordinates"
38 39 40 41 42

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
43 44
#include <datrwxx/readany.h>
#include <datrwxx/writeany.h>
45 46 47 48 49
#include <aff/series.h>
#include <aff/subarray.h>
#include <libtime++.h>
#include <tfxx/commandline.h>
#include <tfxx/error.h>
50
#include <tfxx/stringfunc.h>
51 52 53 54 55 56

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

struct Options {
57
  bool verbose, overwrite, dostations, debug, single, integer;
58
  std::string stationsystem, stationfile, sourcetime, sourcecoo;
59
  bool sesot, sesoc, createsrce;
60
  std::string itype, otype;
61 62 63 64 65 66 67 68 69 70 71 72
}; // 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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;
  }
88
  return(os);
89 90 91
} // std::ostream& operator<<(std::ostream& os, const sff::FREE& free)

/* ====================================================================== */
92 93 94 95 96 97 98 99 100

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"
101
    "              [-itype f] [-otype f] [-integer] [-float] [-screate]\n"
102 103
    "              [-v] [-o] infile outfile" "\n"
    "   or: cooset --help|-h" "\n"
104
    "   or: cooset --xhelp" "\n"
105 106 107 108 109 110 111
  };

  // define full help text
  char help_text[]=
  {
    "set source and station coordinates" "\n"
    "\n"
112 113
    "-xhelp       print I/O format details\n"
    "\n"
114 115 116 117
    "-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"
118 119 120
    "-screate     create source header, if not yet present\n"
    "             without this option, the program will only modify\n"
    "             existing source header data\n"
121 122
    "-v           be verbose" "\n"
    "-o           overwrite" "\n"
123 124 125 126
    "-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"
127 128 129 130 131 132 133 134 135 136
    "\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"
137 138
    "\n"
    "Use program sehefixx to modify other header fields.\n"
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  };

  // 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,"-"},
161 162 163 164 165 166 167 168 169 170
    // 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,"-"},
171 172
    // 13: create source header if not yet present
    {"screate",arg_no,"-"},
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    {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;
191 192 193 194 195 196 197 198 199 200
    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);
    }
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    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);
218 219 220 221
  opt.itype=cmdline.string_arg(8);
  opt.otype=cmdline.string_arg(9);
  opt.integer=cmdline.optset(11);
  opt.single=cmdline.optset(12);
222
  opt.createsrce=cmdline.optset(13);
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

  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
257 258 259 260 261 262 263 264
  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);
265

266 267 268
  if (!opt.overwrite) { datrw::abort_if_exists(outfile); }

  if (opt.verbose)
269
  {
270 271
    cout << "open output file " << outfile 
      << " with format " << opt.otype << endl;
272 273 274
  }
  std::ofstream ofs(outfile.c_str());
  TFXX_assert(ofs.good(),"ERROR: opening output file");
275
  datrw::oanystream os(ofs, opt.otype, opt.debug);
276 277

  /*----------------------------------------------------------------------*/
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  // 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
296
  filefree.append(COOSET_VERSION);
297 298 299 300

  // read and modify SRCE line
  sff::SRCE srceline;
  if (is.hassrce())
301
  {
302 303
    is >> srceline;
    // modify file header
304
    filefree.append("input file has SRCE line"); 
305 306 307
  } // if (is.hassrce())
  if (opt.createsrce || is.hassrce())
  {
308 309
    if (opt.sesot)
    {
310
      filefree.append("set source time");
311 312 313 314 315
      libtime::TAbsoluteTime newsot(opt.sourcetime);
      srceline.date=newsot;
    }
    if (opt.sesoc)
    {
316
      filefree.append("set source coordinates");
317 318 319 320 321 322 323 324 325 326 327
      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;
      srceline.cs=sff::coosysID(syst);
      srceline.cx=x;
      srceline.cy=y;
      srceline.cz=z;
328
      filefree.append(message.str());
329
    }
330
  } // if (opt.createsrce || is.hassrce())
331 332 333

  // write file FREE block
  if (os.handlesfilefree())
334
  {
335
    os << filefree;
336
  }
337 338

  // write SRCE line
339
  if (is.hassrce() || opt.createsrce)
340 341 342 343 344 345 346 347 348 349 350 351 352
  {
    if (os.handlessrce())
    {
      os << srceline;
    }
    else
    {
      if (opt.verbose)
      {
        cout << "  Output format cannot handle SRCE line." << endl;
        cout << "  SRCE line is discarded." << endl;
      }
    }
353
  } // if (is.hassrce() || opt.createsrce)
354 355
  
  // report
356 357 358
  if (opt.verbose) 
  { 
    cout << endl << "output file FREE block:" << endl;
359
    cout << filefree; 
360 361 362 363 364
  }

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

  // handle traces
365 366
  int itrace=0;
  while (is.good())
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 431 432 433 434 435 436 437 438 439
    ++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
    if (is.hasinfo())
440
    {
441
      if (os.handlesinfo())
442
      {
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        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;
467
              freeblock.append(message.str());
468 469 470 471 472 473 474
              i=station.size();
            }
          }
          if (infoset) 
          {
            if (opt.verbose)
            {
475
              cout << freeblock << endl;
476 477 478 479 480
            }
          }
          else
          { 
            cerr << "could not find station " << wid2.station << endl; 
481
            freeblock.append("could not find station!");
482 483 484
          }
        } // if (opt.dostations)
        os << infoline;
485
      }
486
      else
487 488 489
      {
        if (opt.verbose)
        {
490
          cout << "  INFO line is discarded." << endl;
491 492
        }
      }
493 494 495 496 497 498 499 500 501 502 503
    }

    ///////////////////
      
    // write trace FREE block
    if (is.hasfree())
    {
      if (os.handlestracefree())
      {
        os << freeblock;
      }
504
      else
505 506 507 508 509 510
      {
        if (opt.verbose)
        {
          cout << "  Output format cannot handle trace FREE block." << endl;
          cout << "  Trace FREE block is discarded." << endl;
        }
511
      }
512 513 514 515 516 517 518 519 520 521 522 523 524 525
    } // if (is.hasfree())

    // write time series
    if (opt.integer) 
    {
      os << iseries;
    }
    else if (opt.single)
    {
      os << fseries;
    }
    else
    {
      os << dseries;
526 527 528 529 530
    }
  }
}

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