forked from knaughten/roms_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mip_ts_distribution.py
289 lines (272 loc) · 13.4 KB
/
mip_ts_distribution.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from netCDF4 import Dataset
from numpy import *
from matplotlib.pyplot import *
from matplotlib.colors import *
from cartesian_grid_3d import *
# Import FESOM scripts (have to modify path first)
import sys
sys.path.insert(0, '/short/y99/kaa561/fesomtools')
from fesom_grid import *
from unesco import *
# Make a 2x1 plot of T/S distributions south of 65S, colour-coded based on
# depth, in MetROMS (left) and FESOM (right). Include the surface freezing
# point and density contours.
# Input:
# roms_grid = path to ROMS grid file
# roms_file = path to time-averaged ROMS file containing temperature and
# salinity (I used 2002-2016 average)
# fesom_mesh_path_lr, fesom_mesh_path_hr = path to FESOM mesh directories for
# low-res and high-res meshes
# fesom_file_lr, fesom_file_hr = paths to time-averaged FESOM files containing
# temperature and salinity for the low-res and high-res
# simulations respectively, over the same period as roms_file
def mip_ts_distribution (roms_grid, roms_file, fesom_mesh_path_lr, fesom_file_lr, fesom_mesh_path_hr, fesom_file_hr):
# Northern boundary of water masses to consider
nbdry = -65
# Number of temperature and salinity bins
num_bins = 1000
# Bounds on temperature and salinity bins (pre-computed, change if needed)
min_salt = 32.3
max_salt = 35.1
min_temp = -3.1
max_temp = 3.8
# Bounds to actually plot
min_salt_plot = 33.25
max_salt_plot = 35.1
min_temp_plot = -3
max_temp_plot = 3.8
# FESOM grid generation parameters
circumpolar = False
cross_180 = False
# ROMS vertical grid parameters
theta_s = 7.0
theta_b = 2.0
hc = 250
N = 31
print 'Setting up bins'
# Calculate boundaries of temperature bins
temp_bins = linspace(min_temp, max_temp, num=num_bins)
# Calculate centres of temperature bins (for plotting)
temp_centres = 0.5*(temp_bins[:-1] + temp_bins[1:])
# Repeat for salinity
salt_bins = linspace(min_salt, max_salt, num=num_bins)
salt_centres = 0.5*(salt_bins[:-1] + salt_bins[1:])
# Set up 2D arrays of temperature bins x salinity bins to hold average
# depth of water masses, weighted by volume
ts_vals_roms = zeros([size(temp_centres), size(salt_centres)])
ts_vals_fesom_lr = zeros([size(temp_centres), size(salt_centres)])
ts_vals_fesom_hr = zeros([size(temp_centres), size(salt_centres)])
# Also arrays to integrate volume
volume_roms = zeros([size(temp_centres), size(salt_centres)])
volume_fesom_lr = zeros([size(temp_centres), size(salt_centres)])
volume_fesom_hr = zeros([size(temp_centres), size(salt_centres)])
# Calculate surface freezing point as a function of salinity as seen by
# each sea ice model
freezing_pt_roms = salt_centres/(-18.48 + 18.48/1e3*salt_centres)
freezing_pt_fesom = -0.0575*salt_centres + 1.7105e-3*sqrt(salt_centres**3) - 2.155e-4*salt_centres**2
# Get 2D versions of the temperature and salinity bins
salt_2d, temp_2d = meshgrid(salt_centres, temp_centres)
# Calculate potential density of each combination of temperature and
# salinity bins
density = unesco(temp_2d, salt_2d, zeros(shape(temp_centres)))-1000
# Density contours to plot
density_lev = arange(26.6, 28.4, 0.2)
print 'Processing ROMS'
# Read ROMS grid variables we need
id = Dataset(roms_grid, 'r')
roms_lon = id.variables['lon_rho'][:,:]
roms_lat = id.variables['lat_rho'][:,:]
roms_h = id.variables['h'][:,:]
roms_zice = id.variables['zice'][:,:]
id.close()
num_lat = size(roms_lat, 0)
num_lon = size(roms_lon, 1)
# Get integrands on 3D grid
roms_dx, roms_dy, roms_dz, roms_z = cartesian_grid_3d(roms_lon, roms_lat, roms_h, roms_zice, theta_s, theta_b, hc, N)
# Get volume integrand
dV = roms_dx*roms_dy*roms_dz
# Read ROMS output
id = Dataset(roms_file, 'r')
roms_temp = id.variables['temp'][0,:,:,:]
roms_salt = id.variables['salt'][0,:,:,:]
id.close()
# Loop over 2D grid boxes
for j in range(num_lat):
for i in range(num_lon):
# Check for land mask
if roms_temp[0,j,i] is ma.masked:
continue
# Check if we're in the region of interest
if roms_lat[j,i] < nbdry:
# Loop downward
for k in range(N):
# Figure out which bins this falls into
temp_index = nonzero(temp_bins > roms_temp[k,j,i])[0][0] - 1
salt_index = nonzero(salt_bins > roms_salt[k,j,i])[0][0] - 1
# Integrate depth*dV in this bin
ts_vals_roms[temp_index, salt_index] += -roms_z[k,j,i]*dV[k,j,i]
volume_roms[temp_index, salt_index] += dV[k,j,i]
# Mask bins with zero volume
ts_vals_roms = ma.masked_where(volume_roms==0, ts_vals_roms)
volume_roms = ma.masked_where(volume_roms==0, volume_roms)
# Convert depths from integrals to volume-averages
ts_vals_roms /= volume_roms
print 'Processing low-res FESOM'
# Make FESOM grid elements
elements_lr = fesom_grid(fesom_mesh_path_lr, circumpolar, cross_180)
# Read temperature and salinity at each 3D node
id = Dataset(fesom_file_lr, 'r')
fesom_temp_lr = id.variables['temp'][0,:]
fesom_salt_lr = id.variables['salt'][0,:]
id.close()
# Loop over elements
for elm in elements_lr:
# See if we're in the region of interest
if all(elm.lat < nbdry):
# Get area of 2D triangle
area = elm.area()
nodes = [elm.nodes[0], elm.nodes[1], elm.nodes[2]]
# Loop downward
while True:
if nodes[0].below is None or nodes[1].below is None or nodes[2].below is None:
# We've reached the bottom
break
# Calculate average temperature, salinity, depth, and layer
# thickness over this 3D triangular prism
temp_vals = []
salt_vals = []
depth_vals = []
dz = []
for i in range(3):
# Average temperature over 6 nodes
temp_vals.append(fesom_temp_lr[nodes[i].id])
temp_vals.append(fesom_temp_lr[nodes[i].below.id])
# Average salinity over 6 nodes
salt_vals.append(fesom_salt_lr[nodes[i].id])
salt_vals.append(fesom_salt_lr[nodes[i].below.id])
# Average depth over 6 nodes
depth_vals.append(nodes[i].depth)
depth_vals.append(nodes[i].below.depth)
# Average dz over 3 vertical edges
dz.append(abs(nodes[i].depth - nodes[i].below.depth))
# Get ready for next repetition of loop
nodes[i] = nodes[i].below
temp_elm = mean(array(temp_vals))
salt_elm = mean(array(salt_vals))
depth_elm = mean(array(depth_vals))
# Calculate volume of 3D triangular prism
volume = area*mean(array(dz))
# Figure out which bins this falls into
temp_index = nonzero(temp_bins > temp_elm)[0][0] - 1
salt_index = nonzero(salt_bins > salt_elm)[0][0] - 1
# Integrate depth*volume in this bin
ts_vals_fesom_lr[temp_index, salt_index] += depth_elm*volume
volume_fesom_lr[temp_index, salt_index] += volume
# Mask bins with zero volume
ts_vals_fesom_lr = ma.masked_where(volume_fesom_lr==0, ts_vals_fesom_lr)
volume_fesom_lr = ma.masked_where(volume_fesom_lr==0, volume_fesom_lr)
# Convert depths from integrals to volume-averages
ts_vals_fesom_lr /= volume_fesom_lr
print 'Processing high-res FESOM'
elements_hr = fesom_grid(fesom_mesh_path_hr, circumpolar, cross_180)
id = Dataset(fesom_file_hr, 'r')
fesom_temp_hr = id.variables['temp'][0,:]
fesom_salt_hr = id.variables['salt'][0,:]
id.close()
for elm in elements_hr:
if all(elm.lat < nbdry):
area = elm.area()
nodes = [elm.nodes[0], elm.nodes[1], elm.nodes[2]]
while True:
if nodes[0].below is None or nodes[1].below is None or nodes[2].below is None:
break
temp_vals = []
salt_vals = []
depth_vals = []
dz = []
for i in range(3):
temp_vals.append(fesom_temp_hr[nodes[i].id])
temp_vals.append(fesom_temp_hr[nodes[i].below.id])
salt_vals.append(fesom_salt_hr[nodes[i].id])
salt_vals.append(fesom_salt_hr[nodes[i].below.id])
depth_vals.append(nodes[i].depth)
depth_vals.append(nodes[i].below.depth)
dz.append(abs(nodes[i].depth - nodes[i].below.depth))
nodes[i] = nodes[i].below
temp_elm = mean(array(temp_vals))
salt_elm = mean(array(salt_vals))
depth_elm = mean(array(depth_vals))
volume = area*mean(array(dz))
temp_index = nonzero(temp_bins > temp_elm)[0][0] - 1
salt_index = nonzero(salt_bins > salt_elm)[0][0] - 1
ts_vals_fesom_hr[temp_index, salt_index] += depth_elm*volume
volume_fesom_hr[temp_index, salt_index] += volume
ts_vals_fesom_hr = ma.masked_where(volume_fesom_hr==0, ts_vals_fesom_hr)
volume_fesom_hr = ma.masked_where(volume_fesom_hr==0, volume_fesom_hr)
ts_vals_fesom_hr /= volume_fesom_hr
# Find the maximum depth for plotting
max_depth = amax(array([amax(ts_vals_roms), amax(ts_vals_fesom_lr), amax(ts_vals_fesom_hr)]))
# Make a nonlinear scale
bounds = linspace(0, max_depth**(1.0/2.5), num=100)**2.5
norm = BoundaryNorm(boundaries=bounds, ncolors=256)
# Set labels for density contours
manual_locations = [(33.4, 3.0), (33.65, 3.0), (33.9, 3.0), (34.2, 3.0), (34.45, 3.5), (34.65, 3.25), (34.9, 3.0), (35, 1.5)]
print "Plotting"
fig = figure(figsize=(20,9))
# ROMS
ax = fig.add_subplot(1, 3, 1)
pcolor(salt_centres, temp_centres, ts_vals_roms, norm=norm, vmin=0, vmax=max_depth, cmap='jet')
# Add surface freezing point line
plot(salt_centres, freezing_pt_roms, color='black', linestyle='dashed')
# Add density contours
cs = contour(salt_centres, temp_centres, density, density_lev, colors=(0.6,0.6,0.6), linestyles='dotted')
clabel(cs, inline=1, fontsize=14, color=(0.6,0.6,0.6), fmt='%1.1f', manual=manual_locations)
xlim([min_salt_plot, max_salt_plot])
ylim([min_temp_plot, max_temp_plot])
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
xlabel('Salinity (psu)', fontsize=20)
ylabel(r'Temperature ($^{\circ}$C)', fontsize=20)
title('MetROMS', fontsize=24)
# FESOM low-res
ax = fig.add_subplot(1, 3, 2)
img = pcolor(salt_centres, temp_centres, ts_vals_fesom_lr, norm=norm, vmin=0, vmax=max_depth, cmap='jet')
plot(salt_centres, freezing_pt_fesom, color='black', linestyle='dashed')
cs = contour(salt_centres, temp_centres, density, density_lev, colors=(0.6,0.6,0.6), linestyles='dotted')
clabel(cs, inline=1, fontsize=14, color=(0.6,0.6,0.6), fmt='%1.1f', manual=manual_locations)
xlim([min_salt_plot, max_salt_plot])
ylim([min_temp_plot, max_temp_plot])
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
xlabel('Salinity (psu)', fontsize=20)
title('FESOM (low-res)', fontsize=24)
# FESOM high-res
ax = fig.add_subplot(1, 3, 3)
img = pcolor(salt_centres, temp_centres, ts_vals_fesom_hr, norm=norm, vmin=0, vmax=max_depth, cmap='jet')
plot(salt_centres, freezing_pt_fesom, color='black', linestyle='dashed')
cs = contour(salt_centres, temp_centres, density, density_lev, colors=(0.6,0.6,0.6), linestyles='dotted')
clabel(cs, inline=1, fontsize=14, color=(0.6,0.6,0.6), fmt='%1.1f', manual=manual_locations)
xlim([min_salt_plot, max_salt_plot])
ylim([min_temp_plot, max_temp_plot])
ax.tick_params(axis='x', labelsize=16)
ax.tick_params(axis='y', labelsize=16)
xlabel('Salinity (psu)', fontsize=20)
title('FESOM (high-res)', fontsize=24)
# Add a colourbar on the right
cbaxes = fig.add_axes([0.93, 0.2, 0.02, 0.6])
cbar = colorbar(img, cax=cbaxes, ticks=[0,50,100,200,500,1000,2000,4000])
cbar.ax.tick_params(labelsize=18)
# Add the main title
suptitle('Water masses south of 65$^{\circ}$S: depth (m), 2002-2016 average', fontsize=30)
subplots_adjust(wspace=0.1)
fig.show()
fig.savefig('ts_distribution_orig.png')
# Command-line interface
if __name__ == "__main__":
roms_grid = raw_input("Path to ROMS grid file: ")
roms_file = raw_input("Path to time-averaged ROMS file containing temperature and salinity: ")
fesom_mesh_path_lr = raw_input("Path to FESOM low-res mesh directory: ")
fesom_file_lr = raw_input("Path to time-averaged FESOM low-res file containing temperature and salinity: ")
fesom_mesh_path_hr = raw_input("Path to FESOM high-res mesh directory: ")
fesom_file_hr = raw_input("Path to time-averaged FESOM high-res file containing temperature and salinity: ")
mip_ts_distribution(roms_grid, roms_file, fesom_mesh_path_lr, fesom_file_lr, fesom_mesh_path_hr, fesom_file_hr)