28 std::multimap<std::string, netCDF::NcVar>
const& variables)
33 double cell_size_y = 0;
37 for (
auto [variable_name, variable] : variables)
39 if (variable.isNull())
41 OGS_FATAL(
"Variable '{}' not found in file.", variable_name);
43 auto const& attributes = variable.getAtts();
44 for (
auto [name, attribute] : attributes)
46 if (name ==
"GeoTransform")
48 std::vector<char> attribute_values;
49 attribute_values.resize(attribute.getAttLength());
50 attribute.getValues(attribute_values.data());
51 std::string
const str_attribute_values(attribute_values.begin(),
52 attribute_values.end());
54 header.
origin[0] = std::stod(strings[0]);
56 header.
origin[1] = std::stod(strings[3]);
57 cell_size_y = std::stod(strings[5]);
59 "GeoTransform attribute values: origin_x: {}, origin_y: "
60 "{}, cell_size_x: {}, cell_size_y: {}",
64 if (name ==
"_FillValue")
66 attribute.getValues(&header.
no_data);
75 std::multimap<std::string, netCDF::NcVar>
const& variables,
76 std::string_view
const var_name, std::size_t
const dimension_number,
80 std::vector<double> raster_data;
81 for (
auto [variable_name, variable] : variables)
85 if (variable_name == var_name)
87 auto const n_dims = variable.getDimCount();
94 std::vector<std::size_t> counts(n_dims, 1);
95 for (
int i = 0; i < n_dims; ++i)
97 counts[i] = variable.getDim(i).getSize();
100 std::vector<std::size_t> start(n_dims, 0);
101 if (dimension_number > counts[0])
104 "variable '{}' has {} dimensions, requested dimension "
106 variable_name, counts[0], dimension_number);
109 start[0] = dimension_number;
120 counts[2] =
static_cast<int>(
121 std::ceil((min_max_points.
max[0] - min_max_points.
min[0]) /
124 counts[1] =
static_cast<int>(
125 std::ceil((min_max_points.
max[1] - min_max_points.
min[1]) /
129 start[2] =
static_cast<int>(std::floor(
132 start[1] =
static_cast<int>(std::floor(
140 header.
n_cols = counts[2];
141 header.
n_rows = counts[1];
145 std::size_t
const total_length =
146 std::accumulate(counts.cbegin(), counts.cend(), 1,
147 std::multiplies<std::size_t>());
148 raster_data.resize(total_length);
149 variable.getVar(start, counts, raster_data.data());
151 std::replace(raster_data.begin(), raster_data.end(), header.
no_data,
159 std::filesystem::path
const& filepath,
160 std::string_view
const var_name,
161 std::size_t
const dimension_number,
164 netCDF::NcFile dataset(filepath.string(), netCDF::NcFile::read);
165 if (dataset.isNull())
167 OGS_FATAL(
"Error opening file '{}'.", filepath.string());
170 auto const& variables = dataset.getVars();
173 variables, var_name, dimension_number, min_max_points, header);
175 return std::make_unique<GeoLib::Raster>(header, raster_data.begin(),
181 std::filesystem::path
const& path,
182 std::filesystem::path filename,
183 std::string
const& var_name,
184 std::size_t
const dimension_number,
187 INFO(
"readRasterFromFile: '{}/{}'", path.string(), filename.string());
189 if (filename.extension() ==
".nc")
192 auto raster =
readNetCDF(path / filename, var_name, dimension_number,
197 std::to_string(dimension_number),
200 OGS_FATAL(
"OGS was not build with NetCDF support. Can not read {}",
201 (path / filename).
string());
204 auto raster = std::unique_ptr<GeoLib::Raster>(
206 if (raster ==
nullptr)
208 OGS_FATAL(
"Could not read raster from file '{}'.",
209 (path / filename).
string());
213 std::to_string(dimension_number),