Basemap is a module provided by mpl_toolkits tool box, to overlay the 2D gridded plot with a background of continental outlines.
Basemap is available on study server. Some of you may not have Basemap installed in the python system on your computer. (check this by "import mpl_toolkits.basemap"). To install basemap, please see the steps here:http://homepage.ntu.edu.tw/~weitingc/fortran_lecture/Lecture_P_4_Installing_Basemap_NetCDF4.slides.html
Reference for basemap package: https://matplotlib.org/basemap/users/index.html
To import Basemap:
from mpl_toolkits.basemap import Basemap
# other packages we need
import netCDF4 as nc # read nc file
import numpy as np # processing data array
import matplotlib.pyplot as plt # plotting contour
import matplotlib.cm as cm # color map
As an example, we will plot the global map of temperature from the NetCDF file:
rootgrp = nc.Dataset("GFS20171217_tmp.nc") # see example in Lecture NetCDF
# extract the lat, lon, pressure level, and time information
lat = rootgrp.variables['lat'][:]
lon = rootgrp.variables['lon'][:]
lev = rootgrp.variables['lev'][:]
time = rootgrp.variables['time'][:] # get value of time
time_u=rootgrp.variables['time'].units # get unit
time_d=nc.num2date(time,units = time_u,calendar='standard') # get date using NetCDF num2date function
lon2,lat2=np.meshgrid(lon,lat) # convert 1D lat/lon to 2D lat/lon arrays
# extract global temperature at first time step at first level
t = rootgrp.variables['tmp'][0,0,:,:] # first time step (0), first level (0), all lat/lon (:)
m = Basemap(projection='...',lon_0=...,lat_0=..., llcrnrlat=..., llcrnrlon=...,urcrnrlat=..., urcrnrlon=...)
# Ex1. Global map, basic projection and layout
m = Basemap(projection='cyl',lon_0=180,lat_0=0)
m.drawcoastlines() #η«ζ΅·ε²Έη·
cx,cy =m(lon2,lat2) # convert to map projection coordinate
CS = m.pcolormesh(cx,cy,t,cmap=cm.jet,shading='gouraud')
plt.colorbar(CS,orientation='horizontal')
date=time_d[0].strftime('%Y/%m/%d')
plt.title('Cylindrical, T at 1000 hPa, GFS'+date)
plt.show()
# Ex2. Global map, orthographic projection
m = Basemap(projection='ortho',lon_0=120,lat_0=25)
m.drawcoastlines() #η«ζ΅·ε²Έη·
parallels = np.arange(-90.,90,30.)
m.drawparallels(parallels) # draw parallels η«η·―εΊ¦η·
meridians = np.arange(0.,360.,20.)
m.drawmeridians(meridians) # draw meridians η«ηΆεΊ¦η·
cx2,cy2 =m(lon2,lat2) # convert to map projection coordinate
CS2 = m.pcolormesh(cx2,cy2,t,cmap=cm.jet,shading='gouraud')
plt.colorbar(CS,orientation='vertical')
plt.title('Orthographic, T at 1000 hPa, GFS'+date)
plt.show()
# plot Asia Only, maskout continents, using stereographic projection
m = Basemap(projection='stere',lon_0=120,lat_0=20,llcrnrlat=-10,
llcrnrlon=50,urcrnrlat=50, urcrnrlon=190)
m.drawcoastlines() #η«ζ΅·ε²Έη·
m.drawcountries() #η«εη
m.fillcontinents(color='grey')
m.drawparallels(parallels,labels=[1,1,0,0],fontsize=10) # η·―εΊ¦εΊ¦η·γε¨ε·¦ε³ε
©ιε η·―εΊ¦ζ¨η±€
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10) # ηΆεΊ¦η·γε¨δΈζΉε ηΆεΊ¦ζ¨η±€
cx3,cy3 =m(lon2,lat2) # convert to map projection coordinate
clevs = np.arange(260,310,5)
CS3 = m.contourf(cx3,cy3,t,clevs,cmap=cm.jet)
plt.colorbar(CS3,orientation='horizontal').set_label('K')
plt.title('Stereographic, T at 1000 hPa, GFS'+date)
plt.show()