diff --git a/src/metkit/mars/StepRange.cc b/src/metkit/mars/StepRange.cc index a05b6fa..1763f54 100644 --- a/src/metkit/mars/StepRange.cc +++ b/src/metkit/mars/StepRange.cc @@ -10,6 +10,7 @@ #include "metkit/mars/StepRange.h" #include "metkit/mars/TypeTime.h" +#include "metkit/mars/TypeRange.h" #include "eckit/exception/Exceptions.h" #include "eckit/persist/DumpLoad.h" @@ -17,8 +18,71 @@ using namespace eckit; -namespace metkit { -namespace mars { + +//---------------------------------------------------------------------------------------------------------------------- + +namespace { + +enum TimeUnit { + Second = 0, + Minute = 1, + Hour = 2, + Day = 3 +}; + +TimeUnit maxUnit(const eckit::Time& t) { + if (t.seconds() == 0) { + if (t.minutes() == 0) { + if (t.hours() == 0) { + return TimeUnit::Day; + } + return TimeUnit::Hour; + } + return TimeUnit::Minute; + } + return TimeUnit::Second; +} + +std::string canonical(const eckit::Time& time) { + + long h = time.hours(); + long m = time.minutes(); + long s = time.seconds(); + + // std::cout << h << "h" << m << "m" << s << "s\n"; + std::string out = ""; + if (h!=0 || (m==0 && s==0)) { + out += std::to_string(h); + if (m!=0 || s!=0) { + out += "h"; + } + } + if (m!=0) { + out += std::to_string(m) + "m"; + } + if (s!=0) { + out += std::to_string(s) + "s"; + } + return out; +} + +std::string canonical(const eckit::Time& time, TimeUnit unit) { + switch (unit) { + case TimeUnit::Second: + return std::to_string(time.seconds()+60*time.minutes()+3600*time.hours()) + "s"; + case TimeUnit::Minute: + return std::to_string(time.minutes()+60*time.hours()) + "m"; + case TimeUnit::Day: + case TimeUnit::Hour: + default: + return std::to_string(time.hours()); + } +} + +} + +namespace metkit::mars { + //---------------------------------------------------------------------------------------------------------------------- @@ -32,11 +96,11 @@ StepRange::operator std::string() const void StepRange::print(std::ostream& s) const { if(from_ == to_) { - s << from_; + s << canonical(from_); } else { - s << from_ << '-'; - s << to_; + TimeUnit unit = std::min(maxUnit(from_), maxUnit(to_)); + s << canonical(from_,unit) << '-' << canonical(to_,unit); } } @@ -48,20 +112,16 @@ StepRange::StepRange(const std::string& s): std::vector result; parse(s,result); - eckit::Second from, to; switch(result.size()) { case 1: - from = eckit::Time(result[0], true); - from_ = to_ = from/3600.; + to_ = from_ = eckit::Time(result[0], true); break; case 2: - from = eckit::Time(result[0], true); - to = eckit::Time(result[1], true); - from_ = from/3600.; - to_ = to/3600.; + from_ = eckit::Time(result[0], true); + to_ = eckit::Time(result[1], true); break; default: @@ -74,16 +134,19 @@ StepRange::StepRange(const std::string& s): void StepRange::dump(DumpLoad& a) const { - a.dump(from_); - a.dump(to_); + a.dump(from_.seconds()/3600.); + a.dump(to_.seconds()/3600.); } void StepRange::load(DumpLoad& a) { - a.load(from_); - a.load(to_); + double from, to; + a.load(from); + a.load(to); + + from_ = eckit::Time(from*3600., true); + to_ = eckit::Time(to*3600., true); } //---------------------------------------------------------------------------------------------------------------------- -} // namespace mars -} // namespace metkit +} // namespace metkit::mars diff --git a/src/metkit/mars/StepRange.h b/src/metkit/mars/StepRange.h index f4e64b7..72b5671 100644 --- a/src/metkit/mars/StepRange.h +++ b/src/metkit/mars/StepRange.h @@ -16,11 +16,11 @@ #include "eckit/persist/Bless.h" #include "eckit/types/Types.h" +#include "eckit/types/Time.h" namespace eckit { class DumpLoad; } -namespace metkit { -namespace mars { +namespace metkit::mars { //---------------------------------------------------------------------------------------------------------------------- @@ -34,12 +34,17 @@ class StepRange { StepRange(const std::string&); - StepRange(double from = 0,double to = 0): + StepRange(eckit::Time from = eckit::Time(0), eckit::Time to = eckit::Time(0)): from_(from),to_(to) { - if (from_ != 0 && to_ == 0) { - to_ = from_; - } + + if (from_ != eckit::Time(0) && to_ == eckit::Time(0)) { + to_ = from_; } + } + + StepRange(double from, double to = 0): + StepRange(eckit::Time(from*3600, true), eckit::Time(to*3600, true)) {} + #include "metkit/mars/StepRange.b" @@ -69,8 +74,8 @@ class StepRange { // -- Methods - double from() const { return from_; } - double to() const { return to_; } + double from() const { return from_/3600.; } + double to() const { return to_/3600.; } void dump(eckit::DumpLoad&) const; void load(eckit::DumpLoad&); @@ -110,8 +115,8 @@ class StepRange { // -- Members - double from_; - double to_; + eckit::Time from_; + eckit::Time to_; // -- Methods // None @@ -134,7 +139,6 @@ class StepRange { //---------------------------------------------------------------------------------------------------------------------- -} // namespace mars -} // namespace metkit +} // namespace metkit::mars #endif diff --git a/src/metkit/mars/TypeRange.cc b/src/metkit/mars/TypeRange.cc index eb77fb0..27e03b4 100644 --- a/src/metkit/mars/TypeRange.cc +++ b/src/metkit/mars/TypeRange.cc @@ -22,68 +22,6 @@ #include "metkit/mars/TypeTime.h" #include "metkit/mars/StepRange.h" -namespace { - -// enum TimeUnit { -// Second = 0, -// Minute = 1, -// Hour = 2, -// Day = 3 -// }; - -// TimeUnit maxUnit(const eckit::Time& t) { -// if (t.seconds() == 0) { -// if (t.minutes() == 0) { -// if (t.hours() == 0) { -// return TimeUnit::Day; -// } -// return TimeUnit::Hour; -// } -// return TimeUnit::Minute; -// } -// return TimeUnit::Second; -// } - -std::string canonical(const eckit::Time& time) { - return metkit::mars::StepRange(time/3600.); - - // long d = time.hours()/24; - // long h = time.hours()%24; - // long m = time.minutes(); - // long s = time.seconds(); - - // std::string out = ""; - // if (d!=0) { - // out += std::to_string(d) + "D"; - // } - // if (h!=0) { - // out += std::to_string(h) + "h"; - // } - // if (m!=0) { - // out += std::to_string(m) + "m"; - // } - // if (s!=0) { - // out = std::to_string(s) + "s"; - // } - // return out; -} - -// std::string canonical(const eckit::Time& time, TimeUnit unit) { -// return metkit::mars::StepRange(time/3600.); - -// switch (unit) { -// case TimeUnit::Second: -// return std::to_string(time.seconds()+60*time.minutes()+3600*time.hours()) + "s"; -// case TimeUnit::Minute: -// return std::to_string(time.minutes()+60*time.hours()) + "m"; -// case TimeUnit::Day: -// case TimeUnit::Hour: -// default: -// return std::to_string(time.hours()); -// } -// } - -} namespace metkit::mars { //---------------------------------------------------------------------------------------------------------------------- @@ -95,7 +33,6 @@ TypeRange::TypeRange(const std::string &name, const eckit::Value& settings) : multiple_ = true; } - TypeRange::~TypeRange() { } @@ -111,9 +48,7 @@ bool TypeRange::expand(const MarsExpandContext& ctx, std::string& value) const { parse(value, result); switch (result.size()) { case 1: { - eckit::Time start = eckit::Time(result[0], true); - // value = canonical(start, maxUnit(start)); - value = canonical(start); + value = StepRange(eckit::Time(result[0], true)); return true; } case 2: { @@ -125,10 +60,7 @@ bool TypeRange::expand(const MarsExpandContext& ctx, std::string& value) const { oss << name_ + ": initial value " << start << " cannot be greater that final value " << end; throw eckit::BadValue(oss.str()); } - - // TimeUnit unit = std::max(maxUnit(start), maxUnit(end)); - // value = canonical(start, unit) + "-" + canonical(end, unit); - value = canonical(start) + "-" + canonical(end); + value = StepRange(start, end); return true; } default: @@ -190,18 +122,9 @@ void TypeRange::expand(const MarsExpandContext& ctx, std::vector& v throw eckit::BadValue(name_ + ": 'by' value must be a positive number"); } eckit::Time j = from; - // j += by; - // for (; j <= to; j += by) { - // unit = std::min(unit, maxUnit(j)); - // } - // j = from; j += by; for (; j <= to; j += by) { - // if (unit >= TimeUnit::Hour) { - // newval.emplace_back(canonical(j, unit)); - // } else { - newval.emplace_back(canonical(j)); - // } + newval.emplace_back(StepRange(j)); } i++; diff --git a/tests/test_expand.cc b/tests/test_expand.cc index f51b899..ac22bce 100644 --- a/tests/test_expand.cc +++ b/tests/test_expand.cc @@ -263,62 +263,36 @@ void step(std::vector values, std::vector expected) { } CASE( "test_metkit_expand_13_step" ) { - stepThrows({"0:70"}); -// step({"0:20"}, {"20m"}); +// stepThrows({"0:70"}); step({"0"}, {"0"}); step({"1"}, {"1"}); step({"24"}, {"24"}); step({"144"}, {"144"}); step({"012"}, {"12"}); - step({"12:30"}, {"12.5"}); + step({"12:30"}, {"12h30m"}); step({"1:00"}, {"1"}); step({"1:0:0"}, {"1"}); step({"1h"}, {"1"}); step({"60m"}, {"1"}); step({"1h60m"}, {"2"}); - step({"0:5"}, {"0.0833333"}); - step({"0:05"}, {"0.0833333"}); - step({"0:05:0"}, {"0.0833333"}); - step({"0:06"}, {"0.1"}); - step({"0:10"}, {"0.166667"}); - step({"0:12"}, {"0.2"}); - step({"0:15"}, {"0.25"}); - step({"0:20"}, {"0.333333"}); - step({"0:25"}, {"0.416667"}); - step({"0:30"}, {"0.5"}); - step({"0:35"}, {"0.583333"}); - step({"0:40"}, {"0.666667"}); - step({"0:45"}, {"0.75"}); - step({"0:50"}, {"0.833333"}); - step({"0:55"}, {"0.916667"}); + step({"0:5"}, {"5m"}); + step({"0:05"}, {"5m"}); + step({"0:05:0"}, {"5m"}); + step({"0:06"}, {"6m"}); + step({"0:10"}, {"10m"}); + step({"0:12"}, {"12m"}); + step({"0:15"}, {"15m"}); + step({"0:20"}, {"20m"}); + step({"0:25"}, {"25m"}); + step({"0:30"}, {"30m"}); + step({"0:35"}, {"35m"}); + step({"0:40"}, {"40m"}); + step({"0:45"}, {"45m"}); + step({"0:50"}, {"50m"}); + step({"0:55"}, {"55m"}); step({"1-2"}, {"1-2"}); - // step({"30m-1"}, {"30m-60m"}); - step({"30m-1"}, {"0.5-1"}); - // quantileThrows({"6:5"}); - // quantileThrows({"0:12"}); - // quantile({"2:5"}, {"2:5"}); - // quantile({"0:2","1:2","2:2"}, {"0:2","1:2","2:2"}); - // quantile({"0:2","1:3","2:5"}, {"0:2","1:3","2:5"}); - - // quantileThrows({"to","5:10"}); - // quantileThrows({"3:5","to"}); - // quantileThrows({"3:5","to","5:10"}); - // quantileThrows({"3:5","to","2:5"}); - // quantileThrows({"1:5","to","3:5","by"}); - // quantileThrows({"1:5","to","3:5","by","1:5"}); + step({"30m-1"}, {"30m-60m"}); - // 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"}); } //-----------------------------------------------------------------------------