2 #include "NetCDFTools.h"
6 #include <boost/algorithm/minmax_element.hpp>
7 #include <boost/algorithm/string/compare.hpp>
8 #include <boost/algorithm/string.hpp>
11 using namespace gplib;
15 string infilename, outfilename;
17 string version =
"$Id: 1dinvga.cpp 1136 2007-05-15 22:27:10Z max $";
19 cout <<
"Program " << version << endl;
21 <<
"Create a two dimensional density plot from the models in the input file."
23 cout <<
"This would usually be a concatenation of several '.plot' files "
25 cout <<
"Specify the input file name and the output file name. " << endl;
26 cout <<
"Output is in netcdf format. " << endl;
29 cout <<
"Input file: ";
31 bool wantlogparm =
false;
33 cout <<
"Do you want the logarithm of the parametervalue plotted (y/n): ";
36 if (boost::is_equal()(logchoice, yes))
39 cout <<
"Y-Axis will be logarithmic !" << endl;
41 ifstream infile(infilename.c_str());
42 std::vector<double> depth, parmvalue;
43 double currdepth, currparmvalue;
46 infile >> currdepth >> currparmvalue;
49 depth.push_back(currdepth);
50 parmvalue.push_back(currparmvalue);
53 if (depth.size() != parmvalue.size())
55 cerr <<
"Problem reading file !" << endl;
60 cerr <<
"Read in file." << endl;
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(),
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;
75 if (*(parmminmax.first) > 0.0)
76 minparm = *(parmminmax.first) * 0.9;
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;
84 temp = (log10(maxparm) - log10(minparm)) / logparmstep + 1;
88 temp = (maxparm - minparm) / parmstep + 1;
90 const unsigned int nparms = temp;
91 gplib::rmat Density(ndepths, nparms);
93 fill_n(Density.data().begin(), ndepths * nparms, 0.0);
95 for (
unsigned int i = 0; i < depth.size() - 1; ++i)
97 double nextdepth = depth.at(i + 1);
98 if (nextdepth < depth.at(i))
100 nextdepth = maxdepth;
102 unsigned int depthindex = (depth.at(i) - mindepth) / depthstep;
103 unsigned int nextdepthindex = (nextdepth - mindepth) / depthstep - 1;
104 unsigned int parmindex;
107 parmindex = (log10(parmvalue.at(i)) - log10(minparm)) / logparmstep;
111 parmindex = (parmvalue.at(i) - minparm) / parmstep;
113 for (
unsigned int j = depthindex; j < nextdepthindex; ++j)
114 Density(j, parmindex) += 1.0;
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)
125 parmvals(i) = log10(minparm) + (i + 0.5) * logparmstep;
129 parmvals(i) = minparm + (i + 0.5) * parmstep;
132 cout <<
"Output file: ";
134 ofstream avgfile((outfilename +
".avg").c_str());
135 for (
unsigned int i = 0; i < ndepths; ++i)
138 unsigned int count = 0;
139 for (
unsigned int j = 0; j < nparms; ++j)
141 avg += Density(i, j) * parmvals(j);
142 count += Density(i, j);
144 avgfile << depthvals(i) <<
" " << avg / count << endl;
146 Write2DDataAsNetCDF(outfilename, Density, depthvals, parmvals,
"Depth");