Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Sum hourly precip data to storm events
Date: Fri, 4 May 2012 01:07:44 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 56
Message-ID: <jnva50$81f$1@newscl01ah.mathworks.com>
References: <jn7jtt$hf8$1@newscl01ah.mathworks.com> <jnbspg$2dp$1@newscl01ah.mathworks.com> <68360b7e-62ca-446e-a5ab-e2c10d948af4@w9g2000pbm.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1336093664 8239 172.30.248.38 (4 May 2012 01:07:44 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 4 May 2012 01:07:44 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:766785

Olivia Wright <owright2@gmail.com> 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