Skip to content

Commit f0a394f

Browse files
authored
Use FieldTimeSeries in tilted BBL example (with NetCDF output) (#5273)
1 parent 28726af commit f0a394f

4 files changed

Lines changed: 32 additions & 38 deletions

File tree

examples/tilted_bottom_boundary_layer.jl

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,9 @@ run!(simulation)
214214

215215
using CairoMakie
216216

217-
xb, yb, zb = nodes(B)
218-
xω, yω, zω = nodes(ωy)
219-
xv, yv, zv = nodes(V)
220-
221217
# Read in the simulation's `output_writer` for the two-dimensional fields and then create an
222218
# animation showing the ``y``-component of vorticity.
223219

224-
ds = NCDataset(simulation.output_writers[:fields].filepath, "r")
225-
226220
fig = Figure(size = (800, 600))
227221

228222
axis_kwargs = (xlabel = "Across-slope distance (m)",
@@ -234,21 +228,22 @@ ax_v = Axis(fig[3, 1]; title = "Along-slope velocity (v)", axis_kwargs...)
234228

235229
n = Observable(1)
236230

237-
ωy = @lift ds["ωy"][:, :, $n]
238-
B = @lift ds["B"][:, :, $n]
239-
ωlim = 0.015
240-
hm_ω = heatmap!(ax_ω, xω, zω, ωy, colorrange = (-ωlim, +ωlim), colormap = :balance)
241-
Colorbar(fig[2, 2], hm_ω; label = "s⁻¹")
242-
ct_b = contour!(ax_ω, xb, zb, B, levels=-1e-3:5e-5:1e-3, color=:black)
231+
ωy_timeseries = FieldTimeSeries(simulation.output_writers[:fields].filepath, "ωy")
232+
B_timeseries = FieldTimeSeries(simulation.output_writers[:fields].filepath, "B")
233+
V_timeseries = FieldTimeSeries(simulation.output_writers[:fields].filepath, "V")
243234

244-
V = @lift ds["V"][:, :, $n]
245-
V_max = @lift maximum(abs, ds["V"][:, :, $n])
235+
ωy = @lift ωy_timeseries[$n]
236+
B = @lift B_timeseries[$n]
237+
hm_ω = heatmap!(ax_ω, ωy, colorrange = (-0.015, +0.015), colormap = :balance)
238+
Colorbar(fig[2, 2], hm_ω; label = "s⁻¹")
239+
ct_b = contour!(ax_ω, B, levels=-1e-3:5e-5:1e-3, color=:black)
246240

247-
hm_v = heatmap!(ax_v, xv, zv, V, colorrange = (-V∞, +V∞), colormap = :balance)
241+
V = @lift V_timeseries[$n]
242+
hm_v = heatmap!(ax_v, V, colorrange = (-V∞, +V∞), colormap = :balance)
248243
Colorbar(fig[3, 2], hm_v; label = "m s⁻¹")
249-
ct_b = contour!(ax_v, xb, zb, B, levels=-1e-3:5e-5:1e-3, color=:black)
244+
ct_b = contour!(ax_v, B, levels=-1e-3:5e-5:1e-3, color=:black)
250245

251-
times = collect(ds["time"])
246+
times = ωy_timeseries.times
252247
title = @lift "t = " * string(prettytime(times[$n]))
253248
fig[1, :] = Label(fig, title, fontsize=20, tellwidth=false)
254249

@@ -265,7 +260,3 @@ end
265260
nothing #hide
266261

267262
# ![](tilted_bottom_boundary_layer.mp4)
268-
269-
# Don't forget to close the NetCDF file!
270-
271-
close(ds)

ext/OceananigansNCDatasetsExt/dimensions.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ function default_dimension_attributes(grid::RectilinearGrid, dim_name_generator)
258258
Δyᵃᶠᵃ_name = dim_name_generator("Δy", grid, nothing, f, nothing, Val(:y))
259259
Δyᵃᶜᵃ_name = dim_name_generator("Δy", grid, nothing, c, nothing, Val(:y))
260260

261-
xᶠᵃᵃ_attrs = Dict("long_name" => "Cell face locations in the x-direction.", "units" => "m")
262-
xᶜᵃᵃ_attrs = Dict("long_name" => "Cell center locations in the x-direction.", "units" => "m")
263-
yᵃᶠᵃ_attrs = Dict("long_name" => "Cell face locations in the y-direction.", "units" => "m")
264-
yᵃᶜᵃ_attrs = Dict("long_name" => "Cell center locations in the y-direction.", "units" => "m")
265-
266-
Δxᶠᵃᵃ_attrs = Dict("long_name" => "Spacings between cell centers (located at the cell faces) in the x-direction.", "units" => "m")
267-
Δxᶜᵃᵃ_attrs = Dict("long_name" => "Spacings between cell faces (located at the cell centers) in the x-direction.", "units" => "m")
268-
Δyᵃᶠᵃ_attrs = Dict("long_name" => "Spacings between cell centers (located at cell faces) in the y-direction.", "units" => "m")
269-
Δyᵃᶜᵃ_attrs = Dict("long_name" => "Spacings between cell faces (located at cell centers) in the y-direction.", "units" => "m")
261+
xᶠᵃᵃ_attrs = Dict("long_name" => "Cell face locations in the x-direction.", "units" => "m", "location" => "Face")
262+
xᶜᵃᵃ_attrs = Dict("long_name" => "Cell center locations in the x-direction.", "units" => "m", "location" => "Center")
263+
yᵃᶠᵃ_attrs = Dict("long_name" => "Cell face locations in the y-direction.", "units" => "m", "location" => "Face")
264+
yᵃᶜᵃ_attrs = Dict("long_name" => "Cell center locations in the y-direction.", "units" => "m", "location" => "Center")
265+
266+
Δxᶠᵃᵃ_attrs = Dict("long_name" => "Spacings between cell centers (located at the cell faces) in the x-direction.", "units" => "m", "location" => "Face")
267+
Δxᶜᵃᵃ_attrs = Dict("long_name" => "Spacings between cell faces (located at the cell centers) in the x-direction.", "units" => "m", "location" => "Center")
268+
Δyᵃᶠᵃ_attrs = Dict("long_name" => "Spacings between cell centers (located at cell faces) in the y-direction.", "units" => "m", "location" => "Face")
269+
Δyᵃᶜᵃ_attrs = Dict("long_name" => "Spacings between cell faces (located at cell centers) in the y-direction.", "units" => "m", "location" => "Center")
270270

271271
horizontal_dimension_attributes = Dict(xᶠᵃᵃ_name => xᶠᵃᵃ_attrs,
272272
xᶜᵃᵃ_name => xᶜᵃᵃ_attrs,

ext/OceananigansNCDatasetsExt/netcdf_writer.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ function defVar(ds::AbstractDataset, field_name, fd::AbstractField;
2828
effective_dim_names = create_field_dimensions!(ds, fd, dimension_name_generator; time_dependent, with_halos, array_type, dimension_type)
2929

3030
# Add location to attributes
31-
loc = location(fd) |> convert_for_netcdf
32-
loc_attrib = Dict("location" => loc)
3331
if :attrib keys(kwargs)
34-
attrib = merge(loc_attrib, kwargs[:attrib])
32+
attrib = add_location_attribute!(kwargs[:attrib], fd)
3533
else
36-
attrib = loc_attrib
34+
attrib = add_location_attribute!(Dict(), fd)
3735
end
3836

3937
# Add indices to attributes
@@ -55,6 +53,12 @@ end
5553

5654
defVar(ds::AbstractDataset, field_name::Union{AbstractString, Symbol}, data::Array{Bool}, dim_names; kwargs...) = defVar(ds, field_name, Int8.(data), dim_names; kwargs...)
5755

56+
function add_location_attribute!(attrib, fd::AbstractField)
57+
loc = location(fd) |> convert_for_netcdf
58+
loc_attrib = Dict("location" => loc)
59+
return merge(loc_attrib, attrib)
60+
end
61+
5862
#####
5963
##### Variable attributes
6064
#####
@@ -495,7 +499,6 @@ function write_output!(ow::NetCDFWriter, model::AbstractModel)
495499
update_file_splitting_schedule!(ow.file_splitting, ow.filepath)
496500

497501
ow.dataset = open(ow)
498-
499502
ds, verbose, filepath = ow.dataset, ow.verbose, ow.filepath
500503

501504
time_index = length(ds["time"]) + 1

src/OutputWriters/netcdf_writer.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ NetCDFWriter scheduled on TimeInterval(1 minute):
236236
├── 2 outputs: (c, u)
237237
├── array_type: Array{Float32}
238238
├── file_splitting: NoFileSplitting
239-
└── file size: 32.6 KiB
239+
└── file size: 32.7 KiB
240240
```
241241
242242
```jldoctest netcdf1
@@ -269,7 +269,7 @@ NetCDFWriter scheduled on TimeInterval(1 minute):
269269
├── 2 outputs: (c, u) averaged on AveragedTimeInterval(window=20 seconds, stride=1, interval=1 minute)
270270
├── array_type: Array{Float32}
271271
├── file_splitting: NoFileSplitting
272-
└── file size: 33.8 KiB
272+
└── file size: 33.9 KiB
273273
```
274274
275275
`NetCDFWriter` also accepts output functions that write scalars and arrays to disk,
@@ -320,7 +320,7 @@ NetCDFWriter scheduled on IterationInterval(1):
320320
├── 3 outputs: (profile, slice, scalar)
321321
├── array_type: Array{Float32}
322322
├── file_splitting: NoFileSplitting
323-
└── file size: 34.0 KiB
323+
└── file size: 34.1 KiB
324324
```
325325
326326
`NetCDFWriter` can also be configured for `outputs` that are interpolated or regridded

0 commit comments

Comments
 (0)