Skip to content

Commit 22a938c

Browse files
committed
Add option to set step length when specifying list of time with keys '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
1 parent 018d44b commit 22a938c

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

doc/configuration.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Multiple origin times can also be specified with key `origintimes`.
254254

255255
<a name="Time_2"/>
256256

257-
## Lead time method 1: Listing hours
257+
## Lead time method 1: Listing times
258258

259259
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.
260260

@@ -266,6 +266,19 @@ Example:
266266

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

269+
By default when expanding a list the step value is 1, but it can be changed:
270+
271+
"hours" : "1,2-8-2"
272+
273+
Result: 1,2,4,6,8
274+
275+
For sub-hour values key `times` can be used:
276+
277+
"times" : "0:00:00-9:00:00-0:15:00"
278+
279+
Result: 0:00:00,0:15:00,0:30:00,...,9:00:00
280+
281+
269282
<a name="Time_3"/>
270283

271284
## Lead time method 2: Setting start and stop values
@@ -297,6 +310,13 @@ Example:
297310
"stop_minute" : "1470",
298311
"step" : "15",
299312

313+
start_time and stop_time can be used to specify time duration value
314+
315+
"start_time" : "0:00:00",
316+
"stop_time" : "1:00:00",
317+
"step" : "0:15:00"
318+
319+
300320
<a name="Levels"/>
301321

302322
# Levels

himan-lib/include/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ std::vector<T> Split(const std::string& s, const std::string& delims);
5555
*/
5656

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

5960
/**
6061
* @brief Join a string-vector with given delimiter

himan-lib/source/json_parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,14 @@ void Steps(const boost::property_tree::ptree& pt, shared_ptr<configuration>& con
765765

766766
try
767767
{
768-
vector<string> timesStr = himan::util::Split(pt.get<string>("times"), ",");
768+
vector<time_duration> timeValues = himan::util::ExpandTimeDuration(pt.get<string>("times"));
769769
vector<forecast_time> times;
770770

771771
for (const auto& originDateTime : originDateTimes)
772772
{
773-
for (const auto& str : timesStr)
773+
for (const auto& tv : timeValues)
774774
{
775-
times.push_back(forecast_time(originDateTime, time_duration(str)));
775+
times.push_back(forecast_time(originDateTime, tv));
776776
}
777777
}
778778
conf->Times(times);

himan-lib/source/util.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,27 @@ vector<int> util::ExpandString(const std::string& identifier)
180180
// 1,5,10-12
181181
// --> return a vector of:
182182
// 1,5,10,11,12
183+
// 1,5,10-16-2
184+
// --> return a vector of:
185+
// 1,5,10,12,14,16
183186

184187
vector<int> ret;
185188
const auto split1 = Split(identifier, ",");
186189
for (const auto& tok : split1)
187190
{
188-
const auto split2 = Split<int>(tok, "-");
191+
auto split2 = Split<int>(tok, "-");
192+
193+
int step = 1;
194+
195+
if (split2.size() == 3)
196+
{
197+
step = split2[2];
198+
split2.pop_back();
199+
}
189200

190201
if (split2.size() == 2)
191202
{
192-
int a = split2[0], b = split2[1], step = 1;
203+
int a = split2[0], b = split2[1];
193204
if (a > b)
194205
{
195206
step *= -1;
@@ -207,6 +218,45 @@ vector<int> util::ExpandString(const std::string& identifier)
207218
return ret;
208219
}
209220

221+
vector<time_duration> util::ExpandTimeDuration(const std::string& identifier)
222+
{
223+
vector<time_duration> ret;
224+
const auto split1 = Split(identifier, ",");
225+
for (const auto& tok : split1)
226+
{
227+
auto split2 = Split<std::string>(tok, "-");
228+
229+
time_duration step = ONE_HOUR;
230+
231+
if (split2.size() == 3)
232+
{
233+
step = time_duration(split2[2]);
234+
split2.pop_back();
235+
}
236+
237+
if (split2.size() == 2)
238+
{
239+
time_duration a = split2[0], b = split2[1];
240+
if (a > b)
241+
{
242+
step *= -1;
243+
}
244+
245+
while (a != b)
246+
{
247+
ret.push_back(time_duration(a));
248+
a += step;
249+
}
250+
ret.push_back(time_duration(a)); // include end range value
251+
}
252+
else if (split2.size() == 1)
253+
{
254+
ret.push_back(split2[0]);
255+
}
256+
}
257+
return ret;
258+
}
259+
210260
vector<string> util::Split(const string& s, const string& delims)
211261
{
212262
return Split<std::string>(s, delims);

0 commit comments

Comments
 (0)