ptfreq.cpp

Go to the documentation of this file.
00001 #include <fstream>
00002 #include <iostream>
00003 #include <vector>
00004 #include <iomanip>
00005 #include "MTStationList.h"
00006 #include "FatalException.h"
00007 #include "Adaptors.h"
00008 #include "Jacknife.h"
00009 #include "MTSampleGenerator.h"
00010 #include <sstream>
00011 #include <boost/function.hpp>
00012 #include <boost/bind.hpp>
00013 #include <iterator>
00014 #include "Util.h"
00015 #include <boost/progress.hpp>
00016 
00017 /*!
00018  * \addtogroup UtilProgs Utility Programs
00019  *@{
00020  * \file
00021  * Calculate phase tensor components and errors from MT impedance data
00022  */
00023 
00024 using namespace gplib;
00025 using namespace std;
00026 string version = "$Id: ptfreq.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00027 
00028 //! This helper function prints out a single phase tensor component together with error information to a file
00029 template<typename T> void PrintComponent(const std::string &compname,
00030     boost::function<const T(const MTTensor*)> f, MTStationList &StationList,
00031     int ntestcases = 0, double errorfloor = 0.02)
00032   {
00033 
00034     const unsigned int nstations = StationList.GetList().size();
00035 
00036     cout << "Processing component: " << compname << endl << endl;
00037     boost::progress_display show_progress(nstations); // init progress bar
00038 
00039     for (unsigned int i = 0; i < nstations; ++i) //for all sites
00040       {
00041         string outfilename = StationList.at(i).GetName() + compname + ".dat";
00042         ofstream outfile(outfilename.c_str()); //init output
00043         const unsigned int nfreq = StationList.at(i).GetMTData().size();
00044         for (unsigned int j = 0; j < nfreq; ++j)
00045           {
00046             outfile << setw(12) << setfill(' ') << setprecision(4) << 1.
00047                 / StationList.at(i).GetMTData().at(j).GetFrequency();
00048             // bind the MTTensor object with the correct frequency index
00049             // to the member function that gives the chosen component
00050             outfile << setw(12) << setfill(' ') << setprecision(4)
00051                 << boost::bind(f, &StationList.at(i).GetMTData().at(j))();
00052 
00053             if (ntestcases > 2) // if we want Errors and we have enough samples
00054               {
00055                 double JackMean, JackVar;
00056                 StationList.at(i).SetMTData().at(j).SetdZxx() = max(
00057                     StationList.at(i).at(j).GetdZxx(), errorfloor * abs(
00058                         StationList.at(i).at(j).GetZxx()));
00059                 StationList.at(i).SetMTData().at(j).SetdZxy() = max(
00060                     StationList.at(i).at(j).GetdZxy(), errorfloor * abs(
00061                         StationList.at(i).at(j).GetZxy()));
00062                 StationList.at(i).SetMTData().at(j).SetdZyx() = max(
00063                     StationList.at(i).at(j).GetdZyx(), errorfloor * abs(
00064                         StationList.at(i).at(j).GetZyx()));
00065                 StationList.at(i).SetMTData().at(j).SetdZyy() = max(
00066                     StationList.at(i).at(j).GetdZyy(), errorfloor * abs(
00067                         StationList.at(i).at(j).GetZyy()));
00068                 MTSampleGenerator Generator(f, StationList.at(i).at(j));
00069                 Jacknife<MTSampleGenerator> ErrEst(ntestcases, Generator);
00070                 ErrEst.CalcErrors(JackMean, JackVar); //that calculates the final value from raw data
00071                 outfile << " " << setw(12) << setfill(' ') << setprecision(4)
00072                     << sqrt(JackVar); // output the error
00073               }
00074             outfile << endl; // and start a new line for the next frequency
00075           }
00076         ++show_progress; // make sure progress bar progresses
00077 
00078       }
00079     std::cout << std::endl << std::endl;
00080   }
00081 
00082 int main()
00083   {
00084     string infilename, outfilename; //storing names for input and root of output
00085     MTStationList MTSites; // object for the station data
00086     // write some info
00087     cout << " This is ptfreq, calculate phasetensor elipses etc. from MT data"
00088         << endl;
00089     cout
00090         << " Reads in a list of stations and outputs one file for each station"
00091         << endl;
00092     cout
00093         << " Each file contains the frequency dependency of a component of the phase tensor for one site"
00094         << endl;
00095     cout << " File endings will be automatically appended." << endl;
00096     cout
00097         << " For error calculation, numbers < 2 will be interpreted as no error calculation."
00098         << endl;
00099     cout << " This is Version: " << version << endl << endl;
00100 
00101     infilename = AskFilename("File with station names: ");
00102 
00103     try
00104       {
00105         //try to read in all station data
00106         MTSites.GetData(infilename);
00107         //by default no bootstrap errors
00108         int nbootsamples = -1;
00109 
00110         cout << "How many samples for Jacknife calculation: "; //ask for number of samples
00111         cin >> nbootsamples;
00112         double errorfloor = 0.0;
00113         cout << "Relative errorfloor: ";
00114         cin >> errorfloor;
00115         //calculate and print various components of the phase tensor
00116         //these files contain a single phase tensor quantities and its error as a function of period
00117         PrintComponent<double> ("phi11", &MTTensor::GetPhi11, MTSites,
00118             nbootsamples, errorfloor);
00119         PrintComponent<double> ("phi12", &MTTensor::GetPhi12, MTSites,
00120             nbootsamples, errorfloor);
00121         PrintComponent<double> ("phi21", &MTTensor::GetPhi21, MTSites,
00122             nbootsamples, errorfloor);
00123         PrintComponent<double> ("phi22", &MTTensor::GetPhi22, MTSites,
00124             nbootsamples, errorfloor);
00125         PrintComponent<double> ("phimax", &MTTensor::GetPhiMax, MTSites,
00126             nbootsamples, errorfloor);
00127         PrintComponent<double> ("phimin", &MTTensor::GetPhiMin, MTSites,
00128             nbootsamples, errorfloor);
00129         PrintComponent<double> ("phiellip", &MTTensor::GetPhiEllip, MTSites,
00130             nbootsamples, errorfloor);
00131         PrintComponent<double> ("phialpha", &MTTensor::GetAlpha_phi, MTSites,
00132                     nbootsamples, errorfloor);
00133         PrintComponent<double> ("phibeta", &MTTensor::GetBeta_phi, MTSites,
00134             nbootsamples, errorfloor);
00135         //now we also generate some files that contain various components
00136         //we do this for all files in the list
00137         for (vector<MTStation>::iterator it = MTSites.GetList().begin(); it
00138             != MTSites.GetList().end(); ++it)
00139           {
00140             //the first file simply contains all relevant quantities as a quick overview
00141             string rotfilename = it->GetName() + "_phiall.dat";
00142             ofstream rotfile(rotfilename.c_str());
00143             for (unsigned int i = 0; i < it->GetFrequencies().size(); ++i)
00144               {
00145                 rotfile << setw(12) << setfill(' ') << setprecision(4) << 1.
00146                     / it->GetFrequencies().at(i);
00147                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00148                     << it->GetMTData().at(i).GetPhi11();
00149                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00150                     << it->GetMTData().at(i).GetPhi12();
00151                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00152                     << it->GetMTData().at(i).GetPhi21();
00153                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00154                     << it->GetMTData().at(i).GetPhi22();
00155                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00156                     << it->GetMTData().at(i).GetAlpha_phi();
00157                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00158                     << it->GetMTData().at(i).GetBeta_phi();
00159                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00160                     << it->GetMTData().at(i).GetPhiMax();
00161                 rotfile << setw(12) << setfill(' ') << setprecision(4)
00162                     << it->GetMTData().at(i).GetPhiMin() << endl;
00163 
00164               }
00165             rotfile.close();
00166             //this file is for plotting ellipses with gmt
00167             //we want one ellipse per period
00168             string ptfilename = it->GetName() + "_ptellip.dat";
00169             ofstream ptfile(ptfilename.c_str());
00170             for (unsigned int i = 0; i < it->GetMTData().size(); ++i)
00171               {
00172                 //the x value is the period
00173                 ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.
00174                     / it->GetMTData().at(i).GetFrequency();
00175                 //all ellipses should be at the same height, i.e. y value
00176                 ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.0;
00177                 // the gmt documentation is not clear, but we need the color before the directions etc.
00178                 //we color the ellipses by the skew
00179                 ptfile << setw(12) << setfill(' ') << setprecision(4)
00180                     << it->GetMTData().at(i).GetBeta_phi() * 180. / PI;
00181                 //GMT wants the angle counter clockwise from horizontal
00182                 ptfile << setw(12) << setfill(' ') << setprecision(4) << 90
00183                     - (it->GetMTData().at(i).GetPhiStrike()) * 180. / PI;
00184                 // and then the major axis
00185                 ptfile << setw(12) << setfill(' ') << setprecision(4)
00186                     << it->GetMTData().at(i).GetPhiMax();
00187                 // and the minor axis of the ellipse
00188                 ptfile << setw(12) << setfill(' ') << setprecision(4)
00189                     << it->GetMTData().at(i).GetPhiMin();
00190                 ptfile << endl;
00191               }
00192             ptfile.close();
00193           }
00194       } catch (FatalException &e)
00195       {
00196         cerr << e.what() << endl; // if something fails print error
00197         return -1; // and stop execution
00198       }
00199   }
00200 /*@}*/
00201 

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8