[ncl-talk] ndtooned and loop

Buzan, Jonathan Robert jbuzan at purdue.edu
Wed May 22 03:25:14 MDT 2019


Hi NCL-Talk,

I am attempting to speed up my code.
Originally I was looping:

x(th_index,lat,lon)
y(perc_index,lat,lon)
Where: th_index = 292, perc_index = 101, lat = 192, lon = 288 

do k = 0, dimsizes(lon) -1
   do j = 0, dimsizes(lat) -1
      do l = 0, dimsizes(th_index) -1
	y1 = y(:,j,k)
	x1 = x(l,j,k)
       Functions…
      end do
   end do
end do

But, this is taking too long.  The NCL function, closest_val, only operates on single grid cell and/or a single number within a grid cell. Runs for hours and hits wall-clock time…

I am trying to use ndtooned to reduce the number of loops. 
Then loop over the lat and lons by an increment of the th_index. Unfortunately, this is not behaving the way I expected.  When I print(x1d), the order it seems to be x(0,:,:), then x(1,:,:)… x(291,:,:), instead of x(:,0,0), x(:,0,1)… x(:,191,287). The goal is to operate on the th_index for each grid cell. I am comparing each grid cell to another grid cell of the same lat,lon coordinate. I need the loop increment to be the lat-lon coordinate space, so I can compare the values between multiple 3d arrays (same lat,lon, but different 3 dimension).


Example ‘faster’ code:
x1d = ndtooned(x)
do k = 0, (dimsizes(lat) * dimsizes(lon) -1)
   print(k)
   index_th_st = dimsizes(th_index) * k
   index_th_end = dimsizes(th_index) * k + (dimsizes(th_index) -1)
   index_perc_st = dimsizes(perc_index) * k
   index_perc_end = dimsizes(perc_index) * k + (dimsizes(perc_index) -1)

   y1 = y1d(index_perc_st:index_perc_end)
   x1 = x1d(index_th_st:index_th_end)
      do l = 0, dimsizes(th_index) -1
        x1_s = x1(l)
       Functions…
   end do
 end do


Any help in this respect would be much appreciated! 

-Jonathan



More information about the ncl-talk mailing list