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

croposp [WP][FEATURE]: write PSD to output file

parent 4fb82fc9
......@@ -34,6 +34,7 @@
"CROPOSP V1.0 Cross power spectral density"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
......@@ -50,7 +51,7 @@ using std::cerr;
using std::endl;
struct Options {
bool verbose, trim, debug;
bool verbose, trim, debug, overwrite;
std::string inputformat, labelpattern;
double datetolerance;
bool logscale;
......@@ -138,6 +139,60 @@ void report_collection(const TCollection& collection,
}
} // void report_collection(const Tcollection& collection)
/* ---------------------------------------------------------------------- */
/* output a vector of named series to file
* ---------------------------------------
*/
void write_named_series(const std::string& filename,
const std::string& comment,
psd::TDseries f,
const TNamedSeriesVector& nsv,
const bool& verbose,
const bool& overwrite=true)
{
if (verbose)
{
cout << endl
<< "output to file " << filename << ":" << endl
<< comment << endl;
}
if (!overwrite)
{
std::ifstream file(filename.c_str(), std::ios_base::in);
TFXX_assert((!file.good()),"ERROR: output file exists!");
}
TFXX_assert(nsv.size()>0, "data container is empty");
std::ofstream os(filename.c_str());
os << "# " << CROPOSP_VERSION << endl;
os << "# " << comment << endl;
for (unsigned int i=0; i<nsv.size(); ++i)
{
unsigned int fi=i+1;
os << "# #" << fi << ": " << nsv[i].label << endl;
TFXX_assert(nsv[i].series.size()==f.size(),
"series passed to output function are inconsistent")
}
// set first index to 0 - just in case
f.shift(-f.first());
unsigned int nsamples=nsv[0].series.size();
for (unsigned int i=0; i<nsamples; ++i)
{
os << f(i);
for (unsigned int j=0; j<nsv.size(); ++j)
{
const psd::TDseries::Tcoc& s=nsv[j].series;
os << " " << s(i+s.first());
}
os << endl;
}
} // void write_named_series(const std::string& filename,
// const std::string& comment,
// psd::TDseries f,
// const TNamedSeriesVector& nsv,
// const bool& verbose,
// const bool& overwrite=true)
/* ====================================================================== */
int main(int iargc, char* argv[])
......@@ -148,7 +203,7 @@ int main(int iargc, char* argv[])
{
CROPOSP_VERSION "\n"
"usage: croposp [-verbose] [-itype f] [-trim] [-datetolerance t]" "\n"
" [-label p]" "\n"
" [-label p] [-overwrite]" "\n"
" [-psd f] [-npsd f] [-transfer f] [-coherency f]" "\n"
" file [t:sel] [f:format] [n:label] [file [t:s] [f:f] [n:l]] [...]\n"
" or: croposp --help|-h" "\n"
......@@ -174,6 +229,7 @@ int main(int iargc, char* argv[])
" %NT: number of trace in file\n"
"-log n map averages to logarithmic scale with \"n\"\n"
" samples pre decade\n"
"-overwrite overwrite existing output files\n"
"\n"
"output options:\n"
"-psd f compute power spectral density and write to file \"f\"\n"
......@@ -218,8 +274,10 @@ int main(int iargc, char* argv[])
{"transfer",arg_yes,"-"},
// 10: compute coherency
{"coherency",arg_yes,"-"},
// 11: compute coherency
// 11: map to logarithmic frequency sampling
{"log",arg_yes,"1"},
// 12: overwrite existing output files
{"overwrite",arg_no,"-"},
{NULL}
};
......@@ -277,6 +335,7 @@ int main(int iargc, char* argv[])
opt.logscale=cmdline.optset(11);
opt.n_per_decade=cmdline.int_arg(11);
opt.overwrite=cmdline.optset(12);
TFXX_assert(opt.n_per_decade>0,
"number of samples per decade must be finite and positive");
......@@ -445,7 +504,17 @@ int main(int iargc, char* argv[])
++i_meta;
} // while (i_series != collection_of_series.end() &&
// i_meta != vector_of_metadata.end())
} // if (opt.compute_psd)
if (opt.compute_psd)
{
write_named_series(opt.outfile_psd,
"total power spectral density of signals",
frequencies,
PSD_vector,
opt.verbose,
opt.overwrite);
} // if (opt.compute_psd)
} // if (opt.compute_psd || opt.compute_npsd || opt.compute_transfer ||
// // opt.compute_coherency)
} // main()
......
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