# Re: using ind and num function instead of IF statement

Date: Thu Nov 12 2009 - 08:26:10 MST

Dear Saji,

Thank you so much for your response. The function numBinOneRuns
perfectly works for my problem 2 i.e. calculating number of sequence x
> 20 for consecutive 10
months and more at particular gridcell.

Do you have any suggestion for problem 1 i.e. for consecutive 3 to 9
months at a particular gridcell.

Thanks again

Debasish

Quoting Saji N Hameed <saji@apcc21.net>:

> Hi Debasish,
>
> I am not confident that my algorithm is correct, so I have not attached it.
>
> Here is how I would do it in 3 steps.
>
> 1. Create a binary event -- that is, map your values to 1 when x > 10 and 0
> otherwise
> ___ ___ ___
> 2. count number of delta functions __| |____| |____| |__
> 3. select items in 2, which conform to your specification of duration
>
> Step 1 is simple, and you can customize it for your need:
> example invocation for your problem 1 :: xcat=categorize_var(x,"gt",10)
> function categorize_var(var,sign,crit)
> begin
> if sign.eq."gt"
> return(where(var.gt.crit,1,0))
> end if
> if sign.eq."lt"
> return(where(var.lt.crit,1,0))
> end if
> end
>
> Here is Dave Allured's code for item 2 and maybe 3 too..
> I had to write one in Fortran to run the multi dimensional case, such as
> yours. If you need the fortran version, let me know. However I hope you
> can extend the NCL function below so that it works for the 2-dimensional
> case.
>
> 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
>
> cheers!
> saji
> --
>
> * Debasish PaiMazumder <debasish@sca.uqam.ca> [2009-11-11 16:19:56 -0500]:
>
>> Sorry for posting again... Last post has some numerical errors
>>
>> Hello,
>>
>> I need help regarding a problems mentioned below
>>
>> I have a variable x with dimension month, latitude and longitude
>>
>> x(month,lat, lon) month=360, lat= 172, lon=180
>>
>> I need to find out following things
>>
>> 1) Y (lat,lon)= number of 3 to 9 month events having x exceeding 10
>> That means I need to calculate how many times x > 10 for consecutive
>> 3 to 9 months at a particular gridcell
>>
>> 2) suppose Y1(lat, lon)=number of at least 10 month and more events
>> having x exceeding 20
>> i.e. means I need to calculate how many time x > 20 for consecutive 10
>> months and more at particular gridcell
>>
>> How do I calculate Y and Y1 without using if loop in NCL
>>
>> Thanks
>>
>> Debasish
>>
>> Debasish PaiMazumder, PhD
>> Postdoctoral Researcher
>> Analysis and Simulation of Regional-Scale Climate
>> Department of Earth and Atmospheric Sciences
>> University of Québec at Montréal (UQÀM)
>> 550, Sherbrooke west, 19th floor, west tower,
>> Montréal, QC, Canada H3A 1B9
>> Tel: (514) 282-6464-343
>> (514) 937-0042 (R)
>> email: debasish@sca.uqam.ca
>>
>>
>>
>>
>>
>
>> _______________________________________________
>> 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@apcc21.net
> Fax: +82-51-745-3999
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Nov 12 08:25:33 2009

This archive was generated by hypermail 2.1.8 : Tue Nov 17 2009 - 17:27:43 MST