[ncl-talk] array indexing in ncl
Jatin Kala
jatin.kala.jk at gmail.com
Tue Nov 28 19:59:49 MST 2017
Hi,
Suppose we have a 3-D array, size (103,lat,lon), or whatever...
If i want to extract a time series at two points, say (:,1,2) and
(:,2,4), then in Python you can do it quite simply, and you end up with
an array which is size (103,2), which one would expect:
import netCDF4 as nc
f = nc.Dataset('wrfout_d03_cntl')
hfx = f.variables['HFX'][:]
hfx2 = hfx[:,[1,2],[2,4]]
print(hfx2.shape)
But if i do the same thing in ncl, i actually get an array (103,2,2),
which is not what one would expect:
f = addfile("wrfout_d03_cntl","r")
hfx = f->HFX
hfx2 = hfx(:,(/1,2/),(/2,4/))
print(dimsizes(hfx2))
The only way to get the right answer in ncl, is to pre-define hfx as
(103,2), then loop through the indices, and put in the array within the
loop.
Does anyone else find this rather counter-intuitive? I.e., the way ncl
does array indexing in such cases? Is there a way to achieve the same
result in ncl without having to loop?
Cheers,
Jatin
More information about the ncl-talk
mailing list