PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
input_helpers.cc
Go to the documentation of this file.
1/* Copyright (C) 2025, 2026 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <cmath>
21
22#include "pism/util/Grid.hh"
23#include "pism/util/io/LocalInterpCtx.hh"
24#include "pism/util/io/IO_Flags.hh"
25#include "pism/util/error_handling.hh"
26#include "pism/util/VariableMetadata.hh"
27#include "pism/util/Profiling.hh"
28#include "pism/util/Context.hh"
29#include "pism/util/pism_utilities.hh"
30#include "pism/util/Logger.hh"
31
32namespace pism {
33namespace io {
34
35/*!
36 * Check if units are set in an input file and warn if they are not.
37 */
38static std::string check_units(const VariableMetadata &variable, const std::string &input_units,
39 const Logger &log) {
40 std::string internal_units = variable["units"];
41 if (input_units.empty() and not internal_units.empty()) {
42 log.message(2,
43 "PISM WARNING: Variable '%s' ('%s') does not have the units attribute.\n"
44 " Assuming that it is in '%s'.\n",
45 variable.get_name().c_str(), variable.get_string("long_name").c_str(),
46 internal_units.c_str());
47 return internal_units;
48 }
49
50 return input_units;
51}
52
53//! \brief Bi-(or tri-)linear interpolation.
54/*!
55 * This is the interpolation code itself.
56 *
57 * Note that its inputs are (essentially)
58 * - the definition of the input grid
59 * - the definition of the output grid
60 * - input array (lic->buffer)
61 * - output array (double *output_array)
62 *
63 * The `output_array` is expected to be big enough to contain
64 * `grid.xm()*`grid.ym()*length(zlevels_out)` numbers.
65 *
66 * We should be able to switch to using an external interpolation library
67 * fairly easily...
68 */
69static void interpolate(const grid::DistributedGridInfo &grid, const LocalInterpCtx &lic, double const *input_array,
70 double *output_array) {
71 // We'll work with the raw storage here so that the array we are filling is
72 // indexed the same way as the buffer we are pulling from (input_array)
73
74 unsigned int nlevels = lic.z->n_output();
75
76 int x_count = lic.count[X_AXIS], z_count = lic.count[Z_AXIS];
77 auto input = [input_array, x_count, z_count](int X, int Y, int Z) {
78 // the map from logical to linear indices for the input array
79 int index = (Y * x_count + X) * z_count + Z;
80 return input_array[index];
81 };
82
83 for (auto p : GridPoints(grid)) {
84 const int i_global = p.i(), j_global = p.j();
85
86 const int i = i_global - grid.xs, j = j_global - grid.ys;
87
88 // Indices of neighboring points.
89 const int X_m = lic.x->left(i), X_p = lic.x->right(i);
90 const int Y_m = lic.y->left(j), Y_p = lic.y->right(j);
91
92 for (unsigned int k = 0; k < nlevels; k++) {
93
94 double a_mm = 0.0, a_mp = 0.0, a_pm = 0.0, a_pp = 0.0;
95
96 if (nlevels > 1) {
97 const int Z_m = lic.z->left(k), Z_p = lic.z->right(k);
98
99 const double alpha_z = lic.z->alpha(k);
100
101 // We pretend that there are always 8 neighbors (4 in the map plane,
102 // 2 vertical levels). And compute the indices into the input_array for
103 // those neighbors.
104 const double
105 mmm = input(X_m, Y_m, Z_m),
106 mmp = input(X_m, Y_m, Z_p),
107 pmm = input(X_p, Y_m, Z_m),
108 pmp = input(X_p, Y_m, Z_p),
109 mpm = input(X_m, Y_p, Z_m),
110 mpp = input(X_m, Y_p, Z_p),
111 ppm = input(X_p, Y_p, Z_m),
112 ppp = input(X_p, Y_p, Z_p);
113
114 // linear interpolation in the z-direction
115 a_mm = mmm * (1.0 - alpha_z) + mmp * alpha_z;
116 a_mp = pmm * (1.0 - alpha_z) + pmp * alpha_z;
117 a_pm = mpm * (1.0 - alpha_z) + mpp * alpha_z;
118 a_pp = ppm * (1.0 - alpha_z) + ppp * alpha_z;
119 } else {
120 // no interpolation in Z in the 2-D case
121 a_mm = input(X_m, Y_m, 0);
122 a_mp = input(X_p, Y_m, 0);
123 a_pm = input(X_m, Y_p, 0);
124 a_pp = input(X_p, Y_p, 0);
125 }
126
127 // interpolation coefficient in the x direction
128 const double x_alpha = lic.x->alpha(i);
129 // interpolation coefficient in the y direction
130 const double y_alpha = lic.y->alpha(j);
131
132 // interpolate in x direction
133 const double a_m = a_mm * (1.0 - x_alpha) + a_mp * x_alpha;
134 const double a_p = a_pm * (1.0 - x_alpha) + a_pp * x_alpha;
135
136 auto index = (j * grid.xm + i) * nlevels + k;
137
138 // index into the new array and interpolate in y direction
139 output_array[index] = a_m * (1.0 - y_alpha) + a_p * y_alpha;
140 // done with the point at (x,y,z)
141 } // end of the loop over vertical levels
142 }
143}
144
145/**
146 * Check if the storage order of a variable in the current file
147 * matches the memory storage order used by PISM.
148 *
149 * @param[in] file input file
150 * @param var_name name of the variable to check
151 * @returns false if storage orders match, true otherwise
152 */
153static bool transpose(std::vector<AxisType> dimension_types) {
154
155 std::vector<AxisType> storage, memory = { Y_AXIS, X_AXIS };
156
157 bool first = true;
158 for (auto dimtype : dimension_types) {
159
160 if (first && dimtype == T_AXIS) {
161 // ignore the time dimension, but only if it is the first
162 // dimension in the list
163 first = false;
164 continue;
165 }
166
167 if (dimtype == X_AXIS || dimtype == Y_AXIS) {
168 storage.push_back(dimtype);
169 } else if (dimtype == Z_AXIS) {
170 memory.push_back(dimtype); // now memory = {Y_AXIS, X_AXIS, Z_AXIS}
171 // assume that this variable has only one Z_AXIS in the file
172 storage.push_back(dimtype);
173 } else {
174 // an UNKNOWN_AXIS or T_AXIS at index != 0 was found, use transposed I/O
175 return true;
176 }
177 }
178
179 // we support 2D and 3D in-memory arrays, but not 4D
180 assert(memory.size() <= 3);
181
182 return storage != memory;
183}
184
185static std::vector<AxisType> dimension_types(const File &file, const std::string &var_name,
186 std::shared_ptr<units::System> unit_system) {
187 std::vector<AxisType> result;
188 for (const auto &dimension : file.dimensions(var_name)) {
189 result.push_back(file.dimension_type(dimension, unit_system));
190 }
191 return result;
192}
193
194/*!
195 * Transpose data in `input`, putting results in `output`.
196 *
197 * We assume that both `input` and `output` hold prod(`count`) elements.
198 *
199 * The `output` array uses the Y,X,Z order (columns in Z are contiguous).
200 *
201 * The `input` array uses the ordering corresponding to `input_axes` (ordering present in
202 * an input file).
203 *
204 * The array `count` provides the size of a block in `input`, listing axes in the order of
205 * values of AxisType (T,X,Y,Z).
206 */
207static void transpose(const double *input, const std::vector<AxisType> &input_axes,
208 const std::array<int, 4> &count, double *output) {
209
210 // make a copy of `input_axes`:
211 auto axes = input_axes;
212
213 // Override the last axis if it is "unknown". This is needed to be able to read
214 // variables such as isochrone deposition times.
215 if (axes.back() == UNKNOWN_AXIS) {
216 axes.back() = Z_AXIS;
217 }
218
219 // validate `axes`:
220 int N = (int)axes.size();
221 for (int k = 0; k < N; ++k) {
222 if (axes[k] < 0 or axes[k] > 3) {
223 throw RuntimeError::formatted(PISM_ERROR_LOCATION, "invalid `axes[%d]` = %d", k, axes[k]);
224 }
225 }
226
227 // delta[X_AXIS] is the change in the linear index corresponding to incrementing x in
228 // the `input` ordering. delta[Y_AXIS], delta[Z_AXIS] and delta[T_AXIS] correspond to
229 // changes in y, z, t.
230 std::vector<unsigned> delta = {1, 1, 1, 1}; // 4 to store steps for T,Y,X,Z axes
231 {
232 // compute changes in the linear index corresponding to incrementing one of the
233 // "spatial" indexes, in the order used in `input`:
234 std::vector<unsigned> tmp(N, 1);
235 for (int k = 0; k < N; ++k) {
236 for (int n = k + 1; n < N; ++n) {
237 tmp[k] *= count[axes[n]];
238 }
239 }
240 // re-arrange so that they are stored in the `T,X,Y,Z` order:
241 for (int k = 0; k < N; ++k) {
242 delta[axes[k]] = tmp[k];
243 }
244 }
245
246 // change in the linear index corresponding to incrementing x (`output` ordering):
247 unsigned delta_x = count[Z_AXIS];
248 // change in the linear index corresponding to incrementing y (`output` ordering):
249 unsigned delta_y = count[X_AXIS] * count[Z_AXIS];
250
251 // traverse in the memory storage order:
252 for (int y = 0; y < count[Y_AXIS]; ++y) {
253 for (int x = 0; x < count[X_AXIS]; ++x) {
254 for (int z = 0; z < count[Z_AXIS]; ++z) {
255 auto OUT = x * delta_x + y * delta_y + z * 1;
256 auto IN = x * delta[X_AXIS] + y * delta[Y_AXIS] + z * delta[Z_AXIS];
257
258 output[OUT] = input[IN];
259 }
260 }
261 }
262}
263
264/*!
265 * Check if some values in `buffer` match the _FillValue attribute and stop with an error
266 * message if such values are found.
267 */
268static void check_for_missing_values(const File &file, const std::string &variable_name,
269 double tolerance, const double *buffer, size_t buffer_length) {
270 auto attribute = file.read_double_attribute(variable_name, "_FillValue");
271 if (attribute.size() != 1) {
272 return;
273 }
274
275 // Check that all value in the local portion are finite, stop with an error message if
276 // not:
277 {
278 int failed = 0;
279 for (size_t k = 0; k < buffer_length; ++k) {
280 if (not std::isfinite(buffer[k])) {
281 failed = 1;
282 break;
283 }
284 }
285 int failed_global = 0;
286 MPI_Allreduce(&failed, &failed_global, 1, MPI_INT, MPI_MAX, file.com());
287 if (failed_global > 0) {
290 "Variable '%s' in '%s' contains values that are not finite (NaN or infinity)",
291 variable_name.c_str(), file.name().c_str());
292 }
293 }
294
295 // Check that values in the local portion don't match _FillValue:
296 {
297 int failed = 0;
298 double fill_value = attribute[0];
299 bool fill_value_is_finite = std::isfinite(fill_value);
300 for (size_t k = 0; k < buffer_length; ++k) {
301 if (fill_value_is_finite and fabs(buffer[k] - fill_value) < tolerance) {
302 failed = 1;
303 break;
304 }
305 }
306 int failed_global = 0;
307 MPI_Allreduce(&failed, &failed_global, 1, MPI_INT, MPI_MAX, file.com());
308 if (failed_global > 0) {
311 "Variable '%s' in '%s' contains values matching the _FillValue attribute",
312 variable_name.c_str(), file.name().c_str());
313 }
314 }
315}
316
318 std::vector<unsigned int> start;
319 std::vector<unsigned int> count;
320};
321
322
323/*!
324 * Assemble start and count arrays for use with I/O function calls.
325 *
326 * This function re-arranges provided `start` and `count` (listed in the order T,X,Y,Z) to
327 * get start and count in the order matching the one in `dim_types`.
328 */
329static StartCountInfo compute_start_and_count(std::vector<AxisType> &dim_types,
330 const std::array<int, 4> &start,
331 const std::array<int, 4> &count) {
332 auto ndims = dim_types.size();
333
334 // Resize output vectors:
335 StartCountInfo result;
336 result.start.resize(ndims);
337 result.count.resize(ndims);
338
339 // Assemble start and count:
340 for (unsigned int j = 0; j < ndims; j++) {
341 switch (dim_types[j]) {
342 case T_AXIS:
343 result.start[j] = start[T_AXIS];
344 result.count[j] = count[T_AXIS];
345 break;
346 case Y_AXIS:
347 result.start[j] = start[Y_AXIS];
348 result.count[j] = count[Y_AXIS];
349 break;
350 case X_AXIS:
351 result.start[j] = start[X_AXIS];
352 result.count[j] = count[X_AXIS];
353 break;
354 case EXP_ID_AXIS:
355 result.start[j] = 0;
356 result.count[j] = 1;
357 break;
358 default:
359 // Note: the "default" case is used to handle "3D" variables where the third axis is
360 // not a "Z" axis, i.e. dim_types[j] == UNKNOWN_AXIS. We use this to write
361 // deposition times in the isochrone tracking model, latitude and longitude bounds,
362 // etc. In all these cases the data for the "unknown" axis is input in the slot for
363 // the "Z" axis. (At least this matches the in-memory storage order.)
364 case Z_AXIS:
365 result.start[j] = start[Z_AXIS];
366 result.count[j] = count[Z_AXIS];
367 break;
368 }
369 }
370
371 return result;
372}
373
374//! \brief Read an array distributed according to the grid.
375static void read_distributed_array(const File &file, const std::string &variable_name,
376 std::shared_ptr<units::System> unit_system,
377 const std::array<int,4> &start,
378 const std::array<int,4> &count,
379 double *output) {
380 try {
381 auto dim_types = dimension_types(file, variable_name, unit_system);
382
383 auto sc = compute_start_and_count(dim_types, start, count);
384
385 auto size = count[X_AXIS] * count[Y_AXIS] * count[Z_AXIS];
386
387 if (transpose(dim_types)) {
388 std::vector<double> tmp(size);
389 file.read_variable(variable_name, sc.start, sc.count, tmp.data());
390 transpose(tmp.data(), dim_types, count, output);
391 } else {
392 file.read_variable(variable_name, sc.start, sc.count, output);
393 }
394
395 // Stop with an error message if some values match the _FillValue attribute:
396 check_for_missing_values(file, variable_name, 1e-12, output, size);
397
398 } catch (RuntimeError &e) {
399 e.add_context("reading variable '%s' from '%s'", variable_name.c_str(), file.name().c_str());
400 throw;
401 }
402}
403
404//! \brief Regrid from a NetCDF file into a distributed array `output`.
405/*!
406 - if `flag` is `CRITICAL` or `CRITICAL_FILL_MISSING`, stops if the
407 variable was not found in the input file
408 - if `flag` is one of `CRITICAL_FILL_MISSING` and
409 `OPTIONAL_FILL_MISSING`, replace _FillValue with `default_value`.
410 - sets `v` to `default_value` if `flag` is `OPTIONAL` and the
411 variable was not found in the input file
412 - uses the last record in the file
413*/
415 const Grid &target_grid,
416 const LocalInterpCtx &interp_context, const File &file,
417 const Logger &log,
418 double *output) {
419
420 auto var = file.find_variable(variable.get_name(), variable["standard_name"]);
421
422 const auto &profiling = target_grid.ctx()->profiling();
423
424 profiling.begin("io.regridding.read");
425 std::vector<double> buffer(interp_context.buffer_size());
426 read_distributed_array(file, var.name, variable.unit_system(), interp_context.start,
427 interp_context.count, buffer.data());
428 profiling.end("io.regridding.read");
429
430 // interpolate
431 profiling.begin("io.regridding.interpolate");
432 interpolate(target_grid.info(), interp_context, buffer.data(), output);
433 profiling.end("io.regridding.interpolate");
434
435 // Get the units string from the file and convert the units:
436 {
437 std::string input_units = file.read_text_attribute(var.name, "units");
438 std::string internal_units = variable["units"];
439
440 input_units = check_units(variable, input_units, log);
441
442 const size_t data_size = target_grid.xm() * target_grid.ym() * interp_context.z->n_output();
443
444 // Convert data:
445 units::Converter(variable.unit_system(), input_units, internal_units)
446 .convert_doubles(output, data_size);
447 }
448}
449
450std::vector<double> read_1d_variable(const File &file, const std::string &variable_name,
451 const std::string &units,
452 std::shared_ptr<units::System> unit_system) {
453
454
455 try {
456 if (not file.variable_exists(variable_name)) {
457 throw RuntimeError(PISM_ERROR_LOCATION, "variable " + variable_name + " is missing");
458 }
459
460 auto dims = file.dimensions(variable_name);
461 if (dims.size() != 1) {
463 "variable '%s' in '%s' should to have 1 dimension (got %d)",
464 variable_name.c_str(), file.name().c_str(), (int)dims.size());
465 }
466
467 const auto &dimension_name = dims[0];
468
469 unsigned int length = file.dimension_length(dimension_name);
470 if (length == 0) {
471 throw RuntimeError(PISM_ERROR_LOCATION, "dimension " + dimension_name + " has length zero");
472 }
473
474 units::Unit internal_units(unit_system, units), input_units(unit_system, "1");
475
476 auto input_units_string = file.read_text_attribute(variable_name, "units");
477
478 if (not input_units_string.empty()) {
479 input_units = units::Unit(unit_system, input_units_string);
480 } else {
483 "variable '%s' does not have the units attribute", variable_name.c_str());
484 }
485
486 std::vector<double> result(length); // memory allocation happens here
487
488 file.read_variable(variable_name, { 0 }, { length }, result.data());
489
490 units::Converter(input_units, internal_units).convert_doubles(result.data(), result.size());
491
492 return result;
493 } catch (RuntimeError &e) {
494 e.add_context("reading 1D variable '%s' from '%s'", variable_name.c_str(),
495 file.name().c_str());
496 throw;
497 }
498}
499
500std::vector<double> read_timeseries_variable(const File &file, const std::string &variable_name,
501 const std::string &units,
502 std::shared_ptr<units::System> unit_system,
503 size_t start, size_t count) {
504
505 auto input_units_string = file.read_text_attribute(variable_name, "units");
506
507 if (input_units_string.empty()) {
509 "variable '%s' does not have the units attribute",
510 variable_name.c_str());
511 }
512
513 std::vector<double> result(count); // memory allocation happens here
514 // read from a file
515 {
516 std::vector<unsigned int> start_{}, count_{};
517
518 for (const auto &dim : file.dimensions(variable_name)) {
519 if (file.dimension_type(dim, unit_system) == T_AXIS) {
520 start_.push_back(start);
521 count_.push_back(count);
522 } else {
523 start_.push_back(0);
524 count_.push_back(1);
525 }
526 }
527 file.read_variable(variable_name, start_, count_, result.data());
528 }
529
530 // convert units
531 {
532 auto input_units = units::Unit(unit_system, input_units_string);
533
534 units::Unit internal_units(unit_system, units);
535 units::Converter(input_units, internal_units).convert_doubles(result.data(), result.size());
536 }
537
538 return result;
539}
540
541
542std::vector<double> read_bounds(const File &file, const std::string &bounds_variable_name,
543 const std::string &internal_units,
544 std::shared_ptr<units::System> unit_system) {
545
546 try {
547 units::Unit internal(unit_system, internal_units);
548
549 // Find the variable:
550 if (not file.variable_exists(bounds_variable_name)) {
551 throw RuntimeError(PISM_ERROR_LOCATION, "variable " + bounds_variable_name + " is missing");
552 }
553
554 auto dims = file.dimensions(bounds_variable_name);
555
556 if (dims.size() != 2) {
557 throw RuntimeError(PISM_ERROR_LOCATION, "variable " + bounds_variable_name + " has to have two dimensions");
558 }
559
560 const auto &dimension_name = dims[0];
561 const auto &bounds_dimension_name = dims[1];
562
563 // Check that we have 2 vertices (interval end-points) per record.
564 size_t length = file.dimension_length(bounds_dimension_name);
565 if (length != 2) {
567 "time-bounds variable " + bounds_variable_name + " has to have exactly 2 bounds per time record");
568 }
569
570 // Get the number of time records.
571 length = file.dimension_length(dimension_name);
572 if (length <= 0) {
573 throw RuntimeError(PISM_ERROR_LOCATION, "dimension " + dimension_name + " has length zero");
574 }
575
576 // Find the corresponding coordinate variable. (We get units from the 'time'
577 // variable, because according to CF-1.5 section 7.1 a "boundary variable"
578 // may not have metadata set.)
579 if (not file.variable_exists(dimension_name)) {
581 "coordinate variable " + dimension_name + " is missing");
582 }
583
584 units::Unit input_units(unit_system, "1");
585
586 std::string input_units_string = file.read_text_attribute(dimension_name, "units");
587 if (input_units_string.empty()) {
589 "variable '%s' does not have the units attribute",
590 dimension_name.c_str());
591 }
592
593 input_units = units::Unit(unit_system, input_units_string);
594
595
596 std::vector<double> result(length * 2); // memory allocation happens here
597
598 file.read_variable(bounds_variable_name, {0, 0}, {(unsigned)length, 2}, result.data());
599
600 units::Converter(input_units, internal).convert_doubles(result.data(), result.size());
601
602 return result;
603 } catch (RuntimeError &e) {
604 e.add_context("reading bounds variable '%s' from '%s'", bounds_variable_name.c_str(),
605 file.name().c_str());
606 throw;
607 }
608}
609
610/*!
611 * Reads and validates times and time bounds.
612 */
613void read_time_info(std::shared_ptr<units::System> unit_system, const File &file,
614 const std::string &time_name, const std::string &time_units,
615 std::vector<double> &times, std::vector<double> &bounds) {
616
617 size_t N = 0;
618 {
619 times = io::read_1d_variable(file, time_name, time_units, unit_system);
620
621 if (not is_increasing(times)) {
622 throw RuntimeError::formatted(PISM_ERROR_LOCATION, "times have to be strictly increasing");
623 }
624 N = times.size();
625 }
626
627 // Read time bounds
628 {
629 std::string time_bounds_name = file.read_text_attribute(time_name, "bounds");
630
631 if (time_bounds_name.empty()) {
632 throw RuntimeError::formatted(PISM_ERROR_LOCATION, "please provide time bounds for '%s'",
633 time_name.c_str());
634 }
635
636 bounds = io::read_bounds(file, time_bounds_name, time_units, unit_system);
637
638 if (2 * N != bounds.size()) {
640 "each time record has to have 2 bounds");
641 }
642
643 for (size_t k = 0; k < N; ++k) {
644 if (not (times[k] >= bounds[2 * k + 0] and
645 times[k] <= bounds[2 * k + 1])) {
647 "each time has to be contained in its time interval");
648 }
649 }
650 } // end of the block reading time bounds
651}
652
653//! Read a variable from a file into an array `output`.
654/*! This also converts data from input units to internal units if needed.
655 */
656void read_spatial_variable(const VariableMetadata &variable, const Grid &grid,
657 const File &file, unsigned int time, double *output) {
658
659 const auto &log = *grid.ctx()->log();
660
661 // Find the variable:
662 auto var = file.find_variable(variable.get_name(), variable["standard_name"]);
663
664 if (not var.exists) {
666 PISM_ERROR_LOCATION, "Can't find '%s' (%s) in '%s'.", variable.get_name().c_str(),
667 variable.get_string("standard_name").c_str(), file.name().c_str());
668 }
669
670 // Sanity check: the variable in an input file should have the expected
671 // number of spatial dimensions.
672 {
673 // Set of spatial dimensions for this field:
674 std::set<int> axes{ X_AXIS, Y_AXIS };
675
676 for (const auto &dim : variable.dimensions()) {
677 if (axis_type_from_string(dim["axis"]) == Z_AXIS) {
678 axes.insert(Z_AXIS);
679 break;
680 }
681 }
682
683 int input_spatial_dim_count = 0; // number of spatial dimensions (input file)
684 size_t matching_dim_count = 0; // number of matching dimensions
685
686 auto input_dims = file.dimensions(var.name);
687 for (const auto &d : input_dims) {
688 auto dim_type = file.dimension_type(d, variable.unit_system());
689
690 if (dim_type == X_AXIS or dim_type == Y_AXIS or dim_type == Z_AXIS) {
691 ++input_spatial_dim_count;
692 }
693
694 if (axes.find(dim_type) != axes.end()) {
695 ++matching_dim_count;
696 }
697 }
698
699 if (axes.size() != matching_dim_count) {
700
701 // Print the error message and stop:
704 "found the %dD variable %s (%s) in '%s' while trying to read\n"
705 "'%s' ('%s'), which is %d-dimensional.",
706 input_spatial_dim_count, var.name.c_str(), join(input_dims, ",").c_str(),
707 file.name().c_str(), variable.get_name().c_str(),
708 variable.get_string("long_name").c_str(), static_cast<int>(axes.size()));
709 }
710 }
711
712 // make sure we have at least one level
713 size_t nlevels = std::max(variable.levels().size(), (size_t)1);
714
715 read_distributed_array(file, var.name, variable.unit_system(),
716 {(int)time, grid.xs(), grid.ys(), 0},
717 {1, grid.xm(), grid.ym(), (int)nlevels},
718 output);
719
720 auto input_units = file.read_text_attribute(var.name, "units");
721 const std::string &internal_units = variable["units"];
722
723 input_units = check_units(variable, input_units, log);
724
725 // Convert data:
726 size_t size = grid.xm() * grid.ym() * nlevels;
727
728 units::Converter(variable.unit_system(), input_units, internal_units)
729 .convert_doubles(output, size);
730}
731
732/*!
733 * Check the overlap of the input grid and the internal grid.
734 *
735 * Set `allow_extrapolation` to `true` to "extend" the vertical grid during "bootstrapping".
736 */
738 const grid::DistributedGridInfo &internal,
739 const std::vector<double> &z_internal) {
740
741 // Grid spacing (assume that the grid is equally-spaced) and the
742 // extent of the domain. To compute the extent of the domain, assume
743 // that the grid is cell-centered, so edge of the domain is half of
744 // the grid spacing away from grid points at the edge.
745 //
746 // Note that x_min is not the same as internal.x(0).
747 const double x_min = internal.x0 - internal.Lx, x_max = internal.x0 + internal.Lx,
748 y_min = internal.y0 - internal.Ly, y_max = internal.y0 + internal.Ly,
749 input_x_min = input.x0 - input.Lx, input_x_max = input.x0 + input.Lx,
750 input_y_min = input.y0 - input.Ly, input_y_max = input.y0 + input.Ly;
751
752 // tolerance (one micron)
753 double eps = 1e-6;
754
755 // horizontal grid extent
756 if (not(x_min >= input_x_min - eps and x_max <= input_x_max + eps and
757 y_min >= input_y_min - eps and y_max <= input_y_max + eps)) {
760 "PISM's computational domain is not a subset of the domain in '%s'\n"
761 "PISM grid: x: [%3.3f, %3.3f] y: [%3.3f, %3.3f] meters\n"
762 "input file grid: x: [%3.3f, %3.3f] y: [%3.3f, %3.3f] meters",
763 input.filename.c_str(), x_min, x_max, y_min, y_max, input_x_min, input_x_max, input_y_min,
764 input_y_max);
765 }
766
767 if (z_internal.empty()) {
769 "Internal vertical grid has 0 levels. This should never happen.");
770 }
771
772 if (z_internal.size() == 1 and input.z.size() > 1) {
773 // internal field is 2D or 3D with one level, input variable is 3D with more than one
774 // vertical level
776 "trying to read in a 2D field but the input file %s contains\n"
777 "a 3D field with %d levels",
778 input.filename.c_str(), static_cast<int>(input.z.size()));
779 }
780
781 if (z_internal.size() > 1 and input.z.size() <= 1) {
782 // internal field is 3D with more than one vertical level, input variable is 2D or 3D
783 // with 1 level
785 "trying to read in a 3D field but the input file %s contains\n"
786 "a 2D field",
787 input.filename.c_str());
788 }
789
790 if (z_internal.size() > 1 and (not input.z.empty())) {
791 // both internal field and input variable are 3D: check vertical grid extent
792 // Note: in PISM 2D fields have one vertical level (z = 0).
793 const double input_z_min = vector_min(input.z), input_z_max = vector_max(input.z),
794 z_min = z_internal.front(), z_max = z_internal.back();
795
796 if (not(z_min >= input_z_min - eps and z_max <= input_z_max + eps)) {
799 "PISM's computational domain is not a subset of the domain in '%s'\n"
800 "PISM grid: z: [%3.3f, %3.3f] meters\n"
801 "input file grid: z: [%3.3f, %3.3f] meters",
802 input.filename.c_str(), z_min, z_max, input_z_min, input_z_max);
803 }
804 }
805}
806
807/*! @brief Check that x, y, and z coordinates of the input grid are strictly increasing. */
809 const grid::DistributedGridInfo &internal_grid,
810 const std::vector<double> &internal_z_levels, bool allow_extrapolation) {
811 if (not is_increasing(input_grid.x)) {
813 "input x coordinate has to be strictly increasing");
814 }
815
816 if (not is_increasing(input_grid.y)) {
818 "input y coordinate has to be strictly increasing");
819 }
820
821 if (not is_increasing(input_grid.z)) {
823 "input vertical grid has to be strictly increasing");
824 }
825
826 if (not allow_extrapolation) {
827 check_grid_overlap(input_grid, internal_grid, internal_z_levels);
828 }
829}
830
831/*!
832 * Return the name of the time dimension corresponding to a NetCDF variable.
833 *
834 * Returns an empty string if this variable is time-independent.
835 */
836std::string time_dimension(units::System::Ptr unit_system,
837 const File &file,
838 const std::string &variable_name) {
839
840 auto dims = file.dimensions(variable_name);
841
842 for (const auto &d : dims) {
843 if (file.dimension_type(d, unit_system) == T_AXIS) {
844 return d;
845 }
846 }
847
848 return "";
849}
850
852 const std::string &variable_name,
853 std::shared_ptr<units::System> unit_system) {
854
855 VariableMetadata variable(variable_name, unit_system);
856
857 try {
858
859 if (not file.variable_exists(variable_name)) {
860 throw RuntimeError::formatted(PISM_ERROR_LOCATION, "variable '%s' is missing", variable_name.c_str());
861 }
862
863 unsigned int nattrs = file.nattributes(variable_name);
864
865 for (unsigned int j = 0; j < nattrs; ++j) {
866 std::string attribute_name = file.attribute_name(variable_name, j);
867 io::Type nctype = file.attribute_type(variable_name, attribute_name);
868
869 if (nctype == PISM_CHAR) {
870 variable[attribute_name] = file.read_text_attribute(variable_name,
871 attribute_name);
872 } else {
873 variable[attribute_name] = file.read_double_attribute(variable_name,
874 attribute_name);
875 }
876 } // end of for (int j = 0; j < nattrs; ++j)
877 } catch (RuntimeError &e) {
878 e.add_context("reading attributes of variable '%s' from '%s'",
879 variable_name.c_str(), file.name().c_str());
880 throw;
881 }
882 return variable;
883}
884
885} // namespace io
886} // namespace pism
void read_variable(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, double *ip) const
Definition File.cc:704
std::string attribute_name(const std::string &var_name, unsigned int n) const
Definition File.cc:680
AxisType dimension_type(const std::string &name, units::System::Ptr unit_system) const
Get the "type" of a dimension.
Definition File.cc:460
VariableLookupData find_variable(const std::string &short_name, const std::string &std_name) const
Find a variable using its standard name and/or short name.
Definition File.cc:328
MPI_Comm com() const
Definition File.cc:185
bool variable_exists(const std::string &short_name) const
Checks if a variable exists.
Definition File.cc:378
io::Type attribute_type(const std::string &var_name, const std::string &att_name) const
Definition File.cc:692
std::string name() const
Definition File.cc:274
std::vector< double > read_double_attribute(const std::string &var_name, const std::string &att_name) const
Get a double attribute.
Definition File.cc:622
unsigned int nattributes(const std::string &var_name) const
Definition File.cc:668
unsigned int dimension_length(const std::string &name) const
Get the length of a dimension.
Definition File.cc:420
std::vector< std::string > dimensions(const std::string &variable_name) const
Definition File.cc:390
std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const
Get a text attribute.
Definition File.cc:650
High-level PISM I/O class.
Definition File.hh:57
const grid::DistributedGridInfo & info() const
Definition Grid.cc:511
std::shared_ptr< const Context > ctx() const
Return execution context this grid corresponds to.
Definition Grid.cc:507
int xm() const
Width of this processor's sub-domain.
Definition Grid.cc:569
int ym() const
Width of this processor's sub-domain.
Definition Grid.cc:574
Describes the PISM grid and the distribution of data across processors.
Definition Grid.hh:285
std::array< int, 4 > count
std::shared_ptr< Interpolation1D > z
std::array< int, 4 > start
std::shared_ptr< Interpolation1D > x
std::shared_ptr< Interpolation1D > y
void message(int threshold, const char format[],...) const __attribute__((format(printf
Print a message to the log.
Definition Logger.cc:50
A basic logging class.
Definition Logger.hh:40
void add_context(const std::string &message)
Add a message providing some context. This way we can (sort of) get a stack trace even though C++ exc...
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
const std::vector< double > & levels() const
std::string get_string(const std::string &name) const
Get a string attribute.
std::shared_ptr< units::System > unit_system() const
std::string get_name() const
std::vector< DimensionMetadata > dimensions() const
double Lx
domain half-width in the X direction
Definition GridInfo.hh:46
std::vector< double > y
y coordinates
Definition GridInfo.hh:53
double y0
y-coordinate of the domain center
Definition GridInfo.hh:44
double x0
x-coordinate of the domain center
Definition GridInfo.hh:42
std::vector< double > x
x coordinates
Definition GridInfo.hh:51
double Ly
domain half-width in the Y direction
Definition GridInfo.hh:48
std::string filename
Definition Grid.hh:70
std::vector< double > z
z coordinates: input grids may be 3-dimensional
Definition Grid.hh:82
void convert_doubles(double *data, size_t length) const
Definition Units.cc:255
std::shared_ptr< System > Ptr
Definition Units.hh:47
#define PISM_ERROR_LOCATION
#define n
Definition exactTestM.c:37
std::vector< double > read_timeseries_variable(const File &file, const std::string &variable_name, const std::string &units, std::shared_ptr< units::System > unit_system, size_t start, size_t count)
void read_spatial_variable(const VariableMetadata &variable, const Grid &grid, const File &file, unsigned int time, double *output)
Read a variable from a file into an array output.
static StartCountInfo compute_start_and_count(std::vector< AxisType > &dim_types, const std::array< int, 4 > &start, const std::array< int, 4 > &count)
VariableMetadata read_attributes(const File &file, const std::string &variable_name, std::shared_ptr< units::System > unit_system)
void check_input_grid(const grid::InputGridInfo &input_grid, const grid::DistributedGridInfo &internal_grid, const std::vector< double > &internal_z_levels, bool allow_extrapolation)
Check that x, y, and z coordinates of the input grid are strictly increasing.
void read_time_info(std::shared_ptr< units::System > unit_system, const File &file, const std::string &time_name, const std::string &time_units, std::vector< double > &times, std::vector< double > &bounds)
std::vector< double > read_1d_variable(const File &file, const std::string &variable_name, const std::string &units, std::shared_ptr< units::System > unit_system)
@ PISM_CHAR
Definition IO_Flags.hh:49
static void read_distributed_array(const File &file, const std::string &variable_name, std::shared_ptr< units::System > unit_system, const std::array< int, 4 > &start, const std::array< int, 4 > &count, double *output)
Read an array distributed according to the grid.
static std::string check_units(const VariableMetadata &variable, const std::string &input_units, const Logger &log)
static void check_grid_overlap(const grid::InputGridInfo &input, const grid::DistributedGridInfo &internal, const std::vector< double > &z_internal)
static void interpolate(const grid::DistributedGridInfo &grid, const LocalInterpCtx &lic, double const *input_array, double *output_array)
Bi-(or tri-)linear interpolation.
static void check_for_missing_values(const File &file, const std::string &variable_name, double tolerance, const double *buffer, size_t buffer_length)
void regrid_spatial_variable(const VariableMetadata &variable, const Grid &target_grid, const LocalInterpCtx &interp_context, const File &file, const Logger &log, double *output)
Regrid from a NetCDF file into a distributed array output.
std::vector< double > read_bounds(const File &file, const std::string &bounds_variable_name, const std::string &internal_units, std::shared_ptr< units::System > unit_system)
std::string time_dimension(units::System::Ptr unit_system, const File &file, const std::string &variable_name)
static bool transpose(std::vector< AxisType > dimension_types)
static std::vector< AxisType > dimension_types(const File &file, const std::string &var_name, std::shared_ptr< units::System > unit_system)
bool is_increasing(const std::vector< double > &a)
Checks if a vector of doubles is strictly increasing.
@ UNKNOWN_AXIS
Definition IO_Flags.hh:34
@ T_AXIS
Definition IO_Flags.hh:34
@ X_AXIS
Definition IO_Flags.hh:34
@ EXP_ID_AXIS
Definition IO_Flags.hh:34
@ Z_AXIS
Definition IO_Flags.hh:34
@ Y_AXIS
Definition IO_Flags.hh:34
static const double k
Definition exactTestP.cc:42
double vector_min(const std::vector< double > &input)
std::string join(const std::vector< std::string > &strings, const std::string &separator)
Concatenate strings, inserting separator between elements.
AxisType axis_type_from_string(const std::string &input)
Definition File.cc:436
double vector_max(const std::vector< double > &input)
std::vector< unsigned int > start
std::vector< unsigned int > count
int count
Definition test_cube.c:16