-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprep_velocities.jl
More file actions
445 lines (371 loc) · 18.6 KB
/
Copy pathprep_velocities.jl
File metadata and controls
445 lines (371 loc) · 18.6 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""
Create and save the velocities and free surface to JLD2 format.
Requires: create_grid.jl to have been run first.
Usage:
julia --project prep_velocities.jl
"""
@info "Loading packages and functions"
using Oceananigans
using Oceananigans.AbstractOperations: grid_metric_operation, Ax, Ay, Az
using Oceananigans.Architectures: CPU
using Oceananigans.BoundaryConditions: FPivotZipperBoundaryCondition, FieldBoundaryConditions, fill_halo_regions!
using Oceananigans.Grids: zspacings
using Oceananigans.ImmersedBoundaries: mask_immersed_field!
using Oceananigans.Models.HydrostaticFreeSurfaceModels: _update_zstar_scaling!, surface_kernel_parameters
using Oceananigans.OutputReaders: Cyclical, OnDisk
using Oceananigans.Units: days, seconds
year = years = 365.25days
month = months = year / 12
using YAXArrays
using DimensionalData
using NCDatasets
using NetCDF
using JLD2
using TOML
using CairoMakie
using Statistics: mean, quantile
const ρ₀ = 1035.0 # kg/m^3
include("select_architecture.jl")
include("shared_functions.jl")
# Configuration
(; parentmodel, experiment_dir, monthly_dir, yearly_dir) = load_project_config()
(; VELOCITY_SOURCE) = parse_config_env()
mkpath(monthly_dir)
mkpath(yearly_dir)
build_total_transport = VELOCITY_SOURCE == "totaltransport"
@info "VELOCITY_SOURCE = $VELOCITY_SOURCE (build_total_transport = $build_total_transport)"
dht_check = lowercase(get(ENV, "DHT_CHECK", "no")) ∈ ("yes", "true", "1")
@info "dht/Δzstar sanity check: $dht_check"
make_plots = lowercase(get(ENV, "MAKE_PLOTS", "no")) ∈ ("yes", "true", "1")
@info "Plotting enabled: $make_plots"
if make_plots
plots_dir = joinpath(experiment_dir, "plots")
η_plot_dir = joinpath(plots_dir, "eta")
u_mt_plot_dir = joinpath(plots_dir, "u_from_mass_transport")
v_mt_plot_dir = joinpath(plots_dir, "v_from_mass_transport")
w_mt_plot_dir = joinpath(plots_dir, "w_from_mass_transport")
mkpath(η_plot_dir)
mkpath(u_mt_plot_dir)
mkpath(v_mt_plot_dir)
mkpath(w_mt_plot_dir)
end
Δt = parentmodel == "ACCESS-OM2-1" ? 5400seconds : parentmodel == "ACCESS-OM2-025" ? 1800seconds : 400seconds
################################################################################
# Load grid from JLD2
################################################################################
@info "Loading and reconstructing grid from JLD2 data"
grid_file = joinpath(experiment_dir, "grid.jld2")
grid = load_tripolar_grid(grid_file, arch)
Nx, Ny, Nz = size(grid)
@info "Grid loaded: Nx=$Nx, Ny=$Ny, Nz=$Nz"
################################################################################
# Functions + kernels for creating velocities
################################################################################
# MOM → Oceananigans convention kernels and helpers are in shared_utils/data_loading.jl:
# set_kreversed!, fill_Cgrid_transport_from_MOM_output!,
# fill_continuity_tz_from_tx_ty!
function compute_plot_scale(field2D)
wet_values = field2D[.!isnan.(field2D)]
isempty(wet_values) && return 1.0
scale = quantile(abs.(wet_values), 0.9)
return scale > 0 ? scale : 1.0
end
function save_single_field_plot(field2D, title, filepath)
fig = Figure(size = (1200, 600))
ax = Axis(fig[1, 1], title = title)
scale = compute_plot_scale(field2D)
hm = heatmap!(ax, field2D; colormap = :RdBu_9, colorrange = scale .* (-1, 1), nan_color = :black)
Colorbar(fig[1, 2], hm)
save(filepath, fig)
return nothing
end
function save_field_k_plot(field, k, title, filepath)
field2D = view(make_plottable_array(field), :, :, k)
return save_single_field_plot(field2D, title, filepath)
end
################################################################################
# Create velocities
################################################################################
@info "Creating velocity and sea surface height field time series"
flush(stdout); flush(stderr)
@info "Reading NetCDF inputs from monthly_dir=$monthly_dir"
flush(stdout); flush(stderr)
@info " Opening eta_t_monthly.nc"; flush(stdout); flush(stderr)
η_ds = open_dataset(joinpath(monthly_dir, "eta_t_monthly.nc"))
@info " Opening tx_trans_monthly.nc"; flush(stdout); flush(stderr)
tx_ds = open_dataset(joinpath(monthly_dir, "tx_trans_monthly.nc"))
@info " Opening ty_trans_monthly.nc"; flush(stdout); flush(stderr)
ty_ds = open_dataset(joinpath(monthly_dir, "ty_trans_monthly.nc"))
if build_total_transport
tx_gm_path = joinpath(monthly_dir, "tx_trans_gm_monthly.nc")
ty_gm_path = joinpath(monthly_dir, "ty_trans_gm_monthly.nc")
isfile(tx_gm_path) && isfile(ty_gm_path) ||
error("VELOCITY_SOURCE=totaltransport requires tx_trans_gm_monthly.nc and ty_trans_gm_monthly.nc in $monthly_dir")
@info " Opening tx_trans_gm_monthly.nc"; flush(stdout); flush(stderr)
tx_gm_ds = open_dataset(tx_gm_path)
@info " Opening ty_trans_gm_monthly.nc"; flush(stdout); flush(stderr)
ty_gm_ds = open_dataset(ty_gm_path)
end
if dht_check
@info " Opening dht_monthly.nc (DHT_CHECK)"; flush(stdout); flush(stderr)
dht_ds = open_dataset(joinpath(monthly_dir, "dht_monthly.nc"))
dht_var_name = hasproperty(dht_ds, :dht) ? :dht : error("Could not find variable `dht` in dht_monthly.nc")
end
# prescribed_Δt = 1Δt
prescribed_Δt = 1month
fts_times = ((1:12) .- 0.5) * prescribed_Δt
stop_time = 12 * prescribed_Δt
η_monthly_file = joinpath(monthly_dir, "eta_monthly.jld2")
u_mt_monthly_file = joinpath(monthly_dir, "u_from_mass_transport_monthly.jld2")
v_mt_monthly_file = joinpath(monthly_dir, "v_from_mass_transport_monthly.jld2")
w_mt_monthly_file = joinpath(monthly_dir, "w_from_mass_transport_monthly.jld2")
η_yearly_file = joinpath(yearly_dir, "eta_yearly.jld2")
u_mt_yearly_file = joinpath(yearly_dir, "u_from_mass_transport_yearly.jld2")
v_mt_yearly_file = joinpath(yearly_dir, "v_from_mass_transport_yearly.jld2")
w_mt_yearly_file = joinpath(yearly_dir, "w_from_mass_transport_yearly.jld2")
u_tt_monthly_file = joinpath(monthly_dir, "u_from_total_transport_monthly.jld2")
v_tt_monthly_file = joinpath(monthly_dir, "v_from_total_transport_monthly.jld2")
w_tt_monthly_file = joinpath(monthly_dir, "w_from_total_transport_monthly.jld2")
u_tt_yearly_file = joinpath(yearly_dir, "u_from_total_transport_yearly.jld2")
v_tt_yearly_file = joinpath(yearly_dir, "v_from_total_transport_yearly.jld2")
w_tt_yearly_file = joinpath(yearly_dir, "w_from_total_transport_yearly.jld2")
# remove old files if they exist
rm(η_monthly_file; force = true)
rm(u_mt_monthly_file; force = true)
rm(v_mt_monthly_file; force = true)
rm(w_mt_monthly_file; force = true)
rm(η_yearly_file; force = true)
rm(u_mt_yearly_file; force = true)
rm(v_mt_yearly_file; force = true)
rm(w_mt_yearly_file; force = true)
if build_total_transport
rm(u_tt_monthly_file; force = true)
rm(v_tt_monthly_file; force = true)
rm(w_tt_monthly_file; force = true)
rm(u_tt_yearly_file; force = true)
rm(v_tt_yearly_file; force = true)
rm(w_tt_yearly_file; force = true)
end
# Boundary conditions for velocity FTS (sign flip at north fold for u/v, no flip for w/η)
ubcs = FieldBoundaryConditions(grid, (Face(), Center(), Center()); north = FPivotZipperBoundaryCondition(-1))
vbcs = FieldBoundaryConditions(grid, (Center(), Face(), Center()); north = FPivotZipperBoundaryCondition(-1))
wbcs = FieldBoundaryConditions(grid, (Center(), Center(), Face()); north = FPivotZipperBoundaryCondition(1))
# Create FieldTimeSeries with OnDisk backend directly to write data as we process it
u_mt_monthly_ts = FieldTimeSeries{Face, Center, Center}(grid, fts_times; backend = OnDisk(), path = u_mt_monthly_file, name = "u", time_indexing = Cyclical(stop_time), boundary_conditions = ubcs)
v_mt_monthly_ts = FieldTimeSeries{Center, Face, Center}(grid, fts_times; backend = OnDisk(), path = v_mt_monthly_file, name = "v", time_indexing = Cyclical(stop_time), boundary_conditions = vbcs)
w_mt_monthly_ts = FieldTimeSeries{Center, Center, Face}(grid, fts_times; backend = OnDisk(), path = w_mt_monthly_file, name = "w", time_indexing = Cyclical(stop_time), boundary_conditions = wbcs)
η_monthly_ts = FieldTimeSeries{Center, Center, Nothing}(grid, fts_times; backend = OnDisk(), path = η_monthly_file, name = "η", time_indexing = Cyclical(stop_time))
if build_total_transport
u_tt_monthly_ts = FieldTimeSeries{Face, Center, Center}(grid, fts_times; backend = OnDisk(), path = u_tt_monthly_file, name = "u", time_indexing = Cyclical(stop_time), boundary_conditions = ubcs)
v_tt_monthly_ts = FieldTimeSeries{Center, Face, Center}(grid, fts_times; backend = OnDisk(), path = v_tt_monthly_file, name = "v", time_indexing = Cyclical(stop_time), boundary_conditions = vbcs)
w_tt_monthly_ts = FieldTimeSeries{Center, Center, Face}(grid, fts_times; backend = OnDisk(), path = w_tt_monthly_file, name = "w", time_indexing = Cyclical(stop_time), boundary_conditions = wbcs)
end
@info "All NetCDF datasets opened successfully"
flush(stdout); flush(stderr)
@info "Creating FieldTimeSeries with OnDisk backend"
flush(stdout); flush(stderr)
# Pre-allocate fields reused every month to avoid per-month allocations
η = Field{Center, Center, Nothing}(grid)
if dht_check
dht_diag = Field{Center, Center, Center}(grid)
Δzstar = Field(zspacings(grid, Center(), Center(), Center()))
end
tx = XFaceField(grid; boundary_conditions = ubcs)
ty = YFaceField(grid; boundary_conditions = vbcs)
u_mt = XFaceField(grid; boundary_conditions = ubcs)
v_mt = YFaceField(grid; boundary_conditions = vbcs)
w_mt = ZFaceField(grid; boundary_conditions = wbcs)
tz = Field{Center, Center, Face}(grid; boundary_conditions = wbcs)
if build_total_transport
tx_gm = XFaceField(grid; boundary_conditions = ubcs)
ty_gm = YFaceField(grid; boundary_conditions = vbcs)
u_tt = XFaceField(grid; boundary_conditions = ubcs)
v_tt = YFaceField(grid; boundary_conditions = vbcs)
w_tt = ZFaceField(grid; boundary_conditions = wbcs)
tz_tt = Field{Center, Center, Face}(grid; boundary_conditions = wbcs)
end
# TODO: Alternatives to precomputed area Fields:
# 1) Use AbstractOperation directly (no Field/compute!; lazy eval per iteration, negligible cost)
# 2) Use kernels with Oceananigans.Operators.Axᶠᶜᶜ/Ayᶜᶠᶜ/Azᶜᶜᶠ (fused, no allocations; more code)
AxFCC = Field(grid_metric_operation((Face, Center, Center), Ax, grid))
AyCFC = Field(grid_metric_operation((Center, Face, Center), Ay, grid))
AzCCF = Field(grid_metric_operation((Center, Center, Face), Az, grid))
# Time-average accumulators (reuse same BCs as the corresponding periodic fields)
u_mt_acc = XFaceField(grid; boundary_conditions = ubcs)
v_mt_acc = YFaceField(grid; boundary_conditions = vbcs)
w_mt_acc = ZFaceField(grid; boundary_conditions = wbcs)
η_acc = Field{Center, Center, Nothing}(grid)
if build_total_transport
u_tt_acc = XFaceField(grid; boundary_conditions = ubcs)
v_tt_acc = YFaceField(grid; boundary_conditions = vbcs)
w_tt_acc = ZFaceField(grid; boundary_conditions = wbcs)
end
for month in 1:12
println("month $month")
flush(stdout)
# ── η (set first so _update_zstar_scaling! runs before the dht check) ────
println("- η"); flush(stdout)
η_data = readcubedata(η_ds.eta_t[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, η_data, η_data)
set!(η, η_data)
mask_immersed_field!(η, 0.0)
fill_halo_regions!(η)
set!(η_monthly_ts, η, month)
# Update z-star grid scaling (so Δzstar reflects current η for the optional dht check)
launch!(architecture(grid), grid, surface_kernel_parameters(grid), _update_zstar_scaling!, η, grid)
# ── dht consistency check (optional; DHT_CHECK=yes) ──────────────────────
# mask_immersed_field! sets immersed cells to NaN in both fields so that
# filter(!isnan, ratio) selects only wet cells (Δrᶜᶜᶜ is non-zero for immersed
# cells in PartialCellBottom grids, so Δzᶜᶜᶜ_data .> 0 would be incorrect).
if dht_check
println("- dht consistency check"); flush(stdout)
dht_data = readcubedata(getproperty(dht_ds, dht_var_name)[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, dht_data, dht_data)
size(dht_data) == (Nx, Ny, Nz) || error("Unexpected dht monthly shape $(size(dht_data)); expected ($Nx, $Ny, $Nz)")
set_kreversed!(dht_diag, dht_data)
mask_immersed_field!(dht_diag, NaN)
compute!(Δzstar) # Δr * σⁿ with current σ after _update_zstar_scaling!
mask_immersed_field!(Δzstar, NaN)
wet_ratio = filter(!isnan, vec(Array(interior(dht_diag)) ./ Array(interior(Δzstar))))
println(" - dht/Δzstar wet-cell ratio: min=$(minimum(wet_ratio)), mean=$(mean(wet_ratio)), max=$(maximum(wet_ratio))")
end
# ── u, v, w from mass transports ────────────────────────────────────────
println("- u, v, w from mass transports"); flush(stdout)
tx_data = readcubedata(tx_ds.tx_trans[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, tx_data, tx_data)
ty_data = readcubedata(ty_ds.ty_trans[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, ty_data, ty_data)
fill_Cgrid_transport_from_MOM_output!(tx, ty, grid, tx_data, ty_data)
mask_immersed_field!(tx, 0.0)
mask_immersed_field!(ty, 0.0)
fill_halo_regions!(tx)
fill_halo_regions!(ty)
compute!(AxFCC)
compute!(AyCFC)
compute!(AzCCF)
u_mt .= tx / (ρ₀ * AxFCC)
v_mt .= ty / (ρ₀ * AyCFC)
fill_continuity_tz_from_tx_ty!(tz, grid, tx, ty)
w_mt .= tz / (ρ₀ * AzCCF)
mask_immersed_field!(u_mt, 0.0)
mask_immersed_field!(v_mt, 0.0)
mask_immersed_field!(w_mt, 0.0)
fill_halo_regions!(u_mt)
fill_halo_regions!(v_mt)
fill_halo_regions!(w_mt)
set!(u_mt_monthly_ts, u_mt, month)
set!(v_mt_monthly_ts, v_mt, month)
set!(w_mt_monthly_ts, w_mt, month)
# ── u, v, w from total transports (resolved + GM) ──────────────────
if build_total_transport
println("- u, v, w from total transports (resolved + GM)"); flush(stdout)
tx_gm_data = readcubedata(tx_gm_ds.tx_trans_gm[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, tx_gm_data, tx_gm_data)
ty_gm_data = readcubedata(ty_gm_ds.ty_trans_gm[month = At(month)]).data
map!(x -> isnan(x) ? zero(x) : x, ty_gm_data, ty_gm_data)
fill_Cgrid_transport_from_MOM_output!(tx_gm, ty_gm, grid, tx_gm_data, ty_gm_data)
streamfunction_to_perlayer!(tx_gm, grid)
streamfunction_to_perlayer!(ty_gm, grid)
mask_immersed_field!(tx_gm, 0.0)
mask_immersed_field!(ty_gm, 0.0)
fill_halo_regions!(tx_gm)
fill_halo_regions!(ty_gm)
# Velocity from total transport = resolved + GM per-layer
u_tt .= (tx .+ tx_gm) / (ρ₀ * AxFCC)
v_tt .= (ty .+ ty_gm) / (ρ₀ * AyCFC)
# Vertical velocity from continuity of total transport
tx_gm .+= tx # tx_gm now holds total tx
ty_gm .+= ty # ty_gm now holds total ty
fill_halo_regions!(tx_gm)
fill_halo_regions!(ty_gm)
fill_continuity_tz_from_tx_ty!(tz_tt, grid, tx_gm, ty_gm)
w_tt .= tz_tt / (ρ₀ * AzCCF)
mask_immersed_field!(u_tt, 0.0)
mask_immersed_field!(v_tt, 0.0)
mask_immersed_field!(w_tt, 0.0)
fill_halo_regions!(u_tt)
fill_halo_regions!(v_tt)
fill_halo_regions!(w_tt)
set!(u_tt_monthly_ts, u_tt, month)
set!(v_tt_monthly_ts, v_tt, month)
set!(w_tt_monthly_ts, w_tt, month)
end
if make_plots
println(" - Plot C grid u_mt, v_mt, w_mt")
for k in 25:25
local umt_k_dir = joinpath(u_mt_plot_dir, "k$(k)")
local vmt_k_dir = joinpath(v_mt_plot_dir, "k$(k)")
local wmt_k_dir = joinpath(w_mt_plot_dir, "k$(k)")
mkpath(umt_k_dir)
mkpath(vmt_k_dir)
mkpath(wmt_k_dir)
save_field_k_plot(u_mt, k, "C-grid u from mass transports[k=$k, month=$month]", joinpath(umt_k_dir, "u_from_mass_transport_$(k)_month$(month)_$(arch_str).png"))
save_field_k_plot(v_mt, k, "C-grid v from mass transports[k=$k, month=$month]", joinpath(vmt_k_dir, "v_from_mass_transport_$(k)_month$(month)_$(arch_str).png"))
save_field_k_plot(w_mt, k + 1, "C-grid w from mass transports[k=$k, month=$month]", joinpath(wmt_k_dir, "w_from_mass_transport_$(k)_month$(month)_$(arch_str).png"))
end
println("- Plot η")
plottable_η = view(make_plottable_array(η), :, :, 1)
save_single_field_plot(plottable_η, "sea surface height[month=$month]", joinpath(η_plot_dir, "sea_surface_height_month$(month)_$(arch_str).png"))
end
println("- Accumulate into time-average"); flush(stdout)
u_mt_acc .+= u_mt
v_mt_acc .+= v_mt
w_mt_acc .+= w_mt
η_acc .+= η
if build_total_transport
u_tt_acc .+= u_tt
v_tt_acc .+= v_tt
w_tt_acc .+= w_tt
end
end
println("Done!")
@show u_mt_monthly_ts
@info "saved to $(u_mt_monthly_file)"
@show v_mt_monthly_ts
@info "saved to $(v_mt_monthly_file)"
@show w_mt_monthly_ts
@info "saved to $(w_mt_monthly_file)"
@show η_monthly_ts
@info "saved to $(η_monthly_file)"
if build_total_transport
@show u_tt_monthly_ts
@info "saved to $(u_tt_monthly_file)"
@show v_tt_monthly_ts
@info "saved to $(v_tt_monthly_file)"
@show w_tt_monthly_ts
@info "saved to $(w_tt_monthly_file)"
end
@info "Computing time-averaged (yearly) fields"
u_mt_acc ./= 12
v_mt_acc ./= 12
w_mt_acc ./= 12
η_acc ./= 12
@info "Filling halo regions for time-averaged (yearly) fields"
fill_halo_regions!(u_mt_acc)
fill_halo_regions!(v_mt_acc)
fill_halo_regions!(w_mt_acc)
fill_halo_regions!(η_acc)
@info "Saving time-averaged (yearly) fields"
jldsave(u_mt_yearly_file; u = Array(interior(u_mt_acc)))
jldsave(v_mt_yearly_file; v = Array(interior(v_mt_acc)))
jldsave(w_mt_yearly_file; w = Array(interior(w_mt_acc)))
jldsave(η_yearly_file; η = Array(interior(η_acc, :, :, 1)))
@info "saved yearly u_mt to $(u_mt_yearly_file)"
@info "saved yearly v_mt to $(v_mt_yearly_file)"
@info "saved yearly w_mt to $(w_mt_yearly_file)"
@info "saved yearly η to $(η_yearly_file)"
if build_total_transport
@info "Computing time-averaged (yearly) total transport fields"
u_tt_acc ./= 12
v_tt_acc ./= 12
w_tt_acc ./= 12
fill_halo_regions!(u_tt_acc)
fill_halo_regions!(v_tt_acc)
fill_halo_regions!(w_tt_acc)
jldsave(u_tt_yearly_file; u = Array(interior(u_tt_acc)))
jldsave(v_tt_yearly_file; v = Array(interior(v_tt_acc)))
jldsave(w_tt_yearly_file; w = Array(interior(w_tt_acc)))
@info "saved yearly u_tt to $(u_tt_yearly_file)"
@info "saved yearly v_tt to $(v_tt_yearly_file)"
@info "saved yearly w_tt to $(w_tt_yearly_file)"
end