# [ncl-talk] Detecting local_minima of a time series with a specified length (local_min_1d)

Tue Feb 6 09:49:20 MST 2018

```Hi Lyndz,
local_min_1d allows the user to return the indices of the local mins as
opposed to the actual values. You should run it once for the values, then
run it again to get the indices:
; code is untested!
x = (/35.91,22.43,15.84,6.62,4.71,9.67,12.25,2.01,30.25,85.64/)
qmin=local_min_1d(x,False,-0.25,0)
imin=local_min_1d(x,False,-0.25,1)

qmin2 = new(dimsizes(qmin),typeof(qmin))
cntr = 0
do gg = 0,dimsizes(qmin)-1
ival = imin(gg)
if (ival.ge.2.and.ival.le.(dimsizes(x)-3)) then   ; do not test on end
points of timeseries
if
((x(ival-2)-x(ival-1)).ge.0.and.(x(ival-1)-x(ival)).ge.0.and.(x(ival+1)-x(ival)).ge.0.and.(x(ival+2)-x(ival+1)).ge.0)
then    ; apply your criteria here
qmin2(cntr) = x(ival)
cntr = cntr+1
end if
end if
end do
print(qmin2)

Make sure you test the above code to check that it is doing what you think
it is doing. I did not test it. For my criteria I arbitrarily chose to make
sure that the two points before and after the identified min were greater
than the value of the identified min. You will likely want to alter that in
some way.
Good luck. If you have any further questions please reply to the ncl-talk
email list.

On Mon, Feb 5, 2018 at 8:09 PM, Lyndz <olagueralyndonmark429 at gmail.com>
wrote:

> Dear NCL experts,
>
> I am trying to locate the local minima in a time series (1D) but with
> points(before or after the local minima).
>
> Here's an example
>
>    * x = (/35.91,22.43,15.84,6.62,4.71,9.67,12.25,2.01,30.25,85.64/)*
>
> To detect the local minima I used the following commands:
>
>
> *qmin=local_min_1d(x,False,-0.25,0)*
> *    print(qmin)*
>
> This gives the following:
>
> *     Variable: qmin *
> *    Type: float  *
> *    Total Size: 8 bytes*
> *            2 values*
> *    Number of Dimensions: 1*
> *    Dimensions and sizes: [2]*
> *    Coordinates: *
> *    (0) 4.71*
> *    (1) 2.01*
>
> The answer to my problem above should be the (0) 4.71 since the 2.01 is an
> abrupt decrease.
>
> Here's another example:
>
>     * y = (/14.52,20.50,22.43,3.18,9.39,10.95,65.38,40.45,5.89,16.75/)*
>
> *qmin=local_min_1d(y,False,-0.25,0)*
>    *  print(qmin)*
>
> *    Variable: qmin*
> *    Type: float*
> *    Total Size: 8 bytes*
> *            2 values*
> *    Number of Dimensions: 1*
> *    Dimensions and sizes: [2]*
> *    Coordinates: *
> *    (0) 3.18*
> *    (1) 5.89*
>
> The answer here should be the (0) 3.18. The second one is also an abrupt
> decrease.
>
> Is there an efficient way to implement this in NCL? I will be applying
> this for many csv files.
>
>
> I'll appreciate any suggestion.
>
> Sincerely,
>
> Lyndz
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

--