0

I am trying to plot precipitation data, from a netCDF4 file, on a basemap using python 3.6. The code that I am using has worked perfectly on a very similar data set. The only difference being that the previous precip data was in 'cm' while the data that I am currently trying to plot is in 'kg m-2 s-1'. The variables found in this file are time, time_bnds, lat, lat_bnds, lon, lon_bnds, and pr. pr is the precipitation variable and the one I an interested in plotting.

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*2592000,range(0,1000,10),cmap=cm.s3pcpn,latlon=True)

#data is multiplied by the amount of seconds in a month

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

plt.show() 

If you want to try and run this code with the same file I am using it is a netCDF4 file was downloaded from CMIP5 website: http://pcmdi9.llnl.gov/ My file name:(/data/CCSM4/pr_Amon_CCSM4_historical_r1i1p1_185001-200512.nc) However the original data is in netCDF3_CLASSIC format so you have to change it to netCDF4.

CPG
  • 97
  • 2
  • 15
  • is there an error that we should know about? – Woody Pride Jun 05 '17 at 20:31
  • I don't get an error message when I run this code, instead it displays a map with random colors. But it is not how the precipitation data is suppose to look and I suspect this is because the units are wrong. @WoodyPride – CPG Jun 05 '17 at 20:36

1 Answers1

1

If the only change is in the units for how much precipitation, what about converting the data to cm, that way you know it will work and you are already familiar with how to interpret the data. You could even write a function to convert it.

If you convert it, and it's in the same format you expect to work, yet it still doesn't work, you will have found that there is something else wrong with your data, so there's another plus in trying that approach.

Adam Price
  • 810
  • 2
  • 11
  • 21