Commit 1d458d97 authored by thomas.forbriger's avatar thomas.forbriger
Browse files

croposp [WP]: strip code into several compilation units

introduce namespace croposp
set include statements in compilation units appropriate
parent 8480814d
......@@ -134,8 +134,15 @@ LIBTSIOXX=-ltsioxx -ltsxx -ltfxx $(LIBDATRWXX) -lgsl -lgslcblas
# binary executable targets
# -------------------------
OBJFILES=pairs_adapter.o \
pairs.o \
patsubst.o \
report_collection.o \
triples.o \
write_name_series.o
croposp: \
%: %.o
%: %.o $(OBJFILES)
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ \
-lpsdxx -lfourierxx $(LIBTSIOXX) -lfftw3
......
......@@ -30,21 +30,17 @@
*
* ============================================================================
*/
/*
* version string is set in croposp.h
*
#define CROPOSP_VERSION \
"CROPOSP V1.0 Cross power spectral density"
*/
#include "croposp.h"
#include <iostream>
#include <fstream>
#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>
#include <psdxx/psd.h>
#include <tfxx/misc.h>
using std::cout;
using std::cerr;
......@@ -66,48 +62,6 @@ struct Options {
std::string prefix_phase, prefix_coherency;
}; // 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;
// a struct to hold computation results for exactly one series
struct NamedSeries {
std::string label;
psd::TDseries series;
}; // struct NamedSeries
// a vector type to hold results
typedef std::vector<NamedSeries> TNamedSeriesVector;
/* ====================================================================== */
/*! provide index values to pairs
*
* cycle through a complete set
*/
class Pairs {
public:
Pairs(const unsigned int& n);
}; // class Pairs
/* ---------------------------------------------------------------------- */
/*! provide index values to triples
*
* cycle through a complete set
*/
class Triples {
public:
Triples(const unsigned int& n);
}; // class Triples
/* ====================================================================== */
// define usage information
......@@ -122,107 +76,6 @@ char reference_sleeman_et_al_2006[]=
/* ====================================================================== */
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();
TMetaVector::const_iterator i_meta=metadata.begin();
while (i_series != collection.end() &&
i_meta != metadata.end())
{
cout << " "
<< i_series->header.station << " "
<< i_series->header.channel << " "
<< i_series->header.auxid << " "
<< "dt=" << i_series->header.dt << "s "
<< "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())
<< " " <<
TFXX_value(i_series->l())
<< " " <<
TFXX_value(i_series->size()));
++i_series;
++i_meta;
}
} // 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[])
{
......@@ -459,8 +312,8 @@ int main(int iargc, char* argv[])
}
// collect traces
TCollection collection_of_series;
TMetaVector vector_of_metadata;
croposp::TCollection collection_of_series;
croposp::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();
......@@ -470,26 +323,26 @@ int main(int iargc, char* argv[])
while (i_trace != i_file->data.end())
{
collection_of_series.push_back(*i_trace);
Meta metadata;
croposp::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);
metadata.label=croposp::patsubst(metadata.label, "%C",
i_trace->header.wid2().channel);
metadata.label=croposp::patsubst(metadata.label, "%S",
i_trace->header.wid2().station);
metadata.label=croposp::patsubst(metadata.label, "%A",
i_trace->header.wid2().auxid);
metadata.label=croposp::patsubst(metadata.label, "%I",
i_trace->header.wid2().instype);
metadata.label=croposp::patsubst(metadata.label, "%F",
metadata.filearguments.name);
std::ostringstream oss;
oss << i_trace->traceindex();
metadata.label=patsubst(metadata.label, "%NT", oss.str());
metadata.label=croposp::patsubst(metadata.label, "%NT", oss.str());
if (metadata.filearguments.haskey(labelkey))
{
metadata.label=patsubst(metadata.label, "%N",
metadata.filearguments.value(labelkey));
metadata.label=croposp::patsubst(metadata.label, "%N",
metadata.filearguments.value(labelkey));
}
vector_of_metadata.push_back(metadata);
++i_trace;
......@@ -545,7 +398,7 @@ int main(int iargc, char* argv[])
*/
// provide container for PSD values
TNamedSeriesVector PSD_vector;
croposp::TNamedSeriesVector PSD_vector;
if (opt.compute_psd || opt.compute_npsd ||
opt.compute_coherency)
......@@ -558,8 +411,8 @@ int main(int iargc, char* argv[])
psd_computer.set_padfactor(opt.padfactor);
psd_computer.set_overlap(opt.overlap);
TCollection::const_iterator i_series=collection_of_series.begin();
TMetaVector::const_iterator i_meta=vector_of_metadata.begin();
croposp::TCollection::const_iterator i_series=collection_of_series.begin();
croposp::TMetaVector::const_iterator i_meta=vector_of_metadata.begin();
while (i_series != collection_of_series.end() &&
i_meta != vector_of_metadata.end())
{
......@@ -585,7 +438,7 @@ int main(int iargc, char* argv[])
} // if (opt.logscale) ... else
} // if (i_series==collection_of_series.begin())
NamedSeries named_psd;
croposp::NamedSeries named_psd;
if (opt.logscale)
{
named_psd.series=psd::log_sampling(psd, frequencies);
......
/*! \file croposp.h
* \brief declaration of classes, types and functions used in croposp
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 06/02/2019
*
* declaration of classes, types and functions used in croposp (prototypes)
*
* Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 06/02/2019 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define CROPOSP_VERSION \
"CROPOSP V1.0 Cross power spectral density"
// include guard
#ifndef CROPOSP_H_VERSION
#define CROPOSP_H_VERSION \
"CROPOSP_H V1.0"
#include <string>
#include <vector>
#include <tfxx/commandline.h>
#include <tfxx/xcmdline.h>
#include <tsxx/wid2timeseries.h>
#include <tsxx/tscollection.h>
#include <psdxx/psd.h>
namespace croposp {
// 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;
// a struct to hold computation results for exactly one series
struct NamedSeries {
std::string label;
psd::TDseries series;
}; // struct NamedSeries
// a vector type to hold results
typedef std::vector<NamedSeries> TNamedSeriesVector;
/* ====================================================================== */
/* classes
* -------
*/
/*! provide index values to pairs
*
* cycle through a complete set
*/
class Pairs {
public:
Pairs(const unsigned int& n);
}; // class Pairs
/* ---------------------------------------------------------------------- */
/*! provide index values to triples
*
* cycle through a complete set
*/
class Triples {
public:
Triples(const unsigned int& n);
}; // class Triples
/* ====================================================================== */
/* functions
* ---------
*/
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);
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);
} // namespace croposp
#endif // TF_CROPOSP_H_VERSION (includeguard)
/* ----- END OF croposp.h ----- */
/*! \file pairs.cc
* \brief pairs manipulator (implementation)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 06/02/2019
*
* pairs manipulator (implementation)
*
* Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 06/02/2019 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_PAIRS_CC_VERSION \
"TF_PAIRS_CC V1.0"
#include "croposp.h"
namespace croposp {
} // namespace croposp
/* ----- END OF pairs.cc ----- */
/*! \file pairs_adapter.cc
* \brief adaptor to name series derived from pairs (implementation)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 06/02/2019
*
* adaptor to name series derived from pairs (implementation)
*
* Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 06/02/2019 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_PAIRS_ADAPTER_CC_VERSION \
"TF_PAIRS_ADAPTER_CC V1.0"
#include "croposp.h"
namespace croposp {
} // namespace croposp
/* ----- END OF pairs_adapter.cc ----- */
/*! \file patsubst.cc
* \brief provide pattern substitution as required in croposp (implementation)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 06/02/2019
*
* provide pattern substitution as required in croposp (implementation)
*
* Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 06/02/2019 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_PATSUBST_CC_VERSION \
"TF_PATSUBST_CC V1.0"
#include "croposp.h"
#include<tfxx/stringfunc.h>
namespace croposp {
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)
} // namespace croposp
/* ----- END OF patsubst.cc ----- */
/*! \file report_collection.cc
* \brief report a collection of time series (implementation)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 06/02/2019
*
* report a collection of time series (implementation)
*
* Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 06/02/2019 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_REPORT_COLLECTION_CC_VERSION \
"TF_REPORT_COLLECTION_CC V1.0"
#include "croposp.h"
#include<iostream>
#include<tfxx/misc.h>
using std::cout;
using std::endl;
namespace croposp {
void report_collection(const TCollection& collection,
const TMetaVector& metadata,
const bool& debug)
{
TFXX_assert(collection.size() == metadata.size(),
"data inconsitency; report this as a program bug");
TCollection::const_iterator i_series=collection.begin();
TMetaVector::const_iterator i_meta=metadata.begin();
while (i_series != collection.end() &&
i_meta != metadata.end())
{
cout << " "
<< i_series->header.station << " "
<< i_series->header.channel << " "
<< i_series->header.auxid << " "
<< "dt=" << i_series->header.dt << "s "
<< "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())
<< " " <<
TFXX_value(i_series->l())
<< " " <<
TFXX_value(i_series->size()));
++i_series;
++i_meta;
}
} // void report_collection(const Tcollection& collection)
} // namespace croposp
/* ----- END OF report_collection.cc ----- */