GPLIB++
mtura.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <numeric>
5 #include "Util.h"
6 #include "TimeSeriesData.h"
7 #include "miscfunc.h"
8 #include "WFunc.h"
9 #include "statutils.h"
10 #include <boost/bind.hpp>
11 
12 using namespace std;
13 using namespace gplib;
14 
15 string version = "$Id: mtura.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
16 
17 /*!
18  * \addtogroup UtilProgs Utility Programs
19  *@{
20  * \file
21  * Calculate the a running average for each component of the input file.
22  */
23 
24 int main(int argc, char *argv[])
25  {
26  string infilename;
27  cout << "This is mtura: Calculate a running average of MT time series"
28  << endl << endl;
29  cout << " Usage: mtura infilename " << endl;
30  cout << " Ending '.ra' will be automatically assigned to outfilename"
31  << endl << endl;
32  cout << " This is Version: " << version << endl << endl;
33  if (argc == 2)
34  {
35  infilename = argv[1];
36  }
37  else
38  {
39 
40  infilename = AskFilename(" Mtu-Filename: ");
41  }
42 
43  TimeSeriesData Data;
44  Data.GetData(infilename);
45 
46  // get the width of the averaging window from the user
47  size_t windowlength;
48  cout << "Window length: ";
49  cin >> windowlength;
50 
51  //construct the window time series
52  const size_t tslength = Data.GetData().GetEx().GetData().size();
53  TimeSeriesComponent WindowTS;
54  // set the time series to zero
55  WindowTS.GetData().assign(tslength, 0.0);
56  const size_t windowstart = 0;
57  // put ones where we want non-zero values
58  fill_n(WindowTS.GetData().begin() + windowstart, windowlength, 1.0);
59  //and apply the window function
60  ApplyWindow(WindowTS.GetData().begin() + windowstart,
61  WindowTS.GetData().begin() + windowstart + windowlength,
62  WindowTS.GetData().begin() + windowstart, Hanning());
63  // normalize,
64  //this should be improved
65  transform(WindowTS.GetData().begin() + windowstart,
66  WindowTS.GetData().begin() + windowstart + windowlength,
67  WindowTS.GetData().begin() + windowstart, boost::bind(
68  multiplies<double> (), _1, 1. / double(windowlength)));
69 
70  // make sure all components have zero mean to avoid offsets after windowing
71  SubMean(Data.GetData().GetEx().GetData().begin(),
72  Data.GetData().GetEx().GetData().end());
73  SubMean(Data.GetData().GetEy().GetData().begin(),
74  Data.GetData().GetEy().GetData().end());
75  SubMean(Data.GetData().GetHx().GetData().begin(),
76  Data.GetData().GetHx().GetData().end());
77  SubMean(Data.GetData().GetHy().GetData().begin(),
78  Data.GetData().GetHy().GetData().end());
79  SubMean(Data.GetData().GetHz().GetData().begin(),
80  Data.GetData().GetHz().GetData().end());
81 
82  // convolve the averaging time series with all components
83  Convolve(Data.GetData().GetEx().GetData(), WindowTS.GetData(),
84  Data.GetData().GetEx().GetData());
85  Convolve(Data.GetData().GetEy().GetData(), WindowTS.GetData(),
86  Data.GetData().GetEy().GetData());
87  Convolve(Data.GetData().GetHx().GetData(), WindowTS.GetData(),
88  Data.GetData().GetHx().GetData());
89  Convolve(Data.GetData().GetHy().GetData(), WindowTS.GetData(),
90  Data.GetData().GetHy().GetData());
91  Convolve(Data.GetData().GetHz().GetData(), WindowTS.GetData(),
92  Data.GetData().GetHz().GetData());
93 
94  //correct for the shift introduced by the convolution
95  rotate(Data.GetData().GetEx().GetData().begin(),
96  Data.GetData().GetEx().GetData().begin() + windowlength / 2,
97  Data.GetData().GetEx().GetData().end());
98  rotate(Data.GetData().GetEy().GetData().begin(),
99  Data.GetData().GetEy().GetData().begin() + windowlength / 2,
100  Data.GetData().GetEy().GetData().end());
101  rotate(Data.GetData().GetHx().GetData().begin(),
102  Data.GetData().GetHx().GetData().begin() + windowlength / 2,
103  Data.GetData().GetHx().GetData().end());
104  rotate(Data.GetData().GetHy().GetData().begin(),
105  Data.GetData().GetHy().GetData().begin() + windowlength / 2,
106  Data.GetData().GetHy().GetData().end());
107  rotate(Data.GetData().GetHz().GetData().begin(),
108  Data.GetData().GetHz().GetData().begin() + windowlength / 2,
109  Data.GetData().GetHz().GetData().end());
110  Data.WriteBack(infilename + ".ra");
111  }
112 /*@}*/
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
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
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
TimeSeriesComponent is the base storage class for all types of time series data.
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
int main()
Definition: angleavg.cpp:12
string version
Definition: mtura.cpp:15
void WriteBack(std::string filename_base)
Write in the format it was originally read in.
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
This functor returns the weighting factor for the Hanning window, given the relative position (0...
Definition: WFunc.h:31
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHz()
Definition: TimeSeries.h:43
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35