Here is the code to create Figure 1, which shows the wrong contour plot by using RSS SMAP Level 2 data on April 1, 2015.
- Code: Select all
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
from pylab import *
# Read in SMAP sea surface salinity L2 granule file
filename = 'RSS_smap_SSS_L2C_r00872_v02.0_70km.nc'
ncin = Dataset(filename, 'r')
lons = ncin.variables['cellon'][:]
lats = ncin.variables['cellat'][:]
sss = ncin.variables['sss_smap'][:]
ncin.close()
lats = lats[:,:,1]
lons = lons[:,:,1]
sss = sss[:,:,1]
lats = lats.squeeze()
lons = lons.squeeze()
sss = sss.squeeze()
# Setup plot
plt.figure(figsize=(2, 1.12), dpi=300)
# Contour levels
clevs = np.linspace(31, 39, 101)
cmap=plt.get_cmap("jet")
cmap.set_under("black")
cmap.set_over("DarkViolet")
m = Basemap(projection='cyl', llcrnrlon=0.0, llcrnrlat=-90,
urcrnrlon=360.0, urcrnrlat=90)
m.bluemarble()
# Filled contour
cs=m.contourf(lons,lats, sss, clevs, cmap=cmap, extend='both')
# Create extended colorbar
cb = m.colorbar(cs, 'right', size='2%', pad='0.5%')
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=4)
cb.set_label('SSS (PSU)', fontsize=4,fontweight="bold")
cb.set_ticks(range(30,40,1))
plt.title("RSS_smap_SSS_L2C_r00872_v02.0_70km.nc", fontsize=4,fontweight="bold")
plt.subplots_adjust(left=0.05, right=0.94, top=0.98, bottom=0.01)
plt.show()
The easy and fast way to fix the contour plot is to separate the data before and after the boundary (360.0 degree). User may also fix the contour plot routine contourf in the matplotlib package. Here is the code to create Figure 2, which shows the correct contour plot.
- Code: Select all
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
from pylab import *
# Read in SMAP sea surface salinity L2 granule file
filename = 'RSS_smap_SSS_L2C_r00872_v02.0_70km.nc'
ncin = Dataset(filename, 'r')
lons = ncin.variables['cellon'][:]
lats = ncin.variables['cellat'][:]
sss1 = ncin.variables['sss_smap'][:]
sss2 = ncin.variables['sss_smap'][:]
ncin.close()
lats = lats[:,:,1]
lons = lons[:,:,1]
sss1 = sss1[:,:,1]
sss2 = sss2[:,:,1]
lats = lats.squeeze()
lons = lons.squeeze()
sss1 = sss1.squeeze()
sss2 = sss2.squeeze()
idx1 = np.where(lons>=180.0)
sss1[idx1] = np.nan
idx2 = np.where(lons<180.0)
sss2[idx2] = np.nan
# Setup plot
plt.figure(figsize=(2, 1.12), dpi=300)
# Contour levels
clevs = np.linspace(31, 39, 101)
cmap=plt.get_cmap("jet")
cmap.set_under("black")
cmap.set_over("DarkViolet")
m = Basemap(projection='cyl', llcrnrlon=0.0, llcrnrlat=-90,
urcrnrlon=360.0, urcrnrlat=90)
m.bluemarble()
# Filled contour
cs=m.contourf(lons,lats, sss1, clevs, cmap=cmap, extend='both')
cs=m.contourf(lons,lats, sss2, clevs, cmap=cmap, extend='both')
# Create extended colorbar
cb = m.colorbar(cs, 'right', size='2%', pad='0.5%')
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=4)
cb.set_label('SSS (PSU)', fontsize=4,fontweight="bold")
cb.set_ticks(range(30,40,1))
plt.title("RSS_smap_SSS_L2C_r00872_v02.0_70km.nc", fontsize=4,fontweight="bold")
plt.subplots_adjust(left=0.05, right=0.94, top=0.98, bottom=0.01)