GPLIB++
mtutimefrequency.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <vector>
5 #include "TimeFrequency.h"
6 #include "WFunc.h"
7 #include "TimeSeriesData.h"
8 #include "netcdfcpp.h"
9 #include "VecMat.h"
10 #include "Util.h"
11 
12 using namespace std;
13 using namespace gplib;
14 
15 string version =
16  "$Id: mtutimefrequency.cpp 1850 2010-05-18 09:13:13Z mmoorkamp $";
17 
18 /*!
19  * \addtogroup UtilProgs Utility Programs
20  *@{
21  * \file
22  * Calculate a time-frequency diagram for each component of a MT time-series file.
23  * Output is 4 netcdf file where the ending signals the component.
24  */
25 
26 void CalcTfAndWrite(ttsdata Data, const double samplerate, const int seglength,
27  const int timeavg, const string name)
28  {
29 
30  gplib::cmat result = TimeFrequency(Data.begin(), Data.end(), seglength,
31  Steep());
32  const size_t ylength = result.size2() - timeavg;
33  NcFile combrescdf(name.c_str(), NcFile::Replace);
34  NcDim* xd = combrescdf.add_dim("x", result.size1());
35  NcDim* yd = combrescdf.add_dim("y", ylength);
36  NcVar* x = combrescdf.add_var("x", ncFloat, xd);
37  NcVar* y = combrescdf.add_var("y", ncFloat, yd);
38  NcVar* z = combrescdf.add_var("z", ncFloat, xd, yd);
39  float *xvals = new float[xd->size()];
40  float *yvals = new float[yd->size()];
41  float *zvals = new float[xd->size() * yd->size()];
42  float avg = 0.0;
43  for (size_t i = 0; i < result.size1(); ++i) // time index
44  {
45  xvals[i] = i;
46 
47  for (size_t j = 0; j < ylength; ++j) //frequency index
48  {
49  avg = 0.0;
50  for (int k = 0; k < timeavg; ++k)
51  {
52  avg += abs(result(i, j + k));
53  }
54  zvals[i * ylength + j] = log10(avg);
55  }
56 
57  }
58  for (size_t j = 0; j < ylength; ++j)
59  yvals[j] = j * samplerate / seglength;
60  x->put(xvals, xd->size());
61  y->put(yvals, yd->size());
62  z->put(zvals, z->edges());
63  }
64 
65 int main(int argc, char *argv[])
66  {
67  TimeSeriesData TsData;
68 
69  string infilename;
70  size_t timeavg = 5;
71  size_t seglength = 2400;
72  cout
73  << "This is mtutf: Calculate time frequency matrix from Phoenix time series"
74  << endl << endl;
75  cout << " Usage: mtutf filename" << endl;
76  cout << "if no filename given, the program will ask for one" << endl;
77  cout
78  << " Output will be 4 netcdf files with ending _tfex.nc, _tfey.nc etc."
79  << endl << endl;
80  cout << " This is Version: " << version << endl << endl;
81 
82  if (argc == 2)
83  {
84  infilename = argv[1];
85  }
86  else
87  {
88  cout << "Infilename: ";
89  cin >> infilename;
90  }
91  TsData.GetData(infilename);
92  cout << "Length of individual segments in points: ";
93  cin >> seglength;
94  cout << "Number of segments for time averaging: ";
95  cin >> timeavg;
96  const double samplerate = TsData.GetData().GetSamplerate();
97  CalcTfAndWrite(TsData.GetData().GetEx().GetData(), samplerate, seglength,
98  timeavg, (infilename + "_tfex.nc"));
99  CalcTfAndWrite(TsData.GetData().GetEy().GetData(), samplerate, seglength,
100  timeavg, (infilename + "_tfey.nc"));
101  CalcTfAndWrite(TsData.GetData().GetHx().GetData(), samplerate, seglength,
102  timeavg, (infilename + "_tfhx.nc"));
103  CalcTfAndWrite(TsData.GetData().GetHy().GetData(), samplerate, seglength,
104  timeavg, (infilename + "_tfhy.nc"));
105 
106  }
107 /*@}*/
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 CalcTfAndWrite(ttsdata Data, const double samplerate, const int seglength, const int timeavg, const string name)
This functor rises steeply at the edges and then has a wide range where it is unity.
Definition: WFunc.h:51
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
double GetSamplerate()
The samplerate is stored in each component, we just return the samplerate of Hx assuming they are all...
Definition: TimeSeries.h:71
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
string version
int main()
Definition: angleavg.cpp:12
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35