modeldensity.cpp
Go to the documentation of this file.00001 #include "VecMat.h"
00002 #include "NetCDFTools.h"
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string>
00006 #include <boost/algorithm/minmax_element.hpp>
00007 #include <boost/algorithm/string/compare.hpp>
00008 #include <boost/algorithm/string.hpp>
00009
00010 using namespace std;
00011 using namespace gplib;
00012
00013 int main()
00014 {
00015 string infilename, outfilename;
00016
00017 string version = "$Id: 1dinvga.cpp 1136 2007-05-15 22:27:10Z max $";
00018 cout << endl << endl;
00019 cout << "Program " << version << endl;
00020 cout
00021 << "Create a two dimensional density plot from the models in the input file."
00022 << endl;
00023 cout << "This would usually be a concatenation of several '.plot' files "
00024 << endl;
00025 cout << "Specify the input file name and the output file name. " << endl;
00026 cout << "Output is in netcdf format. " << endl;
00027 cout << endl << endl;
00028
00029 cout << "Input file: ";
00030 cin >> infilename;
00031 bool wantlogparm = false;
00032 string logchoice;
00033 cout << "Do you want the logarithm of the parametervalue plotted (y/n): ";
00034 cin >> logchoice;
00035 string yes("y");
00036 if (boost::is_equal()(logchoice, yes))
00037 {
00038 wantlogparm = true;
00039 cout << "Y-Axis will be logarithmic !" << endl;
00040 }
00041 ifstream infile(infilename.c_str());
00042 std::vector<double> depth, parmvalue;
00043 double currdepth, currparmvalue;
00044 while (infile.good())
00045 {
00046 infile >> currdepth >> currparmvalue;
00047 if (infile.good())
00048 {
00049 depth.push_back(currdepth);
00050 parmvalue.push_back(currparmvalue);
00051 }
00052 }
00053 if (depth.size() != parmvalue.size())
00054 {
00055 cerr << "Problem reading file !" << endl;
00056 return 100;
00057 }
00058 else
00059 {
00060 cerr << "Read in file." << endl;
00061 }
00062 typedef vector<double>::const_iterator it;
00063 pair<it, it> depthminmax =
00064 boost::minmax_element(depth.begin(), depth.end());
00065 pair<it, it> parmminmax = boost::minmax_element(parmvalue.begin(),
00066 parmvalue.end());
00067 const double depthstep = 1.0;
00068 const double parmstep = 0.1;
00069 const double logparmstep = 0.1;
00070 const double deptheps = *(depthminmax.second) * 0.1;
00071 const double parmeps = *(parmminmax.second) * 0.1;
00072 const double mindepth = *(depthminmax.first);
00073 const double maxdepth = *(depthminmax.second) + deptheps;
00074 double minparm;
00075 if (*(parmminmax.first) > 0.0)
00076 minparm = *(parmminmax.first) * 0.9;
00077 else
00078 minparm = *(parmminmax.first) * 1.1;
00079 const double maxparm = *(parmminmax.second) + parmeps;
00080 const unsigned int ndepths = (maxdepth - mindepth) / depthstep;
00081 unsigned int temp = 0;
00082 if (wantlogparm)
00083 {
00084 temp = (log10(maxparm) - log10(minparm)) / logparmstep + 1;
00085 }
00086 else
00087 {
00088 temp = (maxparm - minparm) / parmstep + 1;
00089 }
00090 const unsigned int nparms = temp;
00091 gplib::rmat Density(ndepths, nparms);
00092
00093 fill_n(Density.data().begin(), ndepths * nparms, 0.0);
00094 ;
00095 for (unsigned int i = 0; i < depth.size() - 1; ++i)
00096 {
00097 double nextdepth = depth.at(i + 1);
00098 if (nextdepth < depth.at(i))
00099 {
00100 nextdepth = maxdepth;
00101 }
00102 unsigned int depthindex = (depth.at(i) - mindepth) / depthstep;
00103 unsigned int nextdepthindex = (nextdepth - mindepth) / depthstep - 1;
00104 unsigned int parmindex;
00105 if (wantlogparm)
00106 {
00107 parmindex = (log10(parmvalue.at(i)) - log10(minparm)) / logparmstep;
00108 }
00109 else
00110 {
00111 parmindex = (parmvalue.at(i) - minparm) / parmstep;
00112 }
00113 for (unsigned int j = depthindex; j < nextdepthindex; ++j)
00114 Density(j, parmindex) += 1.0;
00115 }
00116
00117 gplib::rvec depthvals(ndepths);
00118 gplib::rvec parmvals(nparms);
00119 for (unsigned int i = 0; i < ndepths; ++i)
00120 depthvals(i) = -1.0 * i * depthstep;
00121 for (unsigned int i = 0; i < nparms; ++i)
00122 {
00123 if (wantlogparm)
00124 {
00125 parmvals(i) = log10(minparm) + (i + 0.5) * logparmstep;
00126 }
00127 else
00128 {
00129 parmvals(i) = minparm + (i + 0.5) * parmstep;
00130 }
00131 }
00132 cout << "Output file: ";
00133 cin >> outfilename;
00134 ofstream avgfile((outfilename + ".avg").c_str());
00135 for (unsigned int i = 0; i < ndepths; ++i)
00136 {
00137 double avg = 0.0;
00138 unsigned int count = 0;
00139 for (unsigned int j = 0; j < nparms; ++j)
00140 {
00141 avg += Density(i, j) * parmvals(j);
00142 count += Density(i, j);
00143 }
00144 avgfile << depthvals(i) << " " << avg / count << endl;
00145 }
00146 Write2DDataAsNetCDF(outfilename, Density, depthvals, parmvals, "Depth");
00147
00148 }
00149