cooset.cc 13.6 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 33 34 35
 * 
 * ============================================================================
 */
#define COOSET_VERSION \
36
  "COOSET   V1.1   set coordinates"
37 38 39 40 41

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

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

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

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

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

  // define full help text
  char help_text[]=
  {
    "set source and station coordinates" "\n"
    "\n"
111 112
    "-xhelp       print I/O format details\n"
    "\n"
113 114 115 116 117 118
    "-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"
119 120 121 122
    "-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"
123 124 125 126 127 128 129 130 131 132
    "\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"
133 134
    "\n"
    "Use program sehefixx to modify other header fields.\n"
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  };

  // 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,"-"},
157 158 159 160 161 162 163 164 165 166
    // 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,"-"},
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    {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;
185 186 187 188 189 190 191 192 193 194
    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);
    }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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);
212 213 214 215
  opt.itype=cmdline.string_arg(8);
  opt.otype=cmdline.string_arg(9);
  opt.integer=cmdline.optset(11);
  opt.single=cmdline.optset(12);
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

  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
250 251 252 253 254 255 256 257
  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);
258

259 260 261
  if (!opt.overwrite) { datrw::abort_if_exists(outfile); }

  if (opt.verbose)
262
  {
263 264
    cout << "open output file " << outfile 
      << " with format " << opt.otype << endl;
265 266 267
  }
  std::ofstream ofs(outfile.c_str());
  TFXX_assert(ofs.good(),"ERROR: opening output file");
268
  datrw::oanystream os(ofs, opt.otype, opt.debug);
269 270

  /*----------------------------------------------------------------------*/
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  // 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
289
  filefree.append(COOSET_VERSION);
290 291 292 293

  // read and modify SRCE line
  sff::SRCE srceline;
  if (is.hassrce())
294
  {
295 296
    is >> srceline;
    // modify file header
297
    filefree.append("input file has SRCE line"); 
298 299
    if (opt.sesot)
    {
300
      filefree.append("set source time");
301 302 303 304 305
      libtime::TAbsoluteTime newsot(opt.sourcetime);
      srceline.date=newsot;
    }
    if (opt.sesoc)
    {
306
      filefree.append("set source coordinates");
307 308 309 310 311 312 313 314 315 316 317
      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;
318
      filefree.append(message.str());
319 320 321 322 323
    }
  } // if (is.hassrce())

  // write file FREE block
  if (os.handlesfilefree())
324
  {
325
    os << filefree;
326
  }
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

  // write SRCE line
  if (is.hassrce())
  {
    if (os.handlessrce())
    {
      os << srceline;
    }
    else
    {
      if (opt.verbose)
      {
        cout << "  Output format cannot handle SRCE line." << endl;
        cout << "  SRCE line is discarded." << endl;
      }
    }
  } // if (is.hassrce())
  
  // report
346 347 348
  if (opt.verbose) 
  { 
    cout << endl << "output file FREE block:" << endl;
349
    cout << filefree; 
350 351 352 353 354
  }

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

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

    ///////////////////
      
    // write trace FREE block
    if (is.hasfree())
    {
      if (os.handlestracefree())
      {
        os << freeblock;
      }
494
      else
495 496 497 498 499 500
      {
        if (opt.verbose)
        {
          cout << "  Output format cannot handle trace FREE block." << endl;
          cout << "  Trace FREE block is discarded." << endl;
        }
501
      }
502 503 504 505 506 507 508 509 510 511 512 513 514 515
    } // if (is.hasfree())

    // write time series
    if (opt.integer) 
    {
      os << iseries;
    }
    else if (opt.single)
    {
      os << fseries;
    }
    else
    {
      os << dseries;
516 517 518 519 520
    }
  }
}

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