Skip to content

Commit

Permalink
Merge pull request #60 from ecmwf/fix/clear-defaults
Browse files Browse the repository at this point in the history
Add clear defaults
  • Loading branch information
danovaro authored Feb 6, 2025
2 parents f1f3cb2 + 2e56870 commit 7f2ee37
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
### dependencies and options

if(NOT METKIT_CONFIGS_BRANCH)
set(METKIT_CONFIGS_BRANCH chk)
set(METKIT_CONFIGS_BRANCH enum)
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/share")
Expand Down
19 changes: 11 additions & 8 deletions src/metkit/mars/MarsLanguage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ MarsLanguage::MarsLanguage(const std::string& verb) : verb_(verb) {
}
}

if (lang.contains("_clear_defaults")) {
const auto& keywords = lang["_clear_defaults"];
for (auto i = 0; i < keywords.size(); ++i) {
if (auto iter = types_.find(keywords[i]); iter != types_.end()) { iter->second->clearDefaults(); }
}
}

std::set<std::string> keywordsInAxis;
for (const std::string& a : hypercube::AxisOrder::instance().axes()) {
keywordsInAxis.insert(a);
Expand All @@ -101,12 +108,10 @@ MarsLanguage::MarsLanguage(const std::string& verb) : verb_(verb) {
if(it != types_.end()) {
t = (*it).second;
}
typesByAxisOrder_.push_back(std::make_pair(a,t));
typesByAxisOrder_.emplace_back(a, t);
}
for (const auto& [k,t] : types_) {
if (keywordsInAxis.find(k) == keywordsInAxis.end()) {
typesByAxisOrder_.push_back(std::make_pair(k,t));
}
if (keywordsInAxis.find(k) == keywordsInAxis.end()) { typesByAxisOrder_.emplace_back(k, t); }
}
}

Expand Down Expand Up @@ -220,9 +225,7 @@ std::string MarsLanguage::bestMatch(const MarsExpandContext& ctx, const std::str

static std::string empty;
if (best.empty()) {
if (!fail) {
return empty;
}
if (!fail) { return empty; }

std::ostringstream oss;
oss << "Cannot match [" << name << "] in " << values << ctx;
Expand Down Expand Up @@ -250,7 +253,7 @@ std::string MarsLanguage::bestMatch(const MarsExpandContext& ctx, const std::str
if (!fail) {
return empty;
}

std::ostringstream oss;
oss << "Ambiguous value '" << name << "' could be";

Expand Down
100 changes: 78 additions & 22 deletions tests/test_expand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
* nor does it submit to any jurisdiction.
*/

/// @file test_MetFile.cc
/// @file test_expand.cc
/// @date Jan 2016
/// @author Florian Rathgeber

#include <utility>

#include "eckit/types/Date.h"
#include "metkit/mars/MarsRequest.h"
#include "metkit/mars/MarsExpension.h"
Expand All @@ -28,9 +30,13 @@ namespace metkit {
namespace mars {
namespace test {

namespace {
using ExpectedOutput = std::map<std::string, std::vector<std::string>>;
}

//-----------------------------------------------------------------------------

void expand(const MarsRequest& r, const std::string& verb, std::map<std::string, std::vector<std::string>> expected, std::vector<long> dates) {
void expand(const MarsRequest& r, const std::string& verb, const ExpectedOutput& expected, const std::vector<long> dates) {
// MarsExpension exp(false);
// MarsRequest r = exp.expand(req);
std::cout << "comparing " << r << " with " << expected << " dates " << dates << std::endl;
Expand Down Expand Up @@ -59,9 +65,9 @@ void expand(const MarsRequest& r, const std::string& verb, std::map<std::string,
}


void expand(const std::string& text, const std::string& verb, std::map<std::string, std::vector<std::string>> expected, std::vector<long> dates) {
void expand(const std::string& text, const std::string& verb, const ExpectedOutput& expected, std::vector<long> dates) {
MarsRequest r = MarsRequest::parse(text, true);
expand(r, verb, expected, dates);
expand(r, verb, expected, std::move(dates));
}

void expandException(const std::string& text) {
Expand All @@ -70,7 +76,7 @@ void expandException(const std::string& text) {

CASE( "test_metkit_expand_1" ) {
const char* text = "ret,date=-5/to/-1.";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand All @@ -87,8 +93,8 @@ CASE( "test_metkit_expand_1" ) {

CASE( "test_metkit_expand_2" ) {
{
const char* text = "ret";
std::map<std::string, std::vector<std::string>> expected{
const char* text = "ret,date=-1";
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand All @@ -104,7 +110,7 @@ CASE( "test_metkit_expand_2" ) {
}
{
const char* text = "ret,levtype=ml";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand All @@ -123,7 +129,7 @@ CASE( "test_metkit_expand_2" ) {

CASE( "test_metkit_expand_3" ) {
const char* text = "ret,date=-5/to/-1,grid=n640";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand All @@ -141,7 +147,7 @@ CASE( "test_metkit_expand_3" ) {

CASE( "test_metkit_expand_4" ) {
const char* text = "ret,date=-5/to/-1,grid=o640";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand All @@ -159,7 +165,7 @@ CASE( "test_metkit_expand_4" ) {

CASE( "test_metkit_expand_5" ) {
const char* text = "retrieve,class=od,date=20050601,diagnostic=1,expver=1,iteration=0,levelist=1,levtype=ml,param=155.129,stream=sens,time=1200,type=sg";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"diagnostic", {"1"}},
{"domain", {"g"}},
Expand All @@ -178,7 +184,7 @@ CASE( "test_metkit_expand_5" ) {

CASE( "test_metkit_expand_6" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=1/to/31";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
Expand All @@ -199,7 +205,7 @@ CASE( "test_metkit_expand_6" ) {

CASE( "test_metkit_expand_7" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=0.01/0.7";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
Expand All @@ -216,7 +222,7 @@ CASE( "test_metkit_expand_7" ) {

CASE( "test_metkit_expand_8" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=0.1/to/0.7/by/0.2";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
Expand Down Expand Up @@ -271,7 +277,7 @@ CASE( "test_metkit_expand_multirequest-1" ) {
std::istringstream in(text);
std::vector<MarsRequest> reqs = MarsRequest::parse(in, true);
EXPECT_EQUAL(reqs.size(), 2);
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
Expand Down Expand Up @@ -595,7 +601,7 @@ CASE( "test_metkit_expand_param" ) {
CASE( "test_metkit_expand_d1" ) {
{
const char* text = "retrieve,class=d1,dataset=extremes-dt,date=-1";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"d1"}},
{"dataset", {"extremes-dt"}},
{"expver", {"0001"}},
Expand All @@ -610,7 +616,7 @@ CASE( "test_metkit_expand_d1" ) {
expand(text, "retrieve", expected, {-1});
} {
const char* text = "retrieve,class=d1,dataset=extreme-dt,date=-1";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"d1"}},
{"dataset", {"extremes-dt"}},
{"expver", {"0001"}},
Expand All @@ -626,7 +632,7 @@ CASE( "test_metkit_expand_d1" ) {
}
{
const char* text = "retrieve,class=d1,dataset=climate-dt,levtype=pl,date=20000101,activity=CMIP6,experiment=hist,model=IFS-NEMO,generation=1,realization=1,resolution=high,stream=clte,type=fc,param=134/137";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"d1"}},
{"dataset", {"climate-dt"}},
{"activity", {"cmip6"}},
Expand All @@ -644,10 +650,10 @@ CASE( "test_metkit_expand_d1" ) {
{"param", {"134","137"}}
};
expand(text, "retrieve", expected, {20000101});
}
}
{
const char* text = "retrieve,date=20120515,time=0000,dataset=climate-dt,activity=cmip6,experiment=hist,generation=1,model=icon,realization=1,resolution=high,class=d1,expver=0001,type=fc,stream=clte,levelist=1,levtype=o3d,param=263500";
std::map<std::string, std::vector<std::string>> expected{
ExpectedOutput expected{
{"class", {"d1"}},
{"dataset", {"climate-dt"}},
{"activity", {"cmip6"}},
Expand All @@ -665,12 +671,12 @@ CASE( "test_metkit_expand_d1" ) {
{"param", {"263500"}}
};
expand(text, "retrieve", expected, {20120515});
}
}
}
CASE( "test_metkit_expand_ng" ) {
{
const char* text = "retrieve,class=ng,date=20000101,activity=CMIP6,experiment=hist,model=IFS-NEMO,generation=1,realization=1,resolution=high,stream=clte,type=fc,levtype=pl,param=134/137";
std::map<std::string, std::vector<std::string>> expected {
ExpectedOutput expected {
{"class", {"ng"}},
{"levtype", {"pl"}},
{"levelist", {"1000","850","700","500","400","300"}},
Expand All @@ -691,6 +697,56 @@ CASE( "test_metkit_expand_ng" ) {
}
}

CASE("test_metkit_expand_list") {
{
const char *text = "list,date=20250105,domain=g,levtype=pl,expver="
"0001,step=0,stream=oper,levelist=1000/850/700/500/400/"
"300,time=1200,type=an,param=129";
ExpectedOutput expected{
{"class", {"od"}},
{"date", {"20250105"}},
{"domain", {"g"}},
{"levtype", {"pl"}},
{"levelist", {"1000", "850", "700", "500", "400", "300"}},
{"expver", {"0001"}},
{"time", {"1200"}},
{"stream", {"oper"}},
{"type", {"an"}},
{"param", {"129"}}};
expand(text, "list", expected, {20250105});
}
{
const char *text = "list,class=tr,date=20250105";
ExpectedOutput expected{
{"class", {"tr"}}, {"date", {"20250105"}}};
expand(text, "list", expected, {20250105});
}
}

CASE("test_metkit_expand_read") {
{
const char* text = "read,class=tr,date=20250105,domain=g,levtype=pl,expver=0001,step=0,stream=oper,"
"levelist=1000/850/700/500/400/300,time=1200,type=an,param=129";
ExpectedOutput expected {
{"class", {"tr"}},
{"date", {"20250105"}},
{"domain", {"g"}},
{"levtype", {"pl"}},
{"levelist", {"1000", "850", "700", "500", "400", "300"}},
{"expver", {"0001"}},
{"time", {"1200"}},
{"stream", {"oper"}},
{"type", {"an"}},
{"param", {"129"}}};
expand(text, "read", expected, {20250105});
}
{
const char* text = "read,date=20250105,param=129";
ExpectedOutput expected {{"date", {"20250105"}}, {"param", {"129"}}};
expand(text, "read", expected, {20250105});
}
}

//-----------------------------------------------------------------------------

} // namespace test
Expand Down

0 comments on commit 7f2ee37

Please sign in to comment.