GPLIB++
phasetensormap.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <vector>
4 #include <iomanip>
5 #include "MTStationList.h"
6 #include "FatalException.h"
7 #include "Adaptors.h"
8 #include "Jacknife.h"
9 #include "MTSampleGenerator.h"
10 #include "Util.h"
11 #include <sstream>
12 #include <boost/function.hpp>
13 #include <boost/bind.hpp>
14 #include <iterator>
15 #include <boost/progress.hpp>
16 #include <boost/program_options.hpp>
17 
18 using namespace std;
19 using namespace gplib;
20 
21 string version =
22  "$Id: phasetensormap.cpp 1838 2010-03-04 16:19:34Z mmoorkamp $";
23 
24 //! Print out one component of MT data from a list of Stations for plotting with GMT
25 
26 /*! Print Component prints one component of MT Data e.g. phase, strike angle, for all sites into a file with
27  * name file name. Parameters are
28  * @param filename name of the file to print to
29  * @param f Pointer to member function of MTTensor that will print out the desired component
30  * @param StationList Object holding all the station data
31  * @param freqindex the index of the frequency to print, this will be synchronized through the ComFreqIndices array
32  * @param adaptor pointer to a function that maps the result of f to a double value
33  * @param ntestcases how many samples needed for bootstrap error calculation
34  * @param CalcError is ErrorCalculation wanted
35  */
36 template<typename T> void PrintComponent(const std::string &filename,
37  boost::function<const T(const MTTensor*)> f, MTStationList &StationList,
38  const int freqindex, boost::function<double(T)> adaptor, int ntestcases = 0)
39  {
40  const std::vector<tindexvector>
41  realindices(StationList.GetComFreqIndices()); //store the indices of the desired frequency for each station
42  ofstream outfile(filename.c_str()); //init output
43  std::cout << " Working on file: " << filename << std::endl; // feedback for the user
44  boost::progress_display show_progress(StationList.GetList().size()); // init progress bar
45 
46  for (unsigned int i = 0; i < StationList.GetList().size(); ++i) //for all sites
47  {
48  outfile << setw(12) << setfill(' ') << setprecision(4)
49  << StationList.at(i).GetLongitude() << " "; //print latitude
50  outfile << setw(12) << setfill(' ') << setprecision(4)
51  << StationList.at(i).GetLatitude() << " "; //print longitude
52  outfile << setw(12) << setfill(' ') << setprecision(4) << adaptor(
53  boost::bind(f, &StationList.at(i).GetMTData().at(
54  realindices.at(i).at(freqindex)))());// bind the MTTensor object with the correct frequency index
55  // to the member function that gives the chosen component and pass it through the adaptor function
56  if (ntestcases > 2) // if we want Errors and we have enough samples
57  {
58  double JackMean, JackErr;
59  MTSampleGenerator Generator(f, StationList.at(i).GetMTData().at(
60  realindices.at(i).at(freqindex)));
61  Jacknife<MTSampleGenerator> ErrEst(ntestcases, Generator);
62  ErrEst.CalcErrors(JackMean, JackErr); //that calculates the final value from raw data
63  outfile << " " << setw(12) << setfill(' ') << setprecision(4)
64  << JackErr; // output the error
65  }
66  ++show_progress; // make sure progress bar progresses
67  outfile << endl; // and start a new line for the next site
68  }
69  std::cout << std::endl << std::endl;
70  }
71 
72 namespace po = boost::program_options;
73 
74 int main(int argc, char *argv[])
75  {
76  string infilename, outfilename; //storing names for input and root of output
77  MTStationList MTSites; // object for the station data
78  cout
79  << " This is phasetensormap, calculate phasetensor elipses etc. from MT data"
80  << endl; // write some info
81  cout << " Reads in a list of files and outputs files for plotting with gmt"
82  << endl;
83  cout << " File endings will be automatically appended." << endl;
84  cout
85  << " For error calculation, numbers < 2 will be interpreted as no error calculation."
86  << endl;
87  cout << " This is Version: " << version << endl << endl;
88 
89  int nbootsamples = -1; //by default no bootstrap errors
90  double scalefactor = 10; // the default scaling for phase tensor ellipses
91 
92  po::options_description desc("General options");
93  desc.add_options()("help", "produce help message")("calcerr",
94  po::value<int>(&nbootsamples)->default_value(-1),
95  "Calculate error estimates")("scalefactor", po::value<double>(
96  &scalefactor)->default_value(10.0),
97  "Scalefactor for phase tensor ellipse size in plots.");
98 
99  po::variables_map vm;
100  po::store(po::parse_command_line(argc, argv, desc), vm);
101  po::notify(vm);
102 
103  if (vm.count("help"))
104  {
105  std::cout << desc << "\n";
106  return 1;
107  }
108 
109  infilename = AskFilename("File with station names: ");
110  try
111  {
112  MTSites.GetData(infilename); //try to read in all station data
113  if (MTSites.GetCommonFrequencies().size() == 0)
114  {
115  cerr << "No common frequencies found !";
116  return 100;
117  }
118  } catch (FatalException &e)
119  {
120  cerr << e.what() << endl; // if something fails print error
121  return -1; // and stop execution
122  }
123  outfilename = AskFilename("Output Filename Root: ");
124 
125  cout << "Available Periods: " << MTSites.GetCommonFrequencies().size()
126  << endl;
127 
128  for (trealdata::const_iterator frequencies =
129  MTSites.GetCommonFrequencies().begin(); frequencies
130  != MTSites.GetCommonFrequencies().end(); ++frequencies)
131  {
132  cout << setw(9) << setfill(' ') << setprecision(4) << distance(
133  MTSites.GetCommonFrequencies().begin(), frequencies);
134  cout << setw(15) << setfill(' ') << setprecision(8) << 1.
135  / (*frequencies) << endl;
136  }
137  int freqindex;
138  cout << "Select frequency Index: ";
139  cin >> freqindex;
140 
141  ofstream outfile((outfilename + ".ptensor").c_str());
142  for (vector<MTStation>::iterator it = MTSites.GetList().begin(); it
143  != MTSites.GetList().end(); ++it)
144  {
145  int realfreqindex = MTSites.GetComFreqIndices().at(distance(
146  MTSites.GetList().begin(), it)).at(freqindex);
147  double rotangle = (it->GetMTData().at(realfreqindex).GetPhiStrike())
148  * 180. / PI;
149 
150  //cout << it->GetName() << " " << 1./it->GetFrequencies().at(realfreqindex) << " " << it->GetMTData().at(realfreqindex).GetZxx() << endl;
151  outfile << setw(12) << setfill(' ') << setprecision(4)
152  << it->GetLongitude();
153  outfile << setw(12) << setfill(' ') << setprecision(4)
154  << it->GetLatitude();
155  outfile << setw(12) << setfill(' ') << setprecision(4)
156  << it->GetMTData().at(realfreqindex).GetBeta_phi() * 180. / PI;
157  outfile << setw(12) << setfill(' ') << setprecision(4) << 90 - rotangle;
158  outfile << setw(12) << setfill(' ') << setprecision(4)
159  << it->GetMTData().at(realfreqindex).GetPhiMax() * scalefactor;
160  outfile << setw(12) << setfill(' ') << setprecision(4)
161  << it->GetMTData().at(realfreqindex).GetPhiMin() * scalefactor
162  << endl;
163 
164  }
165  outfile.close();
166 
167  PrintComponent<double> ((outfilename + ".ptskew"), &MTTensor::GetBeta_phi,
168  MTSites, freqindex, gplib::absangle(), nbootsamples);
169  PrintComponent<double> ((outfilename + ".ptstrike"),
170  &MTTensor::GetPhiStrike, MTSites, freqindex, boost::bind(multiplies<
171  double> (), _1, -180. / PI), nbootsamples);
172  PrintComponent<double> ((outfilename + ".bstrike"), &MTTensor::GetAlpha,
173  MTSites, freqindex, boost::bind(multiplies<double> (), _1, 180. / PI),
174  nbootsamples);
175  PrintComponent<double> ((outfilename + ".ptellip"), &MTTensor::GetPhiEllip,
176  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
177  PrintComponent<double> ((outfilename + ".ptmin"), &MTTensor::GetPhiMin,
178  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
179  PrintComponent<double> ((outfilename + ".ptmax"), &MTTensor::GetPhiMax,
180  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
181  PrintComponent<double> ((outfilename + ".ptphi1"), &MTTensor::GetPhi1,
182  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
183  PrintComponent<double> ((outfilename + ".ptphi2"), &MTTensor::GetPhi2,
184  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
185  PrintComponent<double> ((outfilename + ".ptphi3"), &MTTensor::GetPhi3,
186  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
187  PrintComponent<double> ((outfilename + ".ptphi11"), &MTTensor::GetPhi11,
188  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
189  PrintComponent<double> ((outfilename + ".ptphi12"), &MTTensor::GetPhi12,
190  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
191  PrintComponent<double> ((outfilename + ".ptphi21"), &MTTensor::GetPhi21,
192  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
193  PrintComponent<double> ((outfilename + ".ptphi22"), &MTTensor::GetPhi22,
194  MTSites, freqindex, gplib::nomod<double>, nbootsamples);
195  return (0);
196  }
197 
double GetLatitude() const
access funtion for Latitude
Definition: MTStation.h:84
MTStation & at(int loc)
Get a reference to a site at a given index.
string version
void CalcErrors(double &m, double &v)
The main function, calculates the error, by generating samples and calling the derived function...
Definition: StatErrEst.h:44
void f(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
Definition: perftest.cpp:17
double GetPhi3(const double phi11, const double phi12, const double phi21, const double phi22)
Definition: ptfuncs.h:98
const trealdata & GetCommonFrequencies()
Get a vector with frequencies that are common to all sites.
Definition: MTStationList.h:47
MTStationList holds a number of MTSites, usually associated with a single project, line, etc.
Definition: MTStationList.h:16
Implements the Jacknifing method of error estimation.
Definition: Jacknife.h:20
Generate random elements of a calculated quantity for MT impedance data.
int main(int argc, char *argv[])
void PrintComponent(const std::string &filename, boost::function< const T(const MTTensor *)> f, MTStationList &StationList, const int freqindex, boost::function< double(T)> adaptor, int ntestcases=0)
Print out one component of MT data from a list of Stations for plotting with GMT. ...
tStationList & GetList()
Access to the complete vector of Stations.
Definition: MTStationList.h:31
const float T[frequenzen]
Definition: mtinvnet.cpp:18
const std::vector< tindexvector > & GetComFreqIndices()
Get a vector that for each site contains the indices to the common frequencies.
Definition: MTStationList.h:42
Stores MT-Tensor components at a single frequency, calculates derived quantities. ...
Definition: MTTensor.h:16
void GetData(const std::string filename)
Read a list of filenames and the associated data in those files to fill the list. ...
double GetPhiEllip(const double phi11, const double phi12, const double phi21, const double phi22)
Return the ellipticity of the phase tensor.
Definition: ptfuncs.h:104
const std::vector< MTTensor > & GetMTData() const
Get the full vector of Tensor elements read only.
Definition: MTStation.h:119
The basic exception class for all errors that arise in gplib.
double GetLongitude() const
Definition: MTStation.h:92