Commit c55dc3a2 authored by thomas.forbriger's avatar thomas.forbriger
Browse files

ts/wf/cross [WP][!!!][API]: re-implement file I/O

Attention: The output file now must be specified in front of the input files
on the command line.

- use proper libdatrwxx access for input and output to support different file
  formats
- make use of time series structures which handle the full header

The implementation is not yet finished, and sclaing of results as well as
settings in the file and trace header differ from the previous version.
parent 3011df63
......@@ -195,7 +195,7 @@ evelox suseix: %x: %.o
# C++ programs
# ------------
sigval sigscale geophone gatherdiff cross autocorr: \
sigval sigscale geophone gatherdiff autocorr: \
%: %.o
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ \
$(LIBDATRWXX) -ltfxx
......@@ -235,7 +235,7 @@ noisymize: \
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ \
$(LIBTSIOXX)
fregra foutra deconv: \
cross fregra foutra deconv: \
%: %.o
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ \
-lfourierxx $(LIBTSIOXX) -lfftw3
......
......@@ -40,23 +40,31 @@
#include <list>
#include <string>
#include <tfxx/commandline.h>
#include <tfxx/xcmdline.h>
#include <tfxx/error.h>
#include <tfxx/rangelist.h>
#include <tfxx/rangestring.h>
#include <sffxx.h>
#include <tsxx/tsxx.h>
#include <tsioxx/sfftimeseries.h>
#include <tsioxx/inputoperators.h>
#include <tsioxx/outputoperators.h>
#include <tsxx/correlate.h>
#include <tsxx/convolve.h>
#include <datrwxx/sff.h>
#include <datrwxx/writeany.h>
#include <datrwxx/readany.h>
#include <aff/seriesoperators.h>
using std::cout;
using std::cerr;
using std::endl;
typedef std::list<std::string> Tnames;
typedef ts::TDsfftimeseries Tts;
typedef aff::Series<double> Tseries;
typedef ts::sff::SFFTimeSeries<Tseries> Tts;
typedef Tts::Tdttimeseries Tdttimeseries;
struct Options {
bool verbose, overwrite, debug, convolve;
std::string inputformat, outputformat;
}; // struct Options
int main(int iargc, char* argv[])
......@@ -66,8 +74,10 @@ int main(int iargc, char* argv[])
char usage_text[]=
{
CROSS_VERSION "\n"
"usage: cross [-v] [-o] [-D] [-convolve] file [file [...]] outfile" "\n"
"usage: cross [-v] [-o] [-D] [-convolve] [--itype t] [--otype f]\n"
" outfile file [f:F] [t:T] [file [f:F] [t:T] [...]]\n"
" or: cross --help|-h" "\n"
" or: cross --xhelp" "\n"
};
// define full help text
......@@ -77,8 +87,17 @@ int main(int iargc, char* argv[])
"-D debug mode" "\n"
"-o overwrite existing output file" "\n"
"-convolve apply convolution rather than correlation\n"
"file ... input file(s)" "\n"
"-itype t file format for input files\n"
"-otype t file format for output files\n"
"\n"
"outfile output file" "\n"
"file ... input file(s)" "\n"
" t:T select traces T, where T may be any range\n"
" specification like '3-4' or '5,6,7-12,20'\n"
" f:F specifies an input file format differing from\n"
" the format selected by \"--itype\"\n"
"\n"
"--xhelp print details on supported I/O formats\n"
"\n"
"The first trace read will be taken as a reference. All other\n"
"traces will be cross-correlated with the reference." "\n"
......@@ -101,8 +120,27 @@ int main(int iargc, char* argv[])
{"D",arg_no,"-"},
// 4: convolve rather than correlate
{"convolve",arg_no,"-"},
// 5: detailed information on I/O formats
{"xhelp",arg_no,"-"},
// 6: convolve rather than correlate
{"itype",arg_yes,"sff"},
// 7: convolve rather than correlate
{"otype",arg_yes,"sff"},
{NULL}
};
/*----------------------------------------------------------------------*/
/* define file specific options
*/
static const char formatkey[]="f";
static const char tracekey[]="t";
// define commandline argument modifier keys
static const char* cmdlinekeys[]
={formatkey, tracekey, 0};
/*----------------------------------------------------------------------*/
// no arguments? print usage...
if (iargc<2)
......@@ -115,107 +153,143 @@ int main(int iargc, char* argv[])
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
if (cmdline.optset(0) || cmdline.optset(5))
{
cerr << usage_text << endl;
cerr << help_text << endl;
cerr << endl;
datrw::supported_data_types(cerr);
cerr << endl;
if (cmdline.optset(5))
{
cerr << endl;
datrw::online_help(cerr);
}
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; }
*/
/* ---------------------------------------------------------------------- */
// evaluate command line parameters
Options opt;
opt.verbose=cmdline.optset(1);
opt.overwrite=cmdline.optset(2);
opt.debug=cmdline.optset(3);
opt.convolve=cmdline.optset(4);
opt.inputformat=cmdline.string_arg(6);
opt.outputformat=cmdline.string_arg(7);
if (opt.verbose) { cout << CROSS_VERSION << endl; }
TFXX_assert(cmdline.extra(), "missing input file!");
std::string outfile=cmdline.next();
TFXX_assert(cmdline.extra(), "missing output file!");
Tnames infiles;
while (cmdline.extra())
tfxx::cmdline::Tparsed arguments=parse_cmdline(cmdline, cmdlinekeys);
if ((arguments.size()>1) && opt.verbose)
{
infiles.push_back(outfile);
outfile=cmdline.next();
cout
<< "NOTICE: file specific information (SRCE line and file FREE)" << endl
<< " will be taken from first file only!" << endl;
}
/* ---------------------------------------------------------------------- */
// open output file
// ----------------
if (opt.verbose) { cout << "open output file " << outfile << endl; }
std::ofstream ofs(outfile.c_str());
sff::SFFostream<Tts::Tseries> os(ofs);
sff::SRCE srce=sff::srce_reference();
os << srce;
// check if output file exists and open
if (!opt.overwrite) { datrw::abort_if_exists(outfile); }
std::ios_base::openmode oopenmode
=datrw::oanystream::openmode(opt.outputformat);
std::ofstream ofs(outfile.c_str(), oopenmode);
datrw::oanystream os(ofs, opt.outputformat, opt.debug);
Tts reference, data;
/*
* the first trace to be read has to be stored as the reference trace
*/
Tts reference;
bool referenceread=false;
Tnames::const_iterator I=infiles.begin();
while (I!=infiles.end())
// cycle through list of file names found on command line
tfxx::cmdline::Tparsed::const_iterator infile=arguments.begin();
while (infile!=arguments.end())
{
if (opt.verbose) { cout << "open " << std::string(*I) << endl; }
std::ifstream ifs(I->c_str());
datrw::isffstream is(ifs, opt.debug);
if (opt.verbose) { cout << "open " << std::string(infile->name) << endl; }
std::string inputformat=opt.inputformat;
if (infile->haskey(formatkey))
{
inputformat=infile->value(formatkey);
}
std::ios_base::openmode iopenmode
=datrw::ianystream::openmode(inputformat);
std::ifstream ifs(infile->name.c_str(), iopenmode);
datrw::ianystream is(ifs, inputformat);
// handle file header
// ------------------
ts::sff::FileHeader fileheader;
is >> fileheader;
if (!referenceread) { os << fileheader; }
// cycle through traces of input file
// ----------------------------------
// setup trace selection
typedef tfxx::RangeList<int> Trangelist;
bool doselect=infile->haskey(tracekey);
Trangelist traceranges=
tfxx::string::rangelist<Trangelist::Tvalue>(infile->value(tracekey));
int iftrace=0;
while (is.good())
{
if (referenceread) {
if (opt.verbose) { cout << "read trace" << endl; }
Tts data;
Tts::Tseries samples;
is >> samples >> data.header;
data.series()=samples;
sff::INFO info;
is >> info;
if (opt.verbose) { cout << data.header.line() << endl; }
TFXX_assert((data.header.dt == reference.header.dt),
"sampling intervals do not match!");
Tts result;
if (opt.convolve)
{
result=ts::convolve(reference,data);
result.header.auxid="corr";
} else {
result=ts::correlate(reference,data);
result.header.auxid="conv";
}
// result *= result.header.dt;
result.header=data.header;
result.header.date=srce.date
+(result.first()*libtime::double2time(result.header.dt))
+(data.header.date-reference.header.date);
os << result.series() << result.header;
if (is.hasinfo()) { os << info; }
++iftrace;
if (doselect && (!traceranges.contains(iftrace)))
{
if (opt.verbose)
{ std::cout << " skip trace #" << iftrace << std::endl; }
is.skipseries();
}
else
{
if (opt.verbose) { cout << "read reference trace" << endl; }
is >> reference.series() >> reference.header;
if (opt.verbose) { cout << reference.header.line() << endl; }
referenceread=true;
}
if (opt.verbose) { std::cout << "process trace #" << iftrace << ": "; }
if (!referenceread)
{
if (opt.verbose)
{ std::cout << "read reference trace" << std::endl; }
is >> reference;
referenceread=true;
if (opt.verbose) { cout << reference.header.wid2().line() << endl; }
}
else
{
if (opt.verbose) { cout << "read trace" << endl; }
Tts data;
is >> data;
if (opt.verbose) { cout << data.header.wid2().line() << endl; }
TFXX_assert((data.header.wid2().dt == reference.header.wid2().dt),
"sampling intervals do not match!");
Tts result(data);
if (opt.convolve)
{
result=ts::convolve(reference,data);
result.header=data.header;
result.header.wid2().auxid="corr";
} else {
result=ts::correlate(reference,data);
result.header=data.header;
result.header.wid2().auxid="conv";
}
// result *= result.header.dt;
/*
result.header.wid2().date=fileheader.srce.date
+(result.series.first()
*libtime::double2time(result.header.wid2().dt))
+(data.header.wid2().date-reference.header.wid2().date);
*/
if (opt.verbose) { cout << result.header.wid2().line() << endl; }
os << result.header << Tts::Tcoc(result);
} // else; if (!referenceread)
} // else; if (doselect && (!traceranges.contains(itrace)))
}
++I;
++infile;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment