What I would like to do is generate a histogram of the consecutive time spent under a level. For example if the level I was interested in was 1.0 and I had this data;
{{{2010, 1, 1, 6, 15, 0.}, 0.04375}, {{2010, 1, 1, 6, 30, 0.},0.04375},
{{2010, 1, 1, 6, 45, 0.},0.04375}, {{2010, 1, 1, 7, 0, 0.},5},
{{2010, 1, 1, 7, 15, 0.}, 0.5},{2010, 1, 1, 7, 30, 0.}, 5}}}
I would get a histogram containing two values, 45 (7:00am - 6:15am) and 15 (7:30am - 7:15am) minutes.
Any help would be appreciated.
Answer
Split time-series data based on a one-place test function, and collect total durations for spells of periods satisfying the test condition:
ClearAll[durationsF];
durationsF[timeseries : {{{__}, _} ..}, test_] :=
With[{ts = timeseries /. {{dt__}, val_} :> {AbsoluteTime[{dt}], test[val]}},
(Last@# - First@#) & /@ Map[First, Split[ts, #[[2]] === True &], {2}]/60 /.
(0) -> Sequence[]];
OP's example:
opdata = {{{2010, 1, 1, 6, 15, 0.}, 0.04375}, {{2010, 1, 1, 6, 30, 0.}, 0.04375},
{{2010, 1, 1, 6, 45, 0.}, 0.04375}, {{2010, 1, 1, 7, 0, 0.}, 5},
{{2010, 1, 1, 7, 15, 0.}, 0.5}, {{2010, 1, 1, 7, 30, 0.}, 5}};
durationsF[opdata , # <= 1 &]
(* {45, 15} *)
Weather data example:
psdnWthr = WeatherData["Pasadena", "Temperature", {{2012, 1, 1}, {2012, 1, 15}}];
DateListPlot[psdnWthr, GridLines -> {Automatic, {15}}, Joined -> True]
durationsF[psdnWthr, # <= 15 &]
(* {180, 1200, 2460, 840, 480, 720, 120, 1980, 3840, 1020} *)
Notes: If the input time series is not sorted, use ts=SortBy[ts,First]
in the function definition. For units of measurement other than minutes, multiply by appropriate factors.
Comments
Post a Comment