Skip to content

Commit

Permalink
Merge pull request #2 from meggart/netcdf_nohandles
Browse files Browse the repository at this point in the history
Make NetCDF dataset not return handles
  • Loading branch information
meggart authored Aug 25, 2020
2 parents ef10136 + f956dd2 commit f51a78a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ version = "0.17.11"
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DiskArrays]]
git-tree-sha1 = "3bfd0eb19711297e1b3656b7d4709f7b4b240195"
uuid = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
version = "0.2.4"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name = "YAXArrayBase"
uuid = "90b8fcef-0c2d-428d-9c56-5f86629e9d14"
authors = ["Fabian Gans <fgans@bgc-jena.mpg.de>"]
version = "0.1.0"
version = "0.2.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
julia = "^1"
DataStructures = "0.17, 0.18"
Requires = "1"
julia = "^1"

[extras]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
26 changes: 23 additions & 3 deletions src/datasets/netcdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,44 @@ struct NetCDFDataset
end
NetCDFDataset(filename) = NetCDFDataset(filename,NC_NOWRITE)

import .NetCDF: AbstractDiskArray, readblock!, writeblock!, haschunks, eachchunk

struct NetCDFVariable{T,N} <: AbstractDiskArray{T,N}
filename::String
varname::String
size::NTuple{N,Int}
end
#Define method forwarding for DiskArray methods
for m in [:haschunks, :eachchunk]
eval(:(function $(m)(v::NetCDFVariable,args...;kwargs...)
NetCDF.open(a->$(m)(a,args...;kwargs...), v.filename, v.varname)
end
))
end
writeblock!(v::NetCDFVariable, aout, r::AbstractUnitRange...) = NetCDF.open(a->writeblock!(a,aout,r...), v.filename, v.varname, mode=NC_WRITE)
readblock!(v::NetCDFVariable, aout, r::AbstractUnitRange...) = NetCDF.open(a->readblock!(a,aout,r...), v.filename, v.varname)

Base.size(v::NetCDFVariable) = v.size

get_var_dims(ds::NetCDFDataset,name) = NetCDF.open(v->map(i->i.name,v[name].dim),ds.filename)
get_varnames(ds::NetCDFDataset) = NetCDF.open(v->collect(keys(v.vars)),ds.filename)
get_var_attrs(ds::NetCDFDataset, name) = NetCDF.open(v->v[name].atts,ds.filename)
function Base.getindex(ds::NetCDFDataset, i)
NetCDF.open(ds.filename,i,mode=ds.mode)
s,et = NetCDF.open(j->(size(j),eltype(j)),ds.filename,i)
NetCDFVariable{et,length(s)}(ds.filename, i, s)
end
Base.haskey(ds::NetCDFDataset,k) = NetCDF.open(nc->haskey(nc.vars,k),ds.filename)

function add_var(p::NetCDFDataset, T::Type, varname, s, dimnames, attr;
chunksize=s, compress = -1)
dimsdescr = Iterators.flatten(zip(dimnames,s))
nccreate(p.filename, varname, dimsdescr..., atts = attr, t=T, chunksize=chunksize, compress=compress)
NetCDF.open(p.filename,varname,mode=p.mode)
NetCDFVariable{T,length(s)}(p.filename,varname,s)
end

function create_empty(::Type{NetCDFDataset}, path)
NetCDF.create(path, NcVar[])
NetCDFDataset(path,NC_WRITE)
NetCDFDataset(path)
end

allow_parallel_write(::NetCDFDataset) = false
Expand Down

2 comments on commit f51a78a

@meggart
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/20173

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" f51a78ad04ac9fec5aadeb0e04cc1246c48421a1
git push origin v0.2.0

Please sign in to comment.