Skip to content

Commit

Permalink
fix #155 support for ADCIRC file
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Sep 12, 2019
1 parent 52cf319 commit 594d6eb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
46 changes: 45 additions & 1 deletion mdal/frmts/mdal_cf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ std::shared_ptr<MDAL::Dataset> MDAL::DriverCF::createFace2DDataset( std::shared_
return dataset;
}

std::shared_ptr<MDAL::Dataset> MDAL::DriverCF::createVertex2DDataset( std::shared_ptr<MDAL::DatasetGroup> group,
size_t ts, const MDAL::CFDatasetGroupInfo &dsi,
const std::vector<double> &vals_x,
const std::vector<double> &vals_y,
double fill_val_x, double fill_val_y )
{
assert( dsi.outputType == CFDimensions::Vertex2D );
size_t nVertices2D = mDimensions.size( CFDimensions::Vertex2D );

std::shared_ptr<MDAL::MemoryDataset> dataset = std::make_shared<MDAL::MemoryDataset>( group.get() );

for ( size_t i = 0; i < nVertices2D; ++i )
{
size_t idx = ts * nVertices2D + i;
populate_vals( dsi.is_vector,
dataset->values(),
i,
vals_x,
vals_y,
idx,
fill_val_x,
fill_val_y );

}

return dataset;
}

void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<double> &times, const MDAL::cfdataset_info_map &dsinfo_map )
{
/* PHASE 2 - add dataset groups */
Expand All @@ -199,7 +227,16 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl
dsi.name
);
group->setIsScalar( !dsi.is_vector );
group->setIsOnVertices( false );

if ( dsi.outputType == CFDimensions::Vertex2D )
group->setIsOnVertices( true );
else if ( dsi.outputType == CFDimensions::Face2D )
group->setIsOnVertices( false );
else
{
// unsupported
continue;
}

// read X data
double fill_val_x = mNcFile.getFillValue( dsi.ncid_x );
Expand All @@ -225,6 +262,13 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl
if ( dsi.outputType == CFDimensions::Face2D )
{
dataset = createFace2DDataset( group, ts, dsi, vals_x, vals_y, fill_val_x, fill_val_y );
}
else // Vertex2D
{
dataset = createVertex2DDataset( group, ts, dsi, vals_x, vals_y, fill_val_x, fill_val_y );
}
if ( dataset )
{
dataset->setTime( time );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
Expand Down
8 changes: 8 additions & 0 deletions mdal/frmts/mdal_cf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ namespace MDAL
const std::vector<double> &vals_y,
double fill_val_x, double fill_val_y );

std::shared_ptr<MDAL::Dataset> createVertex2DDataset(
std::shared_ptr<MDAL::DatasetGroup> group,
size_t ts,
const MDAL::CFDatasetGroupInfo &dsi,
const std::vector<double> &vals_x,
const std::vector<double> &vals_y,
double fill_val_x, double fill_val_y );

void addDatasetGroups( Mesh *mesh,
const std::vector<double> &times,
const cfdataset_info_map &dsinfo_map );
Expand Down
13 changes: 13 additions & 0 deletions mdal/frmts/mdal_netcdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ std::vector<std::string> NetCDFFile::readArrNames() const
return res;
}

bool NetCDFFile::hasAttrInt( const std::string &name, const std::string &attr_name ) const
{
assert( mNcid != 0 );

int arr_id;
if ( nc_inq_varid( mNcid, name.c_str(), &arr_id ) != NC_NOERR ) return false;

int val;
if ( nc_get_att_int( mNcid, arr_id, attr_name.c_str(), &val ) ) return false;

return true;
}

int NetCDFFile::getAttrInt( const std::string &name, const std::string &attr_name ) const
{
assert( mNcid != 0 );
Expand Down
1 change: 1 addition & 0 deletions mdal/frmts/mdal_netcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class NetCDFFile
bool hasArr( const std::string &name ) const;
std::vector<std::string> readArrNames() const;

bool hasAttrInt( 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;
/**
Expand Down
10 changes: 7 additions & 3 deletions mdal/frmts/mdal_ugrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ void MDAL::DriverUgrid::populateFaces( MDAL::Faces &faces )
const std::string mesh2dFaceNodeConnectivity = mNcFile.getAttrStr( mMesh2dName, "face_node_connectivity" );

size_t verticesInFace = mDimensions.size( CFDimensions::MaxVerticesInFace );
int fill_val = mNcFile.getAttrInt( mesh2dFaceNodeConnectivity, "_FillValue" );
int fill_val = -1;
if ( mNcFile.hasAttrInt( mesh2dFaceNodeConnectivity, "_FillValue" ) )
fill_val = mNcFile.getAttrInt( mesh2dFaceNodeConnectivity, "_FillValue" );
int start_index = mNcFile.getAttrInt( mesh2dFaceNodeConnectivity, "start_index" );
std::vector<int> face_nodes_conn = mNcFile.readIntArr( mesh2dFaceNodeConnectivity, faceCount * verticesInFace );

Expand Down Expand Up @@ -352,16 +354,18 @@ void MDAL::DriverUgrid::parseNetCDFVariableMetadata( int varid, const std::strin
}
else
{
if ( MDAL::contains( long_name, ", x-component" ) )
if ( MDAL::contains( long_name, ", x-component" ) || MDAL::contains( long_name, "u component of " ) )
{
*is_vector = true;
name = MDAL::replace( long_name, ", x-component", "" );
name = MDAL::replace( name, "u component of ", "" );
}
else if ( MDAL::contains( long_name, ", y-component" ) )
else if ( MDAL::contains( long_name, ", y-component" ) || MDAL::contains( long_name, "v component of " ) )
{
*is_vector = true;
*is_x = false;
name = MDAL::replace( long_name, ", y-component", "" );
name = MDAL::replace( name, "v component of ", "" );
}
else
{
Expand Down

0 comments on commit 594d6eb

Please sign in to comment.