stackrf.cpp

Go to the documentation of this file.
00001 #include "SeismicStationList.h"
00002 #include "SeismicDataComp.h"
00003 #include "VecMat.h"
00004 #include "miscfunc.h"
00005 #include <iostream>
00006 #include <fstream>
00007 #include <string>
00008 #include "NumUtil.h"
00009 #include "Util.h"
00010 
00011 using namespace std;
00012 using namespace gplib;
00013 
00014 int main()
00015   {
00016     string version = "$Id: stackrf.cpp 1825 2009-11-03 13:38:06Z mmoorkamp $";
00017     cout << endl << endl;
00018     cout << "Program " << version << endl;
00019     cout << " Stack a number of receiver functions and";
00020     cout << " write out the averaged results." << endl;
00021     cout << " The program assumes that all RF have the same";
00022     cout << " sampling rate and shift, it does not correct for moveout."
00023         << endl;
00024     cout << endl << endl;
00025     string reclistname = AskFilename("File with list of filenames: ");
00026     string outfilename = AskFilename("Outfilename root: ");
00027 
00028     SeismicStationList RF;
00029     SeismicDataComp StackedRF;
00030     //read in the data from the filenames in reclistnames
00031     RF.ReadList(reclistname);
00032     //print some consistency information
00033     const unsigned int nrecfunc = RF.GetList().size();
00034     cout << "Read " << nrecfunc << " receiver functions." << endl;
00035     //the length of the first receiver function is the one we assume for everything
00036     const unsigned int rflength = RF.GetList().front()->GetData().size();
00037     //create objects for the average receiver function and the standard deviation
00038     gplib::rvec AvgRF(rflength), StdDev(rflength);
00039     fill_n(AvgRF.begin(), rflength, 0.0);
00040     fill_n(StdDev.begin(), rflength, 0.0);
00041     //go through all receiver functions
00042     for (unsigned int i = 0; i < nrecfunc; ++i)
00043       {
00044         //check that they have the same length
00045         if (RF.GetList().at(i)->GetData().size() != rflength)
00046           {
00047             cerr << "Incompatible data length in entry number " << i << endl;
00048             return 100;
00049           }
00050         transform(RF.GetList().at(i)->GetData().begin(),
00051             RF.GetList().at(i)->GetData().end(), AvgRF.begin(), AvgRF.begin(),
00052             std::plus<double>());
00053         //add current receiver function to average
00054         // for (unsigned int j = 0; j < rflength; ++j)
00055         // {
00056         //  AvgRF(j) += RF.GetList().at(i)->GetData().at(j);
00057         //  if (isnan(AvgRF(j)))
00058         //    cerr << i << " "<< j << " " << AvgRF(j) << std::endl;
00059         // }
00060       }
00061     //after everything has been summed, divide by the number of RF
00062     AvgRF *= 1. / nrecfunc;
00063     //and calculate the standard deviation
00064     for (unsigned int i = 0; i < nrecfunc; ++i)
00065       {
00066         for (unsigned int j = 0; j < rflength; ++j)
00067           {
00068             StdDev(j) += pow2(AvgRF(j) - RF.GetList().at(i)->GetData().at(
00069                 j));
00070           }
00071       }
00072     //copy all header information from the first receiver functions
00073     StackedRF = *RF.GetList().front().get();
00074     //and copy the data from the averaged receiver function
00075     copy(AvgRF.begin(), AvgRF.end(), StackedRF.GetData().begin());
00076     //write out stacked RF in sac format
00077     StackedRF.WriteAsSac(outfilename + ".avg");
00078     StdDev *= 1. / (nrecfunc - 1);
00079     //write out stacked rf and standard deviation in ascii file
00080     ofstream outfile((outfilename + ".avg.asc").c_str());
00081     const double dt = RF.GetList().front()->GetDt();
00082     for (unsigned int i = 0; i < rflength; ++i)
00083       outfile << i * dt + RF.GetList().front()->GetB() << " " << AvgRF(i)
00084           << " " << sqrt(StdDev(i) / nrecfunc) << endl;
00085   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8