[ncl-talk] quicker way to do maxind ?
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Tue Apr 19 15:08:12 MDT 2016
Xiaoming,
Here is an NCL method that is faster than a double loop when ntimes is
significantly smaller than ni * nj. Array "x" is the 3-D input data array,
same as your "mpr".
xmax = dim_max_n_Wrap (x, 0)
missing_value = -99
xind = new (dimsizes (xmax), integer, missing_value)
ntimes = dimsizes (x(:,0,0))
do t = 1, ntimes
tr = ntimes - t
mask1 = (x(tr,:,:) .ge. xmax)
mask1 = where (ismissing (mask1), False, mask1)
xind = where (mask1, tr, xind)
end do
This version handles missing values. It could be a little simpler and
faster without missing values.
LIke the maxind function, this example finds the index of the "first
occurrence" of the maximum, at each grid point. To find the "last
occurrence", simply run the loop forward instead of backward.
--Dave
On Tue, Apr 19, 2016 at 11:26 AM, xiaoming Hu <yuanfangcan at hotmail.com>
wrote:
> I need to get the index of the maximum value at each location.
>
> It turns out the following loop is very slow! (i,j loop between 0-1000)
>
> do j = 0, dimlc(0)-1
> print("working on row "+j)
> do i = 0, dimlc(1)-1
> Time_mpr(j,i) = maxind(mpr(:,j,i))
> end do ;
> end do ;
>
> Better approach to do this quicker?
>
> Thanks
> Xiaoming
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160419/f0b84bb3/attachment.html
More information about the ncl-talk
mailing list