Skip to content

Commit

Permalink
Merge pull request #41 from ecmwf/hotfix/1.11.6
Browse files Browse the repository at this point in the history
METK-120 lowercase metadata
  • Loading branch information
danovaro authored Feb 21, 2024
2 parents 09c04cd + a743595 commit 5f9081b
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 52 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.5
1.11.6
8 changes: 4 additions & 4 deletions share/metkit/language.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ _field: &_field

model:
category: data
type: any
type: lowercase

repres:
flatten: false
Expand Down Expand Up @@ -729,11 +729,11 @@ _field: &_field

activity:
category: data
type: any
type: lowercase

experiment:
category: data
type: any
type: lowercase

generation:
category: data
Expand All @@ -745,7 +745,7 @@ _field: &_field

resolution:
category: data
type: any
type: lowercase

#######################################################################

Expand Down
2 changes: 2 additions & 0 deletions src/metkit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ list( APPEND metkit_srcs
mars/TypeFloat.h
mars/TypeInteger.cc
mars/TypeInteger.h
mars/TypeLowercase.cc
mars/TypeLowercase.h
mars/TypeMixed.cc
mars/TypeMixed.h
mars/TypeParam.cc
Expand Down
44 changes: 44 additions & 0 deletions src/metkit/mars/TypeLowercase.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/


#include "metkit/mars/TypesFactory.h"
#include "metkit/mars/TypeLowercase.h"

#include <algorithm>
#include <cctype>
#include <string>

namespace metkit::mars {

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

TypeLowercase::TypeLowercase(const std::string &name, const eckit::Value& settings) :
Type(name, settings) {
}

TypeLowercase::~TypeLowercase() {
}

void TypeLowercase::print(std::ostream &out) const {
out << "TypeLowercase[name=" << name_ << "]";
}

bool TypeLowercase::expand(const MarsExpandContext& ctx, std::string &value) const {

std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c){ return std::tolower(c); });
return true;
}


static TypeBuilder<TypeLowercase> type("lowercase");

//----------------------------------------------------------------------------------------------------------------------
} // namespace metkit::mars
40 changes: 40 additions & 0 deletions src/metkit/mars/TypeLowercase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/

/// @file TypeLowercase.h
/// @author Emanuele Danovaro
/// @date February 2024

#pragma once

#include "metkit/mars/Type.h"

namespace metkit::mars {

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

class TypeLowercase : public Type {

public: // methods

TypeLowercase(const std::string &name, const eckit::Value& settings = eckit::Value());

virtual ~TypeLowercase() override;

private: // methods

virtual void print(std::ostream &out) const override;
virtual bool expand(const MarsExpandContext& ctx, std::string &value) const override;

};

//----------------------------------------------------------------------------------------------------------------------
} // namespace metkit::mars

93 changes: 47 additions & 46 deletions tests/test_expand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,31 @@ CASE( "test_metkit_expand_10_strict" ) {
}
}

void quantileThrows(std::vector<std::string> values) {

void expandKeyThrows(const std::string& key, std::vector<std::string> values) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("quantile");
EXPECT_THROWS_AS(t->expand(ctx, values), eckit::BadValue);
metkit::mars::Type* t = language.type(key);
std::cout << key << "Throws " << values << std::endl;
EXPECT_THROWS(t->expand(ctx, values));
}

void quantile(std::vector<std::string> values, std::vector<std::string> expected) {
void expandKey(const std::string& key, std::vector<std::string> values, std::vector<std::string> expected) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("quantile");
metkit::mars::Type* t = language.type(key);
std::cout << key << " " << values;
t->expand(ctx, values);
std::cout << " ==> " << values << " - expected " << expected << std::endl;
ASSERT(values == expected);
}

void quantileThrows(std::vector<std::string> values) {
expandKeyThrows("quantile", values);
}
void quantile(std::vector<std::string> values, std::vector<std::string> expected) {
expandKey("quantile", values, expected);
}

CASE( "test_metkit_expand_11_quantile" ) {
quantileThrows({"-1:5"});
quantileThrows({"0:-5"});
Expand Down Expand Up @@ -178,21 +188,10 @@ CASE( "test_metkit_expand_11_quantile" ) {


void timeThrows(std::vector<std::string> values) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("time");
std::cout << "timeThrows " << values << std::endl;
EXPECT_THROWS(t->expand(ctx, values));
expandKeyThrows("time", values);
}

void time(std::vector<std::string> values, std::vector<std::string> expected) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("time");
std::cout << "time " << values;
t->expand(ctx, values);
std::cout << " ==> " << values << " - expected " << expected << std::endl;
ASSERT(values == expected);
expandKey("time", values, expected);
}

CASE( "test_metkit_expand_12_time" ) {
Expand Down Expand Up @@ -228,42 +227,17 @@ CASE( "test_metkit_expand_12_time" ) {
time({"1","to","6","by","6"}, {"0100"});

time({"1","to","3h","by","30m"}, {"0100", "0130", "0200", "0230", "0300"});

// quantile({"0:5","to","0:5"}, {"0:5"});
// quantile({"3:3","to","3:3"}, {"3:3"});
// quantile({"0:5","to","5:5"}, {"0:5","1:5","2:5","3:5","4:5","5:5"});
// quantile({"0:5","to","5:5","by","1"}, {"0:5","1:5","2:5","3:5","4:5","5:5"});
// quantile({"0:5","to","5:5","by","2"}, {"0:5","2:5","4:5"});
// quantile({"0:5","to","5:5","by","3"}, {"0:5","3:5"});
// quantile({"0:5","to","5:5","by","5"}, {"0:5","5:5"});
// quantile({"0:5","to","5:5","by","6"}, {"0:5"});
// quantile({"2:5","to","5:5","by","2"}, {"2:5","4:5"});
// quantile({"3:5","to","5:5","by","2"}, {"3:5","5:5"});
// quantile({"4:5","to","5:5","by","2"}, {"4:5"});
// quantile({"0:10","3:10","to","7:10","by","2","10:10"}, {"0:10","3:10","5:10","7:10","10:10"});
}


void stepThrows(std::vector<std::string> values) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("step");
std::cout << "stepThrows " << values << std::endl;
EXPECT_THROWS(t->expand(ctx, values));
expandKeyThrows("step", values);
}

void step(std::vector<std::string> values, std::vector<std::string> expected) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
metkit::mars::Type* t = language.type("step");
std::cout << "step " << values << " ==> ";
t->expand(ctx, values);
std::cout << values << " - expected: " << expected << std::endl;
ASSERT(values == expected);
expandKey("step", values, expected);
}

CASE( "test_metkit_expand_13_step" ) {
// stepThrows({"0:70"});
step({"0"}, {"0"});
step({"1"}, {"1"});
step({"24"}, {"24"});
Expand Down Expand Up @@ -299,6 +273,33 @@ CASE( "test_metkit_expand_13_step" ) {

}


void activity(std::vector<std::string> values, std::vector<std::string> expected) {
expandKey("activity", values, expected);
}
void experiment(std::vector<std::string> values, std::vector<std::string> expected) {
expandKey("experiment", values, expected);
}
void model(std::vector<std::string> values, std::vector<std::string> expected) {
expandKey("model", values, expected);
}

CASE( "test_metkit_expand_lowercase" ) {
activity({"ScenarioMIP"}, {"scenariomip"});
activity({"CMIP6"}, {"cmip6"});
activity({"ScenarioMIP"}, {"scenariomip"});
activity({"cmip6"}, {"cmip6"});
experiment({"SSP3-7.0"}, {"ssp3-7.0"});
experiment({"ssp3-7.0"}, {"ssp3-7.0"});
experiment({"hist"}, {"hist"});
model({"IFS-NEMO"}, {"ifs-nemo"});
model({"IFS"}, {"ifs"});
model({"ifs-nemo"}, {"ifs-nemo"});
model({"ifs"}, {"ifs"});
model({"ICON"}, {"icon"});
model({"icon"}, {"icon"});
}

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

} // namespace test
Expand Down
2 changes: 1 addition & 1 deletion tests/test_typesfactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CASE ("test_list_types") {
std::stringstream ss;
TypesFactory::list(ss);
std::cout << ss.str() << std::endl;
EXPECT(ss.str() == std::string("[any,date,enum,expver,float,integer,mixed,param,range,regex,time,to-by-list,to-by-list-float,to-by-list-quantile]"));
EXPECT(ss.str() == std::string("[any,date,enum,expver,float,integer,lowercase,mixed,param,range,regex,time,to-by-list,to-by-list-float,to-by-list-quantile]"));
}


Expand Down

0 comments on commit 5f9081b

Please sign in to comment.