[ncl-talk] max index on a single dimension for a multidimensional array without loop

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Wed Sep 9 23:28:38 MDT 2015


Kyle, I agree.  But I think Matt wants a grid of time indices, not just the
maximum values.  Use dim_max_n to begin, then more work to get the indices.

  dims_3d = dimsizes (data)           ; (time,lat,lon)
  ntimes  = dims_3d(0)
  max_2d  = dim_max_n (data, 0)   ; grid of max point values (lat,lon)
  max_3d  = conform_dims (dims_3d, max_2d, (/1,2/))  ; expand to all time
steps
  mask_3d = (data .eq. max_3d)    ; true at all values equal to point
maximums

  inds_1d     = ispan (0, ntimes-1, 1)    ; make data array of time indices
  inds_3d     = conform_dims (dims_3d, inds_1d, 0)  ; expand to all lat,lon
  inds_3d at _FillValue = -999
  inds_masked = where (mask_3d, inds_3d, inds_3d at _FillValue)
                        ; time indices at all values equal to point maximums

; There may be two or more identical maximum values for any grid point.
; Just extract a single time index at each grid point.
; Grid points with no data are set to missing (_FillValue).

  inds_max_2d = dim_min_n (inds_masked, 0)     ; FIRST OCCURRENCE IN TIME
;  inds_max_2d = dim_max_n (inds_masked, 0)    ; LAST  OCCURRENCE IN TIME

Also, see attached working example, which plots the results.

--Dave


On Wed, Sep 9, 2015 at 9:22 PM, Kyle Griffin <ksgriffin2 at wisc.edu> wrote:

> Hi Matt,
>
> I think dim_max_n is what you're looking for.
>
> https://www.ncl.ucar.edu/Document/Functions/Built-in/dim_max_n.shtml
>
> Not sure if I've used it in this manner before, but if you specify the
> dimension to find the max over (2nd argument) as 0, I think you should get
> what you're looking for. As a built-in function, it should be faster than
> looping over each point.
>
>
> Kyle
>
> ----------------------------------------
> Kyle S. Griffin
> Department of Atmospheric and Oceanic Sciences
> University of Wisconsin - Madison
> Room 1407
> 1225 W Dayton St, Madison, WI 53706
> Email: ksgriffin2 at wisc.edu
>
> On Wed, Sep 9, 2015 at 9:08 PM, Matthew Fearon <Matthew.Fearon at dri.edu>
> wrote:
>
>> Dear NCL Users,
>>
>> Trying to get the index of the maximum value on a single dimension as
>> part of a multidimensional array. The array is data(time, lat, lon) where
>> nlat x nlon is 400x500 and ntime=24. Would like the maximum over the time
>> dimension for each grid cell or lat/long. Has anymore performed this
>> without a loop over nlat/nlon ?
>>
>> Making the array 1D (and using ind_resolve) allows me to get the max inds
>> per dimension over the whole array, which isn't what I need.
>>
>> Any suggestions would be greatly appreciated.
>> thanks,
>> Matt
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150909/742b5f59/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: grid-max-indices.ncl
Type: application/octet-stream
Size: 2299 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150909/742b5f59/attachment.obj 


More information about the ncl-talk mailing list