Skip to content

Commit

Permalink
Merge #1448
Browse files Browse the repository at this point in the history
1448: Allow HDF5Writer to append output r=charleskawczynski a=Sbozzolo

This change adds an option to `HDF5Writer` so that data can be appended to an existing file instead of overwriting it.

From https://juliaio.github.io/HDF5.jl/stable/#Opening-and-closing-files

cw = read-write, create file if not existing, preserve existing contents




Co-authored-by: Gabriele Bozzola <[email protected]>
  • Loading branch information
bors[bot] and Sbozzolo authored Sep 8, 2023
2 parents 14740cd + 9610520 commit 45c59c5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/InputOutput/writers.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
abstract type AbstractWriter end

"""
HDF5Writer(filename::AbstractString[, context::ClimaComms.AbstractCommsContext])
HDF5Writer(filename::AbstractString[, context::ClimaComms.AbstractCommsContext];
overwrite::Bool = true)
An `AbstractWriter` for writing to HDF5-formatted files using the ClimaCore
storage conventions. An internal cache is used to avoid writing duplicate
Expand All @@ -12,6 +13,10 @@ The optional `context` can be used for writing distributed fields: in this case,
the `MPICommsContext` used passed as an argument: this must match the context
used for distributing the `Field`.
The writer overwrites or appends to existing files depending on the value of the
`overwrite` keyword argument. When `overwrite` is `false`, the writer appends to
`filename` if the file already exists, otherwise it creates a new one.
!!! note
The default Julia HDF5 binaries are not built with MPI support. To use the
Expand Down Expand Up @@ -44,12 +49,15 @@ end

function HDF5Writer(
filename::AbstractString,
context::ClimaComms.AbstractCommsContext,
context::ClimaComms.AbstractCommsContext;
overwrite::Bool = true,
)
mode = overwrite ? "w" : "cw"

if context isa ClimaComms.SingletonCommsContext
file = h5open(filename, "w")
file = h5open(filename, mode)
else
file = h5open(filename, "w", context.mpicomm)
file = h5open(filename, mode, context.mpicomm)
end
write_attribute(file, "ClimaCore version", string(VERSION))
cache = Dict{String, String}()
Expand Down

0 comments on commit 45c59c5

Please sign in to comment.