-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_paper_fig_5.py
197 lines (154 loc) · 7.91 KB
/
plot_paper_fig_5.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
"""
A first attempt at quickly plotting fields for the SWIFT paper, test 1
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import numpy as np
from tomplot import (set_tomplot_style, tomplot_contours, tomplot_cmap,
plot_contoured_field, add_colorbar_fig,
tomplot_field_title, extract_lfric_coords,
extract_lfric_field, plot_field_quivers,
regrid_horizontal_slice)
# ---------------------------------------------------------------------------- #
# Things that can be altered and parameters for the test case
# ---------------------------------------------------------------------------- #
# Directories for results and to plot to
branch = 'swift_revision'
results_dirname = f'/data/users/tbendall/results/{branch}'
plotdir = f'/data/users/tbendall/results/swift_revision'
mesh_dt = 'BiP128x128-1000x1000_2p0'
# To alter test-by-test
test_number = 6
fig_idx = 5
plot_stem = 'divergent'
title = r'Test 3: Varying $\rho$, Divergent $u$, $c_{max}=3.83$'
# Options relating to the different fields to be plotted
field_names = ['rho', 'tracer_con', 'tracer_adv']
cbar_labels = [r"$\rho$ (kg m$^{-3}$)",
r"$m$ (kg kg$^{-1}$)",
r"$m^L$ (kg kg$^{-1}$)"]
colour_schemes = ['YlGn', 'BuPu', 'BuPu']
remove_contours = [0.8, 0.0, 0.0]
# Options for different configurations
test_configs = ['cosmic', 'swift']
config_titles = ['COSMIC', 'SWIFT']
# Options shared for all plots and subplots
time_factor = 25.0 # For some reason LFRic outputs the wrong time
xlabel = r'$x \ /$ km'
ylabel = r'$y \ /$ km'
contour_method = 'tricontour'
time_idxs = [0,1]
level = 0
xlims = [-0.5, 0.5]
ylims = [-0.5, 0.5]
vector_magnitude_cutoff = 0.01
spatial_filter_step = 5
contour_dict = {'rho': np.linspace(0.3, 1.7, 15),
'tracer_adv': np.linspace(-0.2, 1.2, 15),
'tracer_con': np.linspace(-0.2, 1.2, 15)}
# This is declared BEFORE figure and ax are initialised
set_tomplot_style(16)
# ---------------------------------------------------------------------------- #
# Loop through the different time-points
# ---------------------------------------------------------------------------- #
# Make figure
fig, axarray = plt.subplots(len(config_titles)*len(time_idxs), len(field_names),
figsize=(12, 16), sharex='col', sharey='row')
all_cf = []
for l, time_idx in enumerate(time_idxs):
for i, (field_name, colour_scheme, remove_contour) in \
enumerate(zip(field_names, colour_schemes, remove_contours)):
# -------------------------------------------------------------------- #
# Loop through subplots
# -------------------------------------------------------------------- #
for j, (results_opt, config_title) in \
enumerate(zip(test_configs, config_titles)):
k = j + l*len(config_titles)
ax = axarray.flatten()[k*len(field_names)+i]
main_filename = f'{results_dirname}/{results_opt}_test_{test_number}_{mesh_dt}_diag.nc'
data_file = Dataset(main_filename, 'r')
# Extract coords
time = time_factor*data_file['time_instant'][time_idx]
coords_X, coords_Y = extract_lfric_coords(data_file, field_name)
field_data = extract_lfric_field(data_file, field_name, time_idx, level=level)
# Scale coordinates
coords_X *= 10.
coords_Y *= 10.
# ---------------------------------------------------------------- #
# Plot data
# ---------------------------------------------------------------- #
# Contours based on data for whole row
contours = contour_dict[field_name]
cmap, lines = tomplot_cmap(contours, colour_scheme,
remove_contour=remove_contour,
cmap_rescale_type='top', cmap_rescaling=0.75)
cf, _ = plot_contoured_field(ax, coords_X, coords_Y, field_data,
contour_method, contours, line_contours=lines,
cmap=cmap, plot_contour_lines=True)
# Save cf for later
all_cf.append(cf)
# ---------------------------------------------------------------- #
# Add wind vectors
# ---------------------------------------------------------------- #
if field_name == 'rho':
coords_X_raw, coords_Y_raw = extract_lfric_coords(data_file, 'u_in_w2h')
# Scale coordinates
coords_X_raw *= 10.
coords_Y_raw *= 10.
field_data_X_raw = extract_lfric_field(data_file, 'u_in_w2h', time_idx, level)
field_data_Y_raw = extract_lfric_field(data_file, 'v_in_w2h', time_idx, level)
# To be able to filter plotted vectors nicely, regrid to 2d-array
new_coords_X, new_coords_Y = \
np.meshgrid(np.linspace(xlims[0], xlims[1], 50),
np.linspace(ylims[0], ylims[1], 50), indexing='ij')
field_data_X = regrid_horizontal_slice(new_coords_X, new_coords_Y,
coords_X_raw, coords_Y_raw,
field_data_X_raw)
field_data_Y = regrid_horizontal_slice(new_coords_X, new_coords_Y,
coords_X_raw, coords_Y_raw,
field_data_Y_raw)
field_data_mag = np.sqrt(field_data_X**2 + field_data_Y**2)
# Quivers
_ = plot_field_quivers(ax, new_coords_X, new_coords_Y, field_data_X, field_data_Y,
scale=250, minlength=vector_magnitude_cutoff,
magnitude_filter=vector_magnitude_cutoff,
spatial_filter_step=spatial_filter_step)
data_file.close()
# ---------------------------------------------------------------- #
# Add labels
# ---------------------------------------------------------------- #
if i == 0:
ax.set_ylabel(ylabel, labelpad=-20)
ax.set_yticks(ylims)
ax.set_yticklabels(ylims)
ax.set_ylim(ylims)
if k == len(config_titles)*len(time_idxs) - 1:
ax.set_xlabel(xlabel, labelpad=-12)
ax.set_xticks(xlims)
ax.set_xticklabels(xlims)
ax.set_xlim(xlims)
tomplot_field_title(ax, None, minmax=True, field_data=field_data,
minmax_format='.4f')
if i == len(field_names)-1:
ax2 = ax.twinx()
ax2.set_yticks([])
text = f'{config_title}, '+r'$t=$'+f' {time:0.0f} s'
ax2.set_ylabel(text, labelpad=10)
# ---------------------------------------------------------------------------- #
# Add colorbars and adjust figures
# ---------------------------------------------------------------------------- #
for i, cbar_label in enumerate(cbar_labels):
ik = (len(config_titles)*len(time_idxs)-1)*len(field_names) + i
add_colorbar_fig(fig, all_cf[len(config_titles)*len(field_names)+1+i*len(time_idxs)], cbar_label,
ax_idxs=[ik], cbar_labelpad=-10, cbar_padding=-0.03,
location='bottom', cbar_thickness=0.015)
fig.subplots_adjust(hspace=0.2)
# ---------------------------------------------------------------------------- #
# Save figure
# ---------------------------------------------------------------------------- #
plt.suptitle(f'{title}', x=0.5, y=0.92)
plotname = f'{plotdir}/fig_{fig_idx}_{plot_stem}.jpg'
print(f'Saving figure to {plotname}')
fig.savefig(plotname, bbox_inches='tight', dpi=300)
plt.close()