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

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Thu, 04 Jun 2009 18:09:58 -0600

Hi Saji

A generalization of Dave Allured's code

Good luck
D

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
>
>

function numBinOneRuns ( a[*]:integer, nCrit:integer)

; Dave Allured: CU/CIRES Climate Diagnostics Center (CDC)
; Return the number of runs [sequences] of 1s

local inds, ni, deltas, starts, ends, lengths
begin
dim_nCrit = dimsizes(nCrit)
if (dim_nCrit.gt.2) then
print("numBinOneRuns: nCrit size must be .le. 2: nCrit="+dim_nCrit)
return(-999)
end if

inds = ind (a .eq. 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

if (dim_nCrit.eq.1) then
return(num (lengths.ge.nCrit) )
else
return(num (lengths.ge.nCrit(0) .and. lengths.le.nCrit(1)) )
end if
end

a = (/ 1,1,1,0,0,0,1,0,0,1,1,1,1 /)

ncrit = 1
nqual = numBinOneRuns ( a, ncrit)
print("nqual="+nqual+" (ncrit="+ncrit+")")

ncrit = 3
nqual = numBinOneRuns ( a, ncrit)
print("nqual="+nqual+" (ncrit="+ncrit+")")

ncrit = 4
nqual = numBinOneRuns ( a, ncrit)
print("nqual="+nqual+" (ncrit="+ncrit+")")

print(" ")
print("============================================")
print(" ")

x = (/1,1,1,0,0,0,1,0,0,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1,1 \
,0,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1/)
do ncrit=1,10
nqual = numBinOneRuns ( x, ncrit)
print("nqual="+nqual+" (ncrit="+ncrit+")")
end do

print(" ")
print("============================================")
print(" ")

kcrit = (/2, 5/) ; number of runs between 2 an 5 inclusive
kqual = numBinOneRuns ( x, kcrit)
print("kqual="+kqual+" (kcrit=["+kcrit(0)+","+kcrit(1)+"] )")

print(" ")
print("============================================")
print(" ")

kcrit = (/3, 3/) ; number of runs of length 3
kqual = numBinOneRuns ( x, kcrit)
print("kqual="+kqual+" (kcrit=["+kcrit(0)+","+kcrit(1)+"] )")

kcrit = (/8, 8/) ; number of runs of length 8
kqual = numBinOneRuns ( x, kcrit)
print("kqual="+kqual+" (kcrit=["+kcrit(0)+","+kcrit(1)+"] )")

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jun 04 2009 - 18:09:58 MDT

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