Hello everyone,
I'm doing some IDL-abuse in hydrology, so my question might seem a bit
odd, maybe.
A task that is occurring quite regularly is to determine the duration of
certain events. For example "What is the longest contiguous duration of
stream flow below/above a certain discharge"
Assuming I have an equidistant time series (e.g. one value each day)
this basically reduces to the question of how can I transform an array
like this
series = [1,1,0,0,0,0,1,0,1,1,1,0,0,1,1]
into something like this
durations = [2,1,3,2]
which is I want to count all contiguous fields of '1's in an array.
Somehow my brain wants to use HISTOGRAM for this, but I just can't see
how to do it.
At the moment I'm helping myself by using CONVOL(to highlight the edges)
and WHERE(to get the differences between two adjacent edge indices) but
as the data gets more, this becomes extremely tedious as well as memory
consuming (see the example below). Besides, CONVOL wouldn't work if a
series started or ended with '1's as it can't correctly apply the kernel
to those elements.
Any ideas? Anyone who has seen this problem in one of his/her maths
textbooks? Hints to literature are also highly appreciated.
Thanks in advance,
Thomas
Here's an example how I did it 'the hard way'
;sim_mask is an array consisting of 0 and 1
sim_mask = round(randomu(10, 2000))
;create space for the 'histogram'
sim_hist = replicate( 0, $
n_elements(where(convol(sim_mask, [1,3,1]) eq 3)) $
+ n_elements(where(convol(sim_mask, [1,3,1]) eq 4))/2)
;determine the durations by subtracting the respective pairs of
;occurences of the 'edge-marker' 4
for i=1, n_elements(where(convol(sim_mask,[1,3,1]) eq 4))-1, 2 do $
sim_hist[i/2] = (where(convol(sim_mask,[1,3,1]) eq 4))[i] $
- (where(convol(sim_mask,[1,3,1]) eq 4))[i-1]
;all values are now one less than the actual duration
sim_hist +=1
--
_______________________________________________________
Dipl.-Ing Thomas Pfaff, M.Eng./Univ. of Tokyo
Dr.-Ing. Karl Ludwig
Beratender Ingenieur
Wasserwirtschaft - Wasserbau
Herrenstr. 14
76133 Karlsruhe
Tel: 0721 / 91251-46
Fax: 0721 / 91251-19
thomas.pfaff@ludwig-wawi.de
|