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
Expand Up @@ -20,24 +20,24 @@ MDAL::Driver3Di *MDAL::Driver3Di::create()
return new Driver3Di();
}

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

// 2D Mesh
ncFile.getDimension( "nMesh2D_nodes", &count, &ncid );
mNcFile.getDimension( "nMesh2D_nodes", &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 );

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

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

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

void MDAL::Driver3Di::addBedElevation( MDAL::Mesh *mesh )
void MDAL::Driver3Di::addBedElevation( MemoryMesh *mesh )
{
assert( mesh );
if ( 0 == mesh->facesCount() )
Expand Down Expand Up @@ -189,12 +189,6 @@ std::set<std::string> MDAL::Driver3Di::ignoreNetCDFVariables()
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 )
{
*is_vector = false;
Expand Down
5 changes: 2 additions & 3 deletions external/mdal/frmts/mdal_3di.hpp
Expand Up @@ -44,12 +44,11 @@ namespace MDAL
Driver3Di *create() override;

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

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

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
std::string name;
bool is_vector = true;
bool is_x = false;

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

// Add it to the map
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 )
{
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] = MDAL::safeValue( vals_x[idx], fill_val_x );
vals[2 * i + 1] = MDAL::safeValue( vals_y[idx], fill_val_y );
}
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
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;

// 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 )
{
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;
}

Expand Down Expand Up @@ -254,7 +251,8 @@ bool MDAL::DriverCF::canRead( const std::string &uri )
{
NetCDFFile ncFile;
ncFile.openFile( uri );
populateDimensions( ncFile );
mNcFile = ncFile;
populateDimensions( );
}
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 );

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

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

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

protected:
virtual CFDimensions populateDimensions( const NetCDFFile &ncFile ) = 0;
virtual CFDimensions populateDimensions( ) = 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::set<std::string> ignoreNetCDFVariables() = 0;
virtual std::string nameSuffix( CFDimensions::Type type ) = 0;
virtual void parseNetCDFVariableMetadata( int varid, const std::string &variableName,
std::string &name, bool *is_vector, bool *is_x ) = 0;

Expand Down
6 changes: 6 additions & 0 deletions external/mdal/frmts/mdal_flo2d.cpp
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)
while ( std::getline( cadptsStream, line ) )
{
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
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 ) )
{
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
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 ) )
{
line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
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)
while ( std::getline( depthStream, line ) )
{
line = MDAL::rtrim( line );
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh;

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;

line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
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;

line = MDAL::rtrim( line );
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() != 4 )
{
Expand Down
43 changes: 1 addition & 42 deletions external/mdal/frmts/mdal_gdal.cpp
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()
{
// 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 );
}
activateFaces( dataset );
MDAL::activateFaces( mMesh.get(), dataset );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
}
Expand Down
1 change: 0 additions & 1 deletion external/mdal/frmts/mdal_gdal.hpp
Expand Up @@ -90,7 +90,6 @@ namespace MDAL
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 );
bool addSrcProj();
void activateFaces( std::shared_ptr<MemoryDataset> tos );
void addDatasetGroups();
void createMesh();
void parseRasterBands( const GdalDataset *cfGDALDataset );
Expand Down
13 changes: 10 additions & 3 deletions external/mdal/frmts/mdal_netcdf.cpp
Expand Up @@ -103,21 +103,21 @@ std::string NetCDFFile::getAttrStr( const std::string &name, const std::string &
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 );

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
return std::string();
}

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';

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


int NetCDFFile::getVarId( const std::string &name )
{
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_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
Expand Up @@ -28,11 +28,19 @@ class NetCDFFile

int getAttrInt( const std::string &name, 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, int varid ) const;
std::string getAttrStr( const std::string &attr_name, int varid ) const;
double getFillValue( int varid ) const;
int getVarId( const std::string &name );
void getDimension( const std::string &name, size_t *val, int *ncid_val ) const;
bool hasDimension( const std::string &name ) const;

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

0 comments on commit 435b594

Please sign in to comment.