Skip to content

Commit

Permalink
METK-112 updated canonical representation of step: hours (for back co…
Browse files Browse the repository at this point in the history
…mpatibility) or hours + minutes/seconds with unit
  • Loading branch information
danovaro committed Sep 28, 2023
1 parent e8fabc9 commit 893c0ff
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 154 deletions.
99 changes: 81 additions & 18 deletions src/metkit/mars/StepRange.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,79 @@

#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"
#include "eckit/utils/Tokenizer.h"

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 {


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

Expand All @@ -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);
}
}

Expand All @@ -48,20 +112,16 @@ StepRange::StepRange(const std::string& s):
std::vector<std::string> 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:
Expand All @@ -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
28 changes: 16 additions & 12 deletions src/metkit/mars/StepRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

Expand All @@ -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"

Expand Down Expand Up @@ -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&);
Expand Down Expand Up @@ -110,8 +115,8 @@ class StepRange {

// -- Members

double from_;
double to_;
eckit::Time from_;
eckit::Time to_;

// -- Methods
// None
Expand All @@ -134,7 +139,6 @@ class StepRange {

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

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

#endif
83 changes: 3 additions & 80 deletions src/metkit/mars/TypeRange.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

//----------------------------------------------------------------------------------------------------------------------
Expand All @@ -95,7 +33,6 @@ TypeRange::TypeRange(const std::string &name, const eckit::Value& settings) :
multiple_ = true;
}


TypeRange::~TypeRange() {
}

Expand All @@ -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: {
Expand All @@ -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:
Expand Down Expand Up @@ -190,18 +122,9 @@ void TypeRange::expand(const MarsExpandContext& ctx, std::vector<std::string>& 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++;
Expand Down
Loading

0 comments on commit 893c0ff

Please sign in to comment.