update to MDAL 0.3.3

This commit is contained in:
Peter Petrik 2019-05-27 13:15:59 +02:00 committed by Nyall Dawson
parent 507c10c7c1
commit f33ebc8100
5 changed files with 21 additions and 41 deletions

View File

@ -34,11 +34,6 @@ struct VertexCompare
} }
}; };
static inline bool is_nodata( double val )
{
return MDAL::equals( val, -9999.0, 1e-8 );
}
static std::string fileNameFromDir( const std::string &mainFileName, const std::string &name ) static std::string fileNameFromDir( const std::string &mainFileName, const std::string &name )
{ {
std::string dir = MDAL::dirName( mainFileName ); std::string dir = MDAL::dirName( mainFileName );
@ -47,7 +42,7 @@ static std::string fileNameFromDir( const std::string &mainFileName, const std::
static double getDouble( double val ) static double getDouble( double val )
{ {
if ( is_nodata( val ) ) if ( MDAL::equals( val, 0.0, 1e-8 ) )
{ {
return MDAL_NAN; return MDAL_NAN;
} }
@ -58,20 +53,12 @@ static double getDouble( double val )
} }
static double getDouble( const std::string &val ) static double getDouble( const std::string &val )
{
if ( MDAL::isNumber( val ) )
{ {
double valF = MDAL::toDouble( val ); double valF = MDAL::toDouble( val );
return getDouble( valF ); return getDouble( valF );
} }
else
{
return MDAL_NAN;
}
}
void MDAL::DriverFlo2D::addStaticDataset( void MDAL::DriverFlo2D::addStaticDataset(
bool isOnVertices,
std::vector<double> &vals, std::vector<double> &vals,
const std::string &groupName, const std::string &groupName,
const std::string &datFileName ) const std::string &datFileName )
@ -82,7 +69,7 @@ void MDAL::DriverFlo2D::addStaticDataset(
datFileName, datFileName,
groupName groupName
); );
group->setIsOnVertices( isOnVertices ); group->setIsOnVertices( false );
group->setIsScalar( true ); group->setIsScalar( true );
std::shared_ptr<MDAL::MemoryDataset> dataset = std::make_shared< MemoryDataset >( group.get() ); std::shared_ptr<MDAL::MemoryDataset> dataset = std::make_shared< MemoryDataset >( group.get() );
@ -257,7 +244,7 @@ void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const s
double depth = getDouble( lineParts[1] ); double depth = getDouble( lineParts[1] );
depthDataset->values()[face_idx] = depth; depthDataset->values()[face_idx] = depth;
if ( !is_nodata( depth ) ) depth += elevations[face_idx]; if ( !std::isnan( depth ) ) depth += elevations[face_idx];
waterLevelDataset->values()[face_idx] = depth; waterLevelDataset->values()[face_idx] = depth;
face_idx ++; face_idx ++;
@ -295,9 +282,9 @@ void MDAL::DriverFlo2D::parseDEPTHFile( const std::string &datFileName, const st
std::ifstream depthStream( depthFile, std::ifstream::in ); std::ifstream depthStream( depthFile, std::ifstream::in );
std::string line; std::string line;
size_t nVertices = mMesh->verticesCount(); size_t nFaces = mMesh->facesCount();
std::vector<double> maxDepth( nVertices ); std::vector<double> maxDepth( nFaces );
std::vector<double> maxWaterLevel( nVertices ); std::vector<double> maxWaterLevel( nFaces );
size_t vertex_idx = 0; size_t vertex_idx = 0;
@ -305,7 +292,7 @@ void MDAL::DriverFlo2D::parseDEPTHFile( const std::string &datFileName, const st
while ( std::getline( depthStream, line ) ) while ( std::getline( depthStream, line ) )
{ {
line = MDAL::rtrim( line ); line = MDAL::rtrim( line );
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nFaces ) throw MDAL_Status::Err_IncompatibleMesh;
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 4 ) if ( lineParts.size() != 4 )
@ -317,23 +304,23 @@ void MDAL::DriverFlo2D::parseDEPTHFile( const std::string &datFileName, const st
maxDepth[vertex_idx] = val; maxDepth[vertex_idx] = val;
//water level //water level
if ( !is_nodata( val ) ) val += elevations[vertex_idx]; if ( !std::isnan( val ) ) val += elevations[vertex_idx];
maxWaterLevel[vertex_idx] = val; maxWaterLevel[vertex_idx] = val;
vertex_idx++; vertex_idx++;
} }
addStaticDataset( true, maxDepth, "Depth/Maximums", datFileName ); addStaticDataset( maxDepth, "Depth/Maximums", datFileName );
addStaticDataset( true, maxWaterLevel, "Water Level/Maximums", datFileName ); addStaticDataset( maxWaterLevel, "Water Level/Maximums", datFileName );
} }
void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName ) void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
{ {
// these files are optional, so if not present, reading is skipped // these files are optional, so if not present, reading is skipped
size_t nVertices = mMesh->verticesCount(); size_t nFaces = mMesh->facesCount();
std::vector<double> maxVel( nVertices ); std::vector<double> maxVel( nFaces );
{ {
std::string velocityFile( fileNameFromDir( datFileName, "VELFP.OUT" ) ); std::string velocityFile( fileNameFromDir( datFileName, "VELFP.OUT" ) );
@ -350,7 +337,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
// VELFP.OUT - COORDINATES (ELEM NUM, X, Y, MAX VEL) - Maximum floodplain flow velocity; // VELFP.OUT - COORDINATES (ELEM NUM, X, Y, MAX VEL) - Maximum floodplain flow velocity;
while ( std::getline( velocityStream, line ) ) while ( std::getline( velocityStream, line ) )
{ {
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nFaces ) throw MDAL_Status::Err_IncompatibleMesh;
line = MDAL::rtrim( line ); line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
@ -381,7 +368,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
// VELOC.OUT - COORDINATES (ELEM NUM, X, Y, MAX VEL) - Maximum channel flow velocity // VELOC.OUT - COORDINATES (ELEM NUM, X, Y, MAX VEL) - Maximum channel flow velocity
while ( std::getline( velocityStream, line ) ) while ( std::getline( velocityStream, line ) )
{ {
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nFaces ) throw MDAL_Status::Err_IncompatibleMesh;
line = MDAL::rtrim( line ); line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
@ -391,7 +378,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
} }
double val = getDouble( lineParts[3] ); double val = getDouble( lineParts[3] );
if ( !is_nodata( val ) ) // overwrite value from VELFP if it is not 0 if ( !std::isnan( val ) ) // overwrite value from VELFP if it is not 0
{ {
maxVel[vertex_idx] = val; maxVel[vertex_idx] = val;
} }
@ -400,7 +387,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
} }
} }
addStaticDataset( true, maxVel, "Velocity/Maximums", datFileName ); addStaticDataset( maxVel, "Velocity/Maximums", datFileName );
} }
double MDAL::DriverFlo2D::calcCellSize( const std::vector<CellCenter> &cells ) double MDAL::DriverFlo2D::calcCellSize( const std::vector<CellCenter> &cells )
@ -671,7 +658,7 @@ std::unique_ptr< MDAL::Mesh > MDAL::DriverFlo2D::load( const std::string &result
createMesh( cells, cell_size / 2.0 ); createMesh( cells, cell_size / 2.0 );
// create output for bed elevation // create output for bed elevation
addStaticDataset( false, elevations, "Bed Elevation", mDatFileName ); addStaticDataset( elevations, "Bed Elevation", mDatFileName );
if ( parseHDF5Datasets( mDatFileName ) ) if ( parseHDF5Datasets( mDatFileName ) )
{ {

View File

@ -45,7 +45,7 @@ namespace MDAL
void parseTIMDEPFile( const std::string &datFileName, const std::vector<double> &elevations ); void parseTIMDEPFile( const std::string &datFileName, const std::vector<double> &elevations );
void parseFPLAINFile( std::vector<double> &elevations, const std::string &datFileName, std::vector<CellCenter> &cells ); void parseFPLAINFile( std::vector<double> &elevations, const std::string &datFileName, std::vector<CellCenter> &cells );
void parseCADPTSFile( const std::string &datFileName, std::vector<CellCenter> &cells ); void parseCADPTSFile( const std::string &datFileName, std::vector<CellCenter> &cells );
void addStaticDataset( bool isOnVertices, std::vector<double> &vals, const std::string &groupName, const std::string &datFileName ); void addStaticDataset( std::vector<double> &vals, const std::string &groupName, const std::string &datFileName );
static MDAL::Vertex createVertex( size_t position, double half_cell_size, const CellCenter &cell ); static MDAL::Vertex createVertex( size_t position, double half_cell_size, const CellCenter &cell );
static double calcCellSize( const std::vector<CellCenter> &cells ); static double calcCellSize( const std::vector<CellCenter> &cells );
}; };

View File

@ -22,7 +22,7 @@ static MDAL_Status sLastStatus;
const char *MDAL_Version() const char *MDAL_Version()
{ {
return "0.3.2"; return "0.3.3";
} }
MDAL_Status MDAL_LastStatus() MDAL_Status MDAL_LastStatus()

View File

@ -113,12 +113,6 @@ double MDAL::toDouble( const std::string &str )
return atof( str.c_str() ); return atof( str.c_str() );
} }
bool MDAL::isNumber( const std::string &str )
{
// https://stackoverflow.com/a/16465826/2838364
return ( strspn( str.c_str(), "-.0123456789" ) == str.size() );
}
int MDAL::toInt( const std::string &str ) int MDAL::toInt( const std::string &str )
{ {
return atoi( str.c_str() ); return atoi( str.c_str() );

View File

@ -65,7 +65,6 @@ namespace MDAL
int toInt( const std::string &str ); int toInt( const std::string &str );
double toDouble( const std::string &str ); double toDouble( const std::string &str );
bool toBool( const std::string &str ); bool toBool( const std::string &str );
bool isNumber( const std::string &str );
/** /**
* Splits by deliminer and skips empty parts. * Splits by deliminer and skips empty parts.