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

From: Saji N. Hameed <saji_at_nyahnyahspammersnyahnyah>
Date: Fri, 5 Jun 2009 03:35:08 +0900

Hi Wei Huang,

Thanks a lot for the reply and suggestions. The new string functions look very
useful and I look forward to version 5.1.1. Meanwhile I created a custom function
for this kind of problems (reproduced below) as a temporary solution since the
version of NCL on our main machines is still around 4.0 :(

Best wishes,
saji

procedure start_window(is,iwin,i)
begin
is(iwin)=i
end

procedure end_window(ie,iwin,i)
begin
ie(iwin)=i
iwin=iwin+1
end

function delta_window_widths(var)
begin
nvar=dimsizes(var)
is=new(dimsizes(var),typeof(var))
ie=var
prev=0
iwin=0
do i = 0,nvar-1
curr=var(i)
if prev.eq.0 .and. curr.eq.1
start_window(is,iwin,i)
prev=1
end if
if prev.eq.1 .and. curr.eq.0
end_window(ie,iwin,i-1)
prev=0
end if
if i.eq.(nvar-1)
end_window(ie,iwin,i)
end if
end do
return(ie-is+1)
end
function width_of_delta_function(var,crit,wide)
begin
all_widths=delta_window_widths(var)
if crit.eq."ge"
return(all_widths(ind(all_widths.ge.wide)))
end if
if crit.eq."gt"
return(all_widths(ind(all_widths.gt.wide)))
end if
if crit.eq."lt"
return(all_widths(ind(all_widths.lt.wide)))
end if
if crit.eq."le"
return(all_widths(ind(all_widths.le.wide)))
end if
end

a=(/1,1,0,0,1,1,0,0,0,1,0,1,1,1,1,0,0,0,0,0,1,1,1/)
delta_windows=width_of_delta_function(a,"le",3)
print(""+delta_windows)
print("------")
print("no. of events ="+dimsizes(delta_windows))

* Wei Huang <huangwei_at_ucar.edu> [2009-06-04 09:25:10 -0600]:

> Saji,
>
> The soon to be released [mid-late June 2009] NCL v5.1.1
> has a number of new string handling functions. See:
> http://www.ncl.ucar.edu/future_release.shtml
>
> These functions could be used to perform tasks like your
> ruby code.
>
> I have a sample, which use the new functions in NCL v5.1.1
> to perform something similar to what you have done with Ruby.
>
> ;----------------------------------------------------
> a = (/1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1/)
> print(a)
>
> ;Convert integer array a to string array b
> ;After add new type conversion, probably in the release after NCL
> v5.1.1
> ;b = tostring(a)
>
> ;For NCL v5.1.1
> b = sprinti("%i", a)
> print(b)
>
> ;concatenate string array b to a single string
> str = str_concat(b)
> print(str) ;str = "1110001001111"
>
> ;find the index of "111" in str
> c = str_index_of_substr(str, "111", 0)
>
> print(dimsizes(c)) ;dimsizes(c) = 2
>
> print(c) ;c = (/0, 9/)
>
> ;Or do it differently
> delim = "0"
>
> ;find number of fields separated by delimiter
> nfields = str_fields_count(str, delim)
> print(nfields) ; nfields = 3
>
> ;define a new string array of size nfields
> newStrArr = new(nfields, string)
>
> ;get each field from str separated by delimiter, and save to newStrArr
> do n = 1, nfields
> newStrArr(n-1) = str_get_field(str, n, delim)
> end do
>
> print(newStrArr) ;newStrArr = (/"111", "1", "1111"/)
>
> ;At this point, you can use NCL current functions to process this new
> string array,
> ;or you can convert it to integer array, and so on...
> ;----------------------------------------------------
>
> Wei Huang
> huangwei_at_ucar.edu
> VETS/CISL
> National Center for Atmospheric Research
> P.O. Box 3000 (1850 Table Mesa Dr.)
> Boulder, CO 80307-3000 USA
> (303) 497-8924
>
>
>
>
>
> On Jun 4, 2009, at 3:58 AM, 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
>>
>>
>> --
>> Saji N. Hameed
>>
>> APEC Climate Center
>> 1463 U-dong, Haeundae-gu, +82 51 745
>> 3951
>> BUSAN 612-020, KOREA saji_at_apcc21.net
>> Fax: +82-51-745-3999
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

```--
Saji N. Hameed
APEC Climate Center
1463 U-dong, Haeundae-gu,                               +82 51 745 3951
BUSAN 612-020, KOREA                    		saji_at_apcc21.net
Fax: +82-51-745-3999
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
```
Received on Thu Jun 04 2009 - 12:35:08 MDT

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