Class: NCVar
Constant Summary
collapse
- TYPENAME =
{
NC_CHAR => "char",
NC_BYTE => "byte",
NC_SHORT => "short",
NC_INT => "int",
NC_FLOAT => "float",
NC_DOUBLE => "double",
}
- TIME_UNITS =
{
"day" => 86400,
"days" => 86400,
"d" => 86400,
"hour" => 3600,
"hours" => 3600,
"hr" => 3600,
"h" => 3600,
"minute" => 60,
"minutes" => 60,
"min" => 60,
"second" => 1,
"seconds" => 1,
"sec" => 1,
"s" => 1,
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#[](*argv) ⇒ Object
-
#decode(value) ⇒ Object
-
#definition ⇒ Object
-
#get(*argv) ⇒ Object
-
#get!(*argv) ⇒ Object
-
#get_var ⇒ Object
-
#get_var! ⇒ Object
-
#get_var1(*index) ⇒ Object
-
#get_var1!(*index) ⇒ Object
-
#get_vara(start, count) ⇒ Object
-
#get_vara!(start, count) ⇒ Object
-
#get_varm(start, count, stride, imap) ⇒ Object
-
#get_varm!(start, count, stride, imap) ⇒ Object
-
#get_vars(start, count, stride) ⇒ Object
-
#get_vars!(start, count, stride) ⇒ Object
-
#initialize(ncfile, var_id) ⇒ NCVar
constructor
-
#inspect ⇒ Object
-
#is_dim? ⇒ Boolean
-
#method_missing(id, *argv) ⇒ Object
-
#to_ca ⇒ Object
-
#to_time ⇒ Object
Methods included from NC
nc_decode, nc_put_att_simple, nc_put_var_all
Methods inherited from NCObject
#attribute, #get_attributes, #get_attributes!
Constructor Details
#initialize(ncfile, var_id) ⇒ NCVar
Returns a new instance of NCVar.
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/io/netcdf.rb', line 98
def initialize (ncfile, var_id)
@ncfile = ncfile
@file_id = ncfile.file_id
@var_id = var_id
@name = nc_inq_varname(@file_id, var_id)
@vartype = nc_inq_vartype(@file_id, var_id)
@dims = ncfile.dims.values_at(*nc_inq_vardimid(@file_id, var_id))
@shape = @dims.map{|d| d.len}
@attributes = get_attributes(@file_id, var_id)
@dims.freeze
@shape.freeze
@attributes.freeze
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *argv) ⇒ Object
321
322
323
|
# File 'lib/io/netcdf.rb', line 321
def method_missing (id, *argv)
to_ca.send(id, *argv)
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
112
113
114
|
# File 'lib/io/netcdf.rb', line 112
def attributes
@attributes
end
|
#dims ⇒ Object
Returns the value of attribute dims.
112
113
114
|
# File 'lib/io/netcdf.rb', line 112
def dims
@dims
end
|
#name ⇒ Object
Returns the value of attribute name.
112
113
114
|
# File 'lib/io/netcdf.rb', line 112
def name
@name
end
|
Instance Method Details
#[](*argv) ⇒ Object
211
212
213
|
# File 'lib/io/netcdf.rb', line 211
def [] (*argv)
return get!(*argv)
end
|
#decode(value) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/io/netcdf.rb', line 140
def decode (value)
if @attributes.has_key?("_FillValue")
fill_value = @attributes["_FillValue"]
case value
when CArray
value[:eq, fill_value] = UNDEF
else
value = UNDEF if value == fill_value
end
end
if @attributes.has_key?("missing_value")
missing_values = [@attributes["missing_value"]].flatten
missing_values.each do |mv|
case value
when CArray
value[:eq, mv] = UNDEF
else
if value == mv
value = UNDEF
break
end
end
end
end
if @attributes.has_key?("scale_factor")
value *= @attributes["scale_factor"]
end
if @attributes.has_key?("add_offset")
value += @attributes["add_offset"]
end
return value
end
|
#definition ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/io/netcdf.rb', line 114
def definition
defs = { "type" => TYPENAME[@vartype] }
if not dims.empty?
defs["dim"] = dims.map{|x| x.name }
end
atts = {}
get_attributes!(@file_id, @var_id).each do |k,v|
atts[k] = NCFile.convert_attribute_value(v)
end
defs.update(atts)
return defs
end
|
#get(*argv) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/io/netcdf.rb', line 215
def get (*argv)
if argv.size > 0 and argv[0].is_a?(Struct::CAIndexInfo)
info = argv.shift
else
info = CArray.scan_index(@shape, argv)
end
out = nil
case info.type
when CA_REG_ADDRESS
addr = info.index[0]
index = []
(0..@shape.size-1).reverse_each do |i|
index[i] = addr % @shape[i]
addr /= @shape[i]
end
out = get_var1(*index)
when CA_REG_FLATTEN
out = get_var[nil]
when CA_REG_POINT
out = get_var1(*info.index)
when CA_REG_ALL
out = get_var()
when CA_REG_BLOCK
use_compact = false
start = []
count = []
stride = []
info.index.each do |idx|
case idx
when Array
start << idx[0]
count << idx[1]
stride << idx[2]
else
use_compact = true
start << idx
count << 1
stride << 1
end
end
if stride.all?{|x| x == 1 }
out = get_vara(start, count)
else
out = get_vars(start, count, stride)
end
if use_compact and out.is_a?(CArray)
out = out.compact
end
when CA_REG_SELECT, CA_REG_GRID
out = get_var[*argv]
else
raise "invalid index"
end
return out
end
|
#get!(*argv) ⇒ Object
271
272
273
274
275
276
277
278
279
|
# File 'lib/io/netcdf.rb', line 271
def get! (*argv)
info = CArray.scan_index(@shape, argv)
case info.type
when CA_REG_METHOD_CALL
return decode(get_var)[*argv]
else
return decode(get(info, *argv))
end
end
|
#get_var ⇒ Object
289
290
291
|
# File 'lib/io/netcdf.rb', line 289
def get_var ()
return nc_get_var(@file_id, @var_id)
end
|
#get_var! ⇒ Object
293
294
295
|
# File 'lib/io/netcdf.rb', line 293
def get_var! ()
return decode(nc_get_var(@file_id, @var_id))
end
|
#get_var1(*index) ⇒ Object
281
282
283
|
# File 'lib/io/netcdf.rb', line 281
def get_var1 (*index)
return nc_get_var1(@file_id, @var_id, index)
end
|
#get_var1!(*index) ⇒ Object
285
286
287
|
# File 'lib/io/netcdf.rb', line 285
def get_var1! (*index)
return decode(get_var1(*index))
end
|
#get_vara(start, count) ⇒ Object
297
298
299
|
# File 'lib/io/netcdf.rb', line 297
def get_vara (start, count)
return nc_get_vara(@file_id, @var_id, start, count)
end
|
#get_vara!(start, count) ⇒ Object
301
302
303
|
# File 'lib/io/netcdf.rb', line 301
def get_vara! (start, count)
return decode(nc_get_vara(@file_id, @var_id, start, count))
end
|
#get_varm(start, count, stride, imap) ⇒ Object
313
314
315
|
# File 'lib/io/netcdf.rb', line 313
def get_varm (start, count, stride, imap)
return nc_get_varm(@file_id, @var_id, start, count, stride, imap)
end
|
#get_varm!(start, count, stride, imap) ⇒ Object
317
318
319
|
# File 'lib/io/netcdf.rb', line 317
def get_varm! (start, count, stride, imap)
return decode(nc_get_varm(@file_id, @var_id, start, count, stride, imap))
end
|
#get_vars(start, count, stride) ⇒ Object
305
306
307
|
# File 'lib/io/netcdf.rb', line 305
def get_vars (start, count, stride)
return nc_get_vars(@file_id, @var_id, start, count, stride)
end
|
#get_vars!(start, count, stride) ⇒ Object
309
310
311
|
# File 'lib/io/netcdf.rb', line 309
def get_vars! (start, count, stride)
return decode(nc_get_vars(@file_id, @var_id, start, count, stride))
end
|
#inspect ⇒ Object
127
128
129
|
# File 'lib/io/netcdf.rb', line 127
def inspect
return "#{@name}#{@dims}"
end
|
#is_dim? ⇒ Boolean
131
132
133
134
135
136
137
138
|
# File 'lib/io/netcdf.rb', line 131
def is_dim?
begin
nc_inq_dimlen(@file_id, @var_id)
return true
rescue RuntimeError
return false
end
end
|
#to_ca ⇒ Object
173
174
175
|
# File 'lib/io/netcdf.rb', line 173
def to_ca
return self[]
end
|
#to_time ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/io/netcdf.rb', line 194
def to_time
if not @attributes.has_key?("units")
raise "variable #{@name} has no attribute 'units'"
elsif @attributes["units"] =~ /\A(.+?)\s+since\s+(.+)\Z/
interval = TIME_UNITS[$1]
basetime = Time.parse($2)
if interval
return self[].convert(:object) {|x| basetime + interval*x }
else
raise "#{$1} is invalid for time units"
end
else
raise "format of #{@name}:units is not valid for time"
end
end
|