10 std::cerr << "\n See " << __LINE__ << " in file " << __FILE__ << ". Exiting...\n"; \
11 georegion_file.close(); \
14 #define CHECK_BOUNDS(max_val, min_val) \
16 if (min_val > max_val) { \
17 std::cerr << "--Error--: " << #min_val << "=" << min_val << " is larger than " << #max_val << "=" \
23 netCDF::NcFile georegion_file;
24 netCDF::NcVar georegion_variable;
25 netCDF::NcVar lat_variable;
26 netCDF::NcVar lon_variable;
40 var =
file.getVar(varname);
42 std::cerr <<
"--Error--: variable " << varname <<
" is not found in the georegion file";
48 georegion_file.close();
52 if (georegion_variable.isNull()) {
53 std::cout <<
"Loading the georegion file: " <<
input->georegionfile << std::endl;
54 georegion_file.open(
input->georegionfile, netCDF::NcFile::read);
55 read_variable(georegion_file, georegion_variable,
"georegion");
58 std::vector<netCDF::NcDim> dims = georegion_variable.getDims();
59 if (dims.size() != 2) {
60 std::cerr <<
"--Error-- : The georegion variable should be a two dimensional variable.";
63 if (dims.at(0).getName() !=
"lat" || dims.at(1).getName() !=
"lon") {
64 std::cerr <<
"--Error-- : The first dimension of the georegion should be lat, the second one "
65 "should be lon. The dimensions found in the geofile are: first \""
66 << dims.at(0).getName() <<
"\" and second \"" << dims.at(1).getName() <<
"\"";
69 nlat = dims.at(0).getSize();
70 nlon = dims.at(1).getSize();
71 std::vector<netCDF::NcDim> dim_lat = lat_variable.getDims();
72 std::vector<netCDF::NcDim> dim_lon = lon_variable.getDims();
73 if (dim_lat.size() != 1) {
74 std::cerr <<
"--Error-- : The latitude variable should be a one dimensional variable.";
77 if (dim_lon.size() != 1) {
78 std::cerr <<
"--Error-- : The latitude variable should be a one dimensional variable.";
81 if (dim_lat.at(0).getName() !=
"lat" || dim_lon.at(0).getName() !=
"lon") {
82 std::cerr <<
"--Error-- : The dimension mismatch between latitude/longitude and the georegion";
86 if (netCDF::NcType::nc_BYTE != georegion_variable.getType().getTypeClass()) {
87 std::cerr <<
"--Error-- : The datatype of the georegion should be signed byte";
90 if (netCDF::NcType::nc_DOUBLE != lat_variable.getType().getTypeClass()) {
91 std::cerr <<
"--Error-- : The datatype of the latitude should be double";
94 if (netCDF::NcType::nc_DOUBLE != lon_variable.getType().getTypeClass()) {
95 std::cerr <<
"--Error-- : The datatype of the longitude should be double";
98 latitude = std::vector<double>(nlat);
100 lat_variable.getVar(
latitude.data());
118 netCDF::NcGroupAtt geospatial_lat_min = georegion_file.getAtt(
"geospatial_lat_min");
119 netCDF::NcGroupAtt geospatial_lat_max = georegion_file.getAtt(
"geospatial_lat_max");
120 netCDF::NcGroupAtt geospatial_lon_min = georegion_file.getAtt(
"geospatial_lon_min");
121 netCDF::NcGroupAtt geospatial_lon_max = georegion_file.getAtt(
"geospatial_lon_max");
122 if (!geospatial_lat_min.isNull()) {
123 geospatial_lat_min.getValues(&
min_lat);
125 if (!geospatial_lat_max.isNull()) {
126 geospatial_lat_max.getValues(&max_lat);
128 if (!geospatial_lon_min.isNull()) {
129 geospatial_lon_min.getValues(&min_lon);
131 if (!geospatial_lon_max.isNull()) {
132 geospatial_lon_max.getValues(&
max_lon);
142 size_t ilat =
static_cast<size_t>(std::round((
lat -
latitude[0]) / step_lat));
143 size_t ilon =
static_cast<size_t>(std::round((
lon -
longitude[0]) / step_lon));
146 std::vector<size_t>
start = {ilat, ilon};
147 std::vector<size_t>
count = {1, 1};
149 georegion_variable.getVar(
start,
count, &flag);