Skip to content

Commit

Permalink
add units and longname to nc_write
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Sep 23, 2024
1 parent ce0cd4e commit db787aa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 44 deletions.
41 changes: 8 additions & 33 deletions src/nc_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
will be stored in the _FillValue attribute.
+ chunksizes: Vector integers setting the chunk size. The total size of a
chunk must be less than 4 GiB.
chunk must be less than 4 GiB. Such as `(10, 10, 1)`
+ shuffle: If true, the shuffle filter is activated which can improve the
compression ratio.
Expand All @@ -35,7 +35,7 @@ dims = [
]
dims = make_dims(range, cellsize, dates) # another option
nc_write(f, varname, val, dims)
nc_write(f, varname, val, dims; units, longname)
```
$(METHODLIST)
Expand All @@ -44,22 +44,24 @@ $(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)

# check whether variable defined
if !check_file(f) || overwrite
if isfile(f)
rm(f)
end
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=compress, kw...)
ncvar_def(ds, varname, val, dimnames, attrib; compress, kw...)
close(ds)
else
println("[file exist]: $(basename(f))")
Expand Down Expand Up @@ -95,30 +97,3 @@ function nc_write!(f::AbstractString, data::NamedTuple,
nc_write!(f, string(varname), val, dims; kw...)
end
end



# =============================================================================
# ! DEPRECATED ================================================================
# =============================================================================
function nc_write(val::AbstractArray, f::AbstractString, dims::Vector{NcDim}, attrib=Dict();
varname="x", kw...)

printstyled("Deprecated nc_write function!\n", color=:red)
printstyled("latest: nc_write(f, varname, val, dims, attrib; kw...)\n", color=:red)

nc_write(f, varname, val, dims, attrib; kw...)
end


"""
$(TYPEDSIGNATURES)
$(METHODLIST)
"""
function nc_write!(val::AbstractArray, f::AbstractString, dims::Vector{<:Union{NcDim,AbstractString}}, attrib=Dict();
varname="x", kw...)

printstyled("Deprecated nc_write! function!\n", color=:red)
nc_write!(f, varname, val, dims, attrib; kw...)
end
19 changes: 8 additions & 11 deletions test/test-nc_write.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using NetCDFTools
using Test

using NetCDFTools, Test
import Random: seed!

@testset "nc_write" begin
Expand All @@ -16,7 +14,7 @@ import Random: seed!
seed!(1)
dat = rand(Float64, nlon, nlat, ntime)
dat[1] = NaN

# time = 1:size(dat2, 3)
dims = [
NcDim("lon", lon, Dict("longname" => "Longitude", "units" => "degrees east"))
Expand All @@ -30,20 +28,19 @@ import Random: seed!
isfile(fn) && rm(fn)

## test for nc_write `type`
nc_write(dat, fn, dims, Dict("longname" => "Heatwave Index");
varname="HI", overwrite=true,
type=Float32)

nc_write(fn, "HI", dat, dims; longname="Heatwave Index", units="degC",
overwrite=true, type=Float32)

dat2 = nc_read(fn; raw=false)
dat2 = nc_read(fn; ind = (:, :, 1), raw=false)
dat2 = nc_read(fn; ind=(:, :, 1), raw=false)
@test dat2[1] === NaN32

@test nc_read(fn) |> eltype == Float32
@test nc_read(fn, type=Float64) |> eltype == Float64

## test for overwrite
nc_write(dat, fn, dims, Dict("longname" => "Heatwave Index"); varname="HI", overwrite=true)
nc_write!(dat, fn, dims; varname="HI2") # test for multiple variables
nc_write(fn, "HI", dat, dims; longname="Heatwave Index", overwrite=true)
nc_write!(fn, "HI2", dat, dims;) # test for multiple variables

nc_info(fn)
@test nc_bands(fn) == ["HI", "HI2"]
Expand Down

0 comments on commit db787aa

Please sign in to comment.