Skip to content

Commit

Permalink
copy variable with defVar
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Oct 10, 2023
1 parent 8d8287c commit ddd45a5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
keywords = ["netcdf", "climate and forecast conventions", "oceanography", "meteorology", "climatology", "opendap"]
license = "MIT"
desc = "Load and create NetCDF files in Julia"
version = "0.13.0"
version = "0.13.1"

[deps]
CFTime = "179af706-886a-5703-950a-314cd64e0468"
Expand All @@ -17,7 +17,7 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
CFTime = "0.1.1"
CommonDataModel = "0.2.3"
CommonDataModel = "0.2.5"
DataStructures = "0.17, 0.18"
DiskArrays = "0.3"
NetCDF_jll = "=400.701.400, =400.702.400, =400.902.5, =400.902.208"
Expand Down
9 changes: 6 additions & 3 deletions src/cfvariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ function defVar(ds::NCDataset,name::SymbolOrString,vtype::DataType,dimnames;
end


function defVar(dest::AbstractDataset,srcvar::AbstractNCVariable;
_ignore_checksum = false,
)
function defVar(dest::AbstractDataset,srcvar::AbstractNCVariable; kwargs...)
_ignore_checksum = false
if haskey(kwargs,:checksum)
_ignore_checksum = kwargs[:checksum] === nothing
end

src = dataset(srcvar)
varname = name(srcvar)

Expand Down
15 changes: 12 additions & 3 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,14 @@ function _write(dest::NCDataset, src::AbstractDataset;
(varname exclude) && continue
@debug "Writing variable $varname..."

defVar(dest,variable(src,varname),
_ignore_checksum = _ignore_checksum)
kwargs =
if _ignore_checksum
(checksum = nothing,)
else
()
end

defVar(dest,variable(src,varname); kwargs...)
end

# loop over all global attributes
Expand Down Expand Up @@ -524,9 +530,12 @@ function Base.write(dest::NCDataset, src::AbstractDataset;
Base.depwarn(
"The parameter `idimensions` is deprecated. Please use views instead",
:write)

src_subset = view(src;((Symbol(k)=>v) for (k,v) in idimensions)...)
else
src_subset = src
end

src_subset = view(src;((Symbol(k)=>v) for (k,v) in idimensions)...)
_write(dest, src_subset;
include = include,
exclude = exclude,
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers())
include("test_bitarray.jl")
include("test_variable.jl")
include("test_variable_unlim.jl")
include("test_copyvar.jl")
include("test_subvariable.jl")
include("test_strings.jl")
include("test_lowlevel.jl")
Expand Down
36 changes: 36 additions & 0 deletions test/test_copyvar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Dates
using NCDatasets
using Test

var = 10.0:10.0:40.0
tax = DateTime(2001,1,1) .+ Day.(Int.(var))
fname = tempname()
fname2 = tempname()
ds = NCDataset(fname, "c")
defDim(ds, "time", Inf) # "unlimited"
defVar(ds, "time", tax, ("time",))
defVar(ds, "var", var, ("time",),deflatelevel=9)
close(ds)

NCDataset(fname, "r") do ds
time = ds["time"]
var = ds["var"]
NCDataset(fname2, "c") do ds2
defVar(ds2, "time", time, ("time",))
defVar(ds2, "var", var, ("time",))
@test "time" in unlimited(ds)
end
end

NCDataset(fname, "r") do ds
NCDataset(fname2, "c") do ds2
defVar(ds2, ds["time"])
defVar(ds2, ds["var"])
@test "time" in unlimited(ds)
isshuffled,isdeflated,deflatelevel = deflate(ds["var"])
@test deflatelevel == 9
end
end

rm(fname)
rm(fname2)
4 changes: 2 additions & 2 deletions test/test_multifile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ for deferopen in (false,true)
@test mfds["lon"][1:1] == ds_merged["lon"][:]
close(ds_merged)


#=
# save subset of aggregated file (deprecated)
fname_merged = tempname()
write(fname_merged,mfds,idimensions = Dict("lon" => 1:1))
ds_merged = NCDataset(fname_merged)
@test mfds["lon"][1:1] == ds_merged["lon"][:]
close(ds_merged)

=#
# show
buf = IOBuffer()
show(buf,mfds)
Expand Down
4 changes: 2 additions & 2 deletions test/test_select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ end
ds = NCDataset(fname,"r")

v = ds["SST"]
coord_value,dim = coordinate_value(v,:lon)
coord_value,dim_number = coordinate_value(v,:lon)
@test coord_value == lon
@test dim == 1
@test dim_number == 1


@test coordinate_names(ds["SST"]) == [:lon, :lat, :time]
Expand Down
16 changes: 9 additions & 7 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Dates
using Printf
using NCDatasets
using DataStructures

#=
sz = (4,5)
filename = tempname()
#filename = "/tmp/test-6.nc"
Expand Down Expand Up @@ -224,32 +224,33 @@ ds.attrib["x_range"] = x
close(ds)
rm(filename)

=#
# issue 180
using NCDatasets

filename = tempname()
ds = NCDataset(filename, "c")

sample_data = [UInt8(1),Int64(2),Float64(3.),"string",'a']
sample_data = [UInt8(1),"string"]

for data = sample_data
local ncv, T
T = typeof(data)

ncv = defVar(ds, "$(T)_scalar1", T, ())
#= ncv = defVar(ds, "$(T)_scalar1", T, ())
ncv[] = data
@test ncv[] == data

=#
ncv = defVar(ds, "$(T)_scalar2", data, ())
@test ncv[] == data

#=
ncv = defVar(ds, "$(T)_scalar3", data)
@test ncv[] == data
@test ncv[] == data=#
end
close(ds)


#=
# issue 207
filename_src = tempname()
ds_src = NCDataset(filename_src, "c")
Expand Down Expand Up @@ -288,3 +289,4 @@ data2 = zeros(Int,1)
data2 = zeros(Int,10)
# asking too many elements
@test_throws BoundsError NCDatasets.load!(ds["data"].var,data2,1:10)
=#

0 comments on commit ddd45a5

Please sign in to comment.