Skip to content

Commit 3965bce

Browse files
committed
MARS-962 added support for TypeClimateDaily and TypeClimateMonthly
1 parent dfaa11a commit 3965bce

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/metkit/mars/TypeDate.cc

+40-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010

1111
#include "eckit/utils/Translator.h"
12-
12+
#include "eckit/utils/Tokenizer.h"
1313
#include "eckit/types/Date.h"
14-
#include "metkit/mars/MarsRequest.h"
1514

15+
#include "metkit/mars/MarsRequest.h"
1616
#include "metkit/mars/TypesFactory.h"
1717
#include "metkit/mars/TypeDate.h"
1818
#include "eckit/utils/StringTools.h"
@@ -24,6 +24,24 @@ namespace metkit::mars {
2424

2525
//----------------------------------------------------------------------------------------------------------------------
2626

27+
static const char* months[] = {
28+
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
29+
};
30+
31+
int month(const std::string& value) {
32+
if (value.size() == 3) {
33+
std::string month = eckit::StringTools::lower(value).substr(0,3);
34+
for (size_t i=0; i<12; i++) { // check month name
35+
if (months[i] == month) {
36+
return i+1;
37+
}
38+
}
39+
}
40+
41+
eckit::Translator<std::string, int> s2i;
42+
return s2i(value);
43+
}
44+
2745
TypeDate::TypeDate(const std::string &name, const eckit::Value& settings) :
2846
Type(name, settings), TypeToByList<eckit::Date, long>(name, settings) {
2947

@@ -42,18 +60,35 @@ void TypeDate::pass2(const MarsExpandContext& ctx, MarsRequest& request) {
4260

4361
bool TypeDate::expand(const MarsExpandContext& ctx, std::string &value) const {
4462
if (!value.empty()) {
63+
eckit::Translator<std::string, long> s2l;
4564
eckit::Translator<long, std::string> l2s;
4665
if (value[0] == '0' || value[0] == '-') {
47-
eckit::Translator<std::string, long> s2l;
4866
long n = s2l(value);
4967
if (n <= 0) {
5068
eckit::Date now(n);
5169
value = l2s(now.yyyymmdd());
5270
}
5371
}
5472
else {
55-
eckit::Date date(value);
56-
value = l2s(date.yyyymmdd());
73+
eckit::Tokenizer t("-");
74+
std::vector<std::string> tokens = t.tokenize(value);
75+
76+
if (tokens.size() == 2) { // TypeClimateDaily
77+
int m = month(tokens[0]);
78+
long d = s2l(tokens[1]);
79+
80+
value = l2s(100*m + d);
81+
}
82+
else {
83+
if (tokens[0].size() <= 3) {
84+
int m = month(tokens[0]);
85+
value = l2s(m);
86+
}
87+
else {
88+
eckit::Date date(value);
89+
value = l2s(date.yyyymmdd());
90+
}
91+
}
5792
}
5893
}
5994
return true;

0 commit comments

Comments
 (0)