GPLIB++
zavg.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include "MTStationList.h"
4 
5 using namespace std;
6 using namespace gplib;
7 
8 /*!
9  * \addtogroup UtilProgs Utility Programs
10  *@{
11  * \file
12  * Average impedances from a number of different sites. Each datum is weighted
13  * by its variance. Can be used to extract average 1D responses, but is affected
14  * by static distortions, you can also try ptavg to average phase tensor elements.
15  */
16 
17 int main()
18  {
19  MTStationList MTSites;
20 
21  string infilename;
22 
23  string version = "$Id: zavg.cpp 1836 2009-11-27 12:17:24Z mmoorkamp $";
24  cout << endl << endl;
25  cout << "Program " << version << endl;
26  cout << "Average MT responses from a list of file " << endl;
27  cout << "The average response will be written in a file called avg.mtt "
28  << endl;
29  cout << "List filename:";
30  cin >> infilename;
31  double xxweights = 0;
32  double xyweights = 0;
33  double yxweights = 0;
34  double yyweights = 0;
35 
36  MTSites.GetData(infilename);
37  MTStation AvgSite(MTSites.at(0).GetMTData().size());
38  AvgSite.SetFrequencies(MTSites.at(0).GetFrequencies());
39  const size_t nfreq = MTSites.at(0).GetFrequencies().size();
40  const size_t nsites = MTSites.GetList().size();
41  const double errorfloor = 0.02;
42  const double absolutemin = 1e-4; // have to find a better solution to avoid strong weighting of 0 values
43  for (size_t j = 0; j < nfreq; ++j)
44  {
45  xxweights = 0;
46  xyweights = 0;
47  yxweights = 0;
48  yyweights = 0;
49  dcomp currzxx = 0;
50  dcomp currzxy = 0;
51  dcomp currzyx = 0;
52  dcomp currzyy = 0;
53  for (size_t i = 0; i < nsites; ++i)
54  {
55  double minweight = max(
56  abs(MTSites.at(i).GetMTData().at(j).GetZxx()) * errorfloor,
57  absolutemin);
58  double currweight = 1. / pow(max(
59  MTSites.at(i).GetMTData().at(j).GetdZxx(), minweight), 2);
60  currzxx += MTSites.at(i).GetMTData().at(j).GetZxx() * currweight;
61  xxweights += currweight;
62 
63  minweight = max(abs(MTSites.at(i).GetMTData().at(j).GetZxy())
64  * errorfloor, absolutemin);
65  currweight = 1. / pow(max(
66  MTSites.at(i).GetMTData().at(j).GetdZxy(), minweight), 2);
67  currzxy += MTSites.at(i).GetMTData().at(j).GetZxy() * currweight;
68  xyweights += currweight;
69 
70  minweight = max(abs(MTSites.at(i).GetMTData().at(j).GetZyx())
71  * errorfloor, absolutemin);
72  currweight = 1. / pow(max(
73  MTSites.at(i).GetMTData().at(j).GetdZyx(), minweight), 2);
74  currzyx += MTSites.at(i).GetMTData().at(j).GetZyx() * currweight;
75  yxweights += currweight;
76 
77  minweight = max(abs(MTSites.at(i).GetMTData().at(j).GetZyy())
78  * errorfloor, absolutemin);
79  currweight = 1. / pow(max(
80  MTSites.at(i).GetMTData().at(j).GetdZyy(), minweight), 2);
81  currzyy += MTSites.at(i).GetMTData().at(j).GetZyy() * currweight;
82  yyweights += currweight;
83  }
84  AvgSite.SetMTData().at(j).SetZxx() = currzxx / xxweights;
85  AvgSite.SetMTData().at(j).SetZxy() = currzxy / xyweights;
86  AvgSite.SetMTData().at(j).SetZyx() = currzyx / yxweights;
87  AvgSite.SetMTData().at(j).SetZyy() = currzyy / yyweights;
88  AvgSite.SetMTData().at(j).SetErrors(sqrt(1. / xxweights), sqrt(1.
89  / xyweights), sqrt(1. / yxweights), sqrt(1. / yyweights));
90  }
91 
92  AvgSite.WriteAsMtt("avg");
93  }
94 /*@}*/
95 
MTStation & at(int loc)
Get a reference to a site at a given index.
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
MTStationList holds a number of MTSites, usually associated with a single project, line, etc.
Definition: MTStationList.h:16
int main()
Definition: angleavg.cpp:12
tStationList & GetList()
Access to the complete vector of Stations.
Definition: MTStationList.h:31
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
const std::vector< MTTensor > & GetMTData() const
Get the full vector of Tensor elements read only.
Definition: MTStation.h:119
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