# Re: any good way to count no. of specified events

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu, 04 Jun 2009 15:42:30 -0600

Saji,

a = (/ 1,1,1,0,0,0,1,0,0,1,1,1,1 /)
inds = ind (a .ge. 1)
ni = dimsizes (inds)
deltas = new (ni+1, integer)
deltas = 0
deltas(1:ni-1) = (inds(1:ni-1) - inds(0:ni-2))
starts = inds(ind (deltas(0:ni-1).ne.1 .and. deltas(1:ni).eq.1))
ends = inds(ind (deltas(0:ni-1).eq.1 .and. deltas(1:ni).ne.1))
lengths = ends - starts + 1
qual_count = num (lengths .ge. 3)
print ("Number of qualifying sequences = " + qual_count)

Dave Allured
CU/CIRES Climate Diagnostics Center (CDC)
NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
http://www.cdc.noaa.gov/psd1/

Saji N. Hameed wrote:
> Dear NCL-ers,
>
> Suppose I have a 1D array of binaries (/1,1,1,0,0,0,1,0,0,1,1,1,1/)
> and I want to calculate number of 3 or more sucessive 1's
> (in this example, there are two such events)
>
> is there a good way to do that? FYI, I am trying to make an
> index of number of warm spells in a given period. I can write
> an algorithm with some do loops, but was wondering if there is
> a simpler way?
>
> saji
>
> ps: in ruby, i could have done perhaps this way
> irb> a=[1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1]
> irb> b=a.join.split(/[0]+/) # array to string, split string at multiple 0's
> => ["111", "1", "1111"]
> irb> b.map! {|num| (num.to_i>=111)? num=1 : num=nil}
> => [1, nil, 1]
> irb> b.nitems
> => 2
>
>
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jun 04 2009 - 15:42:30 MDT

This archive was generated by hypermail 2.2.0 : Mon Jun 08 2009 - 09:30:31 MDT