Skip to content

Commit

Permalink
Add option to set step length when specifying list of time with keys …
Browse files Browse the repository at this point in the history
…'hours' or 'times'

"hours" : "0-12-3" -> 0,3,6,9,12
"times" : "0:00:00-1:00:00-0:15:00" -> 0:00:00,0:15:00,0:30:00,0:45:00,1:00:00
  • Loading branch information
mpartio committed Jan 31, 2024
1 parent 018d44b commit 22a938c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
22 changes: 21 additions & 1 deletion doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ Multiple origin times can also be specified with key `origintimes`.

<a name="Time_2"/>

## Lead time method 1: Listing hours
## Lead time method 1: Listing times

With key `hours` the hours (lead times) that should be calculated are listed. Values are separated with a comma, if a hyphen ('-') is used, Himan will interpolate values to fill the gap.

Expand All @@ -266,6 +266,19 @@ Example:

This example will result to hours 1,2,3,4,5,6,7 and 8.

By default when expanding a list the step value is 1, but it can be changed:

"hours" : "1,2-8-2"

Result: 1,2,4,6,8

For sub-hour values key `times` can be used:

"times" : "0:00:00-9:00:00-0:15:00"

Result: 0:00:00,0:15:00,0:30:00,...,9:00:00


<a name="Time_3"/>

## Lead time method 2: Setting start and stop values
Expand Down Expand Up @@ -297,6 +310,13 @@ Example:
"stop_minute" : "1470",
"step" : "15",

start_time and stop_time can be used to specify time duration value

"start_time" : "0:00:00",
"stop_time" : "1:00:00",
"step" : "0:15:00"


<a name="Levels"/>

# Levels
Expand Down
1 change: 1 addition & 0 deletions himan-lib/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ std::vector<T> Split(const std::string& s, const std::string& delims);
*/

std::vector<int> ExpandString(const std::string& identifier);
std::vector<time_duration> ExpandTimeDuration(const std::string& identifier);

/**
* @brief Join a string-vector with given delimiter
Expand Down
6 changes: 3 additions & 3 deletions himan-lib/source/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,14 @@ void Steps(const boost::property_tree::ptree& pt, shared_ptr<configuration>& con

try
{
vector<string> timesStr = himan::util::Split(pt.get<string>("times"), ",");
vector<time_duration> timeValues = himan::util::ExpandTimeDuration(pt.get<string>("times"));
vector<forecast_time> times;

for (const auto& originDateTime : originDateTimes)
{
for (const auto& str : timesStr)
for (const auto& tv : timeValues)
{
times.push_back(forecast_time(originDateTime, time_duration(str)));
times.push_back(forecast_time(originDateTime, tv));
}
}
conf->Times(times);
Expand Down
54 changes: 52 additions & 2 deletions himan-lib/source/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,27 @@ vector<int> util::ExpandString(const std::string& identifier)
// 1,5,10-12
// --> return a vector of:
// 1,5,10,11,12
// 1,5,10-16-2
// --> return a vector of:
// 1,5,10,12,14,16

vector<int> ret;
const auto split1 = Split(identifier, ",");
for (const auto& tok : split1)
{
const auto split2 = Split<int>(tok, "-");
auto split2 = Split<int>(tok, "-");

int step = 1;

if (split2.size() == 3)
{
step = split2[2];
split2.pop_back();
}

if (split2.size() == 2)
{
int a = split2[0], b = split2[1], step = 1;
int a = split2[0], b = split2[1];
if (a > b)
{
step *= -1;
Expand All @@ -207,6 +218,45 @@ vector<int> util::ExpandString(const std::string& identifier)
return ret;
}

vector<time_duration> util::ExpandTimeDuration(const std::string& identifier)
{
vector<time_duration> ret;
const auto split1 = Split(identifier, ",");
for (const auto& tok : split1)
{
auto split2 = Split<std::string>(tok, "-");

time_duration step = ONE_HOUR;

if (split2.size() == 3)
{
step = time_duration(split2[2]);
split2.pop_back();
}

if (split2.size() == 2)
{
time_duration a = split2[0], b = split2[1];
if (a > b)
{
step *= -1;
}

while (a != b)
{
ret.push_back(time_duration(a));
a += step;
}
ret.push_back(time_duration(a)); // include end range value
}
else if (split2.size() == 1)
{
ret.push_back(split2[0]);
}
}
return ret;
}

vector<string> util::Split(const string& s, const string& delims)
{
return Split<std::string>(s, delims);
Expand Down

0 comments on commit 22a938c

Please sign in to comment.