Debian Codegraph @

src/atlas/output/detail/PointCloudIO.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 // file deepcode ignore MissingOpenCheckOnFile: False positive
12
13 #include <fstream>
14 #include <iomanip>
15 #include <limits>
16 #include <sstream>
17
18 #include "eckit/filesystem/PathName.h"
19
20 #include "atlas/array/ArrayView.h"
21 #include "atlas/array/DataType.h"
22 #include "atlas/array/MakeView.h"
23 #include "atlas/field/Field.h"
24 #include "atlas/field/FieldSet.h"
25 #include "atlas/functionspace/FunctionSpace.h"
26 #include "atlas/functionspace/NodeColumns.h"
27 #include "atlas/mesh/Mesh.h"
28 #include "atlas/mesh/Nodes.h"
29 #include "atlas/output/detail/PointCloudIO.h"
30 #include "atlas/runtime/Exception.h"
31 #include "atlas/runtime/Log.h"
32 #include "atlas/util/CoordinateEnums.h"
33
34 namespace atlas {
35 namespace output {
36 namespace detail {
37
38 // ------------------------------------------------------------------
39
40 namespace {
41
42 std::string sanitize_field_name(const std::string& s) {
43 // replace non-printable characters, then trim right & left
44 std::string r(s);
45 std::replace_if(r.begin(), r.end(), ::isspace, '_');
46 r.erase(r.find_last_not_of('_') + 1);
47 r.erase(0, r.find_first_not_of('_'));
48 if (!r.length()) {
49 r = "_";
50 }
51 return r;
52 }
53
54 } // end anonymous namespace
55
56 // ------------------------------------------------------------------
57
58 Mesh PointCloudIO::read(const eckit::PathName& path, std::vector<std::string>& vfnames) {
59 const std::string msg("PointCloudIO::read: ");
60
61 Log::debug() << "PointCloudIO reading " << path << std::endl;
62
63 Mesh mesh;
64
65 vfnames.clear();
66
67 {
68 std::string line;
69 std::istringstream iss;
70 std::ostringstream oss;
71 size_t nb_pts; // # points/nodes
72 size_t nb_columns; // # data columns (lon,lat,field1,field2,...)
73 size_t nb_fld; // # fields (nb_fld := nb_columns-2)
74
75 // open file and read all of header & data
76 std::ifstream f(path.asString().c_str());
77 if (!f.is_open()) {
78 throw_CantOpenFile(path.asString());
79 }
80
81 // header, part 1:
82 // determine number of rows/columns
83 // (read all of line, look for "PointCloudIO" signature, nb_pts, nb_columns,
84 // ...)
85 std::getline(f, line);
86 iss.str(line);
87 iss >> line >> nb_pts >> nb_columns;
88 if (line != "PointCloudIO") {
89 std::stringstream errmsg;
90 errmsg << msg << "beginning of file `" << path << "` not found (expected: PointCloudIO, got: " << line
91 << ")";
92 throw_Exception(errmsg.str(), Here());
93 }
94 if (nb_pts == 0) {
95 throw_AssertionFailed(msg + " invalid number of points (failed: nb_pts>0)");
96 }
97 if (nb_columns < 2) {
98 throw_AssertionFailed(msg + " invalid number of columns (failed: nb_columns>=2)");
99 }
100
101 mesh.nodes().resize(static_cast<idx_t>(nb_pts));
102
103 mesh::Nodes& nodes = mesh.nodes();
104 array::ArrayView<double, 2> xy = array::make_view<double, 2>(nodes.xy());
105 array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(nodes.lonlat());
106 array::ArrayView<gidx_t, 1> glb_idx = array::make_view<gidx_t, 1>(nodes.global_index());
107 array::ArrayView<int, 1> part = array::make_view<int, 1>(nodes.partition());
108 part.assign(0);
109 // header, part 2:
110 // determine columns' labels
111 // (check end of first line for possible column labels, starting from
112 // defaults)
113
114 vfnames.resize(nb_columns);
115 for (size_t j = 0; j < nb_columns; ++j) {
116 std::stringstream name;
117 name.str("column_");
118 name << (j + 1);
119 vfnames[j] = (iss && iss >> line) ? sanitize_field_name(line) : name.str();
120 }
121
122 // (preallocate data, and define fields without the first two columns
123 // (lon,lat) because they are treated differently)
124 vfnames.erase(vfnames.begin(), vfnames.begin() + 2);
125 nb_fld = nb_columns - 2; // always >= 0, considering previous check
126
127 std::vector<array::ArrayView<double, 1>> fields;
128 for (size_t j = 0; j < nb_fld; ++j) {
129 fields.emplace_back(array::make_view<double, 1>(nodes.add(
130 Field(vfnames[j], array::make_datatype<double>(), array::make_shape(static_cast<idx_t>(nb_pts))))));
131 }
132
133 size_t i, j; // (index for node/row and field/column, out of scope to check
134 // at end of loops)
135 for (i = 0; f && i < nb_pts; ++i) {
136 std::getline(f, line);
137 iss.clear();
138 iss.str(line);
139
140 // NOTE always expects (lon,lat) order, maybe make it configurable?
141 PointXY pxy;
142 iss >> pxy.x() >> pxy.y();
143
144 xy(i, (size_t)XX) = pxy.x();
145 xy(i, (size_t)YY) = pxy.y();
146 lonlat(i, (size_t)LON) = pxy.x();
147 lonlat(i, (size_t)LAT) = pxy.y();
148 glb_idx(i) = i + 1;
149
150 for (j = 0; iss && j < nb_fld; ++j) {
151 iss >> fields[j](i);
152 }
153 if (j < nb_fld) {
154 oss << " Invalid number of fields in data section, on line " << (i + 1) << ", read " << j
155 << " fields, expected " << nb_fld << ".";
156 throw_AssertionFailed(msg + oss.str());
157 }
158 }
159 if (i < nb_pts) {
160 oss << " Invalid number of lines in data section, read " << (i) << " lines, expected " << nb_pts << ".";
161 throw_AssertionFailed(msg + oss.str());
162 }
163
164 f.close();
165 }
166
167 return mesh;
168 }
169
170 Mesh PointCloudIO::read(const eckit::PathName& path) {
171 std::vector<std::string> vfnames;
172 return read(path, vfnames);
173 }
174
175 void PointCloudIO::write(const eckit::PathName& path, const Mesh& mesh) {
176 const std::string msg("PointCloudIO::write: ");
177
178 Log::debug() << "PointCloudIO writing " << path << std::endl;
179
180 // operate in mesh function space, creating transversing data structures
181 // @warning: several copy operations here
182
183 const mesh::Nodes& nodes = mesh.nodes();
184
185 auto lonlat = array::make_view<double, 2>(nodes.lonlat());
186 if (!lonlat.size()) {
187 throw_Exception(msg + "invalid number of points (failed: nb_pts>0)");
188 }
189
190 // get the fields (sanitized) names and values
191 // (bypasses fields ("lonlat"|"lonlat") as shape(1)!=1)
192 std::vector<std::string> vfnames;
193 std::vector<array::ArrayView<const double, 1>> vfvalues;
194 for (idx_t i = 0; i < nodes.nb_fields(); ++i) {
195 const Field& field = nodes.field(i);
196 if (((field.rank() == 1 && field.shape(0) == lonlat.shape(0)) ||
197 (field.rank() == 2 && field.shape(0) == lonlat.shape(0) && field.shape(1) == 1)) &&
198 field.datatype() == array::DataType::real64()) { // FIXME: no support for non-double types!
199 vfnames.push_back(sanitize_field_name(field.name()));
200 vfvalues.push_back(array::make_view<double, 1>(field));
201 }
202 }
203
204 std::ofstream f(path.asString().c_str());
205 if (!f.is_open()) {
206 throw_CantOpenFile(path.asString());
207 }
208
209 const size_t Npts = lonlat.shape(0);
210 const size_t Nfld = vfvalues.size();
211
212 // header
213 f << "PointCloudIO\t" << Npts << '\t' << (2 + Nfld) << "\tlon\tlat";
214 for (size_t j = 0; j < Nfld; ++j) {
215 f << '\t' << vfnames[j];
216 }
217 f << '\n';
218
219 // data
220 for (size_t i = 0; i < Npts; ++i) {
221 f << lonlat(i, (size_t)0) << '\t' << lonlat(i, (size_t)1);
222 for (size_t j = 0; j < Nfld; ++j) {
223 f << '\t' << vfvalues[j](i);
224 }
225 f << '\n';
226 }
227
228 f.close();
229 }
230
231 void PointCloudIO::write(const eckit::PathName& path, const FieldSet& fieldset,
232 const functionspace::NodeColumns& function_space) {
233 const std::string msg("PointCloudIO::write: ");
234
235 Log::debug() << "PointCloudIO writing " << path << std::endl;
236
237 // operate in field sets with same grid and consistent size(s), creating
238 // transversing data structures
239 // @warning: several copy operations here
240
241 ATLAS_ASSERT(fieldset.size());
242
243 array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(function_space.nodes().xy());
244 if (!lonlat.size()) {
245 throw_Exception(msg + "invalid number of points (failed: nb_pts>0)");
246 }
247
248 // get the fields (sanitized) names and values
249 // (bypasses fields ("lonlat"|"lonlat") as shape(1)!=1)
250 std::vector<std::string> vfnames;
251 std::vector<array::ArrayView<const double, 1>> vfvalues;
252 for (idx_t i = 0; i < fieldset.size(); ++i) {
253 const Field& field = fieldset[i];
254 if (field.shape(0) == lonlat.shape(0) && field.rank() == 1 &&
255 field.name() != "glb_idx") // FIXME: no support for non-int types!
256 {
257 vfnames.push_back(sanitize_field_name(field.name()));
258 vfvalues.push_back(array::make_view<double, 1>(field));
259 }
260 }
261
262 std::ofstream f(path.asString().c_str());
263 if (!f.is_open()) {
264 throw_CantOpenFile(path.asString());
265 }
266 const size_t Npts = lonlat.shape(0), Nfld = vfvalues.size();
267
268 // header
269 f << "PointCloudIO\t" << Npts << '\t' << (2 + Nfld) << "\tlon\tlat";
270 for (size_t j = 0; j < Nfld; ++j) {
271 f << '\t' << vfnames[j];
272 }
273 f << '\n';
274
275 f.precision(std::numeric_limits<double>::digits10);
276
277 // data
278 for (size_t i = 0; i < Npts; ++i) {
279 f << lonlat(i, (size_t)0) << '\t' << lonlat(i, (size_t)1);
280 for (size_t j = 0; j < Nfld; ++j) {
281 f << '\t' << vfvalues[j](i);
282 }
283 f << '\n';
284 }
285
286 f.close();
287 }
288
289 void PointCloudIO::write(const eckit::PathName& path, const std::vector<PointLonLat>& pts) {
290 Log::debug() << "PointCloudIO writing " << path << std::endl;
291
292 std::ofstream f(path.asString().c_str());
293 if (!f.is_open()) {
294 throw_CantOpenFile(path.asString());
295 }
296
297 // header
298 f << "PointCloudIO\t" << pts.size() << '\t' << 2 << "\tlon\tlat\n";
299
300 // data
301 for (size_t i = 0; i < pts.size(); ++i) {
302 f << pts[i].lon() << '\t' << pts[i].lat() << '\n';
303 }
304
305 f.close();
306 }
307
308 void PointCloudIO::write(const eckit::PathName& path, const std::vector<double>& lon, const std::vector<double>& lat,
309 const std::vector<std::vector<double>*>& vfvalues, const std::vector<std::string>& vfnames) {
310 Log::debug() << "PointCloudIO writing " << path << std::endl;
311
312 const std::string msg("PointCloudIO::write: ");
313 const size_t Npts(lon.size()), Nfld(vfvalues.size());
314 if (Npts != lat.size()) {
315 throw_Exception(msg + "number of points inconsistent (failed: #lon == #lat)");
316 }
317 if (Nfld != vfnames.size()) {
318 throw_Exception(msg + "number of fields inconsistent (failed: #vfvalues == #vfnames)");
319 }
320 for (size_t j = 0; j < Nfld; ++j) {
321 if (Npts != vfvalues[j]->size()) {
322 throw_Exception(msg +
323 "number of points inconsistent (failed: "
324 "#lon == #lat == #*vfvalues[])");
325 }
326 }
327
328 std::ofstream f(path.asString().c_str());
329 if (!f.is_open()) {
330 throw_CantOpenFile(path.asString());
331 }
332
333 // header
334 f << "PointCloudIO\t" << Npts << '\t' << (2 + Nfld) << "\tlon\tlat";
335 for (size_t j = 0; j < Nfld; ++j) {
336 f << '\t' << sanitize_field_name(vfnames[j]);
337 }
338 f << '\n';
339
340 // data
341 for (size_t i = 0; i < Npts; ++i) {
342 f << lon[i] << '\t' << lat[i];
343 for (size_t j = 0; j < Nfld; ++j) {
344 f << '\t' << vfvalues[j]->operator[](i);
345 }
346 f << '\n';
347 }
348
349 f.close();
350 }
351
352 void PointCloudIO::write(const eckit::PathName& path, const int& nb_pts, const double* lon, const double* lat,
353 const int& nb_fld, const double** afvalues, const char** afnames) {
354 Log::debug() << "PointCloudIO writing " << path << std::endl;
355
356
357 const std::string msg("PointCloudIO::write: ");
358
359 const size_t Npts(nb_pts > 0 ? nb_pts : 0), Nfld(nb_fld > 0 && afvalues && afnames ? nb_fld : 0);
360 if (!Npts) {
361 throw_Exception(msg + "invalid number of points (nb_nodes)");
362 }
363 if (!lon) {
364 throw_Exception(msg + "invalid array describing longitude (lon)");
365 }
366 if (!lat) {
367 throw_Exception(msg + "invalid array describing latitude (lat)");
368 }
369
370 std::ofstream f(path.asString().c_str());
371 if (!f.is_open()) {
372 throw_CantOpenFile(path.asString());
373 }
374
375 // header
376 f << "PointCloudIO\t" << Npts << '\t' << (2 + Nfld) << "\tlon\tlat";
377 for (size_t j = 0; j < Nfld; ++j) {
378 f << '\t' << sanitize_field_name(afnames[j]);
379 }
380 f << '\n';
381
382 // data
383 for (size_t i = 0; i < Npts; ++i) {
384 f << lon[i] << '\t' << lat[i];
385 for (size_t j = 0; j < Nfld; ++j) {
386 f << '\t' << afvalues[j][i];
387 }
388 f << '\n';
389 }
390
391 f.close();
392 }
393
394 // ------------------------------------------------------------------
395 // C wrapper interfaces to C++ routines
396
397 // PointCloudIO* atlas__pointcloud__new()
398 // { return new PointCloudIO(); }
399 //
400 //
401 // void atlas__pointcloud__delete (PointCloudIO* This)
402 // { delete This; }
403 //
404 //
405 // mesh::Mesh* atlas__pointcloud__read (PointCloudIO* This, char* file_path)
406 // { return This->read(file_path); }
407 //
408 //
409 // mesh::Mesh* atlas__read_pointcloud (char* file_path)
410 // { return PointCloudIO::read(file_path); }
411 //
412 //
413 // void atlas__write_pointcloud_fieldset (char* file_path, const
414 // field::FieldSetImpl* fieldset, const functionspace::detail::NodeColumns*
415 // functionspace)
416 // { PointCloudIO::write(file_path, *fieldset, *functionspace); }
417
418 // ------------------------------------------------------------------
419
420 } // namespace detail
421 } // namespace output
422 } // namespace atlas