GPLIB++
ptfreq.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <fstream>
3 #include <iostream>
4 #include <vector>
5 #include <iomanip>
6 #include "MTStationList.h"
7 #include "FatalException.h"
8 #include "Adaptors.h"
9 #include "Jacknife.h"
10 #include "MTSampleGenerator.h"
11 #include <sstream>
12 #include <boost/function.hpp>
13 #include <boost/bind.hpp>
14 #include <boost/program_options.hpp>
15 #include <iterator>
16 #include <boost/progress.hpp>
17 
18 /*!
19  * \addtogroup UtilProgs Utility Programs
20  *@{
21  * \file
22  * Calculate phase tensor components and errors from MT impedance data
23  */
24 
25 using namespace gplib;
26 using namespace std;
27 namespace po = boost::program_options;
28 
29 string version = "$Id: ptfreq.cpp 1851 2010-05-18 13:48:45Z mmoorkamp $";
30 
31 //! This helper function prints out a single phase tensor component together with error information to a file
32 template<typename T>
33 inline void PrintComponent(const std::string &compname,
34  boost::function<const T(const MTTensor*)> f, MTStationList &StationList,
35  int ntestcases = 0, double errorfloor = 0.02)
36  {
37 
38  const unsigned int nstations = StationList.GetList().size();
39 
40  cout << "Processing component: " << compname << endl << endl;
41  boost::progress_display show_progress(nstations); // init progress bar
42 
43  for (unsigned int i = 0; i < nstations; ++i) //for all sites
44  {
45  string outfilename = StationList.at(i).GetName() + compname + ".dat";
46  ofstream outfile(outfilename.c_str()); //init output
47  const unsigned int nfreq = StationList.at(i).GetMTData().size();
48  for (unsigned int j = 0; j < nfreq; ++j)
49  {
50  outfile << setw(12) << setfill(' ') << setprecision(4) << 1.
51  / StationList.at(i).GetMTData().at(j).GetFrequency();
52  // bind the MTTensor object with the correct frequency index
53  // to the member function that gives the chosen component
54  outfile << setw(12) << setfill(' ') << setprecision(4)
55  << boost::bind(f, &StationList.at(i).GetMTData().at(j))();
56 
57  if (ntestcases > 2) // if we want Errors and we have enough samples
58  {
59  double JackMean, JackVar;
60  StationList.at(i).SetMTData().at(j).SetdZxx() = max(
61  StationList.at(i).at(j).GetdZxx(), errorfloor * abs(
62  StationList.at(i).at(j).GetZxx()));
63  StationList.at(i).SetMTData().at(j).SetdZxy() = max(
64  StationList.at(i).at(j).GetdZxy(), errorfloor * abs(
65  StationList.at(i).at(j).GetZxy()));
66  StationList.at(i).SetMTData().at(j).SetdZyx() = max(
67  StationList.at(i).at(j).GetdZyx(), errorfloor * abs(
68  StationList.at(i).at(j).GetZyx()));
69  StationList.at(i).SetMTData().at(j).SetdZyy() = max(
70  StationList.at(i).at(j).GetdZyy(), errorfloor * abs(
71  StationList.at(i).at(j).GetZyy()));
72  MTSampleGenerator Generator(f, StationList.at(i).at(j));
73  Jacknife<MTSampleGenerator> ErrEst(ntestcases, Generator);
74  ErrEst.CalcErrors(JackMean, JackVar); //that calculates the final value from raw data
75  outfile << " " << setw(12) << setfill(' ') << setprecision(4)
76  << sqrt(JackVar); // output the error
77  }
78  outfile << endl; // and start a new line for the next frequency
79  }
80  ++show_progress; // make sure progress bar progresses
81 
82  }
83  std::cout << std::endl << std::endl;
84  }
85 
86 int main(int argc, char *argv[])
87  {
88  string infilename, outfilename; //storing names for input and root of output
89  MTStationList MTSites; // object for the station data
90  // write some info
91  cout << " This is ptfreq, calculate phasetensor elipses etc. from MT data"
92  << endl;
93  cout
94  << " Reads in a list of stations and outputs one file for each station"
95  << endl;
96  cout
97  << " Each file contains the frequency dependency of a component of the phase tensor for one site"
98  << endl;
99  cout << " File endings will be automatically appended." << endl;
100  cout
101  << " For error calculation, numbers < 2 will be interpreted as no error calculation."
102  << endl;
103  cout << " This is Version: " << version << endl << endl;
104 
105  int nbootsamples = -1;
106  double errorfloor = 0.0;
107  po::options_description desc("Allowed options");
108  desc.add_options()("help", "produce help message")("nboot", po::value<int>(
109  &nbootsamples)->default_value(10000),
110  "The number of samples for bootstrap error calculation")("errfloor",
111  po::value<double>(&errorfloor)->default_value(0.01),
112  "The minimum relative error to assume for the data")
113  ("input-file", po::value< string>(&infilename), "input file");
114 
115  po::positional_options_description p;
116  p.add("input-file", -1);
117 
118  po::variables_map vm;
119  po::store(po::command_line_parser(argc, argv).
120  options(desc).positional(p).run(), vm);
121  po::notify(vm);
122 
123  if (vm.count("help")) {
124  cout << desc << "\n";
125  return 1;
126  }
127 
128 
129  try
130  {
131  //try to read in all station data
132  MTSites.GetData(infilename);
133 
134  //calculate and print various components of the phase tensor
135  //these files contain a single phase tensor quantities and its error as a function of period
136  PrintComponent<double> ("phi11", &MTTensor::GetPhi11, MTSites,
137  nbootsamples, errorfloor);
138  PrintComponent<double> ("phi12", &MTTensor::GetPhi12, MTSites,
139  nbootsamples, errorfloor);
140  PrintComponent<double> ("phi21", &MTTensor::GetPhi21, MTSites,
141  nbootsamples, errorfloor);
142  PrintComponent<double> ("phi22", &MTTensor::GetPhi22, MTSites,
143  nbootsamples, errorfloor);
144  PrintComponent<double> ("phimax", &MTTensor::GetPhiMax, MTSites,
145  nbootsamples, errorfloor);
146  PrintComponent<double> ("phimin", &MTTensor::GetPhiMin, MTSites,
147  nbootsamples, errorfloor);
148  PrintComponent<double> ("phiellip", &MTTensor::GetPhiEllip, MTSites,
149  nbootsamples, errorfloor);
150  PrintComponent<double> ("phialpha", &MTTensor::GetAlpha_phi, MTSites,
151  nbootsamples, errorfloor);
152  PrintComponent<double> ("phibeta", &MTTensor::GetBeta_phi, MTSites,
153  nbootsamples, errorfloor);
154  //now we also generate some files that contain various components
155  //we do this for all files in the list
156  for (vector<MTStation>::iterator it = MTSites.GetList().begin(); it
157  != MTSites.GetList().end(); ++it)
158  {
159  //the first file simply contains all relevant quantities as a quick overview
160  string rotfilename = it->GetName() + "_phiall.dat";
161  ofstream rotfile(rotfilename.c_str());
162  for (unsigned int i = 0; i < it->GetFrequencies().size(); ++i)
163  {
164  rotfile << setw(12) << setfill(' ') << setprecision(4) << 1.
165  / it->GetFrequencies().at(i);
166  rotfile << setw(12) << setfill(' ') << setprecision(4)
167  << it->GetMTData().at(i).GetPhi11();
168  rotfile << setw(12) << setfill(' ') << setprecision(4)
169  << it->GetMTData().at(i).GetPhi12();
170  rotfile << setw(12) << setfill(' ') << setprecision(4)
171  << it->GetMTData().at(i).GetPhi21();
172  rotfile << setw(12) << setfill(' ') << setprecision(4)
173  << it->GetMTData().at(i).GetPhi22();
174  rotfile << setw(12) << setfill(' ') << setprecision(4)
175  << it->GetMTData().at(i).GetAlpha_phi();
176  rotfile << setw(12) << setfill(' ') << setprecision(4)
177  << it->GetMTData().at(i).GetBeta_phi();
178  rotfile << setw(12) << setfill(' ') << setprecision(4)
179  << it->GetMTData().at(i).GetPhiMax();
180  rotfile << setw(12) << setfill(' ') << setprecision(4)
181  << it->GetMTData().at(i).GetPhiMin() << endl;
182 
183  }
184  rotfile.close();
185  //this file is for plotting ellipses with gmt
186  //we want one ellipse per period
187  string ptfilename = it->GetName() + "_ptellip.dat";
188  ofstream ptfile(ptfilename.c_str());
189  for (unsigned int i = 0; i < it->GetMTData().size(); ++i)
190  {
191  //the x value is the period
192  ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.
193  / it->GetMTData().at(i).GetFrequency();
194  //all ellipses should be at the same height, i.e. y value
195  ptfile << setw(12) << setfill(' ') << setprecision(4) << 1.0;
196  // the gmt documentation is not clear, but we need the color before the directions etc.
197  //we color the ellipses by the skew
198  ptfile << setw(12) << setfill(' ') << setprecision(4)
199  << it->GetMTData().at(i).GetBeta_phi() * 180. / PI;
200  //GMT wants the angle counter clockwise from horizontal
201  ptfile << setw(12) << setfill(' ') << setprecision(4) << 90
202  - (it->GetMTData().at(i).GetPhiStrike()) * 180. / PI;
203  // and then the major axis
204  ptfile << setw(12) << setfill(' ') << setprecision(4)
205  << it->GetMTData().at(i).GetPhiMax();
206  // and the minor axis of the ellipse
207  ptfile << setw(12) << setfill(' ') << setprecision(4)
208  << it->GetMTData().at(i).GetPhiMin();
209  ptfile << endl;
210  }
211  ptfile.close();
212  }
213  } catch (FatalException &e)
214  {
215  cerr << e.what() << endl; // if something fails print error
216  return -1; // and stop execution
217  }
218  }
219 /*@}*/
220 
MTStation & at(int loc)
Get a reference to a site at a given index.
double GetdZxx() const
Return tensor element errors.
Definition: MTTensor.h:152
double GetPhi11() const
All the following quantities are defined in Caldwell GJI 158, 457-469, the phase tensor elements...
Definition: MTTensor.h:422
const MTTensor & at(const unsigned int i) const
direct acces to a tensor at a given index
Definition: MTStation.h:114
double GetPhiEllip() const
Definition: MTTensor.h:517
const float T[frequenzen]
void f(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
Definition: perftest.cpp:17
double GetPhi21() const
Definition: MTTensor.h:438
double GetdZxy() const
Definition: MTTensor.h:156
std::string GetName()
Definition: MTStation.h:104
double GetPhi12() const
Definition: MTTensor.h:430
std::complex< double > GetZyx() const
Definition: MTTensor.h:130
std::complex< double > GetZxy() const
Definition: MTTensor.h:126
double GetPhi22() const
Definition: MTTensor.h:446
double GetAlpha_phi() const
Definition: MTTensor.h:454
MTStationList holds a number of MTSites, usually associated with a single project, line, etc.
Definition: MTStationList.h:16
string version
Definition: ptfreq.cpp:29
Implements the Jacknifing method of error estimation.
Definition: Jacknife.h:20
Generate random elements of a calculated quantity for MT impedance data.
int main()
Definition: angleavg.cpp:12
double GetdZyx() const
Definition: MTTensor.h:160
tStationList & GetList()
Access to the complete vector of Stations.
Definition: MTStationList.h:31
double GetdZyy() const
Definition: MTTensor.h:164
Stores MT-Tensor components at a single frequency, calculates derived quantities. ...
Definition: MTTensor.h:16
void PrintComponent(const std::string &compname, boost::function< const T(const MTTensor *)> f, MTStationList &StationList, int ntestcases=0, double errorfloor=0.02)
This helper function prints out a single phase tensor component together with error information to a ...
Definition: ptfreq.cpp:33
void GetData(const std::string filename)
Read a list of filenames and the associated data in those files to fill the list. ...
const std::vector< MTTensor > & GetMTData() const
Get the full vector of Tensor elements read only.
Definition: MTStation.h:119
std::complex< double > GetZxx() const
Definition: MTTensor.h:122
std::vector< MTTensor > & SetMTData()
Get the full vector of Tensor elements for reading and writing.
Definition: MTStation.h:124
std::complex< double > GetZyy() const
Return tensor elements.
Definition: MTTensor.h:118
double GetBeta_phi() const
Definition: MTTensor.h:462
double GetPhiMin() const
Definition: MTTensor.h:485
double GetPhiMax() const
Definition: MTTensor.h:481
The basic exception class for all errors that arise in gplib.