CPG
CPG

Reputation: 97

Python changing units on a basemap

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.

Upvotes: 0

Views: 158

Answers (1)

Adam Price
Adam Price

Reputation: 850

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.

Upvotes: 1

Related Questions