GPLIB++
simple_processing.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iterator>
3 #include <fstream>
4 #include <boost/program_options.hpp>
5 #include "TsSpectrum.h"
6 #include "TimeSeriesData.h"
7 #include "statutils.h"
8 #include "MTStation.h"
9 #include "types.h"
10 #include "WFunc.h"
11 #include "TimeFrequency.h"
12 #include "VecMat.h"
13 #include "MtuFilter.h"
14 #include "Util.h"
15 
16 using namespace std;
17 using namespace gplib;
18 namespace po = boost::program_options;
19 
20 
21 /*!
22  * \addtogroup UtilProgs Utility Programs
23  *@{
24  * \file simple_processing.cpp
25  * Very simple MT processing code based on least squares estimation as described by Sims 1971.
26  * So far no error calculation.
27  */
28 
29 string version =
30  "$Id: simple_processing.cpp 1853 2010-05-28 10:37:12Z mmoorkamp $";
31 int main(int argc, char *argv[])
32  {
33  cout
34  << "This is simpleproc: Simple most estimation of MT transfer function, spectral stacking"
35  << endl << endl;
36  cout << " Usage simpleproc inputfilename";
37  cout
38  << " Output will have the same name as MTU Input with '.mtt' appended "
39  << endl << endl;
40  cout << " This is Version: " << version << endl << endl;
41 
42  TimeSeriesData MtuData;
43  MTStation Output;
44  string infilename, basefilename;
45  unsigned int seglength = 2400;
46  bool mtufilter = false;
47  double rate = 1.0;
48  po::options_description desc("Allowed options");
49  desc.add_options()("help", "produce help message")("seglength", po::value<
50  unsigned int>(&seglength)->default_value(2400),
51  "The length of an individual segment for spectral calculations")(
52  "rate", po::value(&rate)->default_value(1.0),
53  "Set the sampling rate in Hz")("mtufilter",
54  po::value<bool>(&mtufilter)->default_value(false),
55  "Read in filter information for mtu data")("input-file", po::value<
56  string>(&infilename), "input file");
57 
58  po::positional_options_description p;
59  p.add("input-file", -1);
60 
61  po::variables_map vm;
62  po::store(
63  po::command_line_parser(argc, argv). options(desc).positional(p).run(),
64  vm);
65  po::notify(vm);
66 
67  if (vm.count("help"))
68  {
69  cout << desc << "\n";
70  return 1;
71  }
72  MtuData.GetData(infilename.c_str());
73  size_t dotpos = infilename.find('.', 0);
74  if (dotpos != string::npos)
75  basefilename = infilename.erase(dotpos);
76  string exfiltername = basefilename + "_1.cts";
77  string eyfiltername = basefilename + "_2.cts";
78  string hxfiltername = basefilename + "_3.cts";
79  string hyfiltername = basefilename + "_4.cts";
80 
81 
82  const unsigned int nfreqs = seglength / 2 + 1;
83  tcompdata ExHxCorr(nfreqs, 0);
84  tcompdata ExHyCorr(nfreqs, 0);
85  tcompdata EyHxCorr(nfreqs, 0);
86  tcompdata EyHyCorr(nfreqs, 0);
87  tcompdata HxHxCorr(nfreqs, 0);
88  tcompdata HxHyCorr(nfreqs, 0);
89  tcompdata HyHyCorr(nfreqs, 0);
90  tcompdata Hdet(nfreqs, 0);
91  tcompdata Zxx(nfreqs, 0);
92  tcompdata Zxy(nfreqs, 0);
93  tcompdata Zyx(nfreqs, 0);
94  tcompdata Zyy(nfreqs, 0);
95 
96 
97 
98  double freqstep = MtuData.GetData().GetSamplerate() / (seglength);
99  MtuFilter ExFilter(seglength, freqstep), EyFilter(seglength, freqstep),
100  HxFilter(seglength, freqstep), HyFilter(seglength, freqstep);
101  if (mtufilter)
102  {
103  ExFilter.GetData(exfiltername);
104  EyFilter.GetData(eyfiltername);
105  HxFilter.GetData(hxfiltername);
106  HyFilter.GetData(hyfiltername);
107  }
108  gplib::cmat ExTimeFrequency, EyTimeFrequency, HxTimeFrequency,
109  HyTimeFrequency;
110 
111  ExTimeFrequency = TimeFrequency(
112  MtuData.GetData().GetEx().GetData().begin(),
113  MtuData.GetData().GetEx().GetData().end(), seglength, Hanning());
114  EyTimeFrequency = TimeFrequency(
115  MtuData.GetData().GetEy().GetData().begin(),
116  MtuData.GetData().GetEy().GetData().end(), seglength, Hanning());
117  HxTimeFrequency = TimeFrequency(
118  MtuData.GetData().GetHx().GetData().begin(),
119  MtuData.GetData().GetHx().GetData().end(), seglength, Hanning());
120  HyTimeFrequency = TimeFrequency(
121  MtuData.GetData().GetHy().GetData().begin(),
122  MtuData.GetData().GetHy().GetData().end(), seglength, Hanning());
123 
124  const unsigned int nsegs = ExTimeFrequency.size1();
125  ofstream logfile((infilename + ".log").c_str());
126  for (size_t i = 0; i < Zxx.size(); ++i)
127  {
128  for (size_t j = 0; j < nsegs; ++j)
129  {
130  if (mtufilter)
131  {
132  ExTimeFrequency(j, i) /= ExFilter.GetFilterCoeff().at(i);
133  EyTimeFrequency(j, i) /= EyFilter.GetFilterCoeff().at(i);
134  HxTimeFrequency(j, i) /= HxFilter.GetFilterCoeff().at(i);
135  HyTimeFrequency(j, i) /= HyFilter.GetFilterCoeff().at(i);
136  }
137  ExHxCorr.at(i) += (ExTimeFrequency(j, i) * conj(HxTimeFrequency(j,
138  i)));
139  ExHyCorr.at(i) += (ExTimeFrequency(j, i) * conj(HyTimeFrequency(j,
140  i)));
141  EyHxCorr.at(i) += (EyTimeFrequency(j, i) * conj(HxTimeFrequency(j,
142  i)));
143  EyHyCorr.at(i) += (EyTimeFrequency(j, i) * conj(HyTimeFrequency(j,
144  i)));
145  HxHyCorr.at(i) += (HxTimeFrequency(j, i) * conj(HyTimeFrequency(j,
146  i)));
147  HxHxCorr.at(i) += (HxTimeFrequency(j, i) * conj(HxTimeFrequency(j,
148  i)));
149  HyHyCorr.at(i) += (HyTimeFrequency(j, i) * conj(HyTimeFrequency(j,
150  i)));
151 
152  }
153 
154  Hdet.at(i) = (HxHxCorr.at(i) * HyHyCorr.at(i) - HxHyCorr.at(i) * conj(
155  HxHyCorr.at(i)));
156  logfile << "Hdet: " << Hdet.at(i) << endl;
157  Zxx.at(i) = ((ExHxCorr.at(i) * HyHyCorr.at(i) - ExHyCorr.at(i) * conj(
158  HxHyCorr.at(i))) / Hdet.at(i));
159  Zxy.at(i) = ((ExHyCorr.at(i) * HxHxCorr.at(i) - ExHxCorr.at(i)
160  * HxHyCorr.at(i)) / Hdet.at(i));
161  Zyx.at(i) = ((EyHxCorr.at(i) * HyHyCorr.at(i) - EyHyCorr.at(i) * conj(
162  HxHyCorr.at(i))) / Hdet.at(i));
163  Zyy.at(i) = ((EyHyCorr.at(i) * HxHxCorr.at(i) - EyHxCorr.at(i)
164  * HxHyCorr.at(i)) / Hdet.at(i));
165  }
166  Output.AssignAll(Zxx.size());
167  for (size_t i = 0; i < Zxx.size(); ++i)
168  {
169  MTTensor
170  CurrZ(Zxx.at(i), Zxy.at(i), Zyx.at(i), Zyy.at(i), i * freqstep);
171  Output.SetMTData().at(i) = CurrZ;
172  }
173  vector<double> transferfunc(Zxx.size() * 2 - 1, 0.0);
174  TsSpectrum().CalcTimeSeries(Zxy.begin(), Zxy.end(), transferfunc.begin(),
175  transferfunc.end());
176  ofstream tffile((infilename + ".tf").c_str());
177  copy(transferfunc.begin(), transferfunc.end(), ostream_iterator<double> (
178  tffile, "\n"));
179 
180  Output.WriteAsMtt(infilename.c_str());
181  }
182 /*@}*/
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
TimeSeries & GetData()
return a reference to the actual object stored in the pointer
TimeSeriesComponent & GetEx()
Definition: TimeSeries.h:47
Store the filter coefficients for one component of Phoenix mtu data.
Definition: MtuFilter.h:13
void AssignAll(const int nfreq)
Definition: MTStation.cpp:70
gplib::cmat TimeFrequency(InputIterator tsbegin, InputIterator tsend, const size_t seglength, WindowFunctype WFunc)
Calculate a sliding windowed fourier transform for a time series and store the results for each segme...
Definition: TimeFrequency.h:23
void erase(const int startindex, const int endindex)
Erase data between startindex and endindex.
Definition: TimeSeries.cpp:59
double GetSamplerate()
The samplerate is stored in each component, we just return the samplerate of Hx assuming they are all...
Definition: TimeSeries.h:71
const tcompdata & GetFilterCoeff()
Definition: MtuFilter.h:19
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
string version
The class CTsSpectrum is used to calculate spectra from (real) time series data.
Definition: TsSpectrum.h:21
int main()
Definition: angleavg.cpp:12
virtual void GetData(const std::string filename)
Definition: MtuFilter.cpp:16
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
void WriteAsMtt(const std::string filename)
Write data in goettingen .mtt format.
Definition: MTStation.cpp:681
Stores MT-Tensor components at a single frequency, calculates derived quantities. ...
Definition: MTTensor.h:16
This functor returns the weighting factor for the Hanning window, given the relative position (0...
Definition: WFunc.h:31
void CalcTimeSeries(_InputIterator freqbegin, _InputIterator freqend, _OutputIterator tsbegin, _OutputIterator tsend)
The member function to calculate a time series from (complex) spectra.
Definition: TsSpectrum.h:106
std::vector< MTTensor > & SetMTData()
Get the full vector of Tensor elements for reading and writing.
Definition: MTStation.h:124
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35