Skip to content

Commit

Permalink
[WIP] severals modification in duration/DateTime
Browse files Browse the repository at this point in the history
and implementation in some formats
  • Loading branch information
vcloarec committed Dec 10, 2019
1 parent 5423223 commit 5def408
Show file tree
Hide file tree
Showing 22 changed files with 728 additions and 498 deletions.
2 changes: 2 additions & 0 deletions mdal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SET(MDAL_SOURCES
mdal_utils.cpp
mdal_driver_manager.cpp
mdal_data_model.cpp
mdal_date_time.cpp
mdal_memory_data_model.cpp
frmts/mdal_driver.cpp
frmts/mdal_2dm.cpp
Expand All @@ -20,6 +21,7 @@ SET(MDAL_HEADERS
mdal_utils.hpp
mdal_driver_manager.hpp
mdal_data_model.hpp
mdal_date_time.hpp
mdal_memory_data_model.hpp
frmts/mdal_driver.hpp
frmts/mdal_2dm.hpp
Expand Down
2 changes: 1 addition & 1 deletion mdal/frmts/mdal_3di.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void MDAL::Driver3Di::addBedElevation( MemoryMesh *mesh )
group->setIsScalar( true );

std::shared_ptr<MDAL::MemoryDataset2D> dataset = std::make_shared< MemoryDataset2D >( group.get() );
dataset->setTime( 0.0 );
dataset->setTime( MDAL::Duration() );
double *values = dataset->values();
for ( size_t i = 0; i < faceCount; ++i )
{
Expand Down
4 changes: 2 additions & 2 deletions mdal/frmts/mdal_ascii_dat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void MDAL::DriverAsciiDat::loadNewFormat(
}
else if ( cardType == "RT_JULIAN" && items.size() >= 2 )
{
referenceTime = MDAL::DateTime::fromJulianDay( MDAL::toDouble( items[1] ) );
referenceTime = MDAL::DateTime( MDAL::toDouble( items[1] ) );
}
else if ( cardType == "TIMEUNITS" && items.size() >= 2 )
{
Expand All @@ -268,7 +268,7 @@ void MDAL::DriverAsciiDat::loadNewFormat(
else if ( cardType == "TS" && items.size() >= 3 )
{
double rawTime = toDouble( items[2] );
MDAL::Duration t = convertTimeData( rawTime, group->getMetadata( "TIMEUNITS" ) );
MDAL::Duration t( rawTime, MDAL::parseUnitTime( group->getMetadata( "TIMEUNITS" ) ) );

if ( faceCentered )
{
Expand Down
26 changes: 3 additions & 23 deletions mdal/frmts/mdal_binary_dat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,6 @@ static bool readIStat( std::ifstream &in, int sflg, char *flag )
return false;
}

static MDAL::Duration convertTimeData( double time, const std::string &originalTimeDataUnit )
{
MDAL::Duration::Unit unit = MDAL::Duration::hours;

if ( originalTimeDataUnit == "seconds" )
{
unit = MDAL::Duration::seconds;
}
else if ( originalTimeDataUnit == "minutes" )
{
unit = MDAL::Duration::minutes;
}
else if ( originalTimeDataUnit == "days" )
{
unit = MDAL::Duration::days;
}

return MDAL::Duration( time, unit );
}

MDAL::DriverBinaryDat::DriverBinaryDat():
Driver( "BINARY_DAT",
"Binary DAT",
Expand Down Expand Up @@ -271,7 +251,7 @@ void MDAL::DriverBinaryDat::load( const std::string &datFile, MDAL::Mesh *mesh,
return exit_with_error( status, MDAL_Status::Err_UnknownFormat, "unable to read reference time" );

referenceTime = static_cast<double>( time );
group->setReferenceTime( DateTime::fromJulianDay( referenceTime ) );
group->setReferenceTime( DateTime( referenceTime ) );
break;

case CT_TIMEUNITS:
Expand Down Expand Up @@ -309,7 +289,7 @@ void MDAL::DriverBinaryDat::load( const std::string &datFile, MDAL::Mesh *mesh,
return exit_with_error( status, MDAL_Status::Err_UnknownFormat, "Invalid time step" );

double rawTime = static_cast<double>( time );
MDAL::Duration t = convertTimeData( rawTime, timeUnitStr );
MDAL::Duration t( rawTime, MDAL::parseUnitTime( timeUnitStr ) );

if ( readVertexTimestep( mesh, group, groupMax, t, istat, sflg, in ) )
return exit_with_error( status, MDAL_Status::Err_UnknownFormat, "Unable to read vertex timestep" );
Expand Down Expand Up @@ -394,7 +374,7 @@ bool MDAL::DriverBinaryDat::readVertexTimestep( const MDAL::Mesh *mesh,
}
else
{
dataset->setTime( time ); // TODO read TIMEUNITS
dataset->setTime( time );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
}
Expand Down
103 changes: 90 additions & 13 deletions mdal/frmts/mdal_cf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,78 @@ static void populate_vals( bool is_vector, double *vals, size_t i,
}
}

void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<double> &times, const MDAL::cfdataset_info_map &dsinfo_map )
static MDAL::DateTime parseReferenceTime( std::string timeInformation, std::string calendar )
{
auto strings = MDAL::split( timeInformation, ' ' );
if ( strings.size() < 3 )
return MDAL::DateTime();

if ( strings[1] != "since" )
return MDAL::DateTime();

std::string dateString = strings[2];

auto dateStringValues = MDAL::split( dateString, '-' );
if ( dateStringValues.size() != 3 )
return MDAL::DateTime();

int year = MDAL::toInt( dateStringValues[0] );
int month = MDAL::toInt( dateStringValues[1] );
int day = MDAL::toInt( dateStringValues[2] );

int hours = 0;
int minutes = 0;
double seconds = 0;

if ( strings.size() > 3 )
{
std::string timeString = strings[3];
auto timeStringsValue = MDAL::split( timeString, ":" );
if ( timeStringsValue.size() == 3 )
{
hours = MDAL::toInt( timeStringsValue[0] );
minutes = MDAL::toInt( timeStringsValue[0] );
seconds = MDAL::toDouble( timeStringsValue[0] );
}
}

if ( calendar == "gregorian" || calendar == "standard" || calendar.empty() )
return MDAL::DateTime( year, month, day, hours, minutes, seconds );

return MDAL::DateTime();

}

static MDAL::Duration::Unit parseUnitCFTime( std::string timeInformation )
{
auto strings = MDAL::split( timeInformation, ' ' );
if ( strings.size() < 3 )
return MDAL::Duration::hours; //default value

if ( strings[1] == "since" )
{
std::string timeUnit = strings[0];
if ( timeUnit == "month" ||
timeUnit == "months" ||
timeUnit == "mon" ||
timeUnit == "mons" )
{
return MDAL::Duration::months_CF;
}
else if ( timeUnit == "year" ||
timeUnit == "years" ||
timeUnit == "yr" ||
timeUnit == "yrs" )
{
return MDAL::Duration::exact_years;
}
return MDAL::parseUnitTime( strings[0] );
}

return MDAL::Duration::hours;//default value
}

void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<Duration> &times, const MDAL::cfdataset_info_map &dsinfo_map, const MDAL::DateTime &referenceTime )
{
/* PHASE 2 - add dataset groups */
for ( const auto &it : dsinfo_map )
Expand Down Expand Up @@ -203,7 +274,6 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl
for ( size_t ts = 0; ts < dsi.nTimesteps; ++ts )
{
std::shared_ptr<MDAL::Dataset> dataset;
double time = times[ts];
if ( dsi.outputType == CFDimensions::Volume3D )
{
dataset = create3DDataset(
Expand All @@ -220,7 +290,7 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl

if ( dataset )
{
dataset->setTime( time );
dataset->setTime( times[ts] );
group->datasets.push_back( dataset );
}
}
Expand All @@ -234,25 +304,32 @@ void MDAL::DriverCF::addDatasetGroups( MDAL::Mesh *mesh, const std::vector<doubl
}
}

void MDAL::DriverCF::parseTime( std::vector<double> &times )
MDAL::DateTime MDAL::DriverCF::parseTime( std::vector<Duration> &times )
{

size_t nTimesteps = mDimensions.size( CFDimensions::Time );
if ( 0 == nTimesteps )
{
//if no time dimension is present creates only one time step to store the potential time-independent variable
nTimesteps = 1;
times = std::vector<double>( 1, 0 );
return;
times = std::vector<Duration>( 1, Duration() );
return MDAL::DateTime();
}
const std::string timeArrName = getTimeVariableName();
times = mNcFile->readDoubleArr( timeArrName, nTimesteps );
std::string units = mNcFile->getAttrStr( timeArrName, "units" );
double div_by = MDAL::parseTimeUnits( units );
std::vector<double> rawTimes = mNcFile->readDoubleArr( timeArrName, nTimesteps );

std::string timeUnitInformation = mNcFile->getAttrStr( timeArrName, "units" );
std::string calendar = mNcFile->getAttrStr( timeArrName, "calendar" );
MDAL::DateTime referenceTime = parseReferenceTime( timeUnitInformation, calendar );
MDAL::Duration::Unit unit = parseUnitCFTime( timeUnitInformation );

times = std::vector<Duration>( nTimesteps );
for ( size_t i = 0; i < nTimesteps; ++i )
{
times[i] /= div_by;
times[i] = Duration( rawTimes[i], unit );
}

return referenceTime;
}

std::shared_ptr<MDAL::Dataset> MDAL::DriverCF::create2DDataset( std::shared_ptr<MDAL::DatasetGroup> group, size_t ts, const MDAL::CFDatasetGroupInfo &dsi, double fill_val_x, double fill_val_y )
Expand Down Expand Up @@ -356,7 +433,7 @@ std::unique_ptr< MDAL::Mesh > MDAL::DriverCF::load( const std::string &fileName,
if ( status ) *status = MDAL_Status::None;

//Dimensions dims;
std::vector<double> times;
std::vector<MDAL::Duration> times;

try
{
Expand Down Expand Up @@ -386,13 +463,13 @@ std::unique_ptr< MDAL::Mesh > MDAL::DriverCF::load( const std::string &fileName,
setProjection( mesh.get() );

// Parse time array
parseTime( times );
MDAL::DateTime referenceTime = parseTime( times );

// Parse dataset info
cfdataset_info_map dsinfo_map = parseDatasetGroupInfo();

// Create datasets
addDatasetGroups( mesh.get(), times, dsinfo_map );
addDatasetGroups( mesh.get(), times, dsinfo_map, referenceTime );

return std::unique_ptr<Mesh>( mesh.release() );
}
Expand Down
6 changes: 3 additions & 3 deletions mdal/frmts/mdal_cf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ namespace MDAL

void setProjection( MDAL::Mesh *m );
cfdataset_info_map parseDatasetGroupInfo();
void parseTime( std::vector<double> &times );
DateTime parseTime( std::vector<Duration> &times ); //Return the reference time
void addDatasetGroups( Mesh *mesh,
const std::vector<double> &times,
const cfdataset_info_map &dsinfo_map );
const std::vector<Duration> &times,
const cfdataset_info_map &dsinfo_map, const DateTime &referenceTime );

std::string mFileName;
std::shared_ptr<NetCDFFile> mNcFile;
Expand Down
2 changes: 1 addition & 1 deletion mdal/frmts/mdal_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void MDAL::Driver::createDatasetGroup( MDAL::Mesh *mesh, const std::string &grou
mesh->datasetGroups.push_back( grp );
}

void MDAL::Driver::createDataset( MDAL::DatasetGroup *group, double time, const double *values, const int *active )
void MDAL::Driver::createDataset( MDAL::DatasetGroup *group, MDAL::Duration time, const double *values, const int *active )
{
std::shared_ptr<MDAL::MemoryDataset2D> dataset = std::make_shared< MemoryDataset2D >( group );
dataset->setTime( time );
Expand Down
2 changes: 1 addition & 1 deletion mdal/frmts/mdal_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace MDAL

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

Expand Down
11 changes: 8 additions & 3 deletions mdal/frmts/mdal_flo2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void MDAL::DriverFlo2D::addStaticDataset(

std::shared_ptr<MDAL::MemoryDataset2D> dataset = std::make_shared< MemoryDataset2D >( group.get() );
assert( vals.size() == dataset->valuesCount() );
dataset->setTime( 0.0 );
dataset->setTime( MDAL::Duration() );
double *values = dataset->values();
memcpy( values, vals.data(), vals.size() * sizeof( double ) );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
Expand Down Expand Up @@ -187,7 +187,7 @@ void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const s
size_t nVertexs = mMesh->verticesCount();
size_t ntimes = 0;

double time = 0.0;
Duration time = Duration();
size_t face_idx = 0;

std::shared_ptr<DatasetGroup> depthDsGroup = std::make_shared< DatasetGroup >(
Expand Down Expand Up @@ -228,7 +228,7 @@ void MDAL::DriverFlo2D::parseTIMDEPFile( const std::string &datFileName, const s
std::vector<std::string> lineParts = MDAL::split( line, ' ' );
if ( lineParts.size() == 1 )
{
time = MDAL::toDouble( line );
time = Duration( MDAL::toDouble( line ), Duration::hours );
ntimes++;

if ( depthDataset ) addDatasetToGroup( depthDsGroup, depthDataset );
Expand Down Expand Up @@ -530,9 +530,14 @@ bool MDAL::DriverFlo2D::parseHDF5Datasets( MemoryMesh *mesh, const std::string &
HdfGroup grp = timedataGroup.group( grpName );
if ( !grp.isValid() ) return true;

auto g = grp.objects();

HdfAttribute groupType = grp.attribute( "Grouptype" );
if ( !groupType.isValid() ) return true;

HdfAttribute timeUnitAttribute = grp.attribute( "TimeUnits" );
std::string timeUnitString = timeUnitAttribute.readString();

/* Min and Max arrays in TIMDEP.HDF5 files have dimensions 1xntimesteps .
HdfDataset minDs = grp.dataset("Mins");
if (!minDs.isValid()) return true;
Expand Down
19 changes: 3 additions & 16 deletions mdal/frmts/mdal_hec2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ static std::string getDataTimeUnit( HdfDataset &dsTime )
return dataTimeUnit;
}

static void convertTimeDataToHours( std::vector<float> &times, const std::string &originalTimeDataUnit )
{
if ( originalTimeDataUnit != "Hours" )
{
for ( size_t i = 0; i < times.size(); i++ )
{
if ( originalTimeDataUnit == "Seconds" ) { times[i] /= 3600.0f; }
else if ( originalTimeDataUnit == "Minutes" ) { times[i] /= 60.0f; }
else if ( originalTimeDataUnit == "Days" ) { times[i] *= 24; }
}
}
}

static std::vector<MDAL::Duration> convertTimeData( std::vector<float> &times, const std::string &originalTimeDataUnit )
{
std::vector<MDAL::Duration> convertedTime( times.size() );
Expand Down Expand Up @@ -143,7 +130,7 @@ static MDAL::DateTime convertToDateTime( const std::string strDateTime )
{
auto data = MDAL::split( strDateTime, " " );
if ( data.size() < 2 )
return MDAL::DateTime::fromDefault();
return MDAL::DateTime();

std::string dateStr = data[0];

Expand Down Expand Up @@ -199,7 +186,7 @@ static MDAL::DateTime convertToDateTime( const std::string strDateTime )
sec = MDAL::toDouble( timeData[2] );
}

return MDAL::DateTime::fromStandartValue( year, month, day, hours, min, sec );
return MDAL::DateTime( year, month, day, hours, min, sec );
}

static std::string readReferenceTime( const HdfFile &hdfFile )
Expand All @@ -226,7 +213,7 @@ static MDAL::DateTime readReferenceDateTime( const HdfFile &hdfFile )
if ( timeStamps.size() > 0 )
return convertToDateTime( timeStamps[0] );

return MDAL::DateTime::fromDefault();
return MDAL::DateTime();
}

static std::vector<MDAL::Duration> readTimes( const HdfFile &hdfFile )
Expand Down
4 changes: 2 additions & 2 deletions mdal/frmts/mdal_selafin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void MDAL::DriverSelafin::parseFile( std::vector<std::string> &var_names,
if ( params[9] == 1 )
{
std::vector<int> datetime = mReader.read_int_arr( 6 );
MDAL_UNUSED( datetime )
DateTime referenceTime( datetime[0], datetime[1], datetime[2], datetime[3], datetime[4], double( datetime[5] ) );
}

/* 1 record containing the integers NELEM,NPOIN,NDP,1 (number of
Expand Down Expand Up @@ -476,7 +476,7 @@ void MDAL::DriverSelafin::addData( const std::vector<std::string> &var_names, co
else
{
dataset = std::make_shared< MemoryDataset2D >( group.get() );
dataset->setTime( it->first );
dataset->setTime( it->first, Duration::seconds ); //seems that time unit in this format is only seconds
group->datasets.push_back( dataset );
}
double *values = dataset->values();
Expand Down
Loading

0 comments on commit 5def408

Please sign in to comment.