mtutimefrequency.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <fstream>
00004 #include <vector>
00005 #include "TimeFrequency.h"
00006 #include "WFunc.h"
00007 #include "TimeSeriesData.h"
00008 #include "netcdfcpp.h"
00009 #include "VecMat.h"
00010 #include "Util.h"
00011 using namespace std;
00012 string version = "$Id: mtutimefrequency.cpp 1704 2008-06-02 14:50:05Z mmoorkamp $";
00013 
00014 /*!
00015  * \addtogroup UtilProgs Utility Programs
00016  *@{
00017  * \file
00018  * Calculate a time-frequency diagram for each component of a MT time-series file.
00019  * Output is 4 netcdf file where the ending signals the component.
00020  */
00021 
00022 void CalcTfAndWrite(ttsdata Data, const double samplerate, const int seglength, const int timeavg, const string name)
00023 {
00024         
00025         gplib::cmat result = TimeFrequency(Data.begin(), Data.end(), seglength, Steep());
00026         const size_t ylength = result.size2() - timeavg;
00027         NcFile combrescdf(name.c_str(),NcFile::Replace);
00028         NcDim* xd = combrescdf.add_dim("x",result.size1());
00029         NcDim* yd = combrescdf.add_dim("y",ylength);
00030         NcVar* x = combrescdf.add_var("x",ncFloat,xd);
00031         NcVar* y = combrescdf.add_var("y",ncFloat,yd);
00032         NcVar* z = combrescdf.add_var("z",ncFloat,xd,yd);
00033         float *xvals = new float[xd->size()];
00034         float *yvals = new float[yd->size()];
00035         float *zvals = new float[xd->size()*yd->size()];
00036         float avg = 0.0;
00037         for (int i = 0; i < result.size1(); ++i) // time index
00038         {
00039                 xvals[i] = i;
00040                 
00041                 for (int j = 0; j < ylength; ++j) //frequency index
00042                 {
00043                         avg = 0.0;
00044                         for (int k = 0; k < timeavg; ++k)
00045                           {
00046                                 avg += abs(result(i,j+k));
00047                           }
00048                         zvals[i*ylength +j] = log10(avg);
00049                 }
00050                 
00051         }
00052         for (int j = 0; j < ylength; ++j)
00053                 yvals[j] = j * samplerate/seglength;
00054         x->put(xvals,xd->size());
00055         y->put(yvals,yd->size());
00056         z->put(zvals,z->edges());
00057 }
00058 
00059 int main(int argc, char *argv[])
00060 {
00061         TimeSeriesData TsData;
00062         
00063         string infilename;
00064         size_t timeavg = 5;
00065         size_t seglength = 2400;
00066         cout << "This is mtutf: Calculate time frequency matrix from  Phoenix time series" << endl<< endl;
00067         cout  << " Usage:      mtutf filename" << endl;     
00068         cout  << "if no filename given, the program will ask for one" << endl;
00069         cout << " Output will be 4 netcdf files with ending _tfex.nc, _tfey.nc etc." << endl<< endl;
00070         cout << " This is Version: " << version << endl << endl;
00071         
00072         if (argc == 2)
00073         {
00074                 infilename = argv[1];
00075         }
00076         else
00077         {
00078                 cout << "Infilename: ";
00079                 cin >> infilename;
00080         }
00081         TsData.GetData(infilename);
00082         cout << "Length of individual segments in points: ";
00083         cin >> seglength;
00084         cout <<  "Number of segments for time averaging: ";
00085         cin >> timeavg;
00086         const double samplerate = TsData.GetData().GetSamplerate();
00087         CalcTfAndWrite(TsData.GetData().GetEx().GetData(),samplerate,seglength,timeavg,(infilename+"_tfex.nc"));
00088         CalcTfAndWrite(TsData.GetData().GetEy().GetData(),samplerate,seglength,timeavg,(infilename+"_tfey.nc"));
00089         CalcTfAndWrite(TsData.GetData().GetHx().GetData(),samplerate,seglength,timeavg,(infilename+"_tfhx.nc"));
00090         CalcTfAndWrite(TsData.GetData().GetHy().GetData(),samplerate,seglength,timeavg,(infilename+"_tfhy.nc"));
00091         
00092 }
00093 /*@}*/

Generated on Fri Jul 4 15:30:21 2008 for GPLIB++ by  doxygen 1.5.5