# Thread Subject: Sum hourly precip data to storm events

 Subject: Sum hourly precip data to storm events From: Olivia Date: 25 Apr, 2012 01:27:25 Message: 1 of 10 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!
 Subject: Sum hourly precip data to storm events From: Roger Stafford Date: 26 Apr, 2012 16:23:12 Message: 2 of 10 "Olivia " wrote in message ... > 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! - - - - - - - - - - -   How about a simple for-loop 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.)   Let x be a vector giving the hourly precipitation.  y = zeros(size(x));  f = false;  iy = 0;  for ix = 1:length(x)    if x(ix) ~= 0      c = 0;      if ~f        f = true;        iy = iy + 1;      end      y(iy) = y(iy) + x(ix);    else      if f        c = c + 1;        if c >= 24          f = false;        end      end    end  end  y = y(1:iy); Then y is a vector giving total precipitation for each storm. Roger Stafford
 Subject: Sum hourly precip data to storm events From: ImageAnalyst Date: 26 Apr, 2012 17:22:02 Message: 3 of 10 On Apr 24, 9:27 pm, "Olivia " wrote: > 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! ------------------------------------------------------------------------- Did you ever try the solutions given in your duplicate posting in Answers: http://www.mathworks.com/matlabcentral/answers/36584-sum-hourly-precipiation-data-into-individual-storm-events
 Subject: Sum hourly precip data to storm events From: Olivia Wright Date: 27 Apr, 2012 01:24:02 Message: 4 of 10 On Apr 26, 9:23 am, "Roger Stafford" wrote: > "Olivia " wrote in message ... > > 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! > > - - - - - - - - - - - >   How about a simple for-loop 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.) > >   Let x be a vector giving the hourly precipitation. > >  y = zeros(size(x)); >  f = false; >  iy = 0; >  for ix = 1:length(x) >    if x(ix) ~= 0 >      c = 0; >      if ~f >        f = true; >        iy = iy + 1; >      end >      y(iy) = y(iy) + x(ix); >    else >      if f >        c = c + 1; >        if c >= 24 >          f = false; >        end >      end >    end >  end >  y = y(1:iy); > > Then y is a vector giving total precipitation for each storm. > > Roger Stafford That worked great! Thank you Roger!
 Subject: Sum hourly precip data to storm events From: Roger Stafford Date: 27 Apr, 2012 06:49:08 Message: 5 of 10 Olivia Wright wrote in message ... > That worked great! Thank you Roger! - - - - - - - - -   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 for-loop solution.   As before, x is an hourly precipitation column vector and the result y is a list of storm totals.  d = diff([false;x~=0;false]);  p = find(d<0);  if p==[]   y = []; % No rainfall at all  else   q = find(d>0);   p = p(1:end-1);   s = [1;zeros(size(x,1)-1,1)];   s(p(q(2:end)-p>=24)) = 1;   y = accumarray(cumsum(s),x);  end Roger Stafford
 Subject: Sum hourly precip data to storm events From: Olivia Wright Date: 3 May, 2012 02:02:07 Message: 6 of 10 On Apr 26, 11:49 pm, "Roger Stafford" wrote: > Olivia Wright wrote in message ... > > That worked great! Thank you Roger! > > - - - - - - - - - >   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 for-loop solution. > >   As before, x is an hourly precipitation column vector and the result y is a list of storm totals. > >  d = diff([false;x~=0;false]); >  p = find(d<0); >  if p==[] >   y = []; % No rainfall at all >  else >   q = find(d>0); >   p = p(1:end-1); >   s = [1;zeros(size(x,1)-1,1)]; >   s(p(q(2:end)-p>=24)) = 1; >   y = accumarray(cumsum(s),x); >  end > > Roger Stafford Is there a way to use your code in the first reply and add the same time periods in another array? I have streamflow data for the same time span as the precipitation data. I want to sum the streamflow events that happen with each precip event but the streamflow data does not reach zero because of baseflow contribution. I have not played witht he code above yet but I am sure that will help me gain a better understanding the previous code as well. Thank you! -Olivia -Olivia
 Subject: Sum hourly precip data to storm events From: Roger Stafford Date: 3 May, 2012 03:09:08 Message: 7 of 10 Olivia Wright wrote in message <9e2bc7b1-61be-4a13-95e9-d651f17fb106@qg3g2000pbc.googlegroups.com>... > Is there a way to use your code in the first reply and add the same > time periods in another array? I have streamflow data for the same > time span as the precipitation data. I want to sum the streamflow > events that happen with each precip event but the streamflow data does > not reach zero because of baseflow contribution. I have not played > witht he code above yet but I am sure that will help me gain a better > understanding the previous code as well. Thank you! - - - - - - - - -   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 non-zero 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 non-precipitation periods greater than 24 hours which again might not be what you want since it is between storms.   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. Roger Stafford
 Subject: Sum hourly precip data to storm events From: Roger Stafford Date: 4 May, 2012 01:07:44 Message: 9 of 10 Olivia Wright wrote in message <68360b7e-62ca-446e-a5ab-e2c10d948af4@w9g2000pbm.googlegroups.com>... > The basin that I'm looking at is very responsive to rain events. There > is approx a 1 hour delay in the peak of streamflow events once the > rainfall peaks. Since I'm looking at 24 hours of dry for a rain event > to end, I'm going to assume the resulting streamflow event occurs > during the same time frame. I want the code to be flexible so I can > change the number of dry hours between events to best capture > corresponding rain and streamflow events. This may be too much to code > in matlab but I have over 30,000 hours of data. > ......... > Thanks for all of your help! I was hoping to do this analysis hourly > but if it seems to complicated, I may have to attempt the to do it by > hand or switch to daily data which will be much easier. - - - - - - - - - - -   I modified the vectorized code (2nd reply) so that it can sum the "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.)   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 if-else technique that was used in that 2nd reply code. Execute the following matlab code:  d = diff([false;p~=0;false]);  f1 = find(d>0);  f2 = find(d<0);  t1 = f1(2:end)-f2(1:end-1)>=h;  f1 = f1([true;t1]);  f2 = f2([t1;true]);  t2 = zeros(size(p,1)+1,1);  t2(f1) = 1;  t2(f2) = -1;  t = cumsum(t2(1:end-1))>0;  ix = zeros(size(p));  ix(f1) = 1;  ix = cumsum(ix);  rt = accumarray(ix(t),p(t));  st = accumarray(ix(t),s(t)); 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 non-zero 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.   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.   Here is a simple test example with h = 3. p = [0 3 2 0 0 4 7 0 0 0 2 8 0 8 0 0 0 0 4].'; s = [1 5 7 2 3 9 7 2 3 1 4 7 8 0 2 3 0 2 3].'; You should get three "storms" with the following totals, along with the following intermediate array values: t = [F T T T T T T F F F T T T T F F F F T].' ix= [0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3].' rt = [16 18 4].' st = [33 19 3].' (Note: p and s don't have to be integer-valued - it was just easier to add integers in my head.)   Please let me know if you have problems with this. Roger Stafford
 Subject: Sum hourly precip data to storm events From: Olivia Wright Date: 4 May, 2012 19:23:10 Message: 10 of 10 On May 3, 6:07 pm, "Roger Stafford" wrote: > Olivia Wright wrote in message <68360b7e-62ca-446e-a5ab-e2c10d948...@w9g2000pbm.googlegroups.com>... > > The basin that I'm looking at is very responsive to rain events. There > > is approx a 1 hour delay in the peak of streamflow events once the > > rainfall peaks. Since I'm looking at 24 hours of dry for a rain event > > to end, I'm going to assume the resulting streamflow event occurs > > during the same time frame. I want the code to be flexible so I can > > change the number of dry hours between events to best capture > > corresponding rain and streamflow events. This may be too much to code > > in matlab but I have over 30,000 hours of data. > > ......... > > Thanks for all of your help! I was hoping to do this analysis hourly > > but if it seems to complicated, I may have to attempt the to do it by > > hand or switch to daily data which will be much easier. > > - - - - - - - - - - - >   I modified the vectorized code (2nd reply) so that it can sum the > "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.) > >   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 if-else technique that was used in that 2nd reply code.  Execute the following matlab code: > >  d = diff([false;p~=0;false]); >  f1 = find(d>0); >  f2 = find(d<0); >  t1 = f1(2:end)-f2(1:end-1)>=h; >  f1 = f1([true;t1]); >  f2 = f2([t1;true]); >  t2 = zeros(size(p,1)+1,1); >  t2(f1) = 1; >  t2(f2) = -1; >  t = cumsum(t2(1:end-1))>0; >  ix = zeros(size(p)); >  ix(f1) = 1; >  ix = cumsum(ix); >  rt = accumarray(ix(t),p(t)); >  st = accumarray(ix(t),s(t)); > > 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 non-zero 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. > >   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. > >   Here is a simple test example with h = 3. > > p = [0 3 2 0 0 4 7 0 0 0 2 8 0 8 0 0 0 0 4].'; > s = [1 5 7 2 3 9 7 2 3 1 4 7 8 0 2 3 0 2 3].'; > > You should get three "storms" with the following totals, along with the following intermediate array values: > > t = [F T T T T T T F F F T T T T F F F F T].' > ix= [0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3].' > rt = [16 18 4].' > st = [33 19 3].' > > (Note: p and s don't have to be integer-valued - it was just easier to add integers in my head.) > >   Please let me know if you have problems with this. > > Roger Stafford This works great! Thank you for all of your help! Saves me so much time

## Tags for this Thread

### Add a New Tag:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
storm events Oli 24 Apr, 2012 21:29:23
Feed for this Thread