I have two netcdf files from WRF runs, one with hourly data and the other smaller file has the coordinates (XLAT and XLONG). I am trying to retrieve a subset of the data based on certain coordinates.
An example of one of the variables is temperature 'T2' which has the dimensions (1,1015,1359) being the (time, south_north, west_east), respectively.
The XLAT and XLONG have the same dimensions (1,1015,1359).
There was an identical question asked (please see netcdf4 extract for subset of lat lon), because my lat/long dimensions are a little different the script did not work for me and I haven't been able to figure out why. I tried to change the coordinates into 1D arrays, so that it would be similar to the previous question, but the script does not work and I get an indexing error.
If someone could please help me out that would be awesome! Thanks in advance :)
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
lons = b.variables['XLONG'][:]
lats = b.variables['XLAT'][:]
lons2d =lons.reshape((1015,1359))
lons1d = lons2d.reshape((1379385))
lats2d =lats.reshape((1015,1359))
lats1d = lats2d.reshape((1379385))
lat_bnds, lon_bnds = [49,53], [-125,-115]
lat_inds = np.where((lats1d > lat_bnds[0]) & (lats1d < lat_bnds[1]))
lon_inds = np.where((lons1d > lon_bnds[0]) & (lons1d < lon_bnds[1]))
T_subset = a.variables['T2'][:,lat_inds,lon_inds]
However I get the following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-2-0f8890d3b1c5> in <module>()
25 lon_inds = np.where((lons1d > lon_bnds[0]) & (lons1d < lon_bnds[1]))
26
---> 27 T_subset = a.variables['T2'][:,lat_inds,lon_inds]
28
29
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__(netCDF4/_netCDF4.c:35672)()
/Users/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/netCDF4/utils.pyc in _StartCountStride(elem, shape, dimensions, grp, datashape, put)
197 # Raise error if multidimensional indexing is used.
198 if ea.ndim > 1:
--> 199 raise IndexError("Index cannot be multidimensional")
200 # set unlim to True if dimension is unlimited and put==True
201 # (called from __setitem__)
IndexError: Index cannot be multidimensional