Skip to content

DiskArrays for CFVariable #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ version = "0.2.4"
CFTime = "179af706-886a-5703-950a-314cd64e0468"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"

[compat]
julia = "1.6"
Preferences = "1.3"
DataStructures = "0.17, 0.18"
DiskArrays = "0.3"
CFTime = "0.1.1"
3 changes: 3 additions & 0 deletions src/CommonDataModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ using Printf
using Preferences
import Base: isopen, show, display, close
using DataStructures
import DiskArrays: AbstractDiskArray, readblock!, writeblock!, haschunks, eachchunk
using DiskArrays: GridChunks, Unchunked
using DiskArrays: @implement_diskarray

"""

Expand Down
34 changes: 21 additions & 13 deletions src/cfvariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mutable struct CFVariable{T,N,TV,TA,TSA} <: AbstractVariable{T, N}
_storage_attrib::TSA
end

@implement_diskarray CFVariable

"""
sz = size(var::CFVariable)

Expand Down Expand Up @@ -384,43 +386,49 @@ end
@inline CFinvtransformdata(data::Char,fv,scale_factor,add_offset,time_origin,time_factor,DT) = CFtransform_replace_missing(data,fv)


function Base.getindex(v::CFVariable,
function readblock!(v::CFVariable, aout,
indexes::Union{Int,Colon,AbstractRange{<:Integer}}...)
data = v.var[indexes...]
return CFtransformdata(data,fill_and_missing_values(v),scale_factor(v),add_offset(v),
rawdata = getindex(v.var, indexes...)
aout .= CFtransformdata(rawdata,fill_and_missing_values(v),scale_factor(v),add_offset(v),
time_origin(v),time_factor(v),eltype(v))
end

function Base.setindex!(v::CFVariable,data::Array{Missing,N},indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where N
v.var[indexes...] = fill(fillvalue(v),size(data))
function writeblock!(v::CFVariable,data::Array{Missing,N},indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where N
transformed = fill(fillvalue(v), size(data))
setindex!(v.var, transformed, indexes...)
return transformed
end

function Base.setindex!(v::CFVariable,data::Missing,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...)
v.var[indexes...] = fillvalue(v)
function writeblock!(v::CFVariable,data::Missing,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...)
transformed = fillvalue(v)
setindex!(v.var, transformed, indexes...)
return transformed
end

function Base.setindex!(v::CFVariable,data::Union{T,Array{T,N}},indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where N where T <: Union{AbstractCFDateTime,DateTime,Union{Missing,DateTime,AbstractCFDateTime}}
function writeblock!(v::CFVariable,data::Union{T,Array{T,N}},indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where N where T <: Union{AbstractCFDateTime,DateTime,Union{Missing,DateTime,AbstractCFDateTime}}

if calendar(v) !== nothing
# can throw an convertion error if calendar attribute already exists and
# is incompatible with the provided data
v.var[indexes...] = CFinvtransformdata(
transformed = CFinvtransformdata(
data,fill_and_missing_values(v),scale_factor(v),add_offset(v),
time_origin(v),time_factor(v),eltype(v.var))
return data
setindex!(v.var, transformed, indexes...)
return transformed
end

@error "Time units and calendar must be defined during defVar and cannot change"
end


function Base.setindex!(v::CFVariable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...)
v.var[indexes...] = CFinvtransformdata(
function writeblock!(v::CFVariable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...)
transformed = CFinvtransformdata(
data,fill_and_missing_values(v),
scale_factor(v),add_offset(v),
time_origin(v),time_factor(v),eltype(v.var))
setindex!(v.var, transformed, indexes...)

return data
return transformed
end


Expand Down
6 changes: 6 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ function dataset(v::AbstractVariable)
error("unimplemented for abstract type")
end

function Base.parent(var::AbstractVariable)
error("unimplemented for abstract type")
end

eachchunk(A::AbstractVariable) = GridChunks(A, size(A))
haschunks(A::AbstractVariable) = Unchunked()

function Base.show(io::IO,v::AbstractVariable)
level = get(io, :level, 0)
Expand Down