#11 Find index(es) of the max value of a variable

None
open
None
5
2015-09-01
2012-11-01
kdraeder
No

I've been looking for a way to determine the index(es) of the maximum value
of a variable, within NCO. For example, if T(i=1...5) = (1 2 4 2 0), then
I want a function that returns i=3 after the max(T) is found.
This seems so straightforward and useful that it must be in NCO already,
but I haven't been able to find it in the User's Manual or the Sourceforce NCO forums.
This function should be easily generalizable to any function/method that returns the
result of a search through a variable.

Thanks for any pointers, suggestions, intentions to implement, ...

Kevin Raeder

Discussion

  • Charlie Zender
    Charlie Zender
    2012-11-02

    Hi Kevin,

    This is a good question. It has not come up in this guise before.
    ncap2 can do this in two limited ways, the where() statement and GSL:

    ncap2 -O -v -s 'foo[$lat,$lon]=0;where(precip == precip.max()) foo=1;' ~/nco/data/in.nc ~/foo.nc

    foo is a 2-D array that is flagged true where the condition is true,
    and missing values are handled correctly.

    ncap2 -O -v -s 'foo=gsl_stats_max_index(precip)' ~/nco/data/in.nc ~/foo.nc

    foo is the 1-D index where the max occurs in the 3D array, but
    missing values are not handled correctly.

    Of course there's no reason we can't add an option to return indices
    instead of, or in addition to values with functions.
    One barrier is that multidimensional arrays will want to return
    multi-dimensional indices, and that means ncap2 would need to define
    some dummy dimensions (e.g., rank_of_input_array) to hold those
    values. Nothing insurmountable.

    And that would make it easier to implement onedtond() and ndtooned()
    functions, too, which we also need. I've added this as TODO nco1080.

    c

     
  • Charlie Zender
    Charlie Zender
    2012-11-02

    • assigned_to: nobody --> zender
    • status: open --> closed
     
  • Hi Charlie,

    I currently have the same question, and was wondering whether such routine is currently already implemented? I started with the filterfunction ncmax which actually returns the maximum value of a 2D-field, but now I wonder how I can also retrieve the corresponding indices of the pixel holding this value ...

    I am currently using version 4.0.9

    Thanks.
    Best wishes,
    Matthias

     
    Last edit: Matthias Demuzere 2015-08-31
  • Charlie Zender
    Charlie Zender
    2015-08-31

    We never did anything to make this easier. Now that Henry is coming back onboard, we will take another look at adding this functionality to ncap2.

     
  • Charlie Zender
    Charlie Zender
    2015-08-31

    • status: closed --> open
    • assigned_to: Charlie Zender --> henry Butowsky
    • Group: -->
     
  • That is great news. Thanks for looking into this. Looking forward to test this new feature.