5 #include <boost/lambda/lambda.hpp>
6 #include <boost/lambda/bind.hpp>
8 using namespace boost::lambda;
12 ThreeDMTModel::ThreeDMTModel()
17 ThreeDMTModel::~ThreeDMTModel()
21 double ThreeDMTModel::LookupResistivity(
const int index)
23 return Resistivities[index];
26 int ThreeDMTModel::LookupIndex(
const double resistivity)
28 const size_t nresistivity = Resistivities.size();
29 for (
size_t i = 0; i < nresistivity; ++i)
30 if (Resistivities[i] == resistivity)
33 Resistivities.push_back(resistivity);
34 return (nresistivity);
37 void ThreeDMTModel::ReadWinGLink(std::string filename)
39 ifstream infile(filename.c_str());
40 size_t xlength, ylength, zlength = 0;
43 double currentres = 0;
45 infile >> xlength >> ylength >> zlength >> airlayers;
48 infile.getline(dummy, 255);
50 Model.resize(boost::extents[xlength][ylength][zlength]);
51 xsize.resize(xlength);
52 ysize.resize(ylength);
53 zsize.resize(zlength);
55 for (i = 0; i < xlength; ++i)
57 for (i = 0; i < ylength; ++i)
59 for (i = 0; i < zlength; ++i)
62 for (i = 0; i < zlength; ++i)
64 infile >> currentlayer;
65 for (j = 0; j < ylength; ++j)
66 for (k = 0; k < xlength; ++k)
69 Model[k][j][currentlayer - 1] = LookupIndex(currentres);
77 void ThreeDMTModel::ReadMackie(std::string filename)
79 ifstream infile(filename.c_str());
80 size_t xlength, ylength, zlength;
84 infile >> xlength >> ylength >> zlength;
87 Model.resize(boost::extents[xlength][ylength][zlength]);
88 xsize.resize(xlength);
89 ysize.resize(ylength);
90 zsize.resize(zlength);
92 for (i = 0; i < xlength; ++i)
94 for (i = 0; i < ylength; ++i)
96 for (i = 0; i < zlength; ++i)
99 for (i = 0; i < zlength; ++i)
101 infile >> currentlayer;
102 for (j = 0; j < ylength; ++j)
103 for (k = 0; k < xlength; ++k)
104 infile >> Model[k][j][currentlayer - 1];
107 while (infile.good())
109 double currvalue = 0.0;
111 Resistivities.push_back(currvalue);
119 void ThreeDMTModel::WriteWinGLink(std::string filename)
121 ofstream outfile(filename.c_str());
123 const size_t xlength = xsize.size();
124 const size_t ylength = ysize.size();
125 const size_t zlength = zsize.size();
128 outfile << xlength <<
" " << ylength <<
" " << zlength <<
" "
129 << airlayers <<
" VALUES" << endl;
131 std::copy(xsize.begin(), xsize.end(), std::ostream_iterator<double>(
135 std::copy(ysize.begin(), ysize.end(), std::ostream_iterator<double>(
139 std::copy(zsize.begin(), zsize.end(), std::ostream_iterator<double>(
144 for (i = 0; i < zlength; ++i)
146 outfile << i + 1 << endl;
147 for (j = 0; j < ylength; ++j)
149 for (k = 0; k < xlength; ++k)
150 outfile << LookupResistivity(Model[k][j][i]) <<
" ";
158 void ThreeDMTModel::WriteMackie(std::string filename)
160 ofstream outfile(filename.c_str());
162 const size_t xlength = xsize.size();
163 const size_t ylength = ysize.size();
164 const size_t zlength = zsize.size();
166 outfile << xlength <<
" " << ylength <<
" " << zlength << endl;
168 std::copy(xsize.begin(), xsize.end(), std::ostream_iterator<double>(
172 std::copy(ysize.begin(), ysize.end(), std::ostream_iterator<double>(
176 std::copy(zsize.begin(), zsize.end(), std::ostream_iterator<double>(
180 for (i = 0; i < zlength; ++i)
182 outfile << i + 1 << endl;
183 for (j = 0; j < ylength; ++j)
185 for (k = 0; k < xlength; ++k)
186 outfile << Model[k][j][i] <<
" ";
191 std::copy(Resistivities.begin(), Resistivities.end(),
192 std::ostream_iterator<double>(outfile,
" "));
200 NcFile cdf(filename.c_str(), NcFile::Replace);
204 NcDim* xd = cdf.add_dim(
"x", xsize.size());
205 NcDim* yd = cdf.add_dim(
"y", ysize.size());
206 NcDim* zd = cdf.add_dim(
"z", zsize.size());
207 NcVar* x = cdf.add_var(
"x", ncDouble, xd);
209 x->add_att(
"units",
"meters");
210 x->add_att(
"long_name",
"x-coordinate in Cartesian system");
211 NcVar* y = cdf.add_var(
"y", ncDouble, yd);
212 y->add_att(
"units",
"meters");
213 y->add_att(
"long_name",
"y-coordinate in Cartesian system");
214 NcVar* z = cdf.add_var(
"z", ncDouble, zd);
215 z->add_att(
"units",
"meters");
216 z->add_att(
"long_name",
"z-coordinate in Cartesian system");
217 z->add_att(
"positive",
"down");
218 NcVar* res = cdf.add_var(
"res", ncDouble, xd, yd, zd);
219 res->add_att(
"units",
"ohm meters");
220 res->add_att(
"long_name",
"Resistivity");
221 res->add_att(
"coordinates",
"x y z");
222 std::vector<double> xvals, yvals, zvals, resvals;
224 std::partial_sum(xsize.begin(), xsize.end(), std::back_inserter(xvals));
225 std::partial_sum(ysize.begin(), ysize.end(), std::back_inserter(yvals));
226 std::partial_sum(zsize.begin(), zsize.end(), std::back_inserter(zvals));
227 const size_t nres = Model.num_elements();
229 std::transform(Model.origin(), Model.origin() + nres, back_inserter(
230 resvals), _1 = var(Resistivities)[_1]);
232 x->put(&xvals[0], xd->size());
233 y->put(&yvals[0], yd->size());
234 z->put(&zvals[0], zd->size());
235 res->put(&resvals[0], res->edges());
238 void ThreeDMTModel::WriteVTK(std::string filename)
240 ofstream outfile(filename.c_str());
243 outfile <<
"# vtk DataFile Version 2.0" << endl;
244 outfile <<
"3D Model data" << endl;
245 outfile <<
"ASCII" << endl;
246 outfile <<
"DATASET RECTILINEAR_GRID" << endl;
249 outfile <<
"DIMENSIONS " << xsize.size() + 1 <<
" " << ysize.size() + 1
250 <<
" " << zsize.size() + 1 << endl;
252 outfile <<
"X_COORDINATES " << xsize.size() + 1 <<
" double" << endl;
253 double currcoord = 0.0;
254 outfile << currcoord <<
" ";
255 std::partial_sum(xsize.begin(),xsize.end(),std::ostream_iterator<double>(outfile,
" "));
257 outfile <<
"Y_COORDINATES " << ysize.size() + 1 <<
" double" << endl;
259 outfile << currcoord <<
" ";
260 std::partial_sum(ysize.begin(),ysize.end(),std::ostream_iterator<double>(outfile,
" "));
262 outfile <<
"Z_COORDINATES " << zsize.size() + 1 <<
" double" << endl;
264 outfile << currcoord <<
" ";
265 std::partial_sum(zsize.begin(),zsize.end(),std::ostream_iterator<double>(outfile,
" "));
267 outfile <<
"CELL_DATA " << xsize.size() * ysize.size() * zsize.size()
269 outfile <<
"SCALARS Resistivity double" << endl;
270 outfile <<
"LOOKUP_TABLE default" << endl;
272 for (
size_t i = 0; i < zsize.size(); ++i)
274 for (
size_t j = 0; j < ysize.size(); ++j)
276 for (
size_t k = 0; k < xsize.size(); ++k)
278 outfile << LookupResistivity(Model[k][j][i]) <<
" ";
void WriteNetCDF(const string name, const boost::numeric::ublas::matrix< double, boost::numeric::ublas::column_major > &Matrix)
The basic exception class for all errors that arise in gplib.