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
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]