Skip to content

Commit 18559de

Browse files
committed
update to MDAL 0.3.2
1 parent 546b041 commit 18559de

20 files changed

+1208
-106
lines changed

external/mdal/frmts/mdal_3di.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ MDAL::Driver3Di *MDAL::Driver3Di::create()
2020
return new Driver3Di();
2121
}
2222

23-
MDAL::CFDimensions MDAL::Driver3Di::populateDimensions( const NetCDFFile &ncFile )
23+
MDAL::CFDimensions MDAL::Driver3Di::populateDimensions( )
2424
{
2525
CFDimensions dims;
2626
size_t count;
2727
int ncid;
2828

2929
// 2D Mesh
30-
ncFile.getDimension( "nMesh2D_nodes", &count, &ncid );
30+
mNcFile.getDimension( "nMesh2D_nodes", &count, &ncid );
3131
dims.setDimension( CFDimensions::Face2D, count, ncid );
3232

33-
ncFile.getDimension( "nCorner_Nodes", &count, &ncid );
33+
mNcFile.getDimension( "nCorner_Nodes", &count, &ncid );
3434
dims.setDimension( CFDimensions::MaxVerticesInFace, count, ncid );
3535

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

3939
// Time
40-
ncFile.getDimension( "time", &count, &ncid );
40+
mNcFile.getDimension( "time", &count, &ncid );
4141
dims.setDimension( CFDimensions::Time, count, ncid );
4242

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

114-
void MDAL::Driver3Di::addBedElevation( MDAL::Mesh *mesh )
114+
void MDAL::Driver3Di::addBedElevation( MemoryMesh *mesh )
115115
{
116116
assert( mesh );
117117
if ( 0 == mesh->facesCount() )
@@ -189,12 +189,6 @@ std::set<std::string> MDAL::Driver3Di::ignoreNetCDFVariables()
189189
return ignore_variables;
190190
}
191191

192-
std::string MDAL::Driver3Di::nameSuffix( MDAL::CFDimensions::Type type )
193-
{
194-
MDAL_UNUSED( type );
195-
return "";
196-
}
197-
198192
void MDAL::Driver3Di::parseNetCDFVariableMetadata( int varid, const std::string &variableName, std::string &name, bool *is_vector, bool *is_x )
199193
{
200194
*is_vector = false;

external/mdal/frmts/mdal_3di.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ namespace MDAL
4444
Driver3Di *create() override;
4545

4646
private:
47-
CFDimensions populateDimensions( const NetCDFFile &ncFile ) override;
47+
CFDimensions populateDimensions( ) override;
4848
void populateFacesAndVertices( Vertices &vertices, Faces &faces ) override;
49-
void addBedElevation( Mesh *mesh ) override;
49+
void addBedElevation( MemoryMesh *mesh ) override;
5050
std::string getCoordinateSystemVariableName() override;
5151
std::set<std::string> ignoreNetCDFVariables() override;
52-
std::string nameSuffix( CFDimensions::Type type ) override;
5352
void parseNetCDFVariableMetadata( int varid, const std::string &variableName,
5453
std::string &name, bool *is_vector, bool *is_x ) override;
5554

external/mdal/frmts/mdal_cf.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,13 @@ MDAL::cfdataset_info_map MDAL::DriverCF::parseDatasetGroupInfo()
7979
continue;
8080

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

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

8988
parseNetCDFVariableMetadata( varid, variable_name, name, &is_vector, &is_x );
90-
if ( !suffix.empty() )
91-
name = name + " (" + suffix + ")";
9289

9390
// Add it to the map
9491
auto it = dsinfo_map.find( name );
@@ -136,12 +133,12 @@ static void populate_vals( bool is_vector, double *vals, size_t i,
136133
{
137134
if ( is_vector )
138135
{
139-
vals[2 * i] = MDAL::safeValue( vals_x[idx], fill_val_x );
140-
vals[2 * i + 1] = MDAL::safeValue( vals_y[idx], fill_val_y );
136+
vals[2 * i] = MDAL::safeValue( vals_x[idx], fill_val_x );
137+
vals[2 * i + 1] = MDAL::safeValue( vals_y[idx], fill_val_y );
141138
}
142139
else
143140
{
144-
vals[i] = MDAL::safeValue( vals_x[idx], fill_val_x );
141+
vals[i] = MDAL::safeValue( vals_x[idx], fill_val_x );
145142
}
146143
}
147144

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

191188
// read X data
192189
double fill_val_x = mNcFile.getFillValue( dsi.ncid_x );
193-
std::vector<double> vals_x( dsi.arr_size );
190+
std::vector<double> vals_x( dsi.arr_size, std::numeric_limits<double>::quiet_NaN() );
194191
if ( nc_get_var_double( mNcFile.handle(), dsi.ncid_x, vals_x.data() ) ) CF_THROW_ERR;
195192

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

@@ -254,7 +251,8 @@ bool MDAL::DriverCF::canRead( const std::string &uri )
254251
{
255252
NetCDFFile ncFile;
256253
ncFile.openFile( uri );
257-
populateDimensions( ncFile );
254+
mNcFile = ncFile;
255+
populateDimensions( );
258256
}
259257
catch ( MDAL_Status )
260258
{
@@ -318,7 +316,7 @@ std::unique_ptr< MDAL::Mesh > MDAL::DriverCF::load( const std::string &fileName,
318316
mNcFile.openFile( mFileName );
319317

320318
// Parse dimensions
321-
mDimensions = populateDimensions( mNcFile );
319+
mDimensions = populateDimensions( );
322320

323321
// Create mMesh
324322
Faces faces;

external/mdal/frmts/mdal_cf.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace MDAL
6363

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

7677
protected:
77-
virtual CFDimensions populateDimensions( const NetCDFFile &ncFile ) = 0;
78+
virtual CFDimensions populateDimensions( ) = 0;
7879
virtual void populateFacesAndVertices( Vertices &vertices, Faces &faces ) = 0;
79-
virtual void addBedElevation( MDAL::Mesh *mesh ) = 0;
80+
virtual void addBedElevation( MDAL::MemoryMesh *mesh ) = 0;
8081
virtual std::string getCoordinateSystemVariableName() = 0;
8182
virtual std::set<std::string> ignoreNetCDFVariables() = 0;
82-
virtual std::string nameSuffix( CFDimensions::Type type ) = 0;
8383
virtual void parseNetCDFVariableMetadata( int varid, const std::string &variableName,
8484
std::string &name, bool *is_vector, bool *is_x ) = 0;
8585

external/mdal/frmts/mdal_flo2d.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void MDAL::DriverFlo2D::parseCADPTSFile( const std::string &datFileName, std::ve
109109
// CADPTS.DAT - COORDINATES OF CELL CENTERS (ELEM NUM, X, Y)
110110
while ( std::getline( cadptsStream, line ) )
111111
{
112+
line = MDAL::rtrim( line );
112113
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
113114
if ( lineParts.size() != 3 )
114115
{
@@ -140,6 +141,7 @@ void MDAL::DriverFlo2D::parseFPLAINFile( std::vector<double> &elevations,
140141

141142
while ( std::getline( fplainStream, line ) )
142143
{
144+
line = MDAL::rtrim( line );
143145
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
144146
if ( lineParts.size() != 7 )
145147
{
@@ -220,6 +222,7 @@ void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const s
220222

221223
while ( std::getline( inStream, line ) )
222224
{
225+
line = MDAL::rtrim( line );
223226
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
224227
if ( lineParts.size() == 1 )
225228
{
@@ -301,6 +304,7 @@ void MDAL::DriverFlo2D::parseDEPTHFile( const std::string &datFileName, const st
301304
// DEPTH.OUT - COORDINATES (ELEM NUM, X, Y, MAX DEPTH)
302305
while ( std::getline( depthStream, line ) )
303306
{
307+
line = MDAL::rtrim( line );
304308
if ( vertex_idx == nVertices ) throw MDAL_Status::Err_IncompatibleMesh;
305309

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

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

386+
line = MDAL::rtrim( line );
381387
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
382388
if ( lineParts.size() != 4 )
383389
{

external/mdal/frmts/mdal_gdal.cpp

+1-42
Original file line numberDiff line numberDiff line change
@@ -396,47 +396,6 @@ void MDAL::DriverGdal::addDataToOutput( GDALRasterBandH raster_band, std::shared
396396
}
397397
}
398398

399-
void MDAL::DriverGdal::activateFaces( std::shared_ptr<MemoryDataset> tos )
400-
{
401-
// only for data on vertices
402-
if ( !tos->group()->isOnVertices() )
403-
return;
404-
405-
bool isScalar = tos->group()->isScalar();
406-
407-
// Activate only Faces that do all Vertex's outputs with some data
408-
int *active = tos->active();
409-
const double *values = tos->constValues();
410-
411-
for ( unsigned int idx = 0; idx < meshGDALDataset()->mNVolumes; ++idx )
412-
{
413-
Face elem = mMesh->faces.at( idx );
414-
for ( size_t i = 0; i < 4; ++i )
415-
{
416-
const size_t vertexIndex = elem[i];
417-
if ( isScalar )
418-
{
419-
double val = values[vertexIndex];
420-
if ( std::isnan( val ) )
421-
{
422-
active[idx] = 0; //NOT ACTIVE
423-
break;
424-
}
425-
}
426-
else
427-
{
428-
double x = values[2 * vertexIndex];
429-
double y = values[2 * vertexIndex + 1];
430-
if ( std::isnan( x ) || std::isnan( y ) )
431-
{
432-
active[idx] = 0; //NOT ACTIVE
433-
break;
434-
}
435-
}
436-
}
437-
}
438-
}
439-
440399
void MDAL::DriverGdal::addDatasetGroups()
441400
{
442401
// Add dataset to mMesh
@@ -465,7 +424,7 @@ void MDAL::DriverGdal::addDatasetGroups()
465424
{
466425
addDataToOutput( raster_bands[i], dataset, is_vector, i == 0 );
467426
}
468-
activateFaces( dataset );
427+
MDAL::activateFaces( mMesh.get(), dataset );
469428
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
470429
group->datasets.push_back( dataset );
471430
}

external/mdal/frmts/mdal_gdal.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ namespace MDAL
9090
metadata_hash parseMetadata( GDALMajorObjectH gdalBand, const char *pszDomain = nullptr );
9191
void addDataToOutput( GDALRasterBandH raster_band, std::shared_ptr<MemoryDataset> tos, bool is_vector, bool is_x );
9292
bool addSrcProj();
93-
void activateFaces( std::shared_ptr<MemoryDataset> tos );
9493
void addDatasetGroups();
9594
void createMesh();
9695
void parseRasterBands( const GdalDataset *cfGDALDataset );

external/mdal/frmts/mdal_netcdf.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,21 @@ std::string NetCDFFile::getAttrStr( const std::string &name, const std::string &
103103
return getAttrStr( attr_name, arr_id );
104104
}
105105

106-
std::string NetCDFFile::getAttrStr( const std::string &name, int varid ) const
106+
std::string NetCDFFile::getAttrStr( const std::string &attr_name, int varid ) const
107107
{
108108
assert( mNcid != 0 );
109109

110110
size_t attlen = 0;
111111

112-
if ( nc_inq_attlen( mNcid, varid, name.c_str(), &attlen ) )
112+
if ( nc_inq_attlen( mNcid, varid, attr_name.c_str(), &attlen ) )
113113
{
114114
// attribute is missing
115115
return std::string();
116116
}
117117

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

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

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

142+
142143
int NetCDFFile::getVarId( const std::string &name )
143144
{
144145
int ncid_val;
@@ -153,3 +154,9 @@ void NetCDFFile::getDimension( const std::string &name, size_t *val, int *ncid_v
153154
if ( nc_inq_dimid( mNcid, name.c_str(), ncid_val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat;
154155
if ( nc_inq_dimlen( mNcid, *ncid_val, val ) != NC_NOERR ) throw MDAL_Status::Err_UnknownFormat;
155156
}
157+
158+
bool NetCDFFile::hasDimension( const std::string &name ) const
159+
{
160+
int ncid_val;
161+
return nc_inq_dimid( mNcid, name.c_str(), &ncid_val ) == NC_NOERR;
162+
}

external/mdal/frmts/mdal_netcdf.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ class NetCDFFile
2828

2929
int getAttrInt( const std::string &name, const std::string &attr_name ) const;
3030
double getAttrDouble( int varid, const std::string &attr_name ) const;
31+
/**
32+
* Get string attribute
33+
* \param name name of the variable
34+
* \param attr_name name of the attribute of the variable
35+
* \returns empty string if attribute is missing, else attribute value
36+
*/
3137
std::string getAttrStr( const std::string &name, const std::string &attr_name ) const;
32-
std::string getAttrStr( const std::string &name, int varid ) const;
38+
std::string getAttrStr( const std::string &attr_name, int varid ) const;
3339
double getFillValue( int varid ) const;
3440
int getVarId( const std::string &name );
3541
void getDimension( const std::string &name, size_t *val, int *ncid_val ) const;
42+
bool hasDimension( const std::string &name ) const;
43+
3644
private:
3745
int mNcid; // C handle to the file
3846
};

0 commit comments

Comments
 (0)