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

Matthew Fearon Matthew.Fearon at dri.edu
Thu Sep 10 11:11:28 MDT 2015


Thanks, Dave and Kyle. Both your replies were extremely helpful and I have something working now.
Matt

________________________________
From: Dave Allured - NOAA Affiliate [dave.allured at noaa.gov]
Sent: Wednesday, September 09, 2015 10:28 PM
To: Kyle Griffin
Cc: Matthew Fearon; ncl-talk at ucar.edu
Subject: Re: [ncl-talk] max index on a single dimension for a multidimensional array without loop

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<mailto: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<mailto:ksgriffin2 at wisc.edu>

On Wed, Sep 9, 2015 at 9:08 PM, Matthew Fearon <Matthew.Fearon at dri.edu<mailto: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/20150910/37ae25f7/attachment.html 


More information about the ncl-talk mailing list