Calculate the duration of each storm event

3 views (last 30 days)
Hi all! I am trying to do some statistical anaylsis of precipitation and I would like to implement a code which do the following: I have hourly data of precipitation and what I would like to do is to calculate the duration of each storm. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start calculating the duration of the following storm. The resulting output should be a vector in which there are listed the durations of the different storms. Does anybody know how I could solve it? Thanks a lot Camilla

Answers (1)

Guillaume
Guillaume on 10 Feb 2015
You haven't specified the criteria for the beginning of a storm, so I can only give you a generic solution. In any case, what you ultimately want to do is find the run length of a series of true / false (0 / 1). The first thing is thus to convert your precipitation data into 0/1, possibly like this:
precipitation = [0 0 0.2 0.4 1 1.3 1.1 0.2 0 0 0 0.3 2 2.1 1.9 0.2 0.1 0 0 0]; %made up data
isstorm = precipitation > 0.1; %for example
From there, a combination of diff and find is all you need to do to find the run length of the storms.
%ensure that we start and end with no storm
%so the first transition is always from sunny to storm
%and last transition is always from storm to sunny
isstorm = [0 isstorm 0];
%find transitions between sunny and storm and back
beforetransition = find(diff(isstorm));
%elements 1:2:end are starts of storms
%elements 2:2:end are ends o storms
%duration is the difference
%because we made sure to start and end sunny, there's always an even number of transitions
stormduration = beforetransition(2:2:end) - beforetransition(1:2:end);
  4 Comments
Guillaume
Guillaume on 10 Feb 2015
To sum up, this is the code
precipitation = [
rand(1, 12), zeros(1, 4), rand(1, 4), ... %1st storm duration = 20
zeros(1, 26), ...
rand(1, 4), zeros(1, 5), rand(1, 6), zeros(1, 23), rand(1, 12), ... %2nd storm duration = 50
zeros(1, 24), ...
rand(1, 14)]; %last storm duration = 14
isstorm = [0 precipitation > 0 0];
transitions = find(diff(isstorm));
tooshort = find(transitions(3:2:end) - transitions(2:2:end-2) < 24);
transitions([2*tooshort 2*tooshort+1]) = [];
stormduration = transitions(2:2:end) - transitions(1:2:end)
Camilla Santicoli
Camilla Santicoli on 11 Feb 2015
It works! Thank you very much for your help Guillaume, I was trying to solve it with index and diff but it wasn't working! Thanks again, very kind!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!