Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update mdal to 0.8.91: API for 3D datasets write and driver max faces
  • Loading branch information
PeterPetrik committed Aug 17, 2021
1 parent 21f0a95 commit 7d5c030
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 13 deletions.
71 changes: 69 additions & 2 deletions external/mdal/api/mdal.h
Expand Up @@ -117,7 +117,7 @@ MDAL_EXPORT MDAL_Status MDAL_LastStatus();
*/
MDAL_EXPORT void MDAL_ResetStatus();

/*
/**
* Sets the last status message
* \since MDAL 0.9.0
*/
Expand Down Expand Up @@ -215,6 +215,13 @@ MDAL_EXPORT const char *MDAL_DR_longName( MDAL_DriverH driver );
*/
MDAL_EXPORT const char *MDAL_DR_filters( MDAL_DriverH driver );

/**
* Returns the maximum number of vertices per face supported by the driver
*
* \since MDAL 0.9.0
*/
MDAL_EXPORT int MDAL_DR_faceVerticesMaximumCount( MDAL_DriverH driver );

///////////////////////////////////////////////////////////////////////////////////////
/// MESH
///////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -331,7 +338,7 @@ MDAL_EXPORT int MDAL_M_edgeCount( MDAL_MeshH mesh );
MDAL_EXPORT int MDAL_M_faceCount( MDAL_MeshH mesh );

/**
* Returns maximum number of vertices face can consist of, e.g. 4 for regular quad mesh
* Returns maximum number of vertices per face for this mesh, can consist of, e.g. 4 for regular quad mesh.
*/
MDAL_EXPORT int MDAL_M_faceVerticesMaximumCount( MDAL_MeshH mesh );

Expand All @@ -343,6 +350,36 @@ MDAL_EXPORT int MDAL_M_faceVerticesMaximumCount( MDAL_MeshH mesh );
*/
MDAL_EXPORT void MDAL_M_LoadDatasets( MDAL_MeshH mesh, const char *datasetFile );

/**
* Returns number of metadata values
*
* \since MDAL 0.9.0
*/
MDAL_EXPORT int MDAL_M_metadataCount( MDAL_MeshH mesh );

/**
* Returns dataset metadata key
* not thread-safe and valid only till next call
*
* \since MDAL 0.9.0
*/
MDAL_EXPORT const char *MDAL_M_metadataKey( MDAL_MeshH mesh, int index );

/**
* Returns dataset metadata value
* not thread-safe and valid only till next call
*
* \since MDAL 0.9.0
*/
MDAL_EXPORT const char *MDAL_M_metadataValue( MDAL_MeshH mesh, int index );

/**
* Adds new metadata to the mesh
*
* \since MDAL 0.9.0
*/
MDAL_EXPORT void MDAL_M_setMetadata( MDAL_MeshH mesh, const char *key, const char *val );

/**
* Returns dataset groups count
*/
Expand Down Expand Up @@ -580,6 +617,36 @@ MDAL_EXPORT MDAL_DatasetH MDAL_G_addDataset(
const int *active
);

/**
* Adds empty (new) 3D dataset to the group
* This increases dataset group count MDAL_G_datasetCount() by 1
*
* The dataset is opened in edit mode.
* To persist dataset, call MDAL_G_closeEditMode() on parent group
*
* Minimum and maximum dataset values are automatically calculated
*
* Only for 3D datasets
*
* \param group parent group handle
* \param time time for dataset (hours)
* \param values For scalar data, the size must be volume count
* For vector data , the size must be volume count * 2 (x1, y1, x2, y2, ..., xN, yN)
* \param verticalLevelCount Int Array holding the number of vertical levels for each face.
* Size must be the face count
* \param verticalExtrusion Double Array holding the vertical level values for the voxels
* Size must be Face count + Volume count
* \returns empty pointer if not possible to create dataset (e.g. group opened in read mode), otherwise handle to new dataset
*/

MDAL_EXPORT MDAL_DatasetH MDAL_G_addDataset3D(
MDAL_DatasetGroupH group,
double time,
const double *values,
const int *verticalLevelCount,
const double *verticalExtrusions
);

/**
* Returns whether dataset group is in edit mode
*/
Expand Down
23 changes: 23 additions & 0 deletions external/mdal/frmts/mdal_driver.cpp
Expand Up @@ -116,4 +116,27 @@ void MDAL::Driver::createDataset( MDAL::DatasetGroup *group, MDAL::RelativeTimes
group->datasets.push_back( dataset );
}

void MDAL::Driver::createDataset( MDAL::DatasetGroup *group, MDAL::RelativeTimestamp time, const double *values, const int *verticalLevelCounts, const double *verticalExtrusions )
{
size_t count = 0;
size_t facesCount = group->mesh()->facesCount();
int maxVerticalLevel = 0;
for ( size_t i = 0; i < facesCount; i++ )
{
count += verticalLevelCounts[i];
if ( verticalLevelCounts[i] > maxVerticalLevel ) maxVerticalLevel = verticalLevelCounts[i];
};

std::shared_ptr<MDAL::MemoryDataset3D> dataset = std::make_shared< MemoryDataset3D >( group, count, maxVerticalLevel, verticalLevelCounts, verticalExtrusions );
dataset->setTime( time );

if ( !group->isScalar() )
count *= 2;

memcpy( dataset->values(), values, sizeof( double ) * count );

dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
}

bool MDAL::Driver::persist( MDAL::DatasetGroup * ) { return true; } // failure
9 changes: 8 additions & 1 deletion external/mdal/frmts/mdal_driver.hpp
Expand Up @@ -68,12 +68,19 @@ namespace MDAL
bool hasScalarData,
const std::string &datasetGroupFile );

// create new dataset from array
// create new 2D dataset from array
virtual void createDataset( DatasetGroup *group,
RelativeTimestamp time,
const double *values,
const int *active );

// create new 3D dataset from array
virtual void createDataset( DatasetGroup *group,
RelativeTimestamp time,
const double *values,
const int *verticalLevelCount,
const double *verticalExtrusion );

// persist to the file
// returns true on error, false on success
virtual bool persist( DatasetGroup *group );
Expand Down
2 changes: 1 addition & 1 deletion external/mdal/frmts/mdal_ply.cpp
Expand Up @@ -82,7 +82,7 @@ std::unique_ptr<MDAL::Mesh> MDAL::DriverPly::load( const std::string &meshFile,
}

/*
* The header is a format definition and a series of element definitions and/or comment lines
* The header is a format defintion and a series of element definitions and/or comment lines
* cycle through these until end-header
*/
do
Expand Down
4 changes: 1 addition & 3 deletions external/mdal/frmts/mdal_ugrid.cpp
Expand Up @@ -21,9 +21,7 @@ MDAL::DriverUgrid::DriverUgrid()
"UGRID Results",
"*.nc",
Capability::ReadMesh | Capability::SaveMesh )
{

}
{}

MDAL::DriverUgrid *MDAL::DriverUgrid::create()
{
Expand Down
139 changes: 136 additions & 3 deletions external/mdal/mdal.cpp
Expand Up @@ -21,7 +21,7 @@ static const char *EMPTY_STR = "";

const char *MDAL_Version()
{
return "0.8.90";
return "0.8.91";
}

MDAL_Status MDAL_LastStatus()
Expand Down Expand Up @@ -171,6 +171,17 @@ const char *MDAL_DR_filters( MDAL_DriverH driver )
return _return_str( d->filters() );
}

int MDAL_DR_faceVerticesMaximumCount( MDAL_DriverH driver )
{
if ( !driver )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriver, "Driver is not valid (null)" );
return -1;
}
MDAL::Driver *d = static_cast< MDAL::Driver * >( driver );
return d->faceVerticesMaximumCount();
}

///////////////////////////////////////////////////////////////////////////////////////
/// MESH
///////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -361,6 +372,80 @@ void MDAL_M_LoadDatasets( MDAL_MeshH mesh, const char *datasetFile )
MDAL::DriverManager::instance().loadDatasets( m, datasetFile );
}

int MDAL_M_metadataCount( MDAL_MeshH mesh )
{
if ( !mesh )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Mesh is not valid (null)" );
return 0;
}
MDAL::Mesh *m = static_cast< MDAL::Mesh * >( mesh );
int len = static_cast<int>( m->metadata.size() );
return len;
}

const char *MDAL_M_metadataKey( MDAL_MeshH mesh, int index )
{
if ( !mesh )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Mesh is not valid (null)" );
return EMPTY_STR;
}
MDAL::Mesh *m = static_cast< MDAL::Mesh * >( mesh );
int len = static_cast<int>( m->metadata.size() );
if ( len <= index )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Requested index: " + std::to_string( index ) + " is out of scope for metadata" );
return EMPTY_STR;
}
size_t i = static_cast<size_t>( index );
return _return_str( m->metadata[i].first );
}

const char *MDAL_M_metadataValue( MDAL_MeshH mesh, int index )
{
if ( !mesh )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Mesh is not valid (null)" );
return EMPTY_STR;
}
MDAL::Mesh *m = static_cast< MDAL::Mesh * >( mesh );
int len = static_cast<int>( m->metadata.size() );
if ( len <= index )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Requested index: " + std::to_string( index ) + " is out of scope for metadata" );
return EMPTY_STR;
}
size_t i = static_cast<size_t>( index );
return _return_str( m->metadata[i].second );
}

void MDAL_M_setMetadata( MDAL_MeshH mesh, const char *key, const char *val )
{
if ( !mesh )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Mesh is not valid (null)" );
return;
}

if ( !key )
{
MDAL::Log::error( MDAL_Status::Err_InvalidData, "Passed pointer key is not valid (null)" );
return;
}

if ( !val )
{
MDAL::Log::error( MDAL_Status::Err_InvalidData, "Passed pointer val is not valid (null)" );
return;
}

const std::string k( key );
const std::string v( val );
MDAL::Mesh *m = static_cast< MDAL::Mesh * >( mesh );
m->setMetadata( k, v );
}

int MDAL_M_datasetGroupCount( MDAL_MeshH mesh )
{
if ( !mesh )
Expand Down Expand Up @@ -812,7 +897,7 @@ MDAL_DatasetH MDAL_G_addDataset( MDAL_DatasetGroupH group, double time, const do

if ( g->dataLocation() == MDAL_DataLocation::DataOnVolumes )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriverCapability, "Dataset Group has data on 3D volumes" );
MDAL::Log::error( MDAL_Status::Err_MissingDriverCapability, "Cannot save 3D dataset as a 2D dataset" );
return nullptr;
}

Expand All @@ -835,6 +920,55 @@ MDAL_DatasetH MDAL_G_addDataset( MDAL_DatasetGroupH group, double time, const do
return nullptr;
}

MDAL_DatasetH MDAL_G_addDataset3D( MDAL_DatasetGroupH group, double time, const double *values, const int *verticalLevelCount, const double *verticalExtrusions )
{
if ( !group )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleDataset, "Dataset Group is not valid (null)" );
return nullptr;
}

if ( !values || !verticalLevelCount || !verticalExtrusions )
{
MDAL::Log::error( MDAL_Status::Err_InvalidData, "Passed pointer Values are not valid" );
return nullptr;
}

MDAL::DatasetGroup *g = static_cast< MDAL::DatasetGroup * >( group );
if ( !g->isInEditMode() )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleDataset, "Dataset Group is not in edit mode" );
return nullptr;
}

const std::string driverName = g->driverName();
std::shared_ptr<MDAL::Driver> dr = MDAL::DriverManager::instance().driver( driverName );
if ( !dr )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriver, "Driver name " + driverName + " saved in dataset group could not be found" );
return nullptr;
}

if ( g->dataLocation() != MDAL_DataLocation::DataOnVolumes )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriverCapability, "Cannot write 3D data to a Dataset Group that does not have Data On Volumes" );
return nullptr;
}

const size_t index = g->datasets.size();
MDAL::RelativeTimestamp t( time, MDAL::RelativeTimestamp::hours );
dr->createDataset( g,
t,
values,
verticalLevelCount,
verticalExtrusions
);
if ( index < g->datasets.size() ) // we have new dataset
return static_cast< MDAL_DatasetGroupH >( g->datasets[ index ].get() );
else
return nullptr;
}

bool MDAL_G_isInEditMode( MDAL_DatasetGroupH group )
{
if ( !group )
Expand Down Expand Up @@ -1319,4 +1453,3 @@ void MDAL_M_setProjection( MDAL_MeshH mesh, const char *projection )

static_cast<MDAL::Mesh *>( mesh )->setSourceCrsFromWKT( std::string( projection ) );
}

0 comments on commit 7d5c030

Please sign in to comment.