phasetensormap.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 "Util.h"
00011 #include <sstream>
00012 #include <boost/function.hpp>
00013 #include <boost/bind.hpp>
00014 #include <iterator>
00015 #include <boost/progress.hpp>
00016 #include <boost/program_options.hpp>
00017 
00018 using namespace std;
00019 using namespace gplib;
00020 
00021 string version =
00022     "$Id: phasetensormap.cpp 1838 2010-03-04 16:19:34Z mmoorkamp $";
00023 
00024 //! Print out one component of MT data from a list of Stations for plotting with GMT
00025 
00026 /*! Print Component prints one component of MT Data e.g. phase, strike angle, for all sites into a file with
00027  * name file name. Parameters are
00028  * @param filename  name of the file to print to
00029  * @param f Pointer to member function of MTTensor that will print out the desired component
00030  * @param StationList  Object holding all the station data
00031  * @param freqindex  the index of the frequency to print, this will be synchronized through the ComFreqIndices array
00032  * @param adaptor  pointer to a function that maps the result of f to a double value
00033  * @param ntestcases how many samples needed for bootstrap error calculation
00034  * @param CalcError  is ErrorCalculation wanted
00035  */
00036 template<typename T> void PrintComponent(const std::string &filename,
00037     boost::function<const T(const MTTensor*)> f, MTStationList &StationList,
00038     const int freqindex, boost::function<double(T)> adaptor, int ntestcases = 0)
00039   {
00040     const std::vector<tindexvector>
00041         realindices(StationList.GetComFreqIndices()); //store the indices of the desired frequency for each station 
00042     ofstream outfile(filename.c_str()); //init output
00043     std::cout << " Working on file: " << filename << std::endl; // feedback for the user
00044     boost::progress_display show_progress(StationList.GetList().size()); // init progress bar
00045 
00046     for (unsigned int i = 0; i < StationList.GetList().size(); ++i) //for all sites
00047       {
00048         outfile << setw(12) << setfill(' ') << setprecision(4)
00049             << StationList.at(i).GetLongitude() << " "; //print latitude
00050         outfile << setw(12) << setfill(' ') << setprecision(4)
00051             << StationList.at(i).GetLatitude() << " "; //print longitude
00052         outfile << setw(12) << setfill(' ') << setprecision(4) << adaptor(
00053             boost::bind(f, &StationList.at(i).GetMTData().at(
00054                 realindices.at(i).at(freqindex)))());// bind the MTTensor object with the correct frequency index
00055         // to the member function that gives the chosen component and pass it through the adaptor function
00056         if (ntestcases > 2) // if we want Errors and we have enough samples
00057           {
00058             double JackMean, JackErr;
00059             MTSampleGenerator Generator(f, StationList.at(i).GetMTData().at(
00060                 realindices.at(i).at(freqindex)));
00061             Jacknife<MTSampleGenerator> ErrEst(ntestcases, Generator);
00062             ErrEst.CalcErrors(JackMean, JackErr); //that calculates the final value from raw data
00063             outfile << " " << setw(12) << setfill(' ') << setprecision(4)
00064                 << JackErr; // output the error
00065           }
00066         ++show_progress; // make sure progress bar progresses
00067         outfile << endl; // and start a new line for the next site
00068       }
00069     std::cout << std::endl << std::endl;
00070   }
00071 
00072 namespace po = boost::program_options;
00073 
00074 int main(int argc, char *argv[])
00075   {
00076     string infilename, outfilename; //storing names for input and root of output
00077     MTStationList MTSites; // object for the station data
00078     cout
00079         << " This is phasetensormap, calculate phasetensor elipses etc. from MT data"
00080         << endl; // write some info
00081     cout << " Reads in a list of files and outputs files for plotting with gmt"
00082         << endl;
00083     cout << " File endings will be automatically appended." << endl;
00084     cout
00085         << " For error calculation, numbers < 2 will be interpreted as no error calculation."
00086         << endl;
00087     cout << " This is Version: " << version << endl << endl;
00088 
00089     int nbootsamples = -1; //by default no bootstrap errors
00090     double scalefactor = 10; // the default scaling for phase tensor ellipses
00091 
00092     po::options_description desc("General options");
00093     desc.add_options()("help", "produce help message")("calcerr",
00094         po::value<int>(&nbootsamples)->default_value(-1),
00095         "Calculate error estimates")("scalefactor", po::value<double>(
00096         &scalefactor)->default_value(10.0),
00097         "Scalefactor for phase tensor ellipse size in plots.");
00098 
00099     po::variables_map vm;
00100     po::store(po::parse_command_line(argc, argv, desc), vm);
00101     po::notify(vm);
00102 
00103     if (vm.count("help"))
00104       {
00105         std::cout << desc << "\n";
00106         return 1;
00107       }
00108 
00109     infilename = AskFilename("File with station names: ");
00110     try
00111       {
00112         MTSites.GetData(infilename); //try to read in all station data
00113         if (MTSites.GetCommonFrequencies().size() == 0)
00114           {
00115             cerr << "No common frequencies found !";
00116             return 100;
00117           }
00118       } catch (FatalException &e)
00119       {
00120         cerr << e.what() << endl; // if something fails print error
00121         return -1; // and stop execution
00122       }
00123     outfilename = AskFilename("Output Filename Root: ");
00124 
00125     cout << "Available Periods: " << MTSites.GetCommonFrequencies().size()
00126         << endl;
00127 
00128     for (trealdata::const_iterator frequencies =
00129         MTSites.GetCommonFrequencies().begin(); frequencies
00130         != MTSites.GetCommonFrequencies().end(); ++frequencies)
00131       {
00132         cout << setw(9) << setfill(' ') << setprecision(4) << distance(
00133             MTSites.GetCommonFrequencies().begin(), frequencies);
00134         cout << setw(15) << setfill(' ') << setprecision(8) << 1.
00135             / (*frequencies) << endl;
00136       }
00137     int freqindex;
00138     cout << "Select frequency Index: ";
00139     cin >> freqindex;
00140 
00141     ofstream outfile((outfilename + ".ptensor").c_str());
00142     for (vector<MTStation>::iterator it = MTSites.GetList().begin(); it
00143         != MTSites.GetList().end(); ++it)
00144       {
00145         int realfreqindex = MTSites.GetComFreqIndices().at(distance(
00146             MTSites.GetList().begin(), it)).at(freqindex);
00147         double rotangle = (it->GetMTData().at(realfreqindex).GetPhiStrike())
00148             * 180. / PI;
00149 
00150         //cout << it->GetName() << " " << 1./it->GetFrequencies().at(realfreqindex) << " " << it->GetMTData().at(realfreqindex).GetZxx() <<  endl;
00151         outfile << setw(12) << setfill(' ') << setprecision(4)
00152             << it->GetLongitude();
00153         outfile << setw(12) << setfill(' ') << setprecision(4)
00154             << it->GetLatitude();
00155         outfile << setw(12) << setfill(' ') << setprecision(4)
00156             << it->GetMTData().at(realfreqindex).GetBeta_phi() * 180. / PI;
00157         outfile << setw(12) << setfill(' ') << setprecision(4) << 90 - rotangle;
00158         outfile << setw(12) << setfill(' ') << setprecision(4)
00159             << it->GetMTData().at(realfreqindex).GetPhiMax() * scalefactor;
00160         outfile << setw(12) << setfill(' ') << setprecision(4)
00161             << it->GetMTData().at(realfreqindex).GetPhiMin() * scalefactor
00162             << endl;
00163 
00164       }
00165     outfile.close();
00166 
00167     PrintComponent<double> ((outfilename + ".ptskew"), &MTTensor::GetBeta_phi,
00168         MTSites, freqindex, gplib::absangle(), nbootsamples);
00169     PrintComponent<double> ((outfilename + ".ptstrike"),
00170         &MTTensor::GetPhiStrike, MTSites, freqindex, boost::bind(multiplies<
00171             double> (), _1, -180. / PI), nbootsamples);
00172     PrintComponent<double> ((outfilename + ".bstrike"), &MTTensor::GetAlpha,
00173         MTSites, freqindex, boost::bind(multiplies<double> (), _1, 180. / PI),
00174         nbootsamples);
00175     PrintComponent<double> ((outfilename + ".ptellip"), &MTTensor::GetPhiEllip,
00176         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00177     PrintComponent<double> ((outfilename + ".ptmin"), &MTTensor::GetPhiMin,
00178         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00179     PrintComponent<double> ((outfilename + ".ptmax"), &MTTensor::GetPhiMax,
00180         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00181     PrintComponent<double> ((outfilename + ".ptphi1"), &MTTensor::GetPhi1,
00182         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00183     PrintComponent<double> ((outfilename + ".ptphi2"), &MTTensor::GetPhi2,
00184         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00185     PrintComponent<double> ((outfilename + ".ptphi3"), &MTTensor::GetPhi3,
00186         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00187     PrintComponent<double> ((outfilename + ".ptphi11"), &MTTensor::GetPhi11,
00188         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00189     PrintComponent<double> ((outfilename + ".ptphi12"), &MTTensor::GetPhi12,
00190         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00191     PrintComponent<double> ((outfilename + ".ptphi21"), &MTTensor::GetPhi21,
00192         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00193     PrintComponent<double> ((outfilename + ".ptphi22"), &MTTensor::GetPhi22,
00194         MTSites, freqindex, gplib::nomod<double>, nbootsamples);
00195     return (0);
00196   }
00197 

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