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"
40 std::string internal_units = variable[
"units"];
41 if (input_units.empty() and not internal_units.empty()) {
43 "PISM WARNING: Variable '%s' ('%s') does not have the units attribute.\n"
44 " Assuming that it is in '%s'.\n",
46 internal_units.c_str());
47 return internal_units;
70 double *output_array) {
74 unsigned int nlevels = lic.
z->n_output();
77 auto input = [input_array, x_count, z_count](
int X,
int Y,
int Z) {
79 int index = (Y * x_count + X) * z_count + Z;
80 return input_array[index];
84 const int i_global = p.i(), j_global = p.j();
86 const int i = i_global - grid.xs, j = j_global - grid.ys;
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);
92 for (
unsigned int k = 0;
k < nlevels;
k++) {
94 double a_mm = 0.0, a_mp = 0.0, a_pm = 0.0, a_pp = 0.0;
97 const int Z_m = lic.
z->left(
k), Z_p = lic.
z->right(
k);
99 const double alpha_z = lic.
z->alpha(
k);
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);
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;
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);
128 const double x_alpha = lic.
x->alpha(i);
130 const double y_alpha = lic.
y->alpha(j);
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;
136 auto index = (j * grid.xm + i) * nlevels +
k;
139 output_array[index] = a_m * (1.0 - y_alpha) + a_p * y_alpha;
155 std::vector<AxisType> storage, memory = {
Y_AXIS,
X_AXIS };
160 if (first && dimtype ==
T_AXIS) {
168 storage.push_back(dimtype);
169 }
else if (dimtype ==
Z_AXIS) {
170 memory.push_back(dimtype);
172 storage.push_back(dimtype);
180 assert(memory.size() <= 3);
182 return storage != memory;
186 std::shared_ptr<units::System> unit_system) {
187 std::vector<AxisType> result;
188 for (
const auto &dimension : file.
dimensions(var_name)) {
207static void transpose(
const double *input,
const std::vector<AxisType> &input_axes,
208 const std::array<int, 4> &
count,
double *output) {
211 auto axes = input_axes;
220 int N = (
int)axes.size();
221 for (
int k = 0;
k < N; ++
k) {
222 if (axes[
k] < 0 or axes[
k] > 3) {
230 std::vector<unsigned> delta = {1, 1, 1, 1};
234 std::vector<unsigned> tmp(N, 1);
235 for (
int k = 0;
k < N; ++
k) {
236 for (
int n =
k + 1;
n < N; ++
n) {
241 for (
int k = 0;
k < N; ++
k) {
242 delta[axes[
k]] = tmp[
k];
255 auto OUT = x * delta_x + y * delta_y + z * 1;
258 output[OUT] = input[IN];
269 double tolerance,
const double *buffer,
size_t buffer_length) {
271 if (attribute.size() != 1) {
279 for (
size_t k = 0;
k < buffer_length; ++
k) {
280 if (not std::isfinite(buffer[
k])) {
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());
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) {
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());
330 const std::array<int, 4> &start,
331 const std::array<int, 4> &
count) {
332 auto ndims = dim_types.size();
336 result.
start.resize(ndims);
337 result.
count.resize(ndims);
340 for (
unsigned int j = 0; j < ndims; j++) {
341 switch (dim_types[j]) {
376 std::shared_ptr<units::System> unit_system,
377 const std::array<int,4> &start,
378 const std::array<int,4> &
count,
388 std::vector<double> tmp(size);
389 file.
read_variable(variable_name, sc.start, sc.count, tmp.data());
392 file.
read_variable(variable_name, sc.start, sc.count, output);
399 e.
add_context(
"reading variable '%s' from '%s'", variable_name.c_str(), file.
name().c_str());
415 const Grid &target_grid,
422 const auto &profiling = target_grid.
ctx()->profiling();
424 profiling.begin(
"io.regridding.read");
425 std::vector<double> buffer(interp_context.
buffer_size());
427 interp_context.
count, buffer.data());
428 profiling.end(
"io.regridding.read");
431 profiling.begin(
"io.regridding.interpolate");
432 interpolate(target_grid.
info(), interp_context, buffer.data(), output);
433 profiling.end(
"io.regridding.interpolate");
438 std::string internal_units = variable[
"units"];
440 input_units =
check_units(variable, input_units, log);
442 const size_t data_size = target_grid.
xm() * target_grid.
ym() * interp_context.
z->n_output();
451 const std::string &units,
452 std::shared_ptr<units::System> unit_system) {
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());
467 const auto &dimension_name = dims[0];
474 units::Unit internal_units(unit_system, units), input_units(unit_system,
"1");
478 if (not input_units_string.empty()) {
479 input_units =
units::Unit(unit_system, input_units_string);
483 "variable '%s' does not have the units attribute", variable_name.c_str());
486 std::vector<double> result(length);
488 file.
read_variable(variable_name, { 0 }, { length }, result.data());
494 e.
add_context(
"reading 1D variable '%s' from '%s'", variable_name.c_str(),
495 file.
name().c_str());
501 const std::string &units,
502 std::shared_ptr<units::System> unit_system,
503 size_t start,
size_t count) {
507 if (input_units_string.empty()) {
509 "variable '%s' does not have the units attribute",
510 variable_name.c_str());
513 std::vector<double> result(
count);
516 std::vector<unsigned int> start_{}, count_{};
518 for (
const auto &dim : file.
dimensions(variable_name)) {
520 start_.push_back(start);
521 count_.push_back(
count);
527 file.
read_variable(variable_name, start_, count_, result.data());
532 auto input_units =
units::Unit(unit_system, input_units_string);
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) {
554 auto dims = file.
dimensions(bounds_variable_name);
556 if (dims.size() != 2) {
560 const auto &dimension_name = dims[0];
561 const auto &bounds_dimension_name = dims[1];
567 "time-bounds variable " + bounds_variable_name +
" has to have exactly 2 bounds per time record");
581 "coordinate variable " + dimension_name +
" is missing");
587 if (input_units_string.empty()) {
589 "variable '%s' does not have the units attribute",
590 dimension_name.c_str());
593 input_units =
units::Unit(unit_system, input_units_string);
596 std::vector<double> result(length * 2);
598 file.
read_variable(bounds_variable_name, {0, 0}, {(unsigned)length, 2}, result.data());
604 e.
add_context(
"reading bounds variable '%s' from '%s'", bounds_variable_name.c_str(),
605 file.
name().c_str());
614 const std::string &time_name,
const std::string &time_units,
615 std::vector<double> ×, std::vector<double> &bounds) {
631 if (time_bounds_name.empty()) {
636 bounds =
io::read_bounds(file, time_bounds_name, time_units, unit_system);
638 if (2 * N != bounds.size()) {
640 "each time record has to have 2 bounds");
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");
657 const File &file,
unsigned int time,
double *output) {
659 const auto &log = *grid.ctx()->log();
664 if (not var.exists) {
667 variable.
get_string(
"standard_name").c_str(), file.
name().c_str());
676 for (
const auto &dim : variable.
dimensions()) {
683 int input_spatial_dim_count = 0;
684 size_t matching_dim_count = 0;
687 for (
const auto &d : input_dims) {
691 ++input_spatial_dim_count;
694 if (axes.find(dim_type) != axes.end()) {
695 ++matching_dim_count;
699 if (axes.size() != matching_dim_count) {
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(),
708 variable.
get_string(
"long_name").c_str(),
static_cast<int>(axes.size()));
713 size_t nlevels = std::max(variable.
levels().size(), (
size_t)1);
716 {(int)time, grid.xs(), grid.ys(), 0},
717 {1, grid.xm(), grid.ym(), (int)nlevels},
721 const std::string &internal_units = variable[
"units"];
723 input_units =
check_units(variable, input_units, log);
726 size_t size = grid.xm() * grid.ym() * nlevels;
739 const std::vector<double> &z_internal) {
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;
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,
767 if (z_internal.empty()) {
769 "Internal vertical grid has 0 levels. This should never happen.");
772 if (z_internal.size() == 1 and input.
z.size() > 1) {
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()));
781 if (z_internal.size() > 1 and input.
z.size() <= 1) {
785 "trying to read in a 3D field but the input file %s contains\n"
790 if (z_internal.size() > 1 and (not input.
z.empty())) {
794 z_min = z_internal.front(), z_max = z_internal.back();
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);
810 const std::vector<double> &internal_z_levels,
bool allow_extrapolation) {
813 "input x coordinate has to be strictly increasing");
818 "input y coordinate has to be strictly increasing");
823 "input vertical grid has to be strictly increasing");
826 if (not allow_extrapolation) {
838 const std::string &variable_name) {
842 for (
const auto &d : dims) {
852 const std::string &variable_name,
853 std::shared_ptr<units::System> unit_system) {
863 unsigned int nattrs = file.
nattributes(variable_name);
865 for (
unsigned int j = 0; j < nattrs; ++j) {
866 std::string attribute_name = file.
attribute_name(variable_name, j);
878 e.
add_context(
"reading attributes of variable '%s' from '%s'",
879 variable_name.c_str(), file.
name().c_str());
void read_variable(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, double *ip) const
std::string attribute_name(const std::string &var_name, unsigned int n) const
AxisType dimension_type(const std::string &name, units::System::Ptr unit_system) const
Get the "type" of a dimension.
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.
bool variable_exists(const std::string &short_name) const
Checks if a variable exists.
io::Type attribute_type(const std::string &var_name, const std::string &att_name) const
std::vector< double > read_double_attribute(const std::string &var_name, const std::string &att_name) const
Get a double attribute.
unsigned int nattributes(const std::string &var_name) const
unsigned int dimension_length(const std::string &name) const
Get the length of a dimension.
std::vector< std::string > dimensions(const std::string &variable_name) const
std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const
Get a text attribute.
High-level PISM I/O class.
const grid::DistributedGridInfo & info() const
std::shared_ptr< const Context > ctx() const
Return execution context this grid corresponds to.
int xm() const
Width of this processor's sub-domain.
int ym() const
Width of this processor's sub-domain.
Describes the PISM grid and the distribution of data across processors.
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.
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
double Lx
domain half-width in the X direction
std::vector< double > y
y coordinates
double y0
y-coordinate of the domain center
double x0
x-coordinate of the domain center
std::vector< double > x
x coordinates
double Ly
domain half-width in the Y direction
void convert_doubles(double *data, size_t length) const
std::shared_ptr< System > Ptr
#define PISM_ERROR_LOCATION
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 > ×, 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)
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.
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)
double vector_max(const std::vector< double > &input)
std::vector< unsigned int > start
std::vector< unsigned int > count