GPLIB++
generatemtts.cpp
Go to the documentation of this file.
1 //============================================================================
2 // Name : generatemtts.cpp
3 // Author : May 7, 2010
4 // Version :
5 // Copyright : 2010, mmoorkamp
6 //============================================================================
7 
8 #include "Util.h"
9 #include "TimeSeriesData.h"
10 #include "TsSpectrum.h"
11 #include "WFunc.h"
12 #include "statutils.h"
13 #include "C1DMTSynthData.h"
14 
15 using namespace gplib;
16 using namespace std;
17 
18 /*!
19  * \addtogroup UtilProgs Utility Programs
20  *@{
21  * \file generatemtts.cpp
22  * Generate a synthetic MT times series from a recorded magnetic field
23  * and a 1D model.
24  */
25 
26 string version =
27  "$Id: generatemtts.cpp 1852 2010-05-20 09:14:53Z mmoorkamp $";
28 
29 int main()
30  {
31 
32  cout
33  << "This is generatemtts: Generate a synthetic MT time series."
34  << endl << endl;
35  cout << " You will have to input the name of a time series with two magnetic time series\n";
36  cout << " and a model file\n";
37  cout << " Output will have the same name as the modelfile with _ts.asc appended\n ";
38  cout << " and will contain the magnetic field and an electric field obeying the impedance \n ";
39  cout << " relationship calculate from the 1D model. \n ";
40  cout << " This is Version: " << version << endl << endl;
41 
42  std::string intsname = AskFilename(
43  "File with magnetic field times series: ");
44  TimeSeriesData HTsData;
45  HTsData.GetData(intsname);
46 
47  TsSpectrum SpecCalc;
48  const size_t nsamples = HTsData.GetData().GetHx().GetData().size();
49  const size_t nfreq = nsamples / 2 + 1;
50  cvec HxSpec(nfreq), HySpec(nfreq), ExSpec(nfreq), EySpec(nfreq);
51 
52  SubMean(HTsData.GetData().GetHx().GetData().begin(),
53  HTsData.GetData().GetHx().GetData().end());
54  SubMean(HTsData.GetData().GetHy().GetData().begin(),
55  HTsData.GetData().GetHy().GetData().end());
56 
57  ApplyWindow(HTsData.GetData().GetHx().GetData().begin(),
58  HTsData.GetData().GetHx().GetData().end(),
59  HTsData.GetData().GetHx().GetData().begin(), Hamming());
60  ApplyWindow(HTsData.GetData().GetHy().GetData().begin(),
61  HTsData.GetData().GetHy().GetData().end(),
62  HTsData.GetData().GetHy().GetData().begin(), Hamming());
63 
64  SpecCalc.CalcSpectrum(HTsData.GetData().GetHx().GetData().begin(),
65  HTsData.GetData().GetHx().GetData().end(), HxSpec.begin(), HxSpec.end());
66  SpecCalc.CalcSpectrum(HTsData.GetData().GetHy().GetData().begin(),
67  HTsData.GetData().GetHy().GetData().end(), HySpec.begin(), HySpec.end());
68 
69  string modelfilename = AskFilename("Model filename: ");
70  C1DMTSynthData Synthetic;
71  Synthetic.ReadModel(modelfilename);
72 
73  double samplerate = 1.0;
74  const double eps = 1e-5;
75  const size_t samplelength = nsamples;
76  const double freqstep = samplerate / samplelength;
77  trealdata frequency;
78  frequency.push_back(eps); //zero frequency means not valid so we use a small value for the static
79  for (size_t i = 1; i < samplelength / 2 +1; ++i) // add frequencies
80  {
81  frequency.push_back(freqstep * i);
82  }
83  Synthetic.SetFrequencies(frequency);//copy them to forward calculation object
84  Synthetic.CalcSynthetic();
85  for (size_t i = 0; i < nfreq; ++i)
86  {
87  ExSpec(i) = Synthetic.at(i).GetZxy() * HySpec(i);
88  EySpec(i) = Synthetic.at(i).GetZyx() * HxSpec(i);
89  }
90  SpecCalc.CalcTimeSeries(ExSpec.begin(), ExSpec.end(),
91  HTsData.GetData().GetEx().GetData().begin(),
92  HTsData.GetData().GetEx().GetData().end());
93  SpecCalc.CalcTimeSeries(EySpec.begin(), EySpec.end(),
94  HTsData.GetData().GetEy().GetData().begin(),
95  HTsData.GetData().GetEy().GetData().end());
96 
97  HTsData.WriteAsBirrp(modelfilename+"_ts.asc");
98  }
99 /*@}*/
void CalcSpectrum(_InputIterator tsbegin, _InputIterator tsend, _OutputIterator freqbegin, _OutputIterator freqend)
The member function to calculate a spectrum from a time series.
Definition: TsSpectrum.h:80
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
const MTTensor & at(const unsigned int i) const
direct acces to a tensor at a given index
Definition: MTStation.h:114
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
Definition: statutils.h:85
This functor returns the weighting factor for the Hamming window, given the relative position relpos ...
Definition: WFunc.h:23
std::complex< double > GetZyx() const
Definition: MTTensor.h:130
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
std::complex< double > GetZxy() const
Definition: MTTensor.h:126
void ReadModel(std::string filename)
Read the model from a file.
void ApplyWindow(InputIterator inbegin, InputIterator inend, OutputIterator outbegin, WindowFunctype WFunc, double relshift=0.0)
Apply one of the above window functions to a range.
Definition: WFunc.h:104
string version
virtual void CalcSynthetic()
Calculate the synthetic data given the previously set parameters.
The class CTsSpectrum is used to calculate spectra from (real) time series data.
Definition: TsSpectrum.h:21
int main()
Definition: angleavg.cpp:12
void WriteAsBirrp(std::string filename_base)
Write data to file in ascii format for birrp processing.
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
Calculate synthetic MT data for a 1D model using Cagniard's algorithm.
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
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
void SetFrequencies(const trealdata &freqs)
Set the frequencies of the tensor elements, invalidates the previously stored impedance data...
Definition: MTStation.cpp:144
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35