-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.py
152 lines (119 loc) · 4.79 KB
/
diff.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
import os
import glob
import cdms2
import numpy as np
import numpy.ma as ma
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
plotTitle = {'fontsize': 11.5}
plotSideTitle = {'fontsize': 9.5}
panel = [(0.1691, 0.6810, 0.6465, 0.2258),
(0.1691, 0.3961, 0.6465, 0.2258),
(0.1691, 0.1112, 0.6465, 0.2258),
]
def add_cyclic(var):
lon = var.getLongitude()
print('type(lon): {}'.format(type(lon)))
return var(longitude=(lon[0], lon[0] + 360.0, 'coe'))
def get_ax_size(fig, ax):
bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width, bbox.height
width *= fig.dpi
height *= fig.dpi
return width, height
def plot_panel(n, fig, proj, var, title):
#var = add_cyclic(var)
lon = var.getLongitude()
lat = var.getLatitude()
var = ma.squeeze(var.asma())
# Contour levels
# Contour plot
ax = fig.add_axes(panel[n], projection=proj)
ax.set_global()
p1 = ax.contourf(lon, lat, var,
transform=ccrs.PlateCarree(),
extend='both',
)
ax.set_aspect('auto')
ax.coastlines(lw=0.3)
ax.set_title(title, fontdict=plotTitle)
ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(
zero_direction_label=True, number_format='.0f')
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
ax.tick_params(labelsize=8.0, direction='out', width=1)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Color bar
cbax = fig.add_axes(
(panel[n][0] + 0.6635, panel[n][1] + 0.0215, 0.0326, 0.1792))
cbar = fig.colorbar(p1, cax=cbax)
w, h = get_ax_size(fig, cbax)
cbar.ax.tick_params(labelsize=9.0, length=0)
def plot(test, reference, diff, parameter, fnm):
# Create figure, projection
figsize = [8.5, 11]
dpi = 150
fig = plt.figure(figsize=figsize, dpi=dpi)
proj = ccrs.PlateCarree(central_longitude=180)
# First two panels
plot_panel(0, fig, proj, test, parameter.test_title)
plot_panel(1, fig, proj, reference, parameter.reference_title)
# Third panel
plot_panel(2, fig, proj, diff, parameter.diff_title)
# Figure title
fig.suptitle(parameter.main_title, x=0.5, y=0.96, fontsize=14)
# Save figure
print('Saving diff plot: {}'.format(fnm + '.png'))
plt.savefig(fnm + '.png')
def run(args):
variables = args.vars
output_dir = args.output_dir
start_yr = args.start_yrs
end_yr = args.end_yrs
###cdat_p = '/export/shaheen2/e3sm_diags_timeseries/cdat_climo_results/20180129.DECKv1b_piControl.ne30_oEC.edison_SON_climo.nc'
#cdat_p = '/export/shaheen2/e3sm_diags_timeseries/ncclimo_climo_results/20180129.DECKv1b_piControl.ne30_oEC.edison_SON_climo.nc'
###nco_p = '/export/shaheen2/e3sm_diags_timeseries/ncclimo_climo_results/20180129.DECKv1b_piControl.ne30_oEC.edison_SON_climo.nc'
cdat_paths = glob.glob(os.path.join(output_dir, 'cdat_climo_results', '*'))
# nco_paths = glob.glob(os.path.join(output_dir, 'ncclimo_climo_results', '*'))
nco_path_dir = os.path.join(output_dir, 'ncclimo_climo_results')
output_dir = os.path.join(output_dir, 'diff_results')
if not os.path.exists(output_dir):
os.mkdir(output_dir)
class Namespace:
pass
p = Namespace()
p.test_title = 'CDAT'
p.reference_title = 'ncclimo'
p.diff_title = 'CDAT - ncclimo'
for cdat_p in cdat_paths:
f = cdat_p.split('/')[-1]
nco_p = os.path.join(nco_path_dir, f)
if not os.path.exists(nco_p):
print('File not found, skipping plot for: {}'.format(nco_p))
continue
case_id = cdat_p.split('/')[-1]
season = case_id.split('_')[-2]
case_id = case_id.split('_')[0:-3]
case_id = '.'.join(case_id)
cdat_f = cdms2.open(cdat_p)
nco_f = cdms2.open(nco_p)
print(cdat_f.variables)
print(nco_f.variables)
for v in variables:
print('\ncdat file: {}'.format(cdat_p))
print('nco file: {}'.format(nco_p))
print('variable: {}'.format(v))
cdat_data = cdat_f(v)
nco_data = nco_f(v)
diff = cdat_data - nco_data
p.main_title = '{} {} {}, {} to {}'.format(case_id, season, v, start_yr, end_yr)
fnm = os.path.join(output_dir, 'diff_{}_{}_{}_{}_{}'.format(case_id, season, v, start_yr, end_yr))
plot(cdat_data, nco_data, diff, p, fnm)