[ncl-talk] how to get min index within different sequences

Mary Haley haley at ucar.edu
Thu Oct 9 13:00:34 MDT 2014


I had an error in my code.  Please see the new code.

I encourage you to learn how to debug this code and understand what's going
on so you can extend it for your own use.

--Mary


On Fri, Oct 3, 2014 at 6:04 AM, Verena Lili <verena.prick at gmail.com> wrote:

> Hallo Mary,
>
> Im sorry for disturbing you with this email.
> No solution so far i got to overcome this issue.
> The algorithmn you suggested is failed once i change the number of
> sequences.
>
> Really look fotward to your help.
> Best regards,
>
> Dr. Verena,
>
>
>
>
> On Sunday, September 28, 2014, Verena Lili <verena.prick at gmail.com> wrote:
>
>> Hi Mary and NCL,
>>
>> The logics still produce errors and are not cosistent once I changed the
>> number of sequences:
>> see below my test: I actually only have 5 consecutive series but the
>> result was 7!
>>
>> *x = (/*-999,-999*,9,5,4,6*,-999,-999*,6,5,*-999*,7,*-999*,25,17,3,2,13,*
>> -999*,-5,*-999*/)*
>>
>> x at _FillValue = -999
>>
>> nx = dimsizes(x)
>>
>> run_inds = new(nx,integer)   ; array to hold minimum indexes of runs
>> beg_ind = -1                 ; variables to hold begin and end indexes
>> end_ind = -1                 ; of consecutive sequences of numbers
>> irun    = 0                  ; counter for number of runs
>>
>> ;---Loop through each element and collect groups of runs.
>> do n=0,nx-1
>>   if(ismissing(x(n))) then
>> ;---Check for a run
>>     if(beg_ind.ne.-1) then
>>       run_inds(irun) = beg_ind + minind(x(beg_ind:end_ind))
>>       irun           = irun + 1
>>       beg_ind        = -1        ; reset back to -1
>>     end if
>>     continue
>>   end if
>>   if(beg_ind.eq.-1) then
>> ;---Start a new run
>>     beg_ind  = n
>>     end_ind  = n
>>     prev_ind = n   ; to keep track of consecutive inds
>>   else
>> ;---Still in a run
>>     if((n-prev_ind).eq.1) then
>>       end_ind = n
>>     else
>> ;---End a current run
>>       run_inds(irun) = beg_ind + minind(x(beg_ind:end_ind))
>>       irun           = irun + 1
>>       beg_ind        = -1        ; reset back to -1
>>     end if
>>   end if
>> ;---Special case if on the last number
>>   if(n.eq.(nx-1).and.beg_ind.ne.-1) then
>>     run_inds(irun) = beg_ind + minind(x(beg_ind:end_ind))
>>     irun           = irun + 1
>>   end if
>> end do
>>
>> print("Number of runs      = " + irun)
>> print("Min indexes of runs = " + run_inds(0:irun-1))
>>
>>
>> *ncl 43> print("Number of runs      = " + irun)*
>> *(0) Number of runs      = 7*
>>
>> *ncl 44> print("Min indexes of runs = " + run_inds(0:irun-1))*
>> *(0) Min indexes of runs = 3*
>> *(1) Min indexes of runs = 5*
>> *(2) Min indexes of runs = 9*
>> *(3) Min indexes of runs = 11*
>> *(4) Min indexes of runs = 14*
>> *(5) Min indexes of runs = 16*
>> *(6) Min indexes of runs = 19*
>>
>>
>>
>> On Mon, Sep 22, 2014 at 12:26 PM, Mary Haley <haley at ucar.edu> wrote:
>>
>>> ​Lili,
>>>
>>> In the future, please do not post questions to individual developers
>>> when you don't get a response on ncl-talk.  It's okay to repost to ncl-talk
>>> after a few days, because we generally try to answer all questions.
>>>
>>> You will need to loop through your array and get the start and end index
>>> of each consecutive sequence of numbers, and then use NCL's minind function
>>> to get the index of the minimum of this subset, "i =
>>> minind(x(beg_index:end_index)". There may be a "cleaner" way to do this,
>>> but this script should work.
>>>
>>> See attached script which uses a slightly more complex input array.
>>>
>>> --Mary
>>>
>>> On Sat, Sep 20, 2014 at 12:55 AM, Verena Lili <verena.prick at gmail.com>
>>> wrote:
>>>
>>>> Hi Mary,
>>>>
>>>> I posted this email to ncl talk (see below) and not getting any
>>>> response since then. Would you please help me on this simple issue?
>>>> I've been struggling to figure this out, but not yet get a conclusive
>>>> result
>>>>
>>>> thank you
>>>>
>>>>
>>>> =======================
>>>> Subject: how to get min index within different sequences
>>>> To: "ncl-talk at ucar.edu" <ncl-talk at ucar.edu>
>>>>
>>>>
>>>>
>>>> Hallo,
>>>>
>>>> Anyone can suggest how to get the index of the minimum values within
>>>> different
>>>> sequences:
>>>>
>>>> e.g.:
>>>>
>>>>  x = (/-3, -2, -1, 4, 5, 6, 0, -2, 5/)
>>>>
>>>>  x at _FillValue=-999
>>>>  x = where(x.gt.3,x, x at _FillValue)  ; only x>3
>>>>  print(x)
>>>>
>>>> Variable: x
>>>> (0)     -999
>>>> (1)     -999
>>>> (2)     -999
>>>> (3)     4
>>>> (4)     5
>>>> (5)     6
>>>> (6)     -999
>>>> (7)     -999
>>>> (8)     5
>>>>
>>>> ; then, how to get *index* pointing the minimum value within these two
>>>> sequences (3)-(5) and (8),
>>>> so that the out put will be index:
>>>>
>>>> *3 and 8*
>>>>
>>>> Thank you.
>>>>
>>>>
>>>> --
>>>> Regards,
>>>> Dr. Verena.
>>>> School of Marine and Atmospheric Sciences
>>>> Stony Brook University
>>>> homepage: http://www.somas.stonybrook.edu/
>>>>
>>>>
>>>>
>>>> --
>>>> Regards,
>>>> Dr. Verena.
>>>> School of Marine and Atmospheric Sciences
>>>> Stony Brook University
>>>> homepage: http://www.somas.stonybrook.edu/
>>>>
>>>
>>>
>>
>>
>> --
>> Regards,
>> Dr. Verena.
>> School of Marine and Atmospheric Sciences
>> Stony Brook University
>> homepage: http://www.somas.stonybrook.edu/
>>
>
>
> --
> Regards,
> Dr. Verena.
> School of Marine and Atmospheric Sciences
> Stony Brook University
> homepage: http://www.somas.stonybrook.edu/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20141009/ddc839d6/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xmin.ncl
Type: application/octet-stream
Size: 1775 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20141009/ddc839d6/attachment.obj 


More information about the ncl-talk mailing list