Skip to content

Commit

Permalink
Merge pull request #11 from JuliaDataCubes/datasetconstructors
Browse files Browse the repository at this point in the history
Give Dataset constructors an mode kwarg
  • Loading branch information
meggart authored May 6, 2022
2 parents d8e629b + 44f7e45 commit 48565a5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/datasets/archgdal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct GDALDataset
bands::OrderedDict{String}
end

function GDALDataset(filename)
function GDALDataset(filename;mode="r")
AG.read(filename) do r
nb = AG.nraster(r)
allbands = map(1:nb) do iband
Expand Down
13 changes: 8 additions & 5 deletions src/datasets/datasetinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ backendlist = OrderedDict{Symbol, Any}(

backendregex = Pair[]

function to_dataset(g::String; driver=:all)
function backendfrompath(g::String; driver = :all)
if driver == :all
for p in YAXArrayBase.backendregex
if match(p[1],g) !== nothing
return to_dataset(p[2],g)
return p[2]
end
end
return to_dataset(g,driver=:zarr)
return last(first(backendregex))
else
return to_dataset(backendlist[driver],g)
return backendlist[driver]
end
end

to_dataset(g::String; driver=:all, kwargs...) = to_dataset(backendfrompath(g;driver),g,kwargs...)

to_dataset(g; kwargs...) = g
to_dataset(T::Type{<:Any}, g::String) = T(g)
to_dataset(T::Type{<:Any}, g::String;kwargs...) = T(g;kwargs...)
2 changes: 1 addition & 1 deletion src/datasets/netcdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct NetCDFDataset
filename::String
mode::UInt16
end
NetCDFDataset(filename) = NetCDFDataset(filename,NC_NOWRITE)
NetCDFDataset(filename;mode="r") = mode == "r" ? NetCDFDataset(filename,NC_NOWRITE) : NetCDFDataset(filename,NC_WRITE)

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

Expand Down
4 changes: 2 additions & 2 deletions src/datasets/zarr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ to_zarrtype, zopen, Compressor
struct ZarrDataset
g::ZGroup
end
ZarrDataset(g::String) = ZarrDataset(zopen(g))
ZarrDataset(g::String;mode="r") = ZarrDataset(zopen(g,mode,fill_as_missing=true))

get_var_dims(ds::ZarrDataset,name) = reverse(ds[name].attrs["_ARRAY_DIMENSIONS"])
get_varnames(ds::ZarrDataset) = collect(keys(ds.g.arrays))
Expand All @@ -21,7 +21,7 @@ end
function add_var(p::ZarrDataset, T::Type, varname, s, dimnames, attr;
chunksize=s, kwargs...)
attr2 = merge(attr,Dict("_ARRAY_DIMENSIONS"=>reverse(collect(dimnames))))
za = zcreate(T, p.g, varname, s...;attrs=attr2,chunks=chunksize,kwargs...)
za = zcreate(T, p.g, varname,s...;fill_as_missing=true,attrs=attr2,chunks=chunksize,kwargs...)
za
end

Expand Down
2 changes: 1 addition & 1 deletion test/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end

@testset "Reading Zarr" begin
p = "gs://cmip6/CMIP6/HighResMIP/CMCC/CMCC-CM2-HR4/highresSST-present/r1i1p1f1/6hrPlev/psl/gn/v20170706/"
ds_zarr = to_dataset(p)
ds_zarr = to_dataset(p,driver=:zarr)
vn = get_varnames(ds_zarr)
@test sort(vn) == ["lat", "lat_bnds", "lon", "lon_bnds", "psl", "time", "time_bnds"]
@test get_var_dims(ds_zarr, "psl") == ["lon", "lat", "time"]
Expand Down

2 comments on commit 48565a5

@meggart
Copy link
Member Author

@meggart meggart commented on 48565a5 May 6, 2022

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/59790

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.3.2 -m "<description of version>" 48565a531d87c3eb95166e1d06b733dc372e25f4
git push origin v0.3.2

Please sign in to comment.