From c9ddbcf41f952801ac07f5e7b04a0707ae56b062 Mon Sep 17 00:00:00 2001 From: jbisits Date: Mon, 9 Dec 2024 10:09:25 +1100 Subject: [PATCH 1/2] Check if key is present before writing --- ...ngle_interface_periodic_tanh_background.jl | 3 +- src/staircase_background.jl | 57 ++++++++++--------- src/staircase_diagnostics.jl | 11 +++- src/staircase_simulation.jl | 23 ++++++++ 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/examples/single_interface_periodic_tanh_background.jl b/examples/single_interface_periodic_tanh_background.jl index a29ab88..7f5154c 100644 --- a/examples/single_interface_periodic_tanh_background.jl +++ b/examples/single_interface_periodic_tanh_background.jl @@ -26,7 +26,8 @@ save_schedule = 30 # seconds output_path = joinpath(@__DIR__, "output_periodic") simulation = SDNS_simulation_setup(sdns, stop_time, save_computed_output!, save_vertical_velocities!; Δt, save_schedule, - output_path, max_Δt = 5) + output_path, max_Δt = 5, + overwrite_saved_output = false) ## Run run!(simulation) diff --git a/src/staircase_background.jl b/src/staircase_background.jl index b7dacc1..b4db83a 100644 --- a/src/staircase_background.jl +++ b/src/staircase_background.jl @@ -153,35 +153,40 @@ function save_background_state!(simulation, model, background_state) compute!(σ_background) σ_background_array = Array(interior(σ_background, :, :, :)) - if simulation.output_writers[:tracers] isa NetCDFOutputWriter - - NCDataset(simulation.output_writers[:tracers].filepath, "a") do ds - defVar(ds, "S_background", S_background_array, ("xC", "yC", "zC"), - attrib = Dict("longname" => "Background field for salinity", - "units" => "gkg⁻¹")) - defVar(ds, "T_background", T_background_array, ("xC", "yC", "zC"), - attrib = Dict("longname" => "Background field for temperature", - "units" => "°C")) - end - - NCDataset(simulation.output_writers[:computed_output].filepath, "a") do ds - defVar(ds, "σ_background", σ_background_array, ("xC", "yC", "zC"), - attrib = Dict("longname" => "Background field for potential density (0dbar) computed from the `S` and `T` background fields", - "units" => "kgm⁻³")) - end - - elseif simulation.output_writers[:tracers] isa JLD2OutputWriter + # Here need only check if one key is present as if one is all are + if !check_key_present(simulation, :tracers, "S_background") + + ow = simulation.output_writers + if ow[:tracers] isa NetCDFOutputWriter + + NCDataset(ow[:tracers].filepath, "a") do ds + defVar(ds, "S_background", S_background_array, ("xC", "yC", "zC"), + attrib = Dict("longname" => "Background field for salinity", + "units" => "gkg⁻¹")) + defVar(ds, "T_background", T_background_array, ("xC", "yC", "zC"), + attrib = Dict("longname" => "Background field for temperature", + "units" => "°C")) + end + + NCDataset(ow[:computed_output].filepath, "a") do ds + defVar(ds, "σ_background", σ_background_array, ("xC", "yC", "zC"), + attrib = Dict("longname" => "Background field for potential density (0dbar) computed from the `S` and `T` background fields", + "units" => "kgm⁻³")) + end + + elseif ow[:tracers] isa JLD2OutputWriter + + jldopen(ow[:tracers].filepath, "a+") do f + f["S_background"] = S_background_array + f["T_background"] = T_background_array + end + + jldopen(ow[:computed_output].filepath, "a+") do f + f["σ_background"] = σ_background_array + end - jldopen(simulation.output_writers[:tracers].filepath, "a+") do f - f["S_background"] = S_background_array - f["T_background"] = T_background_array - end - - jldopen(simulation.output_writers[:computed_output].filepath, "a+") do f - f["σ_background"] = σ_background_array end end - return nothing end diff --git a/src/staircase_diagnostics.jl b/src/staircase_diagnostics.jl index 6d5e9d7..19b777c 100644 --- a/src/staircase_diagnostics.jl +++ b/src/staircase_diagnostics.jl @@ -29,8 +29,15 @@ function compute_R_ρ!(computed_output::AbstractString, tracers::AbstractString, R_ρ = @. (0.5 * (ρ_f - ρ_u) + 0.5 * (ρ_l - ρ_g)) / (0.5 * (ρ_f - ρ_l) + 0.5 * (ρ_u - ρ_g)) NCDataset(computed_output, "a") do ds2 - defVar(ds2, "R_ρ", R_ρ, ("time",), - attrib = Dict("long_name" => "Density ratio")) + if haskey(ds2, "R_ρ") + # rename so if picking up can saved the whole array + renameVar(ds2, "R_ρ", "R_ρ_prior_cp") + defVar(ds2, "R_ρ", R_ρ, ("time",), + attrib = Dict("long_name" => "Density ratio")) + else + defVar(ds2, "R_ρ", R_ρ, ("time",), + attrib = Dict("long_name" => "Density ratio")) + end end close(ds) diff --git a/src/staircase_simulation.jl b/src/staircase_simulation.jl index bc2109e..a02ce11 100644 --- a/src/staircase_simulation.jl +++ b/src/staircase_simulation.jl @@ -279,6 +279,29 @@ function checkpointer_setup!(simulation, sdns, output_dir, end checkpointer_setup!(simulation, sdns, output_dir, checkpointer_time_interval::Nothing) = nothing + +""" + function check_key_present(simulation, name::Symbol, key) +Check if `key` is already present in `simlulation.output_writers[name]`. Return `true` if +`key` is present and `false` otherwise. +""" +function check_key_present(simulation, name::Symbol, key) + + ow = simulation.output_writers[name] + key_present = false + + if ow isa NetCDFOutputWriter + NCDataset(ow.filepath) do ds + key_present = haskey(ds, key) + end + elseif ow isa JLD2OutputWriter + jldopen(ow.filepath, "a+") do f + key_present = haskey(f, key) + end + end + + return key_present +end """ function simulation_progress(sim) Useful progress messaging for simulation runs. This function is from an From 11bf2a8b80cb6280fcb4925864b92409c06f246a Mon Sep 17 00:00:00 2001 From: jbisits Date: Mon, 9 Dec 2024 10:11:26 +1100 Subject: [PATCH 2/2] Update single_interface_periodic_tanh_background.jl --- examples/single_interface_periodic_tanh_background.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/single_interface_periodic_tanh_background.jl b/examples/single_interface_periodic_tanh_background.jl index 7f5154c..a29ab88 100644 --- a/examples/single_interface_periodic_tanh_background.jl +++ b/examples/single_interface_periodic_tanh_background.jl @@ -26,8 +26,7 @@ save_schedule = 30 # seconds output_path = joinpath(@__DIR__, "output_periodic") simulation = SDNS_simulation_setup(sdns, stop_time, save_computed_output!, save_vertical_velocities!; Δt, save_schedule, - output_path, max_Δt = 5, - overwrite_saved_output = false) + output_path, max_Δt = 5) ## Run run!(simulation)