[ncl-talk] get first index of each sequence

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Mon Feb 6 18:01:19 MST 2023


When analyzing sequences, it is often useful to compare adjacent elements
by using a vector shifted by one position.  Then find an expression that is
true in the positions of interest, and false everywhere else.

    status = .not. ismissing (x)
    nx = dimsizes (x)
    previous = new (nx, logical)
    previous(0) = False      ; need this to get correct answer for first
position
    previous(1:nx-1) = status(0:nx-2)       ; vector shifted one position
to the right
    change = status(:) .and. .not. previous(:)     ; all places that
changed from missing to not
    istarts = ind (change)

    print (istarts+"")
    (0)     4
    (1)     12
    (2)     16


On Mon, Feb 6, 2023 at 7:18 PM Xi Chang via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Hi NCL community,
>
> Is there anyone who can help me on how to return the first index of each
> sequence in 1D data?
> For example,
>
> x = (/-999,-999,-999,-999,1,1,1,1,1,-999,-999,-999,1,1,1,-999,1,1,1/)
> x at _FillValue=-999
>
> There are 3 sequences of data in that 1D time series, how could I get the
> first index of each sequence, so that the answer would be:
>
> (0) 4
> (1) 12
> (2) 16
>
> Thanks
> Chang
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230206/cfd15498/attachment.htm>


More information about the ncl-talk mailing list