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

croposp [WP][FEATURE]: handle time series labels

parent c8227399
......@@ -19,9 +19,13 @@
"CROPOSP V1.0 Cross power spectral density"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <tfxx/commandline.h>
#include <tfxx/error.h>
#include <tfxx/misc.h>
#include <tfxx/stringfunc.h>
#include <tsioxx/cmdlinefiles.h>
#include <tsxx/tscollection.h>
......@@ -31,19 +35,47 @@ using std::endl;
struct Options {
bool verbose, trim, debug;
std::string inputformat;
std::string inputformat, labelpattern;
double datetolerance;
}; // struct Options
// a struct to hold meta data
struct Meta {
tfxx::cmdline::Filename filearguments;
std::string label;
}; // struct Meta
// type of time series collection to hold input data
typedef ts::TimeSeriesCollection<double> TCollection;
// a vector to gold meta data for each time series
typedef std::vector<Meta> TMetaVector;
/* ====================================================================== */
std::string patsubst(const std::string& templatestring,
const std::string& pattern,
const std::string& content)
{
std::string retval;
retval=tfxx::string::patsubst(templatestring, pattern,
tfxx::string::trimws(content));
return(retval);
} // std::string patsubst(const std::string& templatestring,
// const std::string& pattern,
// const std::string& content)
/* ---------------------------------------------------------------------- */
void report_collection(const TCollection& collection,
const TMetaVector& metadata,
const bool& debug=false)
{
TFXX_assert(collection.size() == metadata.size(),
"data inconsitency; report this as a program bug");
TCollection::const_iterator i_series=collection.begin();
while (i_series != collection.end())
TMetaVector::const_iterator i_meta=metadata.begin();
while (i_series != collection.end() &&
i_meta != metadata.end())
{
cout << " "
<< i_series->header.station << " "
......@@ -53,6 +85,7 @@ void report_collection(const TCollection& collection,
<< "begin: " << i_series->header.date.timestring() << " "
<< "n: " << i_series->header.nsamples
<< endl;
cout << " " << i_meta->label << endl;
TFXX_debug(debug, "report_collection",
TFXX_value(i_series->f())
<< " " <<
......@@ -60,6 +93,7 @@ void report_collection(const TCollection& collection,
<< " " <<
TFXX_value(i_series->size()));
++i_series;
++i_meta;
}
} // void report_collection(const Tcollection& collection)
......@@ -72,7 +106,9 @@ int main(int iargc, char* argv[])
char usage_text[]=
{
CROPOSP_VERSION "\n"
"usage: croposp" "\n"
"usage: croposp [-verbose] [-itype f] [-trim] [-datetolerance t]" "\n"
" [-label p]" "\n"
" file [t:sel] [f:format] [n:label] [file [t:s] [f:f] [n:l]] [...]\n"
" or: croposp --help|-h" "\n"
};
......@@ -86,10 +122,19 @@ int main(int iargc, char* argv[])
"-trim trim time series to identical begin and length\n"
"-datetolerance t set tolerance as a fraction of sampling interval\n"
" when comparing time series header date values\n"
"-label p set pattern for time series label\n"
" %C: channel (WID2 header field)\n"
" %S: station (WID2 header field)\n"
" %I: instype (WID2 header field)\n"
" %A: auxid (WID2 header field)\n"
" %N: label set for file name\n"
" %F: file name\n"
" %NT: number of trace in file\n"
"\n"
"keys to be appended to file names if required:\n"
"t:sel trace selection\n"
"f:format file format\n"
"n:label label to be used in plot legend\n"
};
// define commandline options
......@@ -108,15 +153,18 @@ int main(int iargc, char* argv[])
{"datetolerance",arg_yes,"0."},
// 5: debug mode
{"DEBUG",arg_no,"-"},
// 6: pattern for trace label
{"pattern",arg_yes,"%S:%C:%A:%I"},
{NULL}
};
// define command line keys for input files
static const char tracekey[]="t";
static const char formatkey[]="f";
static const char labelkey[]="n";
// define commandline argument modifier keys
static const char* cmdlinekeys[]={tracekey, formatkey, 0};
static const char* cmdlinekeys[]={tracekey, formatkey, labelkey, 0};
// no arguments? print usage...
if (iargc<2)
......@@ -142,6 +190,7 @@ int main(int iargc, char* argv[])
opt.trim=cmdline.optset(3);
opt.datetolerance=cmdline.double_arg(4);
opt.debug=cmdline.optset(5);
opt.labelpattern=cmdline.string_arg(6);
TFXX_assert(((opt.datetolerance >= 0.) && (opt.datetolerance <=1.)),
"datetolerance is out of accepted range");
......@@ -171,6 +220,7 @@ int main(int iargc, char* argv[])
// collect traces
TCollection collection_of_series;
TMetaVector vector_of_metadata;
typedef ts::sff::DFile::Tfile Tfile;
typedef Tfile::Ttracevector Ttracevector;
ts::sff::TDFileList::const_iterator i_file=input_file_list.begin();
......@@ -180,6 +230,28 @@ int main(int iargc, char* argv[])
while (i_trace != i_file->data.end())
{
collection_of_series.push_back(*i_trace);
Meta metadata;
metadata.filearguments=i_file->arguments;
metadata.label=opt.labelpattern;
metadata.label=patsubst(metadata.label, "%C",
i_trace->header.wid2().channel);
metadata.label=patsubst(metadata.label, "%S",
i_trace->header.wid2().station);
metadata.label=patsubst(metadata.label, "%A",
i_trace->header.wid2().auxid);
metadata.label=patsubst(metadata.label, "%I",
i_trace->header.wid2().instype);
metadata.label=patsubst(metadata.label, "%F",
metadata.filearguments.name);
std::ostringstream oss;
oss << i_trace->traceindex();
metadata.label=patsubst(metadata.label, "%NT", oss.str());
if (metadata.filearguments.haskey(labelkey))
{
metadata.label=patsubst(metadata.label, "%N",
metadata.filearguments.value(labelkey));
}
vector_of_metadata.push_back(metadata);
++i_trace;
}
++i_file;
......@@ -189,7 +261,8 @@ int main(int iargc, char* argv[])
if (opt.verbose)
{
cout << "Time series to be analyzed:" << endl;
report_collection(collection_of_series);
report_collection(collection_of_series,
vector_of_metadata);
}
// trim time series
......@@ -204,7 +277,8 @@ int main(int iargc, char* argv[])
if (opt.verbose)
{
cout << "Time series after trimming:" << endl;
report_collection(collection_of_series, opt.debug);
report_collection(collection_of_series,
vector_of_metadata, opt.debug);
}
}
......
Markdown is supported
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