Nested grids

Does anyone have an example of nested grids.

I need to (for instance), plot a global grid in filled contours, then plot another, higher resolution grid over the US.

The global grid could work something like this, but I’m not sure where to start with the 2nd (nested) grid to go atop.

fig=plt.figure()

m = Basemap(resolution=‘c’,projection=‘cyl’,llcrnrlon=lon1,llcrnrlat=lat1,\

urcrnrlon=lon2,urcrnrlat=lat2)

make a filled contour plot.

x, y = m(lons, lats)

CS = m.contour(x,y,hgt,15,linewidths=0.5,colors=‘k’)

CS = m.contourf(x,y,hgt,15,cmap=plt.cm.jet)

m.drawcoastlines()

m.drawmapboundary()

m.fillcontinents()

draw parallels and meridians.

parallels = np.arange(-90,90,10)

#m.drawparallels(parallels,labels=[1,0,0,1])

m.drawparallels(parallels,labels=[1,0,0,1])

meridians = np.arange(-180.,180.,10.)

m.drawmeridians(meridians,labels=[1,0,0,1])

plt.title(‘Plotted Grid’)

plt.show()

Thanks!

Bruce

···

Bruce W. Ford
Clear Science, Inc.
bruce@…2905…

It should just work as you expect. Do the global contour with the
coarser x/y data, and then do the US contour with finer x/y data.

Ben Root

···

On Monday, June 6, 2011, Bruce Ford <bruce@...2905...> wrote:

Does anyone have an example of nested grids.
I need to (for instance), plot a global grid in filled contours, then plot another, higher resolution grid over the US.The global grid could work something like this, but I'm not sure where to start with the 2nd (nested) grid to go atop.

fig=plt.figure()m = Basemap(resolution='c',projection='cyl',llcrnrlon=lon1,llcrnrlat=lat1,\ urcrnrlon=lon2,urcrnrlat=lat2)# make a filled contour plot.

x, y = m(lons, lats)CS = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')CS = m.contourf(x,y,hgt,15,cmap=plt.cm.jet)m.drawcoastlines()m.drawmapboundary()
m.fillcontinents()
# draw parallels and meridians.parallels = np.arange(-90,90,10)#m.drawparallels(parallels,labels=[1,0,0,1])m.drawparallels(parallels,labels=[1,0,0,1])meridians = np.arange(-180.,180.,10.)

m.drawmeridians(meridians,labels=[1,0,0,1])plt.title('Plotted Grid')plt.show()
Thanks!
Bruce
---------------------------------------
Bruce W. Ford
Clear Science, Inc.
bruce@...2905...

That’s what I thought, but the code below fails (ValueError: Shape mismatch objects cannot be broadcast to a single shape) when trying to plot the 2nd parameter. Do I need another basemap?

Appreciate any thoughts!

from mpl_toolkits.basemap import Basemap, shiftgrid
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure()
lon1 = 0
lon2 = 357.5
lat1 = -90
lat2 = 90

m = Basemap(resolution=‘c’,projection=‘cyl’,llcrnrlon=lon1,llcrnrlat=lat1,
urcrnrlon=lon2,urcrnrlat=lat2)

#get data and plot 1st parameter - This works
filename = “/data/reanal-2/ltm/monthly_ltm_01.nc

print filename
nc = Dataset(filename, mode=“r”)
hgt = nc.variables[‘HGT_2_ISBL_10’][16,:,:]
print hgt.shape
lons = nc.variables[‘longitude’][:]
lats = nc.variables[‘latitude’][:]

x,y = np.meshgrid(lons, lats)
CS = m.contour(x,y,hgt,15,linewidths=0.5,colors=‘k’)
CS = m.contourf(x,y,hgt,cmap=plt.cm.jet)

···

###########################

#extract and plot 2nd paramter - This fails with

ValueError: Shape mismatch objects cannot be broadcast to a single shape

filename1 = “/data/nps_datasets/WNA_T2m_200808.nc”
print filename1
nc1 = Dataset(filename1, mode=“r”)
for var in nc1.variables:

print var

t2m = nc1.variables[‘T2m’][0,:,:]
print t2m.shape
lons1 = nc1.variables[‘lon’][:]
lats1 = nc1.variables[‘lat’][:]
x1,y1 = np.meshgrid(lons1, lats1)
CS1 = m.contour(x1,y1,t2m,15,linewidths=0.5,colors=‘k’)

CS1 = m.contourf(x1,y1,t2m,cmap=plt.cm.jet)
#############################

m.drawcoastlines()
m.drawmapboundary()
#m.fillcontinents()

draw parallels and meridians.

parallels = np.arange(-90,90,30)

#m.drawparallels(parallels,labels=[1,0,0,1])
m.drawparallels(parallels,labels=[1,0,0,1])
meridians = np.arange(0,357.5,30)
m.drawmeridians(meridians,labels=[1,0,0,1])
plt.title(‘Plotted Grid’)
plt.show()


Bruce W. Ford
Clear Science, Inc.
bruce@…2905…
http://www.ClearScienceInc.com

http://www.facebook.com/clearscience
http://www.twitter.com/ROVs_rule
Phone: (904) 796-8101

Fax: (904) 379-9704
8241 Parkridge Circle N.
Jacksonville, FL 32211
Skype: bruce.w.ford

On Tue, Jun 7, 2011 at 1:06 AM, Benjamin Root <ben.root@…1304…> wrote:

On Monday, June 6, 2011, Bruce Ford <bruce@…2905…> wrote:

Does anyone have an example of nested grids.

I need to (for instance), plot a global grid in filled contours, then plot another, higher resolution grid over the US.The global grid could work something like this, but I’m not sure where to start with the 2nd (nested) grid to go atop.

fig=plt.figure()m = Basemap(resolution=‘c’,projection=‘cyl’,llcrnrlon=lon1,llcrnrlat=lat1,\ urcrnrlon=lon2,urcrnrlat=lat2)# make a filled contour plot.

x, y = m(lons, lats)CS = m.contour(x,y,hgt,15,linewidths=0.5,colors=‘k’)CS = m.contourf(x,y,hgt,15,cmap=plt.cm.jet)m.drawcoastlines()m.drawmapboundary()

m.fillcontinents()

draw parallels and meridians.parallels = np.arange(-90,90,10)#m.drawparallels(parallels,labels=[1,0,0,1])m.drawparallels(parallels,labels=[1,0,0,1])meridians = np.arange(-180.,180.,10.)

m.drawmeridians(meridians,labels=[1,0,0,1])plt.title(‘Plotted Grid’)plt.show()

Thanks!

Bruce


Bruce W. Ford

Clear Science, Inc.

bruce@…2905…

It should just work as you expect. Do the global contour with the

coarser x/y data, and then do the US contour with finer x/y data.

Ben Root

I would double-check the shapes of x1 and y1 to make sure it matches
t2m's shape. You shouldn't need another Basemap object.

Ben Root

···

On Tuesday, June 7, 2011, Bruce Ford <bruce@...2905...> wrote:

That's what I thought, but the code below fails (ValueError: Shape mismatch objects cannot be broadcast to a single shape) when trying to plot the 2nd parameter. Do I need another basemap?

Appreciate any thoughts!

from mpl_toolkits.basemap import Basemap, shiftgrid
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure()
lon1 = 0
lon2 = 357.5
lat1 = -90
lat2 = 90
m = Basemap(resolution='c',projection='cyl',llcrnrlon=lon1,llcrnrlat=lat1,\
urcrnrlon=lon2,urcrnrlat=lat2)

#get data and plot 1st parameter - This works
filename = "/data/reanal-2/ltm/monthly_ltm_01.nc"
print filename
nc = Dataset(filename, mode="r")
hgt = nc.variables['HGT_2_ISBL_10'][16,:,:]
print hgt.shape
lons = nc.variables['longitude'][:]
lats = nc.variables['latitude'][:]
x,y = np.meshgrid(lons, lats)
CS = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
CS = m.contourf(x,y,hgt,cmap=plt.cm.jet)
###########################

#extract and plot 2nd paramter - This fails with
# ValueError: Shape mismatch objects cannot be broadcast to a single shape
filename1 = "/data/nps_datasets/WNA_T2m_200808.nc"
print filename1
nc1 = Dataset(filename1, mode="r")
for var in nc1.variables:
print var
t2m = nc1.variables['T2m'][0,:,:]
print t2m.shape
lons1 = nc1.variables['lon'][:]
lats1 = nc1.variables['lat'][:]
x1,y1 = np.meshgrid(lons1, lats1)
CS1 = m.contour(x1,y1,t2m,15,linewidths=0.5,colors='k')
CS1 = m.contourf(x1,y1,t2m,cmap=plt.cm.jet)
#############################

m.drawcoastlines()
m.drawmapboundary()
#m.fillcontinents()
# draw parallels and meridians.
parallels = np.arange(-90,90,30)
#m.drawparallels(parallels,labels=[1,0,0,1])
m.drawparallels(parallels,labels=[1,0,0,1])
meridians = np.arange(0,357.5,30)
m.drawmeridians(meridians,labels=[1,0,0,1])
plt.title('Plotted Grid')
plt.show()

---------------------------------------
Bruce W. Ford
Clear Science, Inc.
bruce@...2905...
http://www.ClearScienceInc.com
Clear Science, Inc.
http://www.twitter.com/ROVs_rule
Phone: (904) 796-8101
Fax: (904) 379-9704
8241 Parkridge Circle N.
Jacksonville, FL 32211
Skype: bruce.w.ford

That's what I thought, but the code below fails (ValueError: Shape
mismatch objects cannot be broadcast to a single shape) when trying to
plot the 2nd parameter. Do I need another basemap?

No, but check the dimensions of t2m; maybe it is transposed relative to what is needed. It needs to have shape (n_latitudes, n_longitudes), just like x1 and y1. It's confusing, but it's a common convention.

Also, although this is not relevant to your particular problem here, it might be good to transform your lon, lat 2-D arrays into map coordinates, so that if you decide to use a different projection some day, it will still work.

Eric

···

On 06/07/2011 11:36 AM, Bruce Ford wrote:

Appreciate any thoughts!

from mpl_toolkits.basemap import Basemap, shiftgrid
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure()
lon1 = 0
lon2 = 357.5
lat1 = -90
lat2 = 90
m = Basemap(resolution='c',projection='cyl',llcrnrlon=lon1,llcrnrlat=lat1,\
             urcrnrlon=lon2,urcrnrlat=lat2)

#get data and plot 1st parameter - This works
filename = "/data/reanal-2/ltm/monthly_ltm_01.nc <http://monthly_ltm_01.nc>"
print filename
nc = Dataset(filename, mode="r")
hgt = nc.variables['HGT_2_ISBL_10'][16,:,:]
print hgt.shape
lons = nc.variables['longitude'][:]
lats = nc.variables['latitude'][:]
x,y = np.meshgrid(lons, lats)
CS = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
CS = m.contourf(x,y,hgt,cmap=plt.cm.jet)
###########################

#extract and plot 2nd paramter - This fails with
# ValueError: Shape mismatch objects cannot be broadcast to a single shape
filename1 = "/data/nps_datasets/WNA_T2m_200808.nc"
print filename1
nc1 = Dataset(filename1, mode="r")
for var in nc1.variables:
     print var
t2m = nc1.variables['T2m'][0,:,:]
print t2m.shape
lons1 = nc1.variables['lon'][:]
lats1 = nc1.variables['lat'][:]
x1,y1 = np.meshgrid(lons1, lats1)
CS1 = m.contour(x1,y1,t2m,15,linewidths=0.5,colors='k')
CS1 = m.contourf(x1,y1,t2m,cmap=plt.cm.jet)
#############################

m.drawcoastlines()
m.drawmapboundary()
#m.fillcontinents()
# draw parallels and meridians.
parallels = np.arange(-90,90,30)
#m.drawparallels(parallels,labels=[1,0,0,1])
m.drawparallels(parallels,labels=[1,0,0,1])
meridians = np.arange(0,357.5,30)
m.drawmeridians(meridians,labels=[1,0,0,1])
plt.title('Plotted Grid')
plt.show()