GPLIB++
stackrf.cpp
Go to the documentation of this file.
1 #include "SeismicStationList.h"
2 #include "SeismicDataComp.h"
3 #include "VecMat.h"
4 #include "miscfunc.h"
5 #include <iostream>
6 #include <fstream>
7 #include <string>
8 #include "NumUtil.h"
9 #include "Util.h"
10 
11 using namespace std;
12 using namespace gplib;
13 
14 int main()
15  {
16  string version = "$Id: stackrf.cpp 1825 2009-11-03 13:38:06Z mmoorkamp $";
17  cout << endl << endl;
18  cout << "Program " << version << endl;
19  cout << " Stack a number of receiver functions and";
20  cout << " write out the averaged results." << endl;
21  cout << " The program assumes that all RF have the same";
22  cout << " sampling rate and shift, it does not correct for moveout."
23  << endl;
24  cout << endl << endl;
25  string reclistname = AskFilename("File with list of filenames: ");
26  string outfilename = AskFilename("Outfilename root: ");
27 
29  SeismicDataComp StackedRF;
30  //read in the data from the filenames in reclistnames
31  RF.ReadList(reclistname);
32  //print some consistency information
33  const unsigned int nrecfunc = RF.GetList().size();
34  cout << "Read " << nrecfunc << " receiver functions." << endl;
35  //the length of the first receiver function is the one we assume for everything
36  const unsigned int rflength = RF.GetList().front()->GetData().size();
37  //create objects for the average receiver function and the standard deviation
38  gplib::rvec AvgRF(rflength), StdDev(rflength);
39  fill_n(AvgRF.begin(), rflength, 0.0);
40  fill_n(StdDev.begin(), rflength, 0.0);
41  //go through all receiver functions
42  for (unsigned int i = 0; i < nrecfunc; ++i)
43  {
44  //check that they have the same length
45  if (RF.GetList().at(i)->GetData().size() != rflength)
46  {
47  cerr << "Incompatible data length in entry number " << i << endl;
48  return 100;
49  }
50  transform(RF.GetList().at(i)->GetData().begin(),
51  RF.GetList().at(i)->GetData().end(), AvgRF.begin(), AvgRF.begin(),
52  std::plus<double>());
53  //add current receiver function to average
54  // for (unsigned int j = 0; j < rflength; ++j)
55  // {
56  // AvgRF(j) += RF.GetList().at(i)->GetData().at(j);
57  // if (isnan(AvgRF(j)))
58  // cerr << i << " "<< j << " " << AvgRF(j) << std::endl;
59  // }
60  }
61  //after everything has been summed, divide by the number of RF
62  AvgRF *= 1. / nrecfunc;
63  //and calculate the standard deviation
64  for (unsigned int i = 0; i < nrecfunc; ++i)
65  {
66  for (unsigned int j = 0; j < rflength; ++j)
67  {
68  StdDev(j) += pow2(AvgRF(j) - RF.GetList().at(i)->GetData().at(
69  j));
70  }
71  }
72  //copy all header information from the first receiver functions
73  StackedRF = *RF.GetList().front().get();
74  //and copy the data from the averaged receiver function
75  copy(AvgRF.begin(), AvgRF.end(), StackedRF.GetData().begin());
76  //write out stacked RF in sac format
77  StackedRF.WriteAsSac(outfilename + ".avg");
78  StdDev *= 1. / (nrecfunc - 1);
79  //write out stacked rf and standard deviation in ascii file
80  ofstream outfile((outfilename + ".avg.asc").c_str());
81  const double dt = RF.GetList().front()->GetDt();
82  for (unsigned int i = 0; i < rflength; ++i)
83  outfile << i * dt + RF.GetList().front()->GetB() << " " << AvgRF(i)
84  << " " << sqrt(StdDev(i) / nrecfunc) << endl;
85  }
tseiscompvector & GetList()
Return the content of the list for manipulation.
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
int main()
Definition: stackrf.cpp:14
Manages a collection of seismic traces, mainly provides functionality to read in data specified in a ...
std::iterator_traits< InputIterator >::value_type StdDev(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mv)
Calculate the Standard deviation with a given mean.
Definition: statutils.h:68
void ReadList(const std::string filename)
read in a file with names and optionally coordinates
string version
Definition: makeinput.cpp:16
int WriteAsSac(const std::string &filename) const
Write the data in sac binary format.