Skip to content

Commit

Permalink
major update for ncvar_def
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 3, 2024
1 parent b1e9411 commit 57dd173
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
34 changes: 10 additions & 24 deletions src/nc_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,41 @@ $(METHODLIST)
"""
function nc_write(f::AbstractString, varname::AbstractString, val,
dims::Vector{NcDim}, attrib::Dict=Dict();
units=nothing, longname=nothing,
compress=1, overwrite=false, mode="c",
global_attrib=Dict(),
kw...)

!isnothing(units) && (attrib["units"] = units)
!isnothing(longname) && (attrib["longname"] = longname)

overwrite=false, mode="c", kw...)

# check whether variable defined
if !check_file(f) || overwrite
isfile(f) && rm(f)

ds = nc_open(f, mode)
ncatt_put(ds, global_attrib)
ncdim_def(ds, dims)

dimnames = names(dims)
ncvar_def(ds, varname, val, dimnames, attrib; compress, kw...)
close(ds)
nc_write!(f, varname, val, dims, attrib; mode, kw...)
else
println("[file exist]: $(basename(f))")
end
end


"""
$(TYPEDSIGNATURES)
$(METHODLIST)
# ! TODO: need to test overwrite
"""
function nc_write!(f::AbstractString, varname::AbstractString, val,
dims::Vector{<:Union{NcDim,AbstractString}}, attrib::Dict=Dict();
dims::Vector{<:Union{NcDim,AbstractString}}, attrib::Dict=Dict();
units=nothing, longname=nothing,
compress=1, kw...)
compress=1, global_attrib=Dict(), mode=nothing,
kw...)

!isnothing(units) && (attrib["units"] = units)
!isnothing(longname) && (attrib["longname"] = longname)

mode = check_file(f) ? "a" : "c"
isnothing(mode) && (mode = check_file(f) ? "a" : "c")

ds = nc_open(f, mode)
ncatt_put(ds, global_attrib)
ncdim_def(ds, dims; verbose=false)

ncvar_def(ds, varname, val, dims, attrib; compress=compress, kw...)
ncvar_def(ds, varname, val, dims, attrib; compress, kw...)
close(ds)
end


function nc_write!(f::AbstractString, data::NamedTuple,
dims::Vector{<:Union{NcDim,AbstractString}}; verbose=false, kw...)

Expand Down
53 changes: 29 additions & 24 deletions src/ncvar_def.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
ncvar_def(ds, name, val, dims::Vector{<:AbstractString}, attrib = Dict();
compress = 1, kwargs...)
compress = 1, kw...)
ncvar_def(ds, name, val, dims::NcDim, attrib = Dict();
compress = 1, kwargs...)
compress = 1, kw...)
# Arguments
Expand Down Expand Up @@ -57,30 +57,35 @@ nc_info(f)
@seealso [`ncdim_def`](@ref)
"""
function ncvar_def(ds, name, val, dims::Vector{<:AbstractString}, attrib=Dict();
compress=1, type=nothing, overwrite=false, kwargs...)

# attrib["deflatelevel"] = compress
if name in keys(ds) && !overwrite
# println(options)
@warn "Variable `$name`: exist!"
return
function ncvar_def(ds, name, val, dims::Vector{D}, attrib=Dict();
compress=1, type=nothing, overwrite=false, kw...) where {S<:AbstractString,D<:Union{NcDim,S}}

# change datatype
if type !== nothing && eltype(val) != type
val = @.(type(val))
end

# attrib["deflatelevel"] = compress
if name in keys(ds)
if !overwrite
@warn "Variable `$name`: exist! Use `overwrite=true` to overwrite."
return
else
@warn "Variable `$name`: exist! Overwrited."

_var = ds[name]
indices = ntuple(i -> :, ndims(_var))
_var[indices...] = val
return
end
# change datatype
if type !== nothing && eltype(val) != type
val = @.(type(val))
end
# defDim(ds, name, length(val))
defVar(ds, name, val, dims; attrib=attrib, deflatelevel=compress, kwargs...)
end

function ncvar_def(ds, name, val, dims::Vector{NcDim}, attrib=Dict();
compress=1, kwargs...)
end

# attrib["deflatelevel"] = compress
dimnames = dims
# 若只有string name,则无法定义dims,无奈之举
if D == NcDim
ncdim_def(ds, dims) # define dimensions if previous not exist
dimnames = map(x -> x.name, dims)
ncvar_def(ds, name, val, dimnames; attrib=attrib, deflatelevel=compress, kwargs...)
# var = NcVar(varname, dims; t = type, compress = compress)
end
end

defVar(ds, name, val, dimnames; attrib, deflatelevel=compress, kw...)
end

0 comments on commit 57dd173

Please sign in to comment.