Skip to content

Commit

Permalink
Merge branch 'feature/METK-112_subHourly' into release/1.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
danovaro committed Dec 8, 2023
2 parents 9d1f9bb + 1b6ce88 commit 8e25608
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 193 deletions.
97 changes: 82 additions & 15 deletions src/metkit/mars/StepRange.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,80 @@
*/

#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 @@ -31,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 @@ -51,12 +116,12 @@ StepRange::StepRange(const std::string& s):
switch(result.size())
{
case 1:
from_ = to_ = atof(result[0].c_str());
to_ = from_ = eckit::Time(result[0], true);
break;

case 2:
from_ = atof(result[0].c_str());
to_ = atof(result[1].c_str());
from_ = eckit::Time(result[0], true);
to_ = eckit::Time(result[1], true);
break;

default:
Expand All @@ -65,21 +130,23 @@ StepRange::StepRange(const std::string& s):
throw eckit::BadValue(msg.str(), Here());
break;
}

}

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: 18 additions & 10 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,8 +34,17 @@ class StepRange {

StepRange(const std::string&);

StepRange(double from = 0,double to = 0):
from_(from),to_(to) {}
StepRange(eckit::Time from = eckit::Time(0), eckit::Time to = eckit::Time(0)):
from_(from),to_(to) {

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 @@ -65,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 @@ -106,8 +115,8 @@ class StepRange {

// -- Members

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

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

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

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

#endif
Loading

0 comments on commit 8e25608

Please sign in to comment.