Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passes grid argument to NetCDFOutputWriter #3576

Merged
merged 25 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
48aa15c
Update netcdf_output_writer.jl
tomchor May 2, 2024
f0143e1
Add grid as property to NetCDFOutputWriter and use it during file ini…
glwagner May 3, 2024
fdee401
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 3, 2024
296ab22
paragraph explaining that netcdf writer also accepts an output grid
iuryt May 3, 2024
1ce9908
change wording and add jldoctest
iuryt May 6, 2024
fa97a72
Update docs/src/model_setup/output_writers.md
iuryt May 6, 2024
077fea6
Do a little better with function output for NetCDF output writer
glwagner May 6, 2024
c61729b
Fixes interpolate! plus some improvements to NetCDFOutputWriter
glwagner May 7, 2024
4134b5b
add new example for docs
iuryt May 7, 2024
ed5ce3e
simplify example
iuryt May 7, 2024
373f15a
simplify example in the docstring
iuryt May 7, 2024
6f0b9fb
Update docs/src/model_setup/output_writers.md
iuryt May 8, 2024
42ccd03
Make grid a kwarg and update language
glwagner May 8, 2024
8a7d7c0
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 8, 2024
14c94d8
Merge branch 'main' into tc/grid-to-netcdfwriter
tomchor May 8, 2024
70e459a
Update src/OutputWriters/netcdf_output_writer.jl
iuryt May 8, 2024
9403243
Update docs/src/model_setup/output_writers.md
iuryt May 8, 2024
45f0293
Update netcdf_output_writer.jl
iuryt May 8, 2024
8c141de
Bugfix plus better error message
glwagner May 8, 2024
c4357e5
Correctly define_output_variable for LagrangianParticles
glwagner May 8, 2024
4a3bd34
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 8, 2024
aa5e65c
Fix znodes in output writers docs
glwagner May 9, 2024
ab73354
fix znodes
iuryt May 9, 2024
a3a2f7e
fix double semicolon
iuryt May 9, 2024
4850ddc
14.5 kB
glwagner May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/src/model_setup/output_writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,47 @@ NetCDFOutputWriter scheduled on IterationInterval(1):
└── file size: 17.8 KiB
```

`NetCDFOutputWriter` can also be configured for `outputs` that are interpolated or regridded to a different grid than `model.grid`. To use this functionality, the `output_grid` must be passed explicitly when constructing `NetCDFOutputWriter` along with the regridded / interpolated `outputs`.
iuryt marked this conversation as resolved.
Show resolved Hide resolved

```jldoctest
using Oceananigans

grid = RectilinearGrid(size = (1, 1, 8), extent = (1,1,1));
model = NonhydrostaticModel(; grid, closure = ScalarDiffusivity(ν=1e-2))

set!(model, u=(x, y, z,) -> z)

simulation = Simulation(model,
Δt=0.5*maximum(zspacings(grid, Center())) / maximum(abs, model.velocities.u),
stop_time=20)

simulation.output_writers[:fullfields] = NetCDFOutputWriter(model, (; model.velocities.u),
filename = "fullfields.nc",
schedule = TimeInterval(5),
overwrite_existing = true,)

coarse_grid = RectilinearGrid(size = (grid.Nx, grid.Ny, grid.Nz÷2), extent = (grid.Lx, grid.Ly, grid.Lz))
coarse_u = Field{Face, Center, Center}(coarse_grid)

using Oceananigans.Fields: interpolate!
update_coarse_u(simulation) = interpolate!(coarse_u, simulation.model.velocities.u)
simulation.callbacks[:update_interp] = Callback(update_coarse_u)

simulation.output_writers[:coarsefields] = NetCDFOutputWriter(model, (; coarse_u,), coarse_grid;
filename="coarsefields.nc",
schedule=TimeInterval(5),
overwrite_existing=true,)
glwagner marked this conversation as resolved.
Show resolved Hide resolved

# output
NetCDFOutputWriter scheduled on TimeInterval(5 seconds):
├── filepath: ./coarsefields.nc
├── dimensions: zC(4), zF(5), xC(1), yF(1), xF(1), yC(1), time(0)
├── 1 outputs: coarse_u
└── array type: Array{Float64}
├── file_splitting: NoFileSplitting
└── file size: 14.6 KiB
```

See [`NetCDFOutputWriter`](@ref) for more information.

## JLD2 output writer
Expand Down
41 changes: 24 additions & 17 deletions src/OutputWriters/netcdf_output_writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using Oceananigans.Utils: versioninfo_with_gpu, oceananigans_versioninfo, pretty
using Oceananigans.TimeSteppers: float_or_date_time
using Oceananigans.Fields: reduced_dimensions, reduced_location, location, validate_indices

mutable struct NetCDFOutputWriter{D, O, T, A, FS} <: AbstractOutputWriter
mutable struct NetCDFOutputWriter{G, D, O, T, A, FS} <: AbstractOutputWriter
grid :: G
filepath :: String
dataset :: D
outputs :: O
Expand Down Expand Up @@ -355,19 +356,21 @@ NetCDFOutputWriter scheduled on IterationInterval(1):
└── file size: 17.8 KiB
```
"""
function NetCDFOutputWriter(model, outputs; filename, schedule,
dir = ".",
array_type = Array{Float64},
indices = (:, :, :),
with_halos = false,
function NetCDFOutputWriter(model, outputs, grid=model.grid;
filename,
schedule,
dir = ".",
array_type = Array{Float64},
indices = (:, :, :),
with_halos = false,
global_attributes = Dict(),
output_attributes = Dict(),
dimensions = Dict(),
overwrite_existing = nothing,
deflatelevel = 0,
part = 1,
file_splitting = NoFileSplitting(),
verbose = false)
dimensions = Dict(),
overwrite_existing = nothing,
deflatelevel = 0,
part = 1,
file_splitting = NoFileSplitting(),
verbose = false)
mkpath(dir)
filename = auto_extension(filename, ".nc")
filepath = joinpath(dir, filename)
Expand All @@ -394,7 +397,7 @@ function NetCDFOutputWriter(model, outputs; filename, schedule,
# with LagrangianParticles output (see the end of the file).
# We shouldn't support this in the future; we should require users to 'name' LagrangianParticles output.
outputs = dictify(outputs)
outputs = Dict(string(name) => construct_output(outputs[name], model.grid, indices, with_halos) for name in keys(outputs))
outputs = Dict(string(name) => construct_output(outputs[name], grid, indices, with_halos) for name in keys(outputs))

output_attributes = dictify(output_attributes)
global_attributes = dictify(global_attributes)
Expand All @@ -414,9 +417,11 @@ function NetCDFOutputWriter(model, outputs; filename, schedule,
dimensions,
overwrite_existing,
deflatelevel,
grid,
model)

return NetCDFOutputWriter(filepath,
return NetCDFOutputWriter(grid,
filepath,
dataset,
outputs,
schedule,
Expand Down Expand Up @@ -633,6 +638,7 @@ function initialize_nc_file!(filepath,
dimensions,
overwrite_existing,
deflatelevel,
grid,
model)

mode = overwrite_existing ? "c" : "a"
Expand All @@ -648,12 +654,12 @@ function initialize_nc_file!(filepath,
# schedule::AveragedTimeInterval
schedule, outputs = time_average_outputs(schedule, outputs, model)

dims = default_dimensions(outputs, model.grid, indices, with_halos)
dims = default_dimensions(outputs, grid, indices, with_halos)

# Open the NetCDF dataset file
dataset = NCDataset(filepath, mode, attrib=global_attributes)

default_dimension_attributes = get_default_dimension_attributes(model.grid)
default_dimension_attributes = get_default_dimension_attributes(grid)

# Define variables for each dimension and attributes if this is a new file.
if mode == "c"
Expand All @@ -669,7 +675,7 @@ function initialize_nc_file!(filepath,

# Creates an unlimited dimension "time"
defDim(dataset, "time", Inf)
defVar(dataset, "time", eltype(model.grid), ("time",), attrib=time_attrib)
defVar(dataset, "time", eltype(grid), ("time",), attrib=time_attrib)

# Use default output attributes for known outputs if the user has not specified any.
# Unknown outputs get an empty tuple (no output attributes).
Expand Down Expand Up @@ -704,4 +710,5 @@ initialize_nc_file!(ow::NetCDFOutputWriter, model) =
ow.dimensions,
ow.overwrite_existing,
ow.deflatelevel,
ow.grid,
model)