GPLIB++
plotrecfunc.cpp
Go to the documentation of this file.
1 #include "SeismicStationList.h"
2 #include <iostream>
3 #include <string>
4 #include <vector>
5 #include <functional>
6 #include <boost/bind.hpp>
7 #include <boost/function.hpp>
8 #include <fstream>
9 #include "Util.h"
10 #include "netcdfcpp.h"
11 #include "statutils.h"
12 
13 using namespace std;
14 using namespace gplib;
15 
16 void WriteToNetCDF(string filename, SeismicStationList &Stations,
17  boost::function<double(SeismicDataComp *)> f)
18  {
19  const size_t nsamples = Stations.GetList().front()->GetData().size();
20  const size_t nstations = Stations.GetList().size();
21 
22  sort(Stations.GetList().begin(), Stations.GetList().end(), boost::bind(
23  less<double> (), boost::bind(f, boost::bind(&boost::shared_ptr<
24  SeismicDataComp>::get, _1)), boost::bind(f, boost::bind(
25  &boost::shared_ptr<SeismicDataComp>::get, _2))));
26  size_t nCompatStations = 0;
27  for (size_t i = 0; i < nstations; ++i)
28  {
29  if (nsamples == Stations.GetList().at(i)->GetData().size())
30  {
31  ++nCompatStations;
32  }
33  }
34 
35  NcFile combrescdf((filename + ".nc").c_str(), NcFile::Replace);
36  NcDim* xd = combrescdf.add_dim("x", nCompatStations);
37  NcDim* yd = combrescdf.add_dim("y", nsamples);
38  NcVar* x = combrescdf.add_var("x", ncFloat, xd);
39  NcVar* y = combrescdf.add_var("y", ncFloat, yd);
40  NcVar* z = combrescdf.add_var("z", ncFloat, xd, yd);
41  float *xvals = new float[xd->size()];
42  float *yvals = new float[yd->size()];
43  float *zvals = new float[xd->size() * yd->size()];
44 
45  for (size_t j = 0; j < nsamples; ++j)
46  yvals[j] = j * Stations.GetList().front()->GetDt()
47  + Stations.GetList().front()->GetB();
48  size_t dataindex = 0;
49  size_t stationindex = 0;
50  while (dataindex < nCompatStations)
51  {
52  xvals[dataindex] = f(Stations.GetList().at(stationindex).get());
53  if (nsamples == Stations.GetList().at(stationindex)->GetData().size())
54  {
55  copy(Stations.GetList().at(stationindex)->GetData().begin(),
56  Stations.GetList().at(stationindex)->GetData().end(),
57  &zvals[dataindex * nsamples]);
58  ++dataindex;
59  }
60  else
61  {
62  cerr << "File: " << Stations.GetList().at(stationindex)->GetName()
63  << " " << "has incompatible length: " << Stations.GetList().at(
64  stationindex)->GetData().size() << " not " << nsamples << endl;
65  }
66  ++stationindex;
67  }
68  x->put(xvals, xd->size());
69  y->put(yvals, yd->size());
70  z->put(zvals, z->edges());
71 
72  delete[] xvals;
73  delete[] yvals;
74  delete[] zvals;
75  }
76 
77 int main(int argc, char* argv[])
78  {
79  SeismicStationList Stations;
80  string listfilename, outfilename;
81 
82  string version =
83  "$Id: plotrecfunc.cpp 1882 2014-06-12 08:15:20Z mmoorkamp $";
84  cout << endl << endl;
85  cout << "Program " << version << endl;
86  cout << " Print a number of receiver functions in one plot" << endl;
87  cout
88  << " Input is a list of files, output an ascii file for plotting with xmgrace and a netcdf file"
89  << endl;
90  cout
91  << " Individual receiver functions are offset in y-direction by the chosen sorting criterion"
92  << endl;
93  cout << endl << endl;
94  if (argc > 2)
95  {
96  listfilename = argv[1];
97  outfilename = argv[2];
98  }
99  else
100  {
101  listfilename = AskFilename("List filename: ");
102  outfilename = AskFilename("Output filename: ");
103  }
104  boost::function<double(SeismicDataComp*)> SortFunc;
105  size_t sortchoice;
106  cout << "Specify Sorting Criterion: " << endl;
107  cout << " 1: Distance " << endl;
108  cout << " 2: Backazimuth " << endl;
109  cin >> sortchoice;
110  switch (sortchoice)
111  {
112  case 1:
113  SortFunc = &SeismicDataComp::GetGcarc;
114  break;
115  case 2:
116  SortFunc = &SeismicDataComp::GetBaz;
117  break;
118  default:
119  cerr << "Invalid choice !";
120  return 100;
121  break;
122  }
123  Stations.ReadList(listfilename);
124  vector<double> distances(Stations.GetList().size(), 0.0);
125  transform(Stations.GetList().begin(), Stations.GetList().end(),
126  distances.begin(), boost::bind(SortFunc, boost::bind(
127  &boost::shared_ptr<SeismicDataComp>::get, _1)));
128  cout << "Parameters: " << endl;
129  copy(distances.begin(), distances.end(), ostream_iterator<double> (cout,
130  "\n"));
131  cout << endl;
132  double meandist = Mean(distances.begin(), distances.end());
133  cout << "Mean Parameter: " << meandist << endl;
134  vector<double> maxamps;
135  for (SeismicStationList::tseiscompvector::iterator CurrentStation =
136  Stations.GetList().begin(); CurrentStation != Stations.GetList().end(); CurrentStation++)
137  {
138  double currmax = *max_element(CurrentStation->get()->GetData().begin(),
139  CurrentStation->get()->GetData().end());
140  if (!std::isnan(currmax))
141  maxamps.push_back(currmax);
142  }
143  double meanamp = Mean(maxamps.begin(), maxamps.end());
144  double yshift = 5.0 * meanamp / meandist;
145  cout << " Mean Amp: " << meanamp << endl;
146  ofstream outfile(outfilename.c_str());
147  for (SeismicStationList::tseiscompvector::iterator CurrentStation =
148  Stations.GetList().begin(); CurrentStation != Stations.GetList().end(); CurrentStation++)
149  {
150  for (size_t i = 0; i < CurrentStation->get()->GetData().size(); ++i)
151  {
152  outfile << i * CurrentStation->get()->GetDt()
153  + CurrentStation->get()->GetB() << " "
154  << CurrentStation->get()->GetData().at(i) + (meandist
155  - SortFunc(CurrentStation->get())) * yshift << endl;
156  }
157  outfile << endl << endl;
158  }
159  WriteToNetCDF(outfilename, Stations, SortFunc);
160  }
tseiscompvector & GetList()
Return the content of the list for manipulation.
int main(int argc, char *argv[])
Definition: plotrecfunc.cpp:77
std::iterator_traits< InputIterator >::value_type Mean(InputIterator begin, InputIterator end)
Calculate the mean for a given range.
Definition: statutils.h:21
void f(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
Definition: perftest.cpp:17
void WriteToNetCDF(string filename, SeismicStationList &Stations, boost::function< double(SeismicDataComp *)> f)
Definition: plotrecfunc.cpp:16
Manages a collection of seismic traces, mainly provides functionality to read in data specified in a ...
void ReadList(const std::string filename)
read in a file with names and optionally coordinates
string version
Definition: makeinput.cpp:16