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
00019
00020
00021
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
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);
00038
00039 for (unsigned int i = 0; i < nstations; ++i)
00040 {
00041 string outfilename = StationList.at(i).GetName() + compname + ".dat";
00042 ofstream outfile(outfilename.c_str());
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
00049
00050 outfile << setw(12) << setfill(' ') << setprecision(4)
00051 << boost::bind(f, &StationList.at(i).GetMTData().at(j))();
00052
00053 if (ntestcases > 2)
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);
00071 outfile << " " << setw(12) << setfill(' ') << setprecision(4)
00072 << sqrt(JackVar);
00073 }
00074 outfile << endl;
00075 }
00076 ++show_progress;
00077
00078 }
00079 std::cout << std::endl << std::endl;
00080 }
00081
00082 int main()
00083 {
00084 string infilename, outfilename;
00085 MTStationList MTSites;
00086
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
00106 MTSites.GetData(infilename);
00107
00108 int nbootsamples = -1;
00109
00110 cout << "How many samples for Jacknife calculation: ";
00111 cin >> nbootsamples;
00112 double errorfloor = 0.0;
00113 cout << "Relative errorfloor: ";
00114 cin >> errorfloor;
00115
00116
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
00136
00137 for (vector<MTStation>::iterator it = MTSites.GetList().begin(); it
00138 != MTSites.GetList().end(); ++it)
00139 {
00140
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
00167
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
00173 ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.
00174 / it->GetMTData().at(i).GetFrequency();
00175
00176 ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.0;
00177
00178
00179 ptfile << setw(12) << setfill(' ') << setprecision(4)
00180 << it->GetMTData().at(i).GetBeta_phi() * 180. / PI;
00181
00182 ptfile << setw(12) << setfill(' ') << setprecision(4) << 90
00183 - (it->GetMTData().at(i).GetPhiStrike()) * 180. / PI;
00184
00185 ptfile << setw(12) << setfill(' ') << setprecision(4)
00186 << it->GetMTData().at(i).GetPhiMax();
00187
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;
00197 return -1;
00198 }
00199 }
00200
00201