Skip to content

Commit a63794e

Browse files
committed
Handle netcdf with defined lat/lon but NaN in the z val which can happen due to reprojecting nc files
1 parent 2b91c60 commit a63794e

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/metdata.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ void metdata::load_from_netcdf(const std::string& path,std::map<std::string, boo
114114
// #pragma omp parallel for
115115
// hangs, unclear why, critical sections around the json and gdal calls
116116
// don't seem to help. Probably _dD_tree is not thread safe
117+
118+
int skipped = 0; // keep track of how many we skipped due to nans
117119
for (size_t y = 0; y < _nc->get_ysize(); y++)
118120
{
119121
for (size_t x = 0; x < _nc->get_xsize(); x++)
@@ -130,23 +132,16 @@ void metdata::load_from_netcdf(const std::string& path,std::map<std::string, boo
130132

131133
// Some Netcdf files have NaN grid squares, For these cases we will just insert a nullptr station and
132134
// don't add the station to the dD list which is the only way it ever gets to modules
133-
if ( std::isnan(latitude) &&
134-
std::isnan(longitude) )
135+
if ( std::isnan(latitude) ||
136+
std::isnan(longitude) ||
137+
std::isnan(z))
135138
{
136139
_stations.at(index) = nullptr;
140+
++skipped;
137141
continue;
138142
}
139143

140144

141-
142-
if( std::isnan(z))
143-
{
144-
CHM_THROW_EXCEPTION(forcing_error,
145-
"Elevation for x,y=" + std::to_string(x) + ","+ std::to_string(y)+
146-
" is NaN. Regardless of the timestep the model is started from, it looks for timestep = 0 to define the elevations. Ensure it is defined then.");
147-
}
148-
149-
150145
std::string station_name = std::to_string(index); // these don't really have names
151146

152147
//need to convert the input lat/long into the coordinate system our mesh is in
@@ -176,6 +171,14 @@ void metdata::load_from_netcdf(const std::string& path,std::map<std::string, boo
176171
}
177172
}
178173

174+
if( skipped == _nstations)
175+
{
176+
CHM_THROW_EXCEPTION(forcing_error,
177+
"All forcing grid cells were skipped due to being NaN values. Elevation and lat/lon,"
178+
" regardless of the timestep the model is started from, are defined from timestep = 0 "
179+
". Ensure it is defined then.");
180+
}
181+
179182
} catch(netCDF::exceptions::NcException& e)
180183
{
181184
OGRCoordinateTransformation::DestroyCT(coordTrans);

0 commit comments

Comments
 (0)