GPLIB++
keepcommon.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include "MTStationList.h"
5 #include "Util.h"
6 using namespace std;
7 using namespace gplib;
8 
9 int main(int argc, char* argv[])
10  {
11  string version = "$Id: keepcommon.cpp 1886 2014-10-29 12:38:46Z mmoorkamp $";
12  cout << endl << endl;
13  cout << "Program " << version << endl;
14  cout << " Read in several files with MT tensor data" << endl;
15  cout << " and keep the common frequencies. The files will be written in" << endl;
16  cout
17  << " .mtt format. If the input files were in that format they will be overwritten"
18  << endl;
19  cout << endl << endl;
20 
21  try
22  {
23  MTStationList MTSites;
24  std::string infilename;
25  //read the file with the name of stations either directly from the command line
26  if (argc > 1)
27  {
28  infilename = argv[1];
29  }
30  else
31  {
32  //or prompt for it.
33  infilename = AskFilename("Station Filename: ");
34  }
35 
36  MTSites.GetData(infilename);
37  const size_t nstats = MTSites.GetList().size();
38  //create a display of frequencies in the different files
39  //first column is the filename, then a column for each frequency
40  //in the first data files
41  std::cout << "Frequency matrix \n" << std::endl;
42  std::cout << std::setw(12) << "Filename ";
43  for (double freq : MTSites.at(0).GetFrequencies())
44  {
45  std::cout << std::setw(10) << freq;
46  }
47  std::cout << std::endl;
48 
49  //the relative tolerance with which we consider two frequencies equal
50  const double tolerance = 0.05;
51  for (size_t i = 0; i < nstats; ++i)
52  {
53  //for each site we output its name in the first column
54  std::cout << std::setw(10) << MTSites.at(i).GetName();
55  const int nfreq = MTSites.at(0).GetFrequencies().size();
56  std::vector<int> havefreq(nfreq, 0);
57  //then we go through all the frequencies for that site
58  for (size_t j = 0; j < nfreq; ++j)
59  {
60  bool foundfrequency = false;
61  size_t k = 0;
62  //as the different sites can have different order for
63  //the frequencies we have to go through all of them
64  //until we found a correspondance with the first site
65  //or reached the end
66  while ((foundfrequency == false)
67  && (k < MTSites.at(i).GetFrequencies().size()))
68  {
69  if (fcmp(MTSites.at(0).GetFrequencies().at(j),
70  MTSites.at(i).GetFrequencies().at(k), tolerance) == 0)
71  foundfrequency = true;
72  ++k;
73  }
74  //if we found the frequency, we mark it in the vector
75  havefreq.at(j) = foundfrequency;
76  }
77  //output the vector of found frequencies in the same order
78  //as the frequencies in the first file
79  for (int tick : havefreq)
80  {
81  std::cout << std::setw(10) << tick;
82  }
83  std::cout << std::endl;
84  }
85 
86  //now we go through the sites again to mark the ones
87  //that are not in the master file
88  for (size_t i = 0; i < nstats; ++i)
89  {
90  trealdata freqs(MTSites.at(i).GetFrequencies());
91  const int nfreq = MTSites.at(i).GetFrequencies().size();
92 
93  for (int j = 0; j < nfreq; ++j)
94  {
95  if (find(MTSites.GetComFreqIndices().at(i).begin(),
96  MTSites.GetComFreqIndices().at(i).end(), j)
97  == MTSites.GetComFreqIndices().at(i).end())
98  {
99  freqs.at(j) = 0;
100  }
101  }
102 
103  MTSites.at(i).SetFrequencies(freqs);
104  MTSites.at(i).WriteAsMtt(MTSites.at(i).GetName());
105 
106  }
107  } catch (FatalException &e)
108  {
109  cerr << e.what() << endl;
110  return -1;
111  }
112  }
113 
MTStation & at(int loc)
Get a reference to a site at a given index.
std::string GetName()
Definition: MTStation.h:104
int main(int argc, char *argv[])
Definition: keepcommon.cpp:9
MTStationList holds a number of MTSites, usually associated with a single project, line, etc.
Definition: MTStationList.h:16
tStationList & GetList()
Access to the complete vector of Stations.
Definition: MTStationList.h:31
void WriteAsMtt(const std::string filename)
Write data in goettingen .mtt format.
Definition: MTStation.cpp:681
const std::vector< tindexvector > & GetComFreqIndices()
Get a vector that for each site contains the indices to the common frequencies.
Definition: MTStationList.h:42
void GetData(const std::string filename)
Read a list of filenames and the associated data in those files to fill the list. ...
string version
Definition: makeinput.cpp:16
trealdata GetFrequencies() const
return the available frequencies in a single vector
Definition: MTStation.cpp:136
void SetFrequencies(const trealdata &freqs)
Set the frequencies of the tensor elements, invalidates the previously stored impedance data...
Definition: MTStation.cpp:144
The basic exception class for all errors that arise in gplib.