Skip to content

Commit

Permalink
table_id check also for a date
Browse files Browse the repository at this point in the history
  • Loading branch information
h-dh committed Dec 9, 2015
1 parent 902a3a2 commit d7b18f2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 34 deletions.
3 changes: 3 additions & 0 deletions include/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class Date
//! Inquire whether a reference date was set.
bool isSet( void ){ return isDateSet ;}

static bool
isValidDate(std::string);

//! Set the calendar type
/*! Valid types: proleptic_georgian|georgian, noleap,
equal_months|360_day. The case of the strings doesn't
Expand Down
2 changes: 2 additions & 0 deletions include/qa_CMIP5.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ struct CMOR
static std::string n_product;
static std::string n_requested;
static std::string n_standard_name;
static std::string n_table_id;
static std::string n_tol_on_requests;
static std::string n_type;
static std::string n_unformatted_units;
Expand Down Expand Up @@ -269,6 +270,7 @@ std::string CMOR::n_priority ="priority";
std::string CMOR::n_product ="product";
std::string CMOR::n_requested ="requested";
std::string CMOR::n_standard_name ="standard_name";
std::string CMOR::n_table_id ="table_id";
std::string CMOR::n_tol_on_requests ="tol_on_requests";
std::string CMOR::n_type ="type";
std::string CMOR::n_unformatted_units ="unformatted_units";
Expand Down
78 changes: 68 additions & 10 deletions src/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,16 +912,6 @@ Date::gregorian2Julian( double y, double mo, double d, double dh )
return prolGreg2Julian(y, mo, d, dh);
}

bool
Date::isDigit(std::string s)
{
for(size_t k=0; k < s.size(); ++k)
if( !isdigit(s[k]) )
return false;

return true;
}

void
Date::init(void)
{
Expand Down Expand Up @@ -949,6 +939,74 @@ Date::isNotASingleDigit(std::string s)
return true;
}

bool
Date::isDigit(std::string s)
{
for(size_t k=0; k < s.size(); ++k)
if( !isdigit(s[k]) )
return false;

return true;
}

bool
Date::isValidDate(std::string s)
{
std::string mn("january february march april may june july");
mn += " august september october november december" ;
Split x_mn(mn);

s = hdhC::Lower()(s);
s = hdhC::replaceChars(s, ',', ' ');
Split x_s(s);

if( x_s.size() != 3 )
return false;

int i_d, i_m, i_y;
i_d = i_m = i_y = 0;

for(size_t i=0 ; i < x_s.size() ; ++i )
{
if( hdhC::isAlpha(x_s[i]) )
{
for(size_t m=0 ; m < x_mn.size() ; ++m )
{
if( x_s[i] == x_mn[m].substr(0, x_s[i].size()) )
{
i_m = hdhC::string2Double(x_s[i]) ;
break;
}
}
}
else
{
if( hdhC::isDigit(x_s[i]) )
{
if( x_s[i].size() > 2 )
i_y = hdhC::string2Double(x_s[i]) ;
else
i_d = hdhC::string2Double(x_s[i]) ;
}
else
i_d = hdhC::string2Double(x_s[i]) ;
}
}

bool is=true;
if( i_d == 0 )
is=false ;
if( i_m == 0 )
is=false ;
if( i_y == 0 )
is=false ;

if(is)
return true;

return false;
}

void
Date::julian2Date( long double jD,
double *yr, double *mo, double *dy, double *hr, double *mi, double *se )
Expand Down
73 changes: 51 additions & 22 deletions src/QA_CMIP5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ DRS_CV::checkMIP_tableName(Split& x_filename)
std::string capt("Ambiguous MIP table names, found ") ;
capt += hdhC::tf_assign("MIP-table(file)", x_filename[1]) ;
capt += " vs. global ";
capt += hdhC::tf_att("table_id",CMOR::tableSheet);
capt += hdhC::tf_att(CMOR::n_table_id,CMOR::tableSheet);

(void) notes->operate(capt) ;
pQA->setExit( notes->getExitValue() ) ;
Expand Down Expand Up @@ -3658,6 +3658,8 @@ CMOR::run(InFile& in, VariableMetaData& vMD)
// Matching the ncfile inherent meta-data against pre-defined
// CMOR tables.

// commented out, because requested type is given in the table
// and checked in that context.
// checkReqVariableType();

checkRequestedAttributes();
Expand Down Expand Up @@ -4037,7 +4039,7 @@ QA_Exp::getFrequency(void)
std::string mip_f = splt[1];

// now, try also global att 'table_id'
Split mip_a( pQA->pIn->nc.getAttString("table_id") ) ;
Split mip_a( pQA->pIn->nc.getAttString(CMOR::n_table_id) ) ;

if( mip_a.size() > 1 )
{
Expand All @@ -4049,10 +4051,10 @@ QA_Exp::getFrequency(void)
frequency = "yr" ;
else if( mip_f.substr(mip_f.size()-3) == "mon" )
frequency = "mon" ;
else if( mip_f.substr(mip_f.size()-3) == "Day" )
frequency = "day" ;
else if( mip_f == "day" )
frequency = "day" ;
else if( mip_f.substr(mip_f.size()-3) == "Day" )
frequency = "day" ;
else if( mip_f.substr(0,3) == "6hr" )
frequency = "6hr" ;
else if( mip_f.substr(0,3) == "3hr" )
Expand All @@ -4075,29 +4077,31 @@ QA_Exp::getMIP_tableName(std::string tName)
if( tName.size() )
tableID = tName ;
else
tableID = pQA->pIn->nc.getAttString("table_id") ;
tableID = pQA->pIn->nc.getAttString(CMOR::n_table_id) ;

if( tableID.size() == 0 )
return tableID;

Split x_tableID(tableID);

// The table sheet name from the global attributes.
// Ignore specific variations
if( x_tableID.size() > 1 )
// Find valid names, even with deviations
size_t x_tSz = x_tableID.size() ;

if( x_tSz > 1 )
{
if( x_tableID[0].substr(1,4) == "able"
|| x_tableID[0].substr(1,4) == "ABLE" )
tableID = x_tableID[1] ;
tName = x_tableID[1] ;
}
else if( x_tableID.size() )
tableID = x_tableID[0] ;
tName = x_tableID[0] ;

//check for valid names
// check the format of the total line
bool is=true;
for( size_t i=0 ; i < MIP_tableNames.size() ; ++i )
{
if( MIP_tableNames[i] == tableID )
if( MIP_tableNames[i] == tName )
{
is=false ;
break;
Expand All @@ -4106,21 +4110,46 @@ QA_Exp::getMIP_tableName(std::string tName)

if( is )
{
if( tName.size() == 0)
{
std::string key("7_7");
std::string key("7_7a");

if( notes->inq( key, CMOR::n_global) )
{
std::string capt("invalid MIP table name in global attribute, found ") ;
capt += hdhC::tf_att(hdhC::empty, "table_id", x_tableID.getStr()) ;
if( notes->inq( key, CMOR::n_global) )
{
std::string capt("invalid MIP table name in global ") ;
capt += hdhC::tf_att(hdhC::empty, CMOR::n_table_id, x_tableID.getStr()) ;

(void) notes->operate(capt) ;
pQA->setExit( notes->getExitValue() ) ;
}
(void) notes->operate(capt) ;
pQA->setExit( notes->getExitValue() ) ;
}

tableID.clear();
tName.clear();
return tName;
}

// Is the date of the table given; within ()?
size_t p0,p1;
std::string dateItem;
if( (p0=tableID.find("(")) < std::string::npos )
if( (p1=tableID.find(")", p0)) < std::string::npos )
dateItem = tableID.substr(p0+1, p1-p0-1);

is=true;
if( dateItem.size() )
if( Date::isValidDate(dateItem) )
is=false;

if( is )
{
std::string key("7_7b");

if( notes->inq( key, CMOR::n_global) )
{
std::string capt(CMOR::n_global + hdhC::blank) ;
capt += hdhC::tf_att(hdhC::empty, CMOR::n_table_id, x_tableID.getStr()) ;
capt += ": missing date of the MIP table, expected e.g. (5 Jan 2016)";

(void) notes->operate(capt) ;
pQA->setExit( notes->getExitValue() ) ;
}
}

return tableID;
Expand Down
4 changes: 2 additions & 2 deletions src/hdhC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,11 +2405,11 @@ std::string unique(std::string str, char c)
}

std::vector<std::string>
unique(std::vector<std::string> &vs, std::string pat)
unique(std::vector<std::string> &vs, std::string pat, bool isStr)
{
std::vector<std::string> vs_t;
for(size_t i=0 ; i < vs.size() ; ++i)
vs_t.push_back( unique(vs[i], pat ) );
vs_t.push_back( unique(vs[i], pat, isStr ) );

return vs_t;
}
Expand Down

0 comments on commit d7b18f2

Please sign in to comment.