I'm trying to plot some data using basemap inside a function (obs_ascsplot(d)). I pass a netcdf4 object to the function. I extract the variables and check the shapes. Then I pass only the 2D slice for the subplot. However, the error indicated that I pass a 1D instead. I cannot see how this is possible. I've included the function and call below. Would appreciate your thoughts on what's going on. I'm using Python3.6 in the Rodeo IDE.
Thanks in advance
def obs_ascsplot(d):
fig = plt.figure(2,(11.,8.),dpi=dpi)
grid_top = ImageGrid(fig, 111, nrows_ncols = (3,2),
cbar_location = "bottom",
cbar_mode="single",
label_mode = "L",
share_all=True,
cbar_size="3%",
cbar_pad="2%",
aspect = True,
axes_pad=0.2)
txt = ['(A)','(D)','(B)','(E)','(C)','(F)']
asky = d.variables['ca_asky'][:]
csky = d.variables['ca_csky'][:]
print(type(asky), 'shape = ',asky.shape)
print(type(csky), 'shape = ',csky.shape)
lon = d.variables['lon'][:]
lat = d.variables['lat'][:]
for g,pos in zip(grid_top,range(6)):
plt.sca(g)
print('Ploting figure %d'%(pos+1))
M = Basemap(projection='hammer',lon_0=0.0,lat_0=0.0,resolution='l')
M.drawcoastlines()
M.drawparallels(np.arange(-90.,120.,30.))
M.drawmeridians(np.arange(0.,400.,60.))
index = pos//2
if(pos in [0,2,4]):
I = M.pcolormesh(lon,lat,asky[index,:,:],vmin=240.,vmax=290.,
latlon=True,cmap=mycmap,shading='flat')
plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
else:
I = M.pcolormesh(lon,lat,csky[index,:,:],vmin=240.,vmax=290.,
latlon=True,cmap=mycmap,shading='flat')
plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
pdb.set_trace()
cb = grid_top.cbar_axes[0].colorbar(I)
fig.savefig(fdir+'mhs_obs_as_cs.pdf',dpi=dpi)
plt.show()
return None
if __name__ == "__main__":
# Main script below
# get obs and model
idir = './data/latest/'
i25 = 'month_means_oifs_mmr_rt_cv_mmr_t511_2007_25_0.nc'
i30 = 'month_means_oifs_mmr_rt_cv_mmr_t511_2007_30_0.nc'
d25 = nc.Dataset(idir+i25,'r')
d30 = nc.Dataset(idir+i30,'r')
print(d25.variables.keys())
print(d30.variables.keys())
x = obs_ascsplot(d25)
odict_keys(['masky', 'month', 'ch', 'lat', 'lon', 'mcsky', 'mce', 'msi', 'mzm', 'ma_asky', 'ma_csky', 'ma_ce', 'ma_si', 'asky', 'months', 'csky', 'ce', 'si', 'zm', 'ca_asky', 'ca_csky', 'ca_ce', 'ca_si'])
odict_keys(['masky', 'month', 'ch', 'lat', 'lon', 'mcsky', 'mce', 'msi', 'mzm', 'ma_asky', 'ma_csky', 'ma_ce', 'ma_si', 'asky', 'months', 'csky', 'ce', 'si', 'zm', 'ca_asky', 'ca_csky', 'ca_ce', 'ca_si'])
<class 'numpy.ma.core.MaskedArray'> shape = (3, 512, 1024)
<class 'numpy.ma.core.MaskedArray'> shape = (3, 512, 1024)
Ploting figure 1
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:1623: MatplotlibDeprecationWarning: The get_axis_bgcolor function was deprecated in version 2.0. Use get_facecolor instead.
fill_color = ax.get_axis_bgcolor()
IndexError: Inconsistant shape between the condition and the input (got (1024,) and (512, 1024))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-93-be39a3427e68> in <module>()
--> 191 x = obs_ascsplot(d25)
<ipython-input-93-be39a3427e68> in obs_ascsplot(d)
94 if(pos in [0,2,4]):
95 I = M.pcolormesh(lon,lat,asky[index,:,:],vmin=pmin,vmax=pmax,
---> 96 latlon=True,cmap=mycmap,shading='flat')
97 plt.annotate(txt[pos], xy=(0,1), xycoords='axes fraction')
98 else:
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in with_transform(self, x, y, data, *args, **kwargs)
516 # cylindrical and pseudo-cylindrical projections.
517 if self.projection in _cylproj or self.projection in _pseudocyl:
--> 518 x, data = self.shiftdata(x, data)
519 # convert lat/lon coords to map projection coords.
520 x, y = self(x,y)
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in shiftdata(self, lonsin, datain, lon_0)
4804 lonsin = np.where(mask,1.e30,lonsin)
4805 if datain is not None and mask.any():
-> 4806 datain = ma.masked_where(mask, datain)
4807 if datain is not None:
4808 return lonsin, datain
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/ma/core.py in masked_where(condition, a, copy)
1913 if cshape and cshape != ashape:
1914 raise IndexError("Inconsistant shape between the condition and the input"
-> 1915 " (got %s and %s)" % (cshape, ashape))
1916 if hasattr(a, '_mask'):
1917 cond = mask_or(cond, a._mask)
IndexError: Inconsistant shape between the condition and the input (got (1024,) and (512, 1024))