GPLIB++
modeldensity.cpp
Go to the documentation of this file.
1 #include "VecMat.h"
2 #include "NetCDFTools.h"
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 #include <boost/algorithm/minmax_element.hpp>
7 #include <boost/algorithm/string/compare.hpp>
8 #include <boost/algorithm/string.hpp>
9 
10 using namespace std;
11 using namespace gplib;
12 
13 int main()
14  {
15  string infilename, outfilename;
16 
17  string version = "$Id: 1dinvga.cpp 1136 2007-05-15 22:27:10Z max $";
18  cout << endl << endl;
19  cout << "Program " << version << endl;
20  cout
21  << "Create a two dimensional density plot from the models in the input file."
22  << endl;
23  cout << "This would usually be a concatenation of several '.plot' files "
24  << endl;
25  cout << "Specify the input file name and the output file name. " << endl;
26  cout << "Output is in netcdf format. " << endl;
27  cout << endl << endl;
28 
29  cout << "Input file: ";
30  cin >> infilename;
31  bool wantlogparm = false;
32  string logchoice;
33  cout << "Do you want the logarithm of the parametervalue plotted (y/n): ";
34  cin >> logchoice;
35  string yes("y");
36  if (boost::is_equal()(logchoice, yes))
37  {
38  wantlogparm = true;
39  cout << "Y-Axis will be logarithmic !" << endl;
40  }
41  ifstream infile(infilename.c_str());
42  std::vector<double> depth, parmvalue;
43  double currdepth, currparmvalue;
44  while (infile.good())
45  {
46  infile >> currdepth >> currparmvalue;
47  if (infile.good())
48  {
49  depth.push_back(currdepth);
50  parmvalue.push_back(currparmvalue);
51  }
52  }
53  if (depth.size() != parmvalue.size())
54  {
55  cerr << "Problem reading file !" << endl;
56  return 100;
57  }
58  else
59  {
60  cerr << "Read in file." << endl;
61  }
62  typedef vector<double>::const_iterator it;
63  pair<it, it> depthminmax =
64  boost::minmax_element(depth.begin(), depth.end());
65  pair<it, it> parmminmax = boost::minmax_element(parmvalue.begin(),
66  parmvalue.end());
67  const double depthstep = 1.0;
68  const double parmstep = 0.1;
69  const double logparmstep = 0.1;
70  const double deptheps = *(depthminmax.second) * 0.1;
71  const double parmeps = *(parmminmax.second) * 0.1;
72  const double mindepth = *(depthminmax.first);
73  const double maxdepth = *(depthminmax.second) + deptheps;
74  double minparm;
75  if (*(parmminmax.first) > 0.0)
76  minparm = *(parmminmax.first) * 0.9;
77  else
78  minparm = *(parmminmax.first) * 1.1;
79  const double maxparm = *(parmminmax.second) + parmeps;
80  const unsigned int ndepths = (maxdepth - mindepth) / depthstep;
81  unsigned int temp = 0;
82  if (wantlogparm)
83  {
84  temp = (log10(maxparm) - log10(minparm)) / logparmstep + 1;
85  }
86  else
87  {
88  temp = (maxparm - minparm) / parmstep + 1;
89  }
90  const unsigned int nparms = temp;
91  gplib::rmat Density(ndepths, nparms);
92 
93  fill_n(Density.data().begin(), ndepths * nparms, 0.0);
94  ;
95  for (unsigned int i = 0; i < depth.size() - 1; ++i)
96  {
97  double nextdepth = depth.at(i + 1);
98  if (nextdepth < depth.at(i))
99  {
100  nextdepth = maxdepth;
101  }
102  unsigned int depthindex = (depth.at(i) - mindepth) / depthstep;
103  unsigned int nextdepthindex = (nextdepth - mindepth) / depthstep - 1;
104  unsigned int parmindex;
105  if (wantlogparm)
106  {
107  parmindex = (log10(parmvalue.at(i)) - log10(minparm)) / logparmstep;
108  }
109  else
110  {
111  parmindex = (parmvalue.at(i) - minparm) / parmstep;
112  }
113  for (unsigned int j = depthindex; j < nextdepthindex; ++j)
114  Density(j, parmindex) += 1.0;
115  }
116 
117  gplib::rvec depthvals(ndepths);
118  gplib::rvec parmvals(nparms);
119  for (unsigned int i = 0; i < ndepths; ++i)
120  depthvals(i) = -1.0 * i * depthstep;
121  for (unsigned int i = 0; i < nparms; ++i)
122  {
123  if (wantlogparm)
124  {
125  parmvals(i) = log10(minparm) + (i + 0.5) * logparmstep;
126  }
127  else
128  {
129  parmvals(i) = minparm + (i + 0.5) * parmstep;
130  }
131  }
132  cout << "Output file: ";
133  cin >> outfilename;
134  ofstream avgfile((outfilename + ".avg").c_str());
135  for (unsigned int i = 0; i < ndepths; ++i) // for all depths
136  {
137  double avg = 0.0;
138  unsigned int count = 0;
139  for (unsigned int j = 0; j < nparms; ++j)
140  {
141  avg += Density(i, j) * parmvals(j);
142  count += Density(i, j);
143  }
144  avgfile << depthvals(i) << " " << avg / count << endl;
145  }
146  Write2DDataAsNetCDF(outfilename, Density, depthvals, parmvals, "Depth");
147 
148  }
149 
string version
Definition: makeinput.cpp:16
int main()