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

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Thu, 4 Jun 2009 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

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

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