variable.jl 4.74 KB
Newer Older
Volker Neff's avatar
Volker Neff committed
1
import Base.size, Base.show, Base.keys
2

3
4
import ..TIME: TIME_t

Volker Neff's avatar
Volker Neff committed
5
############## public types ##############
6
7
struct Variable
    varID::Cint
8
    streamID::Cint
9
10
    missValue::Cdouble
    name::String
Volker Neff's avatar
Volker Neff committed
11
12
    stdName::String
    longName::String
13
    unit::String
Volker Neff's avatar
Volker Neff committed
14
    code::Integer
15
16
17
18
    grid::AbstractGrid
    zAxis::AbstractZAxis
    tAxis::AbstractTAxis
end
Volker Neff's avatar
Volker Neff committed
19
export Variable
20

21
22
23
24
25
26
"""
define a variable template that could use to construct an cdiDataset
"""
struct VariableTemplate
    missValue::Cdouble
    name::String
Volker Neff's avatar
Volker Neff committed
27
28
    stdName::String
    longName::String
29
    unit::String
Volker Neff's avatar
Volker Neff committed
30
    code::Integer
31
32
33
34
35
36
    grid::AbstractGrid
    zAxis::AbstractZAxis
    time::TIME_t
end
export VariableTemplate

37
38

const VariableDataType{N} = AbstractArray{Float64, N}
39
40
const VariableMissingDataType{N} = AbstractArray{Union{Missing,Float64}, N}
export VariableDataType, VariableMissingDataType
41

Volker Neff's avatar
Volker Neff committed
42
############## public functions ##############
Volker Neff's avatar
Volker Neff committed
43
44
Base.size(var::Variable)         = size(var.grid)..., size(var.zAxis)...
Base.size(var::VariableTemplate) = size(var.grid)..., size(var.zAxis)...
Volker Neff's avatar
Volker Neff committed
45
export size
Volker Neff's avatar
Volker Neff committed
46

Volker Neff's avatar
Volker Neff committed
47
48
Base.length(var::Variable)         = length(var.grid) * length(var.zAxis)
Base.length(var::VariableTemplate) = length(var.grid) * length(var.zAxis)
49
50
export length

Volker Neff's avatar
Volker Neff committed
51
52
53
function Base.show(io::IO, v::Variable; indent="")
    printstyled(io, indent, "Variable: ", v.name, "\n", color=:light_green)
    print(io, indent, "ID:              ", v.varID, "\n")
Volker Neff's avatar
Volker Neff committed
54
55
    print(io, indent, "standard name:   ", v.stdName, "\n")
    print(io, indent, "long name:       ", v.longName, "\n")
Volker Neff's avatar
Volker Neff committed
56
    print(io, indent, "unit:            ", v.unit, "\n")
Volker Neff's avatar
Volker Neff committed
57
    print(io, indent, "code:            ", v.code, "\n")
Volker Neff's avatar
Volker Neff committed
58
    print(io, indent, "miss value:      ", v.missValue, "\n")
Volker Neff's avatar
Volker Neff committed
59
60
61
62
end
export show

Base.keys(v::Variable) = v.name
63
64
export keys

Volker Neff's avatar
Volker Neff committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Return the associated grid
"""
getGrid(var::Variable) = var.grid
getGrid(var::VariableTemplate) = var.grid
export getGrid

"""
Return the associated z-axis
"""
getZAxis(var::Variable) = var.zAxis
getZAxis(var::VariableTemplate) = var.zAxis
export getZAxis

79
80
81
82
function loadData(var::Variable)::VariableDataType
    data = Array{Float64}(undef, size(var)...)
    return loadData!(data, var)
end
83
84

function loadMissingData(var::Variable)::VariableMissingDataType
Volker Neff's avatar
Volker Neff committed
85
86
87
88
89
90
    data = loadData(var)
    data = convert(VariableMissingDataType, data)
    for i in CartesianIndices(data)
        data[i] = data[i] == var.missValue ? missing : data[i]
    end
    return data
91
92
end
export loadData, loadMissingData
93
94
95
96
97
98
99
100
101
102

function loadData!(data::VariableDataType{N}, var::Variable)::VariableDataType where {N}
    streamReadVar(var.streamID, var.varID, reshape(data, :))
    return data
end

function loadData!(data::VariableDataType{1}, var::Variable)::VariableDataType
    streamReadVar(var.streamID, var.varID, data)
    return data
end
103
104
export loadData!

105
function loadMissingData!(data::VariableMissingDataType{N}, var::Variable)::VariableMissingDataType where {N}
Volker Neff's avatar
Volker Neff committed
106
    tmp = similar(data, Float64)
Volker Neff's avatar
Volker Neff committed
107
    streamReadVar(var.streamID, var.varID, reshape(tmp, :))
Volker Neff's avatar
Volker Neff committed
108
    for i in CartesianIndices(data)
Volker Neff's avatar
Volker Neff committed
109
        data[i] = tmp[i] == var.missValue ? missing : tmp[i]
Volker Neff's avatar
Volker Neff committed
110
    end
111
112
113
114
    return data
end

function loadMissingData!(data::VariableMissingDataType{1}, var::Variable)::VariableMissingDataType
Volker Neff's avatar
Volker Neff committed
115
    tmp = similar(data, Float64)
Volker Neff's avatar
Volker Neff committed
116
    streamReadVar(var.streamID, var.varID, tmp)
Volker Neff's avatar
Volker Neff committed
117
    for i in CartesianIndices(data)
Volker Neff's avatar
Volker Neff committed
118
        data[i] = tmp[i] == var.missValue ? missing : tmp[i]
Volker Neff's avatar
Volker Neff committed
119
    end
120
121
122
123
    return data
end
export loadMissingData!

Volker Neff's avatar
Volker Neff committed
124
125
126
127
128
129
130
131
132
133
134
135
136
function storeData!(var::Variable, data::VariableDataType{N}) where {N}
    @assert length(var) == length(data) "data has not correct size"
    streamWriteVar(var.streamID, var.varID, reshape(data, :), 0)
    return nothing
end

function storeData!(var::Variable, data::VariableDataType{1})
    @assert length(var) == length(data) "data has not correct size"
    streamWriteVar(var.streamID, var.varID, data, 0)
    return nothing
end
export storeData!

137
function Variable(name::AbstractString, grid::AbstractGrid, zAxis::AbstractZAxis, 
Volker Neff's avatar
Volker Neff committed
138
139
    time::TIME_t; unit::AbstractString="", stdName::AbstractString=name,
    longName::AbstractString="", code::Integer=-1, missValue::Cdouble=typemin(Cdouble))
Volker Neff's avatar
Volker Neff committed
140
    @debug "load variable: " * name
Volker Neff's avatar
Volker Neff committed
141
    return VariableTemplate(missValue, name, stdName, longName, unit, code, grid, zAxis, time)
Volker Neff's avatar
Volker Neff committed
142
143
end

Volker Neff's avatar
Volker Neff committed
144
145
146
147
148
149
150
# Get variable Member

"""
    unit(var)

Return the variable unit as string
"""
Volker Neff's avatar
Volker Neff committed
151
unit(var::Variable) = var.unit
Volker Neff's avatar
Volker Neff committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
unit(var::VariableTemplate) = var.unit
export unit

"""
    zAxis(var)

Return the associated z-axis struct
"""
zAxis(var::Variable) = var.zAxis
zAxis(var::VariableTemplate) = var.zAxis
export zAxis

"""
    tAxis(var)

Return the associated t-axis struct
"""
tAxis(var::Variable) = var.tAxis
export tAxis

"""
    grid(var)

Return the associated grid struct
"""
grid(var::Variable) = var.grid
grid(var::VariableTemplate) = var.grid
export grid