45 #define TIDOFI_VERSION \ 46 "TIDOFI V1.6 time domain filter" 51 #include <tfxx/commandline.h> 52 #include <tfxx/xcmdline.h> 53 #include <tfxx/error.h> 54 #include <tfxx/stringfunc.h> 55 #include <tfxx/rangestring.h> 56 #include <tfxx/rangelist.h> 57 #include <tfxx/misc.h> 58 #include <tfxx/seitosh.h> 59 #include <tsxx/anyfilter.h> 60 #include <datrwxx/readany.h> 61 #include <datrwxx/writeany.h> 63 #include <sffostream.h> 65 #include <tsxx/ovtaper.h> 87 int main(
int iargc,
char* argv[])
94 "usage: tidofi [-v] [-o] [-cf file] [-cs] [-type type] [-Type type]" "\n" 95 " outfile infile [t:T|a:T] [f:F] [prft:file] [poft:file]\n" 96 " [infile [t:T|a:T] [f:F] [prft:file] [poft:file] ... ]" "\n" 97 " or: tidofi --help|-h" "\n" 98 " or: tidofi --xhelp" "\n" 104 "outfile name of file to contain results" "\n" 105 "infile input file" "\n" 106 " t:T select traces T, where T may be any range" "\n" 107 " specification like \'3-4\' or \'5,6,7-12,20\'" "\n" 108 " f:F specifies an input file format differing from" "\n" 109 " the format selected by \"-type\"" "\n" 110 " a:T apply filter to traces T, where T may be any range" "\n" 111 " specification like \'3-4\' or \'5,6,7-12,20\'" "\n" 112 " all other traces are passed to the output unchanged\n" 114 " pre-filter taper from file to be applied before\n" 115 " filtering traces\n" 117 " post-filter taper from file to be applied before\n" 118 " filtering traces\n" 119 " file mofifiers t and a are mutually exclusive, where a\n" 120 " has higher precendence\n" 121 " prft and poft taper parameters are read from \"file\"\n" 122 " taper parameter file is expected in format produced\n" 125 "-xhelp print detailed information regarding file formats" "\n" 127 "-DEBUG produce debug output" "\n" 128 "-DUMP produce debug dumps" "\n" 129 "-o overwrite output" "\n" 130 "-cf file read filter commands from \"file\"" "\n" 131 "-cs read filter commands from stdin" "\n" 132 "-type type choose input file format (default: sff)" "\n" 133 "-Type type choose output file format (default: sff)" "\n" 135 "In command files comment lines may either commence with \"rem\" like" "\n" 136 "in seife command files or with \'#\'. The seife command \"end\" is" "\n" 137 "simply ignored (like a comment line)." 141 using namespace tfxx::cmdline;
142 static Declare options[]=
155 {
"type",arg_yes,
"sff"},
157 {
"DEBUG",arg_no,
"-"},
161 {
"Type",arg_yes,
"sff"},
163 {
"xhelp",arg_no,
"-"},
168 static const char applykey[]=
"a";
170 static const char prefiltertaperkey[]=
"prft";
171 static const char postfiltertaperkey[]=
"poft";
180 cerr << usage_text << endl;
181 cerr << tfxx::seitosh::repository_reference << endl;
186 Commandline cmdline(iargc, argv, options);
189 if (cmdline.optset(0))
191 cerr << usage_text << endl;
192 cerr << help_text << endl;
193 datrw::supported_data_types(cerr);
195 ts::filter::print_any_help(cerr);
196 cerr << endl << tfxx::seitosh::repository_reference << endl;
201 if (cmdline.optset(9))
203 cerr << usage_text << endl;
205 datrw::online_help(cerr);
206 cerr << endl << tfxx::seitosh::repository_reference << endl;
218 opt.
debug=cmdline.optset(6);
227 TFXX_assert(cmdline.extra(),
"missing output file");
228 std::string outfile=cmdline.next();
229 TFXX_assert(cmdline.extra(),
"missing input file");
230 tfxx::cmdline::Tparsed arguments=parse_cmdline(cmdline,
cmdlinekeys);
231 if ((arguments.size()>1) && opt.
verbose)
233 cout <<
"NOTICE: file specific information (SRCE line and file FREE)" <<
235 <<
" will be taken from first file only!" << endl;
241 sff::FREE filtercommands;
246 cout <<
"read filter commands from file \"" 254 line=tfxx::string::trimws(line);
257 filtercommands.append(line);
259 { cout <<
">> " << line << endl; }
268 cout <<
"read filter commands from stdin (terminate with ctrl-D)" 271 while (std::cin.good())
274 getline(std::cin, line);
275 line=tfxx::string::trimws(line);
278 filtercommands.append(line);
280 { cout <<
">> " << line << endl; }
289 ts::filter::FilterCollection filter;
291 sff::FREE::Tlines::const_iterator line=filtercommands.lines.begin();
292 while (line != filtercommands.lines.end())
296 TFXX_debug(opt.
debug,
"main",
297 "skip " + *line +
" if irrelevant" );
298 skip |= (line->substr(0,1)==
"#");
299 skip |= (line->substr(0,3)==
"rem");
300 skip |= (line->substr(0,3)==
"end");
302 std::string command=(*line);
304 std::string::size_type i=command.find(
",",i);
305 while (i!=std::string::npos)
307 command.replace(i,1,
" ");
308 i=command.find(
",",i+1);
310 commands.append(command);
311 filter.push_back(ts::filter::make_any_filter(command, opt.
debug));
315 TFXX_debug(opt.
debug,
"main",
316 "skip " + *line +
"!" );
331 line=
"commands are read from";
337 line +=
" (first) and";
344 filefree.append(line);
349 filefree.append(line);
351 if (filtercommands.lines.size() > 0)
353 filefree.append(filtercommands);
357 filefree.append(
" command list is empty!");
362 filefree.append(
"no filtercommands were read");
364 filefree.append(
"output file name:");
365 filefree.append(
" " + outfile);
366 filefree.append(
"input file selection:");
367 tfxx::cmdline::Tparsed::const_iterator file=arguments.begin();
368 while (file != arguments.end())
370 filefree.append(
" " + file->name);
372 tfxx::cmdline::Toptionmap::const_iterator option=file->options.begin();
373 while (option != file->options.end())
375 line +=
" " + option->first +
":" + option->second;
378 if (line.size()>2) { filefree.append(line); }
381 if (arguments.size()>1)
383 filefree.append(
"In cases where more than one input file is read,");
384 filefree.append(
"the SRCE line is taken from the first file only (if");
385 filefree.append(
"present there).");
394 if (opt.
verbose) { cout <<
"open output file " << outfile << endl; }
398 std::ifstream file(outfile.c_str(),std::ios_base::in);
399 TFXX_assert((!file.good()),
"ERROR: output file exists!");
401 std::ios_base::openmode oopenmode
403 std::ofstream ofs(outfile.c_str(), oopenmode);
410 tfxx::cmdline::Tparsed::const_iterator infile=arguments.begin();
411 while (infile != arguments.end())
414 if (opt.
verbose) { cout <<
"open input file " << infile->name << endl; }
420 std::ios_base::openmode iopenmode
421 =datrw::ianystream::openmode(inputformat);
422 std::ifstream ifs(infile->name.c_str(), iopenmode);
423 datrw::ianystream is(ifs, inputformat);
427 bool applyprefiltertaper=infile->haskey(prefiltertaperkey);
428 bool applypostfiltertaper=infile->haskey(postfiltertaperkey);
431 ts::tapers::OffsetVariableTaper prefiltertaper;
432 ts::tapers::OffsetVariableTaper postfiltertaper;
435 if (applyprefiltertaper)
437 std::string taperfilename
438 =infile->value(prefiltertaperkey);
439 prefiltertaper.read(taperfilename);
441 if (applypostfiltertaper)
443 std::string taperfilename
444 =infile->value(postfiltertaperkey);
445 postfiltertaper.read(taperfilename);
450 sff::SRCE insrceline;
451 bool srceavailable=
false;
456 sff::FREE infilefree;
458 filefree.append(
"block read from first input file:");
459 filefree.append(infilefree);
461 if (os.handlesfilefree()) { os << filefree; }
466 if (os.handlessrce()) { os << insrceline; }
473 typedef tfxx::RangeList<int> Trangelist;
474 bool doselect=infile->haskey(
tracekey);
475 bool doapply=infile->haskey(applykey);
476 Trangelist traceranges=
477 tfxx::string::rangelist<Trangelist::Tvalue>(infile->value(
tracekey));
483 std::cout <<
" trace \"apply\" modifier has precedence " 484 <<
"over trace \"selection\" modifier." 489 =tfxx::string::rangelist<Trangelist::Tvalue>(infile->value(applykey));
495 if ((!doselect) || traceranges.contains(itrace))
497 TFXX_debug(opt.
debug,
"main",
"process trace #" << itrace );
499 { std::cout <<
" process trace #" << itrace <<
":"; }
510 TFXX_debug(opt.
debug,
"main",
511 " series and WID2 are read");
519 if ((!doapply) || traceranges.contains(itrace))
521 double offset, t0, T;
522 if (applyprefiltertaper || applypostfiltertaper)
524 TFXX_assert(is.hasinfo() && srceavailable,
525 "Offset variable taper requires source and receiver " 527 offset=::sff::offset(insrceline, info);
528 t0=libtime::time2double(insrceline.date-wid2.date);
529 T=wid2.dt*wid2.nsamples;
531 if (applyprefiltertaper)
534 ts::tapers::FourPoint taper
535 =prefiltertaper.taper(offset, t0, T);
539 TFXX_debug(opt.
debug,
"main",
540 TFXX_value(filterseries.header.dt)
542 TFXX_value(filterseries.f())
544 TFXX_value(filterseries.l())
546 TFXX_value(filterseries.size())
548 filterseries=filter(filterseries, opt.
debug);
549 wid2.dt=filterseries.header.dt;
551 wid2.nsamples=series.size();
552 TFXX_debug(opt.
debug,
"main",
553 TFXX_value(filterseries.header.dt)
555 TFXX_value(filterseries.f())
557 TFXX_value(filterseries.l())
559 TFXX_value(filterseries.size())
561 if (applypostfiltertaper)
564 ts::tapers::FourPoint taper
565 =postfiltertaper.taper(offset, t0, T);
568 TFXX_debug(opt.
debug,
"main",
569 " series is filtered");
576 if (opt.
verbose) { std::cout <<
" filtered" << std::endl; }
581 if (opt.
verbose) { std::cout <<
" passed unchanged" << std::endl; }
584 TFXX_debug(opt.
debug,
"main",
585 " series and WID are written");
588 if (os.handlesinfo()) { os << info; }
590 if (is.hasfree() ||
true)
595 tracefree.append(
"read from file " + infile->name);
596 if ((!doapply) || traceranges.contains(itrace))
598 tracefree.append(commands);
602 tracefree.append(
"passed unchanged");
604 if (os.handlestracefree()) { os << tracefree; }
606 TFXX_debug(opt.
debug,
"main",
607 "trace #" << itrace <<
" successfully processed");
612 TFXX_debug(opt.
debug,
"main",
"skip trace #" << itrace );
614 { std::cout <<
" skip trace #" << itrace << std::endl; }
ts::sff::SFFTimeSeries< Tseries > Ttimeseries
ts::filter::Ttimeseries Ttimeseries
int main(int iargc, char *argv[])
static const char * cmdlinekeys[]
static const char formatkey[]
Ttimeseries::Tseries Tseries
const char *const tracekey
key to select traces
aff::Series< double > Tseries