/*! \file noisymize.cc * \brief program reads a set of SFF traces, convolves them with white noise and stacks * * ---------------------------------------------------------------------------- * * $Id: noisymize.cc,v 1.3 2006-06-29 13:49:52 tforb Exp $ * \author Thomas Forbriger * \date 27/06/2006 * * program reads a set of SFF traces, convolves them with white noise and stacks * * Copyright (c) 2006 by Thomas Forbriger (BFO Schiltach) * * REVISIONS and CHANGES * - 27/06/2006 V1.0 Thomas Forbriger * * ============================================================================ */ #define NOISYMIZE_VERSION \ "NOISYMIZE V1.0 program reads a set of SFF traces, convolves them with white noise and stacks" #define NOISYMIZE_CVSID \ "$Id: noisymize.cc,v 1.3 2006-06-29 13:49:52 tforb Exp $" #include #include #include #include #include #include #include #include #include #include #include #include using std::cout; using std::cerr; using std::endl; // put all commandline settings into a struct struct Options { bool verbose, setnoiselength, overwrite; int noiselength; std::string inputformat; }; // struct Options // put all filenames in one struct struct Filenames { std::string Zin; std::string Rin; std::string Zout; std::string Rout; }; // struct Filenames // values type to be used for samples typedef double Tvalue; // time series typedef aff::Series Tseries; // full featured time series file typedef ts::sff::File Tfile; int main(int iargc, char* argv[]) { // define usage information char usage_text[]= { NOISYMIZE_VERSION "\n" "usage: noisymize Zin Rin Zout Rout" "\n" "usage: noisymize [-v] [-type t] [-n n]" "\n" " or: noisymize --help|-h" "\n" }; // define full help text char help_text[]= { NOISYMIZE_CVSID "\n" "\n" "Zin vertical component input data" "\n" "Rin radial component input data" "\n" "Zout vertical component output data" "\n" "Rout radial component output data" "\n" "\n" "-v be verbose" "\n" "-type t input file type" "\n" "-n n number of noise samples to use" "\n" "-o overwrite output" "\n" }; // define commandline options using namespace tfxx::cmdline; static Declare options[]= { // 0: print help {"help",arg_no,"-"}, // 1: verbose mode {"v",arg_no,"-"}, // 2: input format {"type",arg_yes,"sff"}, // 3: number of noise samples {"n",arg_yes,"1"}, // 4: number of noise samples {"o",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); } /* // dummy operation: print option settings for (int iopt=0; iopt<2; iopt++) { cout << "option: '" << options[iopt].opt_string << "'" << endl; if (cmdline.optset(iopt)) { cout << " option was set"; } else { cout << "option was not set"; } cout << endl; cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl; cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl; cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl; cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl; cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl; cout << " argument (bool): '"; if (cmdline.bool_arg(iopt)) { cout << "true"; } else { cout << "false"; } cout << "'" << endl; } while (cmdline.extra()) { cout << cmdline.next() << endl; } // dummy operation: print rest of command line while (cmdline.extra()) { cout << cmdline.next() << endl; } */ /*----------------------------------------------------------------------*/ // read command line settings Options opt; opt.verbose=cmdline.optset(1); opt.inputformat=cmdline.string_arg(2); opt.setnoiselength=cmdline.optset(3); opt.noiselength=cmdline.int_arg(3); opt.overwrite=cmdline.optset(4); Filenames filename; TFXX_assert(cmdline.extra(), "ERROR: missing filename Zin!"); filename.Zin=cmdline.next(); TFXX_assert(cmdline.extra(), "ERROR: missing filename Rin!"); filename.Rin=cmdline.next(); TFXX_assert(cmdline.extra(), "ERROR: missing filename Zout!"); filename.Zout=cmdline.next(); TFXX_assert(cmdline.extra(), "ERROR: missing filename Rout!"); filename.Rout=cmdline.next(); /*----------------------------------------------------------------------*/ // read vertical component input data if (opt.verbose) { cout << "read input file " << filename.Zin << endl; } Tfile Zindata; { std::ifstream ifs(filename.Zin.c_str()); datread::ianystream is(ifs, datread::anyID(opt.inputformat)); Zindata.read(is.idatstream(), opt.verbose); } // read radial component input data if (opt.verbose) { cout << "read input file " << filename.Rin << endl; } Tfile Rindata; { std::ifstream ifs(filename.Rin.c_str()); datread::ianystream is(ifs, datread::anyID(opt.inputformat)); Rindata.read(is.idatstream(), opt.verbose); } /*----------------------------------------------------------------------*/ // consistency checks // ------------------ // if (opt.verbose) { cout << "consistency checks:" << endl; } // check number of traces TFXX_assert(Zindata.size()==Rindata.size(), "ERROR: inconsitent number of traces"); if (opt.verbose) { cout << "input files both have " << Zindata.size() << " traces." << endl; } // check trace headers Tfile::Tbase::const_iterator Zit=Zindata.begin(); Tfile::Tbase::const_iterator Rit=Rindata.begin(); sff::WID2compare compare(sff::Fnsamples | sff::Fdt); while((Zit!=Zindata.end()) && (Rit!=Rindata.end())) { TFXX_assert(compare(Zit->header.wid2(), Rit->header.wid2()), "ERROR: inconsitent trace headers"); if (Zit->header.hasinfo()) { sff::INFO Zinfo=Zit->header.info(); sff::INFO Rinfo=Rit->header.info(); TFXX_assert(Zinfo==Rinfo, "ERROR: inconsitent coordinates"); } ++Zit; ++Rit; } if (opt.verbose) { cout << "trace headers and coordinates are consistent" << endl; } /*----------------------------------------------------------------------*/ // go and noisymize data // --------------------- // Zit=Zindata.begin(); Rit=Rindata.begin(); Tseries Zseries(Zit->size()), Rseries(Zit->size()); if (!opt.setnoiselength) { opt.noiselength=Zit->size(); } if (opt.verbose) { cout << "convolve " << Zit->size() << " data samples (each trace) with" << endl << opt.noiselength << " samples of uniform gaussian noise" << " and stack." << endl; } while((Zit!=Zindata.end()) && (Rit!=Rindata.end())) { Tseries noise=ts::rnd::dugauss(opt.noiselength); Tseries fac=ts::rnd::dugauss(1); Zseries += fac(1)*ts::convolve(*Zit, noise); Rseries += fac(1)*ts::convolve(*Rit, noise); ++Zit; ++Rit; } /*----------------------------------------------------------------------*/ // write result if (opt.verbose) { cout << "prepare output data" << endl; } typedef ts::sff::SFFTimeSeries Tsffseries; Tsffseries Zoutdata(Zseries), Routdata(Rseries); Tsffseries::Theader Zheader(Zindata[0].header); Tsffseries::Theader Rheader(Rindata[0].header); sff::WID2 wid2line=Zheader.wid2(); wid2line.nsamples=Zoutdata.size(); wid2line.channel="Z"; Zheader.wid2(wid2line); Zoutdata.header=Zheader; wid2line=Rheader.wid2(); wid2line.nsamples=Routdata.size(); wid2line.channel="R"; Rheader.wid2(wid2line); Routdata.header=Rheader; { if (opt.verbose) { cout << "open output file " << filename.Zout << endl; } // check if output file exists and open if (!opt.overwrite) { std::ifstream file(filename.Zout.c_str(),std::ios_base::in); TFXX_assert((!file.good()),"ERROR: output file exists!"); } std::ofstream ofs(filename.Zout.c_str()); sff::SFFostream os(ofs); // prepare file FREE block sff::FREE filefree; filefree.append(NOISYMIZE_VERSION); os << Zoutdata; } { if (opt.verbose) { cout << "open output file " << filename.Rout << endl; } // check if output file exists and open if (!opt.overwrite) { std::ifstream file(filename.Rout.c_str(),std::ios_base::in); TFXX_assert((!file.good()),"ERROR: output file exists!"); } std::ofstream ofs(filename.Rout.c_str()); sff::SFFostream os(ofs); // prepare file FREE block sff::FREE filefree; filefree.append(NOISYMIZE_VERSION); os << Routdata; } } /* ----- END OF noisymize.cc ----- */