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
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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());
00042 ofstream outfile(filename.c_str());
00043 std::cout << " Working on file: " << filename << std::endl;
00044 boost::progress_display show_progress(StationList.GetList().size());
00045
00046 for (unsigned int i = 0; i < StationList.GetList().size(); ++i)
00047 {
00048 outfile << setw(12) << setfill(' ') << setprecision(4)
00049 << StationList.at(i).GetLongitude() << " ";
00050 outfile << setw(12) << setfill(' ') << setprecision(4)
00051 << StationList.at(i).GetLatitude() << " ";
00052 outfile << setw(12) << setfill(' ') << setprecision(4) << adaptor(
00053 boost::bind(f, &StationList.at(i).GetMTData().at(
00054 realindices.at(i).at(freqindex)))());
00055
00056 if (ntestcases > 2)
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);
00063 outfile << " " << setw(12) << setfill(' ') << setprecision(4)
00064 << JackErr;
00065 }
00066 ++show_progress;
00067 outfile << endl;
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;
00077 MTStationList MTSites;
00078 cout
00079 << " This is phasetensormap, calculate phasetensor elipses etc. from MT data"
00080 << endl;
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;
00090 double scalefactor = 10;
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);
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;
00121 return -1;
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
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