Skip to content

Commit

Permalink
update to MDAL 0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed May 7, 2019
1 parent e213bde commit 435b594
Show file tree
Hide file tree
Showing 20 changed files with 1,208 additions and 106 deletions.
16 changes: 5 additions & 11 deletions external/mdal/frmts/mdal_3di.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ MDAL::Driver3Di *MDAL::Driver3Di::create()
return new Driver3Di(); return new Driver3Di();
} }


MDAL::CFDimensions MDAL::Driver3Di::populateDimensions( const NetCDFFile &ncFile ) MDAL::CFDimensions MDAL::Driver3Di::populateDimensions( )
{ {
CFDimensions dims; CFDimensions dims;
size_t count; size_t count;
int ncid; int ncid;


// 2D Mesh // 2D Mesh
ncFile.getDimension( "nMesh2D_nodes", &count, &ncid ); mNcFile.getDimension( "nMesh2D_nodes", &count, &ncid );
dims.setDimension( CFDimensions::Face2D, count, ncid ); dims.setDimension( CFDimensions::Face2D, count, ncid );


ncFile.getDimension( "nCorner_Nodes", &count, &ncid ); mNcFile.getDimension( "nCorner_Nodes", &count, &ncid );
dims.setDimension( CFDimensions::MaxVerticesInFace, count, ncid ); dims.setDimension( CFDimensions::MaxVerticesInFace, count, ncid );


// Vertices count is populated later in populateFacesAndVertices // Vertices count is populated later in populateFacesAndVertices
// it is not known from the array dimensions // it is not known from the array dimensions


// Time // Time
ncFile.getDimension( "time", &count, &ncid ); mNcFile.getDimension( "time", &count, &ncid );
dims.setDimension( CFDimensions::Time, count, ncid ); dims.setDimension( CFDimensions::Time, count, ncid );


return dims; return dims;
Expand Down Expand Up @@ -111,7 +111,7 @@ void MDAL::Driver3Di::populateFacesAndVertices( Vertices &vertices, Faces &faces
mDimensions.setDimension( CFDimensions::Vertex2D, vertices.size() ); mDimensions.setDimension( CFDimensions::Vertex2D, vertices.size() );
} }


void MDAL::Driver3Di::addBedElevation( MDAL::Mesh *mesh ) void MDAL::Driver3Di::addBedElevation( MemoryMesh *mesh )
{ {
assert( mesh ); assert( mesh );
if ( 0 == mesh->facesCount() ) if ( 0 == mesh->facesCount() )
Expand Down Expand Up @@ -189,12 +189,6 @@ std::set<std::string> MDAL::Driver3Di::ignoreNetCDFVariables()
return ignore_variables; return ignore_variables;
} }


std::string MDAL::Driver3Di::nameSuffix( MDAL::CFDimensions::Type type )
{
MDAL_UNUSED( type );
return "";
}

void MDAL::Driver3Di::parseNetCDFVariableMetadata( int varid, const std::string &variableName, std::string &name, bool *is_vector, bool *is_x ) void MDAL::Driver3Di::parseNetCDFVariableMetadata( int varid, const std::string &variableName, std::string &name, bool *is_vector, bool *is_x )
{ {
*is_vector = false; *is_vector = false;
Expand Down
5 changes: 2 additions & 3 deletions external/mdal/frmts/mdal_3di.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ namespace MDAL
Driver3Di *create() override; Driver3Di *create() override;


private: private:
CFDimensions populateDimensions( const NetCDFFile &ncFile ) override; CFDimensions populateDimensions( ) override;
void populateFacesAndVertices( Vertices &vertices, Faces &faces ) override; void populateFacesAndVertices( Vertices &vertices, Faces &faces ) override;
void addBedElevation( Mesh *mesh ) override; void addBedElevation( MemoryMesh *mesh ) override;
std::string getCoordinateSystemVariableName() override; std::string getCoordinateSystemVariableName() override;
std::set<std::string> ignoreNetCDFVariables() override; std::set<std::string> ignoreNetCDFVariables() override;
std::string nameSuffix( CFDimensions::Type type ) override;
void parseNetCDFVariableMetadata( int varid, const std::string &variableName, void parseNetCDFVariableMetadata( int varid, const std::string &variableName,
std::string &name, bool *is_vector, bool *is_x ) override; std::string &name, bool *is_vector, bool *is_x ) override;


Expand Down
18 changes: 8 additions & 10 deletions external/mdal/frmts/mdal_cf.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,16 +79,13 @@ MDAL::cfdataset_info_map MDAL::DriverCF::parseDatasetGroupInfo()
continue; continue;


size_t arr_size = mDimensions.size( mDimensions.type( dimid ) ) * nTimesteps; size_t arr_size = mDimensions.size( mDimensions.type( dimid ) ) * nTimesteps;
std::string suffix = nameSuffix( mDimensions.type( dimid ) );


// Get name, if it is vector and if it is x or y // Get name, if it is vector and if it is x or y
std::string name; std::string name;
bool is_vector = true; bool is_vector = true;
bool is_x = false; bool is_x = false;


parseNetCDFVariableMetadata( varid, variable_name, name, &is_vector, &is_x ); parseNetCDFVariableMetadata( varid, variable_name, name, &is_vector, &is_x );
if ( !suffix.empty() )
name = name + " (" + suffix + ")";


// Add it to the map // Add it to the map
auto it = dsinfo_map.find( name ); auto it = dsinfo_map.find( name );
Expand Down Expand Up @@ -136,12 +133,12 @@ static void populate_vals( bool is_vector, double *vals, size_t i,
{ {
if ( is_vector ) if ( is_vector )
{ {
vals[2 * i] = MDAL::safeValue( vals_x[idx], fill_val_x ); vals[2 * i] = MDAL::safeValue( vals_x[idx], fill_val_x );
vals[2 * i + 1] = MDAL::safeValue( vals_y[idx], fill_val_y ); vals[2 * i + 1] = MDAL::safeValue( vals_y[idx], fill_val_y );
} }
else else
{ {
vals[i] = MDAL::safeValue( vals_x[idx], fill_val_x ); vals[i] = MDAL::safeValue( vals_x[idx], fill_val_x );
} }
} }


Expand Down Expand Up @@ -190,7 +187,7 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl


// read X data // read X data
double fill_val_x = mNcFile.getFillValue( dsi.ncid_x ); double fill_val_x = mNcFile.getFillValue( dsi.ncid_x );
std::vector<double> vals_x( dsi.arr_size ); std::vector<double> vals_x( dsi.arr_size, std::numeric_limits<double>::quiet_NaN() );
if ( nc_get_var_double( mNcFile.handle(), dsi.ncid_x, vals_x.data() ) ) CF_THROW_ERR; if ( nc_get_var_double( mNcFile.handle(), dsi.ncid_x, vals_x.data() ) ) CF_THROW_ERR;


// read Y data if vector // read Y data if vector
Expand All @@ -199,7 +196,7 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl
if ( dsi.is_vector ) if ( dsi.is_vector )
{ {
fill_val_y = mNcFile.getFillValue( dsi.ncid_y ); fill_val_y = mNcFile.getFillValue( dsi.ncid_y );
vals_y.resize( dsi.arr_size ); vals_y.resize( dsi.arr_size, std::numeric_limits<double>::quiet_NaN() );
if ( nc_get_var_double( mNcFile.handle(), dsi.ncid_y, vals_y.data() ) ) CF_THROW_ERR; if ( nc_get_var_double( mNcFile.handle(), dsi.ncid_y, vals_y.data() ) ) CF_THROW_ERR;
} }


Expand Down Expand Up @@ -254,7 +251,8 @@ bool MDAL::DriverCF::canRead( const std::string &uri )
{ {
NetCDFFile ncFile; NetCDFFile ncFile;
ncFile.openFile( uri ); ncFile.openFile( uri );
populateDimensions( ncFile ); mNcFile = ncFile;
populateDimensions( );
} }
catch ( MDAL_Status ) catch ( MDAL_Status )
{ {
Expand Down Expand Up @@ -318,7 +316,7 @@ std::unique_ptr< MDAL::Mesh > MDAL::DriverCF::load( const std::string &fileName,
mNcFile.openFile( mFileName ); mNcFile.openFile( mFileName );


// Parse dimensions // Parse dimensions
mDimensions = populateDimensions( mNcFile ); mDimensions = populateDimensions( );


// Create mMesh // Create mMesh
Faces faces; Faces faces;
Expand Down
6 changes: 3 additions & 3 deletions external/mdal/frmts/mdal_cf.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace MDAL


//! NetCDF Climate and Forecast (CF) Metadata Conventions //! NetCDF Climate and Forecast (CF) Metadata Conventions
//! http://cfconventions.org //! http://cfconventions.org
//! and http://ugrid-conventions.github.io/ugrid-conventions/
class DriverCF: public Driver class DriverCF: public Driver
{ {
public: public:
Expand All @@ -74,12 +75,11 @@ namespace MDAL
std::unique_ptr< Mesh > load( const std::string &fileName, MDAL_Status *status ) override; std::unique_ptr< Mesh > load( const std::string &fileName, MDAL_Status *status ) override;


protected: protected:
virtual CFDimensions populateDimensions( const NetCDFFile &ncFile ) = 0; virtual CFDimensions populateDimensions( ) = 0;
virtual void populateFacesAndVertices( Vertices &vertices, Faces &faces ) = 0; virtual void populateFacesAndVertices( Vertices &vertices, Faces &faces ) = 0;
virtual void addBedElevation( MDAL::Mesh *mesh ) = 0; virtual void addBedElevation( MDAL::MemoryMesh *mesh ) = 0;
virtual std::string getCoordinateSystemVariableName() = 0; virtual std::string getCoordinateSystemVariableName() = 0;
virtual std::set<std::string> ignoreNetCDFVariables() = 0; virtual std::set<std::string> ignoreNetCDFVariables() = 0;
virtual std::string nameSuffix( CFDimensions::Type type ) = 0;
virtual void parseNetCDFVariableMetadata( int varid, const std::string &variableName, virtual void parseNetCDFVariableMetadata( int varid, const std::string &variableName,
std::string &name, bool *is_vector, bool *is_x ) = 0; std::string &name, bool *is_vector, bool *is_x ) = 0;


Expand Down
6 changes: 6 additions & 0 deletions external/mdal/frmts/mdal_flo2d.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void MDAL::DriverFlo2D::parseCADPTSFile( const std::string &datFileName, std::ve
// CADPTS.DAT - COORDINATES OF CELL CENTERS (ELEM NUM, X, Y) // CADPTS.DAT - COORDINATES OF CELL CENTERS (ELEM NUM, X, Y)
while ( std::getline( cadptsStream, line ) ) while ( std::getline( cadptsStream, line ) )
{ {
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 3 ) if ( lineParts.size() != 3 )
{ {
Expand Down Expand Up @@ -140,6 +141,7 @@ void MDAL::DriverFlo2D::parseFPLAINFile( std::vector<double> &elevations,


while ( std::getline( fplainStream, line ) ) while ( std::getline( fplainStream, line ) )
{ {
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 7 ) if ( lineParts.size() != 7 )
{ {
Expand Down Expand Up @@ -220,6 +222,7 @@ void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const s


while ( std::getline( inStream, line ) ) while ( std::getline( inStream, line ) )
{ {
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() == 1 ) if ( lineParts.size() == 1 )
{ {
Expand Down Expand Up @@ -301,6 +304,7 @@ void MDAL::DriverFlo2D::parseDEPTHFile( const std::string &datFileName, const st
// DEPTH.OUT - COORDINATES (ELEM NUM, X, Y, MAX DEPTH) // DEPTH.OUT - COORDINATES (ELEM NUM, X, Y, MAX DEPTH)
while ( std::getline( depthStream, line ) ) while ( std::getline( depthStream, line ) )
{ {
line = MDAL::rtrim( line );
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh;


std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
Expand Down Expand Up @@ -348,6 +352,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
{ {
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh;


line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 4 ) if ( lineParts.size() != 4 )
{ {
Expand Down Expand Up @@ -378,6 +383,7 @@ void MDAL::DriverFlo2D::parseVELFPVELOCFile( const std::string &datFileName )
{ {
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh; if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh;


line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' ); std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 4 ) if ( lineParts.size() != 4 )
{ {
Expand Down
43 changes: 1 addition & 42 deletions external/mdal/frmts/mdal_gdal.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -396,47 +396,6 @@ void MDAL::DriverGdal::addDataToOutput( GDALRasterBandH raster_band, std::shared
} }
} }


void MDAL::DriverGdal::activateFaces( std::shared_ptr<MemoryDataset> tos )
{
// only for data on vertices
if ( !tos->group()->isOnVertices() )
return;

bool isScalar = tos->group()->isScalar();

// Activate only Faces that do all Vertex's outputs with some data
int *active = tos->active();
const double *values = tos->constValues();

for ( unsigned int idx = 0; idx < meshGDALDataset()->mNVolumes; ++idx )
{
Face elem = mMesh->faces.at( idx );
for ( size_t i = 0; i < 4; ++i )
{
const size_t vertexIndex = elem[i];
if ( isScalar )
{
double val = values[vertexIndex];
if ( std::isnan( val ) )
{
active[idx] = 0; //NOT ACTIVE
break;
}
}
else
{
double x = values[2 * vertexIndex];
double y = values[2 * vertexIndex + 1];
if ( std::isnan( x ) || std::isnan( y ) )
{
active[idx] = 0; //NOT ACTIVE
break;
}
}
}
}
}

void MDAL::DriverGdal::addDatasetGroups() void MDAL::DriverGdal::addDatasetGroups()
{ {
// Add dataset to mMesh // Add dataset to mMesh
Expand Down Expand Up @@ -465,7 +424,7 @@ void MDAL::DriverGdal::addDatasetGroups()
{ {
addDataToOutput( raster_bands[i], dataset, is_vector, i == 0 ); addDataToOutput( raster_bands[i], dataset, is_vector, i == 0 );
} }
activateFaces( dataset ); MDAL::activateFaces( mMesh.get(), dataset );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) ); dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset ); group->datasets.push_back( dataset );
} }
Expand Down
1 change: 0 additions & 1 deletion external/mdal/frmts/mdal_gdal.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ namespace MDAL
metadata_hash parseMetadata( GDALMajorObjectH gdalBand, const char *pszDomain = nullptr ); metadata_hash parseMetadata( GDALMajorObjectH gdalBand, const char *pszDomain = nullptr );
void addDataToOutput( GDALRasterBandH raster_band, std::shared_ptr<MemoryDataset> tos, bool is_vector, bool is_x ); void addDataToOutput( GDALRasterBandH raster_band, std::shared_ptr<MemoryDataset> tos, bool is_vector, bool is_x );
bool addSrcProj(); bool addSrcProj();
void activateFaces( std::shared_ptr<MemoryDataset> tos );
void addDatasetGroups(); void addDatasetGroups();
void createMesh(); void createMesh();
void parseRasterBands( const GdalDataset *cfGDALDataset ); void parseRasterBands( const GdalDataset *cfGDALDataset );
Expand Down
13 changes: 10 additions & 3 deletions external/mdal/frmts/mdal_netcdf.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ std::string NetCDFFile::getAttrStr( const std::string &name, const std::string &
return getAttrStr( attr_name, arr_id ); return getAttrStr( attr_name, arr_id );
} }


std::string NetCDFFile::getAttrStr( const std::string &name, int varid ) const std::string NetCDFFile::getAttrStr( const std::string &attr_name, int varid ) const
{ {
assert( mNcid != 0 ); assert( mNcid != 0 );


size_t attlen = 0; size_t attlen = 0;


if ( nc_inq_attlen( mNcid, varid, name.c_str(), &attlen ) ) if ( nc_inq_attlen( mNcid, varid, attr_name.c_str(), &attlen ) )
{ {
// attribute is missing // attribute is missing
return std::string(); return std::string();
} }


char *string_attr = static_cast<char *>( malloc( attlen + 1 ) ); char *string_attr = static_cast<char *>( malloc( attlen + 1 ) );


if ( nc_get_att_text( mNcid, varid, name.c_str(), string_attr ) ) throw MDAL_Status::Err_UnknownFormat; if ( nc_get_att_text( mNcid, varid, attr_name.c_str(), string_attr ) ) throw MDAL_Status::Err_UnknownFormat;
string_attr[attlen] = '\0'; string_attr[attlen] = '\0';


std::string res( string_attr ); std::string res( string_attr );
Expand All @@ -139,6 +139,7 @@ double NetCDFFile::getAttrDouble( int varid, const std::string &attr_name ) cons
return res; return res;
} }



int NetCDFFile::getVarId( const std::string &name ) int NetCDFFile::getVarId( const std::string &name )
{ {
int ncid_val; int ncid_val;
Expand All @@ -153,3 +154,9 @@ void NetCDFFile::getDimension( const std::string &name, size_t *val, int *ncid_v
if ( nc_inq_dimid( mNcid, name.c_str(), ncid_val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat; if ( nc_inq_dimid( mNcid, name.c_str(), ncid_val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat;
if ( nc_inq_dimlen( mNcid, *ncid_val, val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat; if ( nc_inq_dimlen( mNcid, *ncid_val, val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat;
} }

bool NetCDFFile::hasDimension( const std::string &name ) const
{
int ncid_val;
return nc_inq_dimid( mNcid, name.c_str(), &ncid_val ) == NC_NOERR;
}
10 changes: 9 additions & 1 deletion external/mdal/frmts/mdal_netcdf.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ class NetCDFFile


int getAttrInt( const std::string &name, const std::string &attr_name ) const; int getAttrInt( const std::string &name, const std::string &attr_name ) const;
double getAttrDouble( int varid, const std::string &attr_name ) const; double getAttrDouble( int varid, const std::string &attr_name ) const;
/**
* Get string attribute
* \param name name of the variable
* \param attr_name name of the attribute of the variable
* \returns empty string if attribute is missing, else attribute value
*/
std::string getAttrStr( const std::string &name, const std::string &attr_name ) const; std::string getAttrStr( const std::string &name, const std::string &attr_name ) const;
std::string getAttrStr( const std::string &name, int varid ) const; std::string getAttrStr( const std::string &attr_name, int varid ) const;
double getFillValue( int varid ) const; double getFillValue( int varid ) const;
int getVarId( const std::string &name ); int getVarId( const std::string &name );
void getDimension( const std::string &name, size_t *val, int *ncid_val ) const; void getDimension( const std::string &name, size_t *val, int *ncid_val ) const;
bool hasDimension( const std::string &name ) const;

private: private:
int mNcid; // C handle to the file int mNcid; // C handle to the file
}; };
Expand Down
Loading

0 comments on commit 435b594

Please sign in to comment.