-
Notifications
You must be signed in to change notification settings - Fork 0
/
ED_figure3.py
executable file
·221 lines (155 loc) · 6.54 KB
/
ED_figure3.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""
Author: Victoria McDonald
email: [email protected]
website: http://torimcd.github.com
license: BSD
This script creates maps and anomaly maps of the model cloud climatology in CAM5.
"""
import matplotlib
matplotlib.use("Agg")
import os
import sys
import numpy as np
import netCDF4
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib import ticker
from mpl_toolkits.basemap import Basemap
import processing_functions as pf
# ------------------------------------------------------------------------
# change this section to match where you downloaded the model output files
# ------------------------------------------------------------------------
download_path = '' # enter the path to the directory where you downloaded the archived data, eg '/home/user/Downloads'
filebase = download_path + 'FYSP_clouds_archive/CAM5/'
outfileloc = download_path + 'temp_data/' # this is the location to save the processed netcdf files to
# the fields we want to average for our plots - these must not depend on pressure level
fields = 'CLDHGH,CLDLOW,LHFLX,LWCF,PRECT,SHFLX,SWCF,TS'
# process the fields we're plotting
pf.map_annual_average(filebase, outfileloc, 'cam5', fields) # averages fields over years 31-60, retaining location so can be plotted in map view
# cloud climatology
cloudfields= ['CLDHGH', 'LWCF', 'CLDLOW', 'SWCF']
cloudcmaps=['bone', 'BrBG', 'PuRd', 'RdBu_r', 'bone', 'BrBG', 'OrRd_r', 'RdBu_r']
cloudfilenames = ['c5_map_annual_average', 'c5_map_annual_average', 'c5_map_annual_average', 'c5_map_annual_average']
cloudletters = ['a', 'b', 'c', 'd']
cloudheadings = ['High Cloud Fraction', 'Longwave Cloud Forcing', 'Low Cloud Fraction', 'Shortwave Cloud Forcing']
cloudaxislabels = [r'$\mathrm{Fraction}$', r'$\mathrm{W/m^2}$', r'$\mathrm{Fraction}$', r'$\mathrm{W/m^2}$']
cloudvmins = [0,-0.5, 0, -60, 0, -0.5, -110, -60]
cloudvmaxs = [1, 0.5, 100, 60, 1, 0.5, 0, 60]
#create figure - use figsize=(8.5, 9.5) to make bigger
#fig = plt.figure(figsize=(3.46457, 4.48356))
fig = plt.figure(figsize=(7.08661, 5.2107))
# container with 2 rows of 2 columns, first column is grid of absolute value plots, second column is diff plots. First row is cloud climatology, second row is model climatology
outer_grid = gridspec.GridSpec(1, 2, wspace=0.2, hspace=0.1, width_ratios=(2,1))
# first two columns, absolute value plots
cldabsgrid = gridspec.GridSpecFromSubplotSpec(4, 3, subplot_spec=outer_grid[0], wspace=0.0, hspace=0.45, width_ratios=(15,15,1))
# third colum, anomaly plots
clddiffgrid = gridspec.GridSpecFromSubplotSpec(4, 2, subplot_spec=outer_grid[1], wspace=0.0, hspace=0.45, width_ratios=(25,1))
# -------------------------CLOUD CLIMATOLOGY -------------------------
# keep track of which field/row we're on
n=0
# keep track of which gridspace/column we're plotting in for abs val
a = 0
# keep track of which gridspace/column we're plotting in for diff
d = 0
# keep track of which vmin/max we're on
v = 0
present = '_10'
eight = '_09'
for p in cloudfields:
f = cloudfilenames[n]
cloudfield = cloudfields[n]
presentcase = outfileloc + f + present +'.nc'
eightcase = outfileloc + f + eight +'.nc'
# plot the data - PRESENT
ax = fig.add_subplot(cldabsgrid[a])
a=a+1
ds = netCDF4.Dataset(presentcase)
lons = ds.variables['lon'][:]
lats = ds.variables['lat'][:]
presfld = ds.variables[cloudfield][:]
units = ds.variables[cloudfield].units
ds.close() #close the file
# setup the map
m = Basemap(lat_0=0,lon_0=0, ax=ax)
m.drawcoastlines()
m.drawcountries()
parallels = [-45, 0, 45]
meridians = [-90., 0., 90.]
m.drawparallels(parallels, labels=[True ,False,False, False], fontsize=6)
m.drawmeridians(meridians,labels=[False,False,False,True], fontsize=6)
# Create 2D lat/lon arrays for Basemap
lon2d, lat2d = np.meshgrid(lons, lats)
# Plot the data
cs = m.pcolormesh(lon2d,lat2d,np.squeeze(presfld), cmap=cloudcmaps[v], latlon='True', vmin=cloudvmins[v], vmax=cloudvmaxs[v], rasterized=True)
# This is the fix for the white lines between contour levels
cs.set_edgecolor("face")
# add letter annotation
plt.text(-0.10, 1.0, cloudletters[n], fontsize=6, fontweight="bold", transform=ax.transAxes)
# add heading
plt.text(0.65, 1.05, cloudheadings[n], fontsize=7, transform=ax.transAxes)
#plot the data - EIGHT
ax = fig.add_subplot(cldabsgrid[a])
a=a+1
ds = netCDF4.Dataset(eightcase)
lons = ds.variables['lon'][:]
lats = ds.variables['lat'][:]
efld = ds.variables[cloudfield][:]
units = ds.variables[cloudfield].units
ds.close() #close the file
# setup the map
m = Basemap(lat_0=0,lon_0=0, ax=ax)
m.drawcoastlines()
m.drawcountries()
parallels = [-45, 0, 45]
meridians = [-90., 0., 90.]
m.drawparallels(parallels, labels=[False ,False,False, False], fontsize=6)
m.drawmeridians(meridians,labels=[False,False,False,True], fontsize=6)
# Create 2D lat/lon arrays for Basemap
lon2d, lat2d = np.meshgrid(lons, lats)
# Plot
cs = m.pcolormesh(lon2d,lat2d,np.squeeze(efld), cmap=cloudcmaps[v], latlon='True', vmin=cloudvmins[v], vmax=cloudvmaxs[v], rasterized=True)
v = v+1
# This is the fix for the white lines between contour levels
cs.set_edgecolor("face")
# plot the colorbar - ABS value
ax = fig.add_subplot(cldabsgrid[a])
a=a+1
cb = plt.colorbar(cs, cax=ax)
cb.ax.tick_params(labelsize=6)
tick_locator = ticker.MaxNLocator(nbins=5)
cb.locator = tick_locator
cb.update_ticks()
#plot the data - DIFF
ax = fig.add_subplot(clddiffgrid[d])
d=d+1
if os.path.isfile(eightcase):
# setup the map
m = Basemap(lat_0=0,lon_0=0, ax=ax)
m.drawcoastlines()
m.drawcountries()
parallels = [-45, 0, 45]
meridians = [-90., 0., 90.]
m.drawparallels(parallels, labels=[True ,False,False, False], fontsize=6)
m.drawmeridians(meridians,labels=[False,False,False,True], fontsize=6)
# Create 2D lat/lon arrays for Basemap
lon2d, lat2d = np.meshgrid(lons, lats)
# Plot
cs = m.pcolormesh(lon2d,lat2d,np.squeeze(efld)-np.squeeze(presfld), cmap=cloudcmaps[v], latlon='True', vmin=cloudvmins[v], vmax=cloudvmaxs[v], rasterized=True)
v = v+1
# This is the fix for the white lines between contour levels
cs.set_edgecolor("face")
# plot the colorbar - DIFF value
ax = fig.add_subplot(clddiffgrid[d])
d=d+1
cb = plt.colorbar(cs, cax=ax)
cb.set_label(label=cloudaxislabels[n], fontsize=6)
cb.ax.tick_params(labelsize=6)
tick_locator = ticker.MaxNLocator(nbins=5)
cb.locator = tick_locator
cb.update_ticks()
# go to next field/row
n=n+1
# -----------------------------
plt.show()
fig.savefig("figures_ED/ED_figure3.pdf", format='pdf', bbox_inches='tight')