http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467
MATLAB Central Newsreader  Sum hourly precip data to storm events
Feed for thread: Sum hourly precip data to storm events
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 25 Apr 2012 01:27:25 +0000
Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#874783
Olivia
Hi all! I have hourly precipitation data that I want to sum into individual storm events. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start adding the values of a new storm. The resulting output will be the precipitation depth of each storm event. Anyone have any ideas how I can do this in matlab? Thank you!

Thu, 26 Apr 2012 16:23:12 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875013
Roger Stafford
"Olivia " <owright2@gmail.com> wrote in message <jn7jtt$hf8$1@newscl01ah.mathworks.com>...<br>
> Hi all! I have hourly precipitation data that I want to sum into individual storm events. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start adding the values of a new storm. The resulting output will be the precipitation depth of each storm event. Anyone have any ideas how I can do this in matlab? Thank you!<br>
          <br>
How about a simple forloop solution? It's easier to code than a vectorized method. (Moreover, I am guessing it will be faster in spite of the number of lines.)<br>
<br>
Let x be a vector giving the hourly precipitation.<br>
<br>
y = zeros(size(x));<br>
f = false;<br>
iy = 0;<br>
for ix = 1:length(x)<br>
if x(ix) ~= 0<br>
c = 0;<br>
if ~f<br>
f = true;<br>
iy = iy + 1;<br>
end<br>
y(iy) = y(iy) + x(ix);<br>
else<br>
if f<br>
c = c + 1;<br>
if c >= 24<br>
f = false;<br>
end<br>
end<br>
end<br>
end<br>
y = y(1:iy);<br>
<br>
Then y is a vector giving total precipitation for each storm.<br>
<br>
Roger Stafford

Thu, 26 Apr 2012 17:22:02 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875020
ImageAnalyst
On Apr 24, 9:27 pm, "Olivia " <owrig...@gmail.com> wrote:<br>
> Hi all! I have hourly precipitation data that I want to sum into individual storm events. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start adding the values of a new storm. The resulting output will be the precipitation depth of each storm event. Anyone have any ideas how I can do this in matlab? Thank you!<br>
<br>
<br>
Did you ever try the solutions given in your duplicate posting in<br>
Answers:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/answers/36584sumhourlyprecipiationdataintoindividualstormevents">http://www.mathworks.com/matlabcentral/answers/36584sumhourlyprecipiationdataintoindividualstormevents</a>

Fri, 27 Apr 2012 01:24:02 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875057
Olivia Wright
On Apr 26, 9:23 am, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> "Olivia " <owrig...@gmail.com> wrote in message <jn7jtt$hf...@newscl01ah.mathworks.com>...<br>
> > Hi all! I have hourly precipitation data that I want to sum into individual storm events. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start adding the values of a new storm. The resulting output will be the precipitation depth of each storm event. Anyone have any ideas how I can do this in matlab? Thank you!<br>
><br>
>           <br>
> How about a simple forloop solution? It's easier to code than a vectorized method. (Moreover, I am guessing it will be faster in spite of the number of lines.)<br>
><br>
> Let x be a vector giving the hourly precipitation.<br>
><br>
> y = zeros(size(x));<br>
> f = false;<br>
> iy = 0;<br>
> for ix = 1:length(x)<br>
> if x(ix) ~= 0<br>
> c = 0;<br>
> if ~f<br>
> f = true;<br>
> iy = iy + 1;<br>
> end<br>
> y(iy) = y(iy) + x(ix);<br>
> else<br>
> if f<br>
> c = c + 1;<br>
> if c >= 24<br>
> f = false;<br>
> end<br>
> end<br>
> end<br>
> end<br>
> y = y(1:iy);<br>
><br>
> Then y is a vector giving total precipitation for each storm.<br>
><br>
> Roger Stafford<br>
<br>
That worked great! Thank you Roger!

Fri, 27 Apr 2012 06:49:08 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875069
Roger Stafford
Olivia Wright <owright2@gmail.com> wrote in message <cfd517e86edb48528062cb781fb83848@u4g2000pbu.googlegroups.com>...<br>
> That worked great! Thank you Roger!<br>
         <br>
The following is the best I could think of in the way of vectorizing your problem. You can compare its performance with that of the forloop solution.<br>
<br>
As before, x is an hourly precipitation column vector and the result y is a list of storm totals.<br>
<br>
d = diff([false;x~=0;false]);<br>
p = find(d<0);<br>
if p==[]<br>
y = []; % No rainfall at all<br>
else<br>
q = find(d>0);<br>
p = p(1:end1);<br>
s = [1;zeros(size(x,1)1,1)];<br>
s(p(q(2:end)p>=24)) = 1;<br>
y = accumarray(cumsum(s),x);<br>
end<br>
<br>
Roger Stafford

Thu, 03 May 2012 02:02:07 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875614
Olivia Wright
On Apr 26, 11:49 pm, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> Olivia Wright <owrig...@gmail.com> wrote in message <cfd517e86edb48528062cb781fb83...@u4g2000pbu.googlegroups.com>...<br>
> > That worked great! Thank you Roger!<br>
><br>
>         <br>
> The following is the best I could think of in the way of vectorizing your problem. You can compare its performance with that of the forloop solution.<br>
><br>
> As before, x is an hourly precipitation column vector and the result y is a list of storm totals.<br>
><br>
> d = diff([false;x~=0;false]);<br>
> p = find(d<0);<br>
> if p==[]<br>
> y = []; % No rainfall at all<br>
> else<br>
> q = find(d>0);<br>
> p = p(1:end1);<br>
> s = [1;zeros(size(x,1)1,1)];<br>
> s(p(q(2:end)p>=24)) = 1;<br>
> y = accumarray(cumsum(s),x);<br>
> end<br>
><br>
> Roger Stafford<br>
<br>
Is there a way to use your code in the first reply and add the same<br>
time periods in another array? I have streamflow data for the same<br>
time span as the precipitation data. I want to sum the streamflow<br>
events that happen with each precip event but the streamflow data does<br>
not reach zero because of baseflow contribution. I have not played<br>
witht he code above yet but I am sure that will help me gain a better<br>
understanding the previous code as well. Thank you!<br>
<br>
Olivia<br>
<br>
Olivia

Thu, 03 May 2012 03:09:08 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875617
Roger Stafford
Olivia Wright <owright2@gmail.com> wrote in message <9e2bc7b161be4a1395e9d651f17fb106@qg3g2000pbc.googlegroups.com>...<br>
> Is there a way to use your code in the first reply and add the same<br>
> time periods in another array? I have streamflow data for the same<br>
> time span as the precipitation data. I want to sum the streamflow<br>
> events that happen with each precip event but the streamflow data does<br>
> not reach zero because of baseflow contribution. I have not played<br>
> witht he code above yet but I am sure that will help me gain a better<br>
> understanding the previous code as well. Thank you!<br>
        <br>
No, I think both of the codes would have to be revised somewhat to accomplish what you ask. In the first reply, addition only occurs when a nonzero amount of precipitation falls during any hour even though that might be part of a period less than 24 hours when the storm temporarily stopped raining but was not actually over. You would miss the amount of "streamflow" data occurring at such times. On the other hand the second reply code performs addition at every hour including nonprecipitation periods greater than 24 hours which again might not be what you want since it is between storms.<br>
<br>
I think you would need to define carefully the times you want "streamflow" to be summed, but I think neither code I wrote would do what you want by just replacing the precipitation inputs by streamflow inputs in the addition process.<br>
<br>
Roger Stafford

Thu, 03 May 2012 17:01:54 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875700
Olivia Wright
On May 2, 8:09 pm, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> Olivia Wright <owrig...@gmail.com> wrote in message <9e2bc7b161be4a1395e9d651f17fb...@qg3g2000pbc.googlegroups.com>...<br>
> > Is there a way to use your code in the first reply and add the same<br>
> > time periods in another array? I have streamflow data for the same<br>
> > time span as the precipitation data. I want to sum the streamflow<br>
> > events that happen with each precip event but the streamflow data does<br>
> > not reach zero because of baseflow contribution. I have not played<br>
> > witht he code above yet but I am sure that will help me gain a better<br>
> > understanding the previous code as well. Thank you!<br>
><br>
>         <br>
> No, I think both of the codes would have to be revised somewhat to accomplish what you ask. In the first reply, addition only occurs when a nonzero amount of precipitation falls during any hour even though that might be part of a period less than 24 hours when the storm temporarily stopped raining but was not actually over. You would miss the amount of "streamflow" data occurring at such times. On the other hand the second reply code performs addition at every hour including nonprecipitation periods greater than 24 hours which again might not be what you want since it is between storms.<br>
><br>
> I think you would need to define carefully the times you want "streamflow" to be summed, but I think neither code I wrote would do what you want by just replacing the precipitation inputs by streamflow inputs in the addition process.<br>
><br>
> Roger Stafford<br>
<br>
The basin that I'm looking at is very responsive to rain events. There<br>
is approx a 1 hour delay in the peak of streamflow events once the<br>
rainfall peaks. Since I'm looking at 24 hours of dry for a rain event<br>
to end, I'm going to assume the resulting streamflow event occurs<br>
during the same time frame. I want the code to be flexible so I can<br>
change the number of dry hours between events to best capture<br>
corresponding rain and streamflow events. This may be too much to code<br>
in matlab but I have over 30,000 hours of data.<br>
<br>
Initially when I was approaching this problem. I thought I could<br>
create an new array that tagged a timestep with precip as 1 and no<br>
precip as 0. And if that array as 24 hours of 0s in a row, then sum<br>
the precip and streamflow for that same time frame, but I only knew<br>
how to make it look at one 0 and start summing again, instead of<br>
summing to 24 zeros and starting a new event. Does that make sense?<br>
<br>
Thanks for all of your help! I was hoping to do this analysis hourly<br>
but if it seems to complicated, I may have to attempt the to do it by<br>
hand or switch to daily data which will be much easier.<br>
<br>
Olivia

Fri, 04 May 2012 01:07:44 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875755
Roger Stafford
Olivia Wright <owright2@gmail.com> wrote in message <68360b7e62ca446ea5abe2c10d948af4@w9g2000pbm.googlegroups.com>...<br>
> The basin that I'm looking at is very responsive to rain events. There<br>
> is approx a 1 hour delay in the peak of streamflow events once the<br>
> rainfall peaks. Since I'm looking at 24 hours of dry for a rain event<br>
> to end, I'm going to assume the resulting streamflow event occurs<br>
> during the same time frame. I want the code to be flexible so I can<br>
> change the number of dry hours between events to best capture<br>
> corresponding rain and streamflow events. This may be too much to code<br>
> in matlab but I have over 30,000 hours of data.<br>
> .........<br>
> Thanks for all of your help! I was hoping to do this analysis hourly<br>
> but if it seems to complicated, I may have to attempt the to do it by<br>
> hand or switch to daily data which will be much easier.<br>
          <br>
I modified the vectorized code (2nd reply) so that it can sum the <br>
"streamflow" only over the duration of each storm. Perhaps that comes closer to what you want. (I hate to think of anyone doing 30000 additions by hand when matlab is available.)<br>
<br>
Let p be a column vector of the hourly precipitation, s be another column vector of the same length with streamflow data, and h be the minimum number of hours for each dry period between storms. (h is 24 in your current method.) It is assumed here that there will be at least one storm. If not, you can use the same ifelse technique that was used in that 2nd reply code. Execute the following matlab code:<br>
<br>
d = diff([false;p~=0;false]);<br>
f1 = find(d>0);<br>
f2 = find(d<0);<br>
t1 = f1(2:end)f2(1:end1)>=h;<br>
f1 = f1([true;t1]);<br>
f2 = f2([t1;true]);<br>
t2 = zeros(size(p,1)+1,1);<br>
t2(f1) = 1;<br>
t2(f2) = 1;<br>
t = cumsum(t2(1:end1))>0;<br>
ix = zeros(size(p));<br>
ix(f1) = 1;<br>
ix = cumsum(ix);<br>
rt = accumarray(ix(t),p(t));<br>
st = accumarray(ix(t),s(t));<br>
<br>
The 'rt' and 'st' arrays will contain the total precipitation and total streamflow occurring during each storm, which is defined as the period starting with the first nonzero value in p to the last one for each storm. Each storm is separated from the next one by at least h hours of (rainless) zeros in p.<br>
<br>
To aid in understanding the code, the logical array 't' is true during the period of each storm and false in the intervals in between (each of which must be at least h hours long.) The index vector 'ix' indexes the storms in chronological order.<br>
<br>
Here is a simple test example with h = 3.<br>
<br>
p = [0 3 2 0 0 4 7 0 0 0 2 8 0 8 0 0 0 0 4].';<br>
s = [1 5 7 2 3 9 7 2 3 1 4 7 8 0 2 3 0 2 3].';<br>
<br>
You should get three "storms" with the following totals, along with the following intermediate array values:<br>
<br>
t = [F T T T T T T F F F T T T T F F F F T].'<br>
ix= [0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3].'<br>
rt = [16 18 4].'<br>
st = [33 19 3].'<br>
<br>
(Note: p and s don't have to be integervalued  it was just easier to add integers in my head.)<br>
<br>
Please let me know if you have problems with this.<br>
<br>
Roger Stafford

Fri, 04 May 2012 19:23:10 +0000
Re: Sum hourly precip data to storm events
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319467#875827
Olivia Wright
On May 3, 6:07 pm, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> Olivia Wright <owrig...@gmail.com> wrote in message <68360b7e62ca446ea5abe2c10d948...@w9g2000pbm.googlegroups.com>...<br>
> > The basin that I'm looking at is very responsive to rain events. There<br>
> > is approx a 1 hour delay in the peak of streamflow events once the<br>
> > rainfall peaks. Since I'm looking at 24 hours of dry for a rain event<br>
> > to end, I'm going to assume the resulting streamflow event occurs<br>
> > during the same time frame. I want the code to be flexible so I can<br>
> > change the number of dry hours between events to best capture<br>
> > corresponding rain and streamflow events. This may be too much to code<br>
> > in matlab but I have over 30,000 hours of data.<br>
> > .........<br>
> > Thanks for all of your help! I was hoping to do this analysis hourly<br>
> > but if it seems to complicated, I may have to attempt the to do it by<br>
> > hand or switch to daily data which will be much easier.<br>
><br>
>           <br>
> I modified the vectorized code (2nd reply) so that it can sum the<br>
> "streamflow" only over the duration of each storm. Perhaps that comes closer to what you want. (I hate to think of anyone doing 30000 additions by hand when matlab is available.)<br>
><br>
> Let p be a column vector of the hourly precipitation, s be another column vector of the same length with streamflow data, and h be the minimum number of hours for each dry period between storms. (h is 24 in your current method.) It is assumed here that there will be at least one storm. If not, you can use the same ifelse technique that was used in that 2nd reply code. Execute the following matlab code:<br>
><br>
> d = diff([false;p~=0;false]);<br>
> f1 = find(d>0);<br>
> f2 = find(d<0);<br>
> t1 = f1(2:end)f2(1:end1)>=h;<br>
> f1 = f1([true;t1]);<br>
> f2 = f2([t1;true]);<br>
> t2 = zeros(size(p,1)+1,1);<br>
> t2(f1) = 1;<br>
> t2(f2) = 1;<br>
> t = cumsum(t2(1:end1))>0;<br>
> ix = zeros(size(p));<br>
> ix(f1) = 1;<br>
> ix = cumsum(ix);<br>
> rt = accumarray(ix(t),p(t));<br>
> st = accumarray(ix(t),s(t));<br>
><br>
> The 'rt' and 'st' arrays will contain the total precipitation and total streamflow occurring during each storm, which is defined as the period starting with the first nonzero value in p to the last one for each storm. Each storm is separated from the next one by at least h hours of (rainless) zeros in p.<br>
><br>
> To aid in understanding the code, the logical array 't' is true during the period of each storm and false in the intervals in between (each of which must be at least h hours long.) The index vector 'ix' indexes the storms in chronological order.<br>
><br>
> Here is a simple test example with h = 3.<br>
><br>
> p = [0 3 2 0 0 4 7 0 0 0 2 8 0 8 0 0 0 0 4].';<br>
> s = [1 5 7 2 3 9 7 2 3 1 4 7 8 0 2 3 0 2 3].';<br>
><br>
> You should get three "storms" with the following totals, along with the following intermediate array values:<br>
><br>
> t = [F T T T T T T F F F T T T T F F F F T].'<br>
> ix= [0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3].'<br>
> rt = [16 18 4].'<br>
> st = [33 19 3].'<br>
><br>
> (Note: p and s don't have to be integervalued  it was just easier to add integers in my head.)<br>
><br>
> Please let me know if you have problems with this.<br>
><br>
> Roger Stafford<br>
<br>
This works great! Thank you for all of your help! Saves me so much time