Class: NCVar

Inherits:
NCObject show all
Includes:
NC
Defined in:
lib/io/netcdf.rb

Instance Attribute Summary collapse

Attributes inherited from NCObject

#attributes

Instance Method Summary collapse

Methods included from NC

nc_decode, nc_put_att_simple, nc_put_var_all

Methods inherited from NCObject

#attribute, #get_attributes

Constructor Details

#initialize(ncfile, var_id) ⇒ NCVar

Returns a new instance of NCVar.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/io/netcdf.rb', line 70

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
end

Instance Attribute Details

#dimsObject (readonly)

Returns the value of attribute dims.



83
84
85
# File 'lib/io/netcdf.rb', line 83

def dims
  @dims
end

#nameObject (readonly)

Returns the value of attribute name.



83
84
85
# File 'lib/io/netcdf.rb', line 83

def name
  @name
end

Instance Method Details

#[](*argv) ⇒ Object



142
143
144
# File 'lib/io/netcdf.rb', line 142

def [] (*argv)
  return get!(*argv)
end

#decode(value) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/io/netcdf.rb', line 105

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

#definitionObject



85
86
87
88
89
90
# File 'lib/io/netcdf.rb', line 85

def definition
  {
    type: @vartype,
    dim: dims.map{|x| x.name }
  }.update(@attributes)
end

#get(*argv) ⇒ Object



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/io/netcdf.rb', line 146

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
    start = []
    count = []
    stride = []
    info.index.each do |idx|
      case idx
      when Array
        start << idx[0]
        count << idx[1]
        stride << idx[2]
      else
        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
  when CA_REG_SELECT, CA_REG_GRID
    out = get_var[*argv]
  else
    raise "invalid index"
  end
  case out
  when CArray
    return out.compact
  else
    return out
  end
end

#get!(*argv) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/io/netcdf.rb', line 202

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_varObject



220
221
222
# File 'lib/io/netcdf.rb', line 220

def get_var ()
  return nc_get_var(@file_id, @var_id)
end

#get_var!Object



224
225
226
# File 'lib/io/netcdf.rb', line 224

def get_var! ()
  return decode(nc_get_var(@file_id, @var_id))
end

#get_var1(*index) ⇒ Object



212
213
214
# File 'lib/io/netcdf.rb', line 212

def get_var1 (*index)
  return nc_get_var1(@file_id, @var_id, index)
end

#get_var1!(*index) ⇒ Object



216
217
218
# File 'lib/io/netcdf.rb', line 216

def get_var1! (*index)
  return decode(get_var1(*index))
end

#get_vara(start, count) ⇒ Object



228
229
230
# File 'lib/io/netcdf.rb', line 228

def get_vara (start, count)
  return nc_get_vara(@file_id, @var_id, start, count)
end

#get_vara!(start, count) ⇒ Object



232
233
234
# File 'lib/io/netcdf.rb', line 232

def get_vara! (start, count)
  return decode(nc_get_vara(@file_id, @var_id, start, count))
end

#get_varm(start, count, stride, imap) ⇒ Object



244
245
246
# File 'lib/io/netcdf.rb', line 244

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



248
249
250
# File 'lib/io/netcdf.rb', line 248

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



236
237
238
# File 'lib/io/netcdf.rb', line 236

def get_vars (start, count, stride)
  return nc_get_vars(@file_id, @var_id, start, count, stride)
end

#get_vars!(start, count, stride) ⇒ Object



240
241
242
# File 'lib/io/netcdf.rb', line 240

def get_vars! (start, count, stride)
  return decode(nc_get_vars(@file_id, @var_id, start, count, stride))
end

#inspectObject



92
93
94
# File 'lib/io/netcdf.rb', line 92

def inspect
  return "#{@name}#{@dims}"
end

#is_dim?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
# File 'lib/io/netcdf.rb', line 96

def is_dim? 
  begin
    nc_inq_dimlen(@file_id, @var_id)
    return true
  rescue RuntimeError
    return false
  end
end

#to_caObject



138
139
140
# File 'lib/io/netcdf.rb', line 138

def to_ca
  return self[]
end