0

I am using python 3.6 to plot precipitation data from CMIP5, the file I have downloaded is a netCDF4 file. I have used this code on another similar file and it worked out fine so I am not sure what the problem is. I am not receiving any error message with this code, it just displays a world map that is all one color when it should be a variety of colors. The variables found in this file are time, time_bnds, lat, lat_bnds, lon, lon_bnds, and prc. prc is the precipitation variable and the one I an interested in plotting. Any ideas would be helpful, Thank you!

Here is my code

from mpl_toolkits.basemap import Basemap, cm
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt

nc = NetCDFFile('filename.nc','r')

p = nc.variables['prc']
data = p[:,:,0]

fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1,0.1,0.8,0.8])

m = Basemap(projection='cyl',lon_0=180,lat_0=0,resolution='l')

m.drawcoastlines()
m.drawstates()
m.drawcountries()


ny = data.shape[0]; nx = data.shape[1]
lons, lats = m.makegrid(nx,ny) 
x,y = m(lons, lats) # compute map proj coordinates.

cs=plt.contourf(x,-y,data,range(0,1000,10),cmap=cm.s3pcpn,latlon=True)


cbar = m.colorbar(cs,location='bottom',pad="5%")
cbar.set_label('mm')

plt.show() 
CPG
  • 97
  • 2
  • 15
  • If this code has worked before on another file, then perhaps it's the input file that's the issue. Have you tried printing out the contents of the data array to see if it's valid? We'll need a link to the actual file you are trying to process to make any progress. – Tom Johnson Jun 03 '17 at 12:22
  • What do you get when you execute `nc.variables` ? – swatchai Jun 04 '17 at 14:18
  • Perhaps the data has a very large missing / fill value which isn't being masked properly. Have a look at data.max() and data.min() to check they are sensible. – Duncan WP Jun 05 '17 at 10:24
  • I printed out the contents of the data and the file is actually a NETCDF3_CLASSIC. This code worked before on a netcdf4 file so maybe that is why it is not working? – CPG Jun 05 '17 at 15:24

0 Answers0