Skip to content

Commit

Permalink
mdal 074
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik authored and nyalldawson committed Jan 14, 2021
1 parent 798d97f commit 0761f88
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion external/mdal/frmts/mdal_hdf5.cpp
Expand Up @@ -186,7 +186,7 @@ HdfDataset::HdfDataset( hid_t file, const std::string &path )

HdfDataset::~HdfDataset() = default;

bool HdfDataset::isValid() const { return d->id >= 0; }
bool HdfDataset::isValid() const { return d && d->id >= 0; }

hid_t HdfDataset::id() const { return d->id; }

Expand Down
3 changes: 3 additions & 0 deletions external/mdal/frmts/mdal_hdf5.hpp
Expand Up @@ -193,6 +193,9 @@ class HdfDataset
public:
typedef HdfH<H5I_DATASET> Handle;

//! creates invalid dataset
HdfDataset() = default;

//! Create new, simple 1 dimensional dataset
HdfDataset( hid_t file, const std::string &path, HdfDataType dtype, size_t nItems = 1 );
//! Create new dataset with custom dimensions
Expand Down
56 changes: 39 additions & 17 deletions external/mdal/frmts/mdal_hec2d.cpp
Expand Up @@ -237,7 +237,16 @@ void MDAL::DriverHec2D::readFaceOutput( const HdfFile &hdfFile,
std::string flowAreaName = flowAreaNames[nArea];

HdfGroup gFlowAreaRes = openHdfGroup( rootGroup, flowAreaName );
HdfDataset dsVals = openHdfDataset( gFlowAreaRes, rawDatasetName );
HdfDataset dsVals;
try
{
dsVals = openHdfDataset( gFlowAreaRes, rawDatasetName );
}
catch ( MDAL::Error )
{
return;
}

std::vector<float> vals = dsVals.readArray();

HdfGroup gGeom = openHdfGroup( hdfFile, "Geometry" );
Expand Down Expand Up @@ -334,7 +343,7 @@ void MDAL::DriverHec2D::readFaceOutput( const HdfFile &hdfFile,
}
}

for ( auto dataset : datasets )
for ( auto &dataset : datasets )
{
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
Expand Down Expand Up @@ -371,7 +380,7 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readElemOutput( const
std::shared_ptr<MDAL::MemoryDataset2D> bed_elevation,
const DateTime &referenceTime )
{
double eps = std::numeric_limits<double>::min();
double eps = static_cast<double>( std::numeric_limits<float>::epsilon() ); //hecras use float so comparison needs to use float epsilon

std::shared_ptr<DatasetGroup> group = std::make_shared< DatasetGroup >(
name(),
Expand All @@ -398,7 +407,16 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readElemOutput( const
std::string flowAreaName = flowAreaNames[nArea];
HdfGroup gFlowAreaRes = openHdfGroup( rootGroup, flowAreaName );

HdfDataset dsVals = openHdfDataset( gFlowAreaRes, rawDatasetName );
HdfDataset dsVals;
try
{
dsVals = openHdfDataset( gFlowAreaRes, rawDatasetName );
}
catch ( MDAL::Error )
{
return nullptr;
}

std::vector<float> vals = dsVals.readArray();

for ( size_t tidx = 0; tidx < times.size(); ++tidx )
Expand All @@ -410,7 +428,7 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readElemOutput( const
{
size_t idx = tidx * nAreaElements + i;
size_t eInx = areaElemStartIndex[nArea] + i;
double val = static_cast<double>( vals[idx] );
double val = static_cast<double>( vals[idx] );
if ( !std::isnan( val ) )
{
if ( !bed_elevation )
Expand All @@ -431,7 +449,7 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readElemOutput( const
{
assert( bed_elevation );
double bed_elev = bed_elevation->scalarValue( eInx );
if ( std::isnan( bed_elev ) || fabs( bed_elev - val ) > eps ) // change from bed elevation
if ( std::isnan( bed_elev ) || fabs( val - bed_elev ) > ( val + bed_elev )*eps ) // change from bed elevation
{
values[eInx] = val;
}
Expand All @@ -442,7 +460,7 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readElemOutput( const
}
}

for ( auto dataset : datasets )
for ( auto &dataset : datasets )
{
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
Expand All @@ -461,16 +479,20 @@ std::shared_ptr<MDAL::MemoryDataset2D> MDAL::DriverHec2D::readBedElevation(
std::vector<MDAL::RelativeTimestamp> times( 1 );
DateTime referenceTime;

return readElemOutput(
gGeom2DFlowAreas,
areaElemStartIndex,
flowAreaNames,
"Cells Minimum Elevation",
"Bed Elevation",
times,
std::shared_ptr<MDAL::MemoryDataset2D>(),
referenceTime
);
std::shared_ptr<MDAL::MemoryDataset2D> bedElevation = readElemOutput(
gGeom2DFlowAreas,
areaElemStartIndex,
flowAreaNames,
"Cells Minimum Elevation",
"Bed Elevation",
times,
std::shared_ptr<MDAL::MemoryDataset2D>(),
referenceTime
);

if ( ! bedElevation ) throw MDAL::Error( MDAL_Status::Err_InvalidData, "Unable to read bed elevation values" );

return bedElevation;
}

void MDAL::DriverHec2D::readElemResults(
Expand Down
2 changes: 1 addition & 1 deletion external/mdal/mdal.cpp
Expand Up @@ -21,7 +21,7 @@ static const char *EMPTY_STR = "";

const char *MDAL_Version()
{
return "0.7.3";
return "0.7.4";
}

MDAL_Status MDAL_LastStatus()
Expand Down

0 comments on commit 0761f88

Please sign in to comment.