netcdf4 extract for subset of lat lon

前端 未结 7 844
时光取名叫无心
时光取名叫无心 2020-11-30 06:56

I would like to extract a spatial subset of a rather large netcdf file. From Loop through netcdf files and run calculations - Python or R

from pylab import          


        
7条回答
  •  情话喂你
    2020-11-30 07:48

    Favo's answer works (I assume; haven't checked). A more direct and idiomatic way is to use numpy's where function to find the necessary indices.

    lats = f.variables['latitude'][:] 
    lons = f.variables['longitude'][:]
    lat_bnds, lon_bnds = [40, 43], [-96, -89]
    
    lat_inds = np.where((lats > lat_bnds[0]) & (lats < lat_bnds[1]))
    lon_inds = np.where((lons > lon_bnds[0]) & (lons < lon_bnds[1]))
    
    air_subset = f.variables['air'][:,lat_inds,lon_inds]
    

提交回复
热议问题