Skip to content

Commit

Permalink
improve ncdim_def
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 17, 2023
1 parent a25c3d1 commit 350f6ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/nc_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function nc_write!(f::AbstractString, varname::AbstractString, val, dims::Vector

mode = check_file(f) ? "a" : "c"
ds = nc_open(f, mode)
ncdim_def(ds, dims; verbose=false)

ncvar_def(ds, varname, val, dims, attrib; compress=compress, kw...)
close(ds)
end
Expand Down
18 changes: 12 additions & 6 deletions src/ncdim_def.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# TODO: 这里有提升空间
function NcDim_time(dates)
import Dates: Date, DateTime
import CFTime: AbstractCFDateTime

const DateTimeType = Union{DateTime,AbstractCFDateTime}

function NcDim_time(dates::Vector{<:DateTimeType})
vals = CFTime.timeencode.(dates, "days since 1970-01-01", eltype(dates))
attrib = Dict(
"units" => "days since 1970-01-01",
Expand All @@ -8,6 +12,8 @@ function NcDim_time(dates)
NcDim("time", length(vals), vals, attrib)
end

NcDim_time(dates::Vector{Date}) = NcDim_time(DateTime.(dates))

"""
make_dims(range=[70, 140, 15, 55], cellsize = 0.5, dates)
Expand Down Expand Up @@ -60,13 +66,13 @@ function ncdim_def(ds, name, val, attrib=Dict(); verbose=false)
end

# NcDim
function ncdim_def(ds, dim::NcDim)
ncdim_def(ds, dim.name, dim.vals, dim.atts)
function ncdim_def(ds, dim::NcDim; kw...)
ncdim_def(ds, dim.name, dim.vals, dim.atts; kw...)
end

function ncdim_def(ds, dims::Vector{NcDim})
function ncdim_def(ds, dims::Vector{NcDim}; kw...)
for i = 1:length(dims)
dim = dims[i]
ncdim_def(ds, dim.name, dim.vals, dim.atts)
ncdim_def(ds, dim.name, dim.vals, dim.atts; kw...)
end
end

0 comments on commit 350f6ed

Please sign in to comment.