-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartopy_example
68 lines (54 loc) · 2.23 KB
/
cartopy_example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#this script uses the cartopy package to plot a three-panel figure where each plot has its own colorbar.
#i'm guessing the way i've shifted the plots onto a -180 to +180 grid is not the best way to do it.
# i also ran into a weird issue where there was a white line up the middle of my plots and the
#cyclic_point stuff fixes that.
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from netCDF4 import Dataset
from cartopy.util import add_cyclic_point
d1=Dataset('/glade/scratch/stepheba/archive/dflt/atm/hist/dflt.cam.h0.1979-01.nc')
d2=Dataset('/glade/scratch/stepheba/archive/colin_p_and_f/atm/hist/colin_p_and_f.cam.h0.1979-01.nc')
variable='SWCF'
units='W/m$^2$'
lon=np.array(d1.variables['lon'])
lat=np.array(d1.variables['lat'])
dfltvar=np.array(d1.variables[variable])
testvar=np.array(d2.variables[variable])
#rearrange longitude so pacific is centered
dfltvar=dfltvar[0,:,:]
testvar=testvar[0,:,:]
dfltvarshft=np.zeros((192,288))
testvarshft=np.zeros((192,288))
for i in range(0,288):
if i<144:
dfltvarshft[:,i]=dfltvar[:,i+144]
testvarshft[:,i]=testvar[:,i+144]
else:
dfltvarshft[:,i]=dfltvar[:,i-144]
testvarshft[:,i]=testvar[:,i-144]
lon=lon-180
#print(lon.shape)
#print(dfltvarshft.shape)
dfltvarshft2, lon2 = add_cyclic_point(dfltvarshft, coord=lon)
testvarshft2, lon2 = add_cyclic_point(testvarshft, coord=lon)
#create plot
fig, axs = plt.subplots(3,1,subplot_kw={'projection': ccrs.PlateCarree(central_longitude=180)})
axs=axs.flatten()
mycmap='coolwarm'
cs=axs[0].contourf(lon2,lat,testvarshft2[:,:],transform = ccrs.PlateCarree(),cmap=mycmap,extend='both')
axs[0].set_title('test')
axs[0].coastlines()
cbar=fig.colorbar(cs,ax=axs[0])
cbar.set_label(units)
cs=axs[1].contourf(lon2,lat,dfltvarshft2[:,:],transform = ccrs.PlateCarree(),cmap=mycmap,extend='both')
axs[1].set_title('dflt')
axs[1].coastlines()
cbar=fig.colorbar(cs,ax=axs[1])
cbar.set_label(units)
cs=axs[2].contourf(lon2,lat,testvarshft2[:,:]-dfltvarshft2[:,:],transform = ccrs.PlateCarree(),cmap=mycmap,extend='both')
axs[2].set_title(r'test $-$ dflt')
axs[2].coastlines()
cbar=fig.colorbar(cs,ax=axs[2])
cbar.set_label(units)
plt.savefig('/glade/u/home/stepheba/figs/'+variable+'.png',dpi=300,bbox_inches='tight')