Class: NumRu::GribVar

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/grib/grib.rb,
ext/grib.c

Overview

class GribMessage

Constant Summary collapse

Day0 =
DateTime.new(1900)
MAXNDIM =

lon, lat, lev, t, step, timeInterval, ensemble

7

Instance Method Summary collapse

Instance Method Details

#att(key) ⇒ Object



177
178
179
# File 'lib/numru/grib/grib.rb', line 177

def att(key)
  @attr[key]
end

#att_namesObject



180
181
182
# File 'lib/numru/grib/grib.rb', line 180

def att_names
  @attr.keys
end

#def_dim(name, index) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/numru/grib/grib.rb', line 164

def def_dim(name,index)
  d = ::NumRu::GribDim.new(self,name)
  if index == -1
    @dims.push(d)
  else
    @dims[index] = d
  end
  return d
end

#dim(index) ⇒ Object



159
160
161
162
163
# File 'lib/numru/grib/grib.rb', line 159

def dim(index)
  index = dim_names.index(index) if String===index
  return nil if index.nil?
  @dims[index]
end

#dim_namesObject



153
154
155
# File 'lib/numru/grib/grib.rb', line 153

def dim_names
  @dims.collect{|d| d.name}
end

#fileObject

NumRu::GribVar#file() -> NumRu::Grib



542
543
544
545
546
547
548
# File 'ext/grib.c', line 542

static VALUE
rg_var_file(VALUE self)
{
  rg_var *var;
  Data_Get_Struct(self, rg_var, var);
  return var->file;
}

#get(*indices) ⇒ Object Also known as: [], val



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
270
271
272
273
274
275
# File 'lib/numru/grib/grib.rb', line 193

def get(*indices)
  sha = shape
  mask = nil
  first = Array.new(rank-@xy_dims,0)
  if indices.length != 0
    if indices.include?(false)
      sha2 = sha.dup
      sha2.delete(false)
      raise ArgumentError, 'multiple "false" in indices' if sha.length - sha2.length > 1
      indices[indices.index(false)] = [true]*(sha.length-indices.length+1)
      indices.flatten!
    end

    rank.times{|n|
      ind = indices[n]
      case ind
      when true
        indices[n] = 0..sha[n]-1
      when Fixnum
        sha[n] = 1
      when Range
        f = ind.first
        e = ind.end
        e = sha[n]-1 if e==-1
        e -= 1 if ind.exclude_end?
        sha[n] = e-f+1
        indices[n] = f..e
      else
        raise "invalid indices"
      end
    }
    if rank > @xy_dims
      mask = NArray.byte(*shape[@xy_dims..-1])
      mask[*indices[@xy_dims..-1]] = 1
      (rank-@xy_dims).times do |i|
        ind = indices[@xy_dims+i]
        case ind
        when Fixnum
          first[i] = ind
        when Range
          first[i] = ind.first
          first[i] += shape[@xy_dims+i] if first[i] < 0
        end
      end
    end
  end

  shape2 = shape.dup
  sha2 = sha.dup
  @del_dims.each do |i|
    shape2.insert(i,1)
    sha2.insert(i,1)
    first.insert(i-@xy_dims,0)
  end
  mask.reshape!(*shape2[@xy_dims..-1]) if mask
  
  value = missing_value ? NArrayMiss.sfloat(*sha2) : NArray.sfloat(*sha2)
  index = Array.new(MAXNDIM-2)
  @msgs.each_with_index do |msg,i|
    idx = @idx[i]
    next if (mask && mask[*idx]==0)
    val = msg.get_value("values")
    if @xy_dims > 0
      val.reshape!(*shape[0...@xy_dims])
      #        val = msg.get_data[2].reshape!(*shape[0...@xy_dims])
      unless indices.length==0 || indices[0...@xy_dims].inject(true){|t,v| t &&= v==true}
        val = val.slice(*indices[0...@xy_dims])
      end
    end
    (MAXNDIM-2).times do |i| index[i] = idx[i]-first[i] end
    value[*(Array.new(@xy_dims,true)+index)] = val
  end
  ns = sha.length
  ns.times do |ii|
    i = ns-ii-1
    sha.delete_at(i) if indices[i].kind_of?(Fixnum)
  end
  value.reshape!(*sha)
  if missing_value
    value.set_mask value.get_array!.ne(missing_value)
  end
  return value
end

#get_messagesObject

NumRu::GribVar#get_messages() -> Array



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'ext/grib.c', line 564

static VALUE
rg_var_get_messages(VALUE self)
{
  rg_var *var;
  Data_Get_Struct(self, rg_var, var);
  var_list *var_list = var->var_list;
  size_t len = var_list->ary->len;
  VALUE ary = rb_ary_new2(len);
  rg_message *message;
  VALUE rmessage;
  int i;
  for (i=0; i<len; i++) {
    rmessage = message_alloc(cMessage);
    Data_Get_Struct(rmessage, rg_message, message);
    message->file = self;
    message->handle = get_msg(var_list->ary, i);
    rb_ary_store(ary, i, rmessage);
  }
  return ary;

}

#inspectObject



278
279
280
# File 'lib/numru/grib/grib.rb', line 278

def inspect
  "GribVar: #{name} in #{file.path}, [#{shape.join(",")}]"
end

#missing_valueObject



190
191
192
# File 'lib/numru/grib/grib.rb', line 190

def missing_value
  @msgs[0].missing_value
end

#nameObject

NumRu::GribVar#name() -> String



553
554
555
556
557
558
559
# File 'ext/grib.c', line 553

static VALUE
rg_var_name(VALUE self)
{
  rg_var *var;
  Data_Get_Struct(self, rg_var, var);
  return rb_str_new2(var->var_list->vname);
}

#put_att(key, val) ⇒ Object Also known as: set_att



173
174
175
# File 'lib/numru/grib/grib.rb', line 173

def put_att(key,val)
  @attr[key] = val
end

#rankObject Also known as: ndims



146
147
148
# File 'lib/numru/grib/grib.rb', line 146

def rank
  @dims.length
end

#shapeObject



156
157
158
# File 'lib/numru/grib/grib.rb', line 156

def shape
  @dims.collect{|d| d.length}
end

#totalObject



150
151
152
# File 'lib/numru/grib/grib.rb', line 150

def total
  @dims.inject(1){|t,d| t*d.length}
end

#typecodeObject



183
184
185
186
187
188
189
# File 'lib/numru/grib/grib.rb', line 183

def typecode
  if missing_value
    NArrayMiss::FLOAT
  else
    NArray::FLOAT
  end
end