Class: GrADS::Gridded::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/grads/gridded.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ctl) ⇒ Variable

Returns a new instance of Variable.



45
46
47
48
49
50
51
52
53
# File 'lib/grads/gridded.rb', line 45

def initialize (name, ctl)
  @name      = name
  @dim       = ctl.vardims[name]
  @tsize     = ctl.tsize
  @dset      = ctl.dset
  @chunksize = ctl.chunksize
  @offset    = ctl.varoffsets[name]
  @undef     = ctl.undef
end

Instance Attribute Details

#dimObject (readonly)

Returns the value of attribute dim.



55
56
57
# File 'lib/grads/gridded.rb', line 55

def dim
  @dim
end

#dsetObject (readonly)

Returns the value of attribute dset.



55
56
57
# File 'lib/grads/gridded.rb', line 55

def dset
  @dset
end

#nameObject (readonly)

Returns the value of attribute name.



55
56
57
# File 'lib/grads/gridded.rb', line 55

def name
  @name
end

#offsetObject (readonly)

Returns the value of attribute offset.



55
56
57
# File 'lib/grads/gridded.rb', line 55

def offset
  @offset
end

#sizeObject (readonly)

Returns the value of attribute size.



55
56
57
# File 'lib/grads/gridded.rb', line 55

def size
  @size
end

Instance Method Details

#[](*argv) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/grads/gridded.rb', line 84

def [] (*argv)
  trng = argv.shift
  buf = CArray.float(*@dim)      
  out = nil
  open(@dset) { |io|
    if tsize(trng) == 1
      each_trange(trng) { |t|
        io.seek(t * @chunksize + offset)        
        buf.load_binary(io)
        out = buf[*argv]
      }
    else
      outlist = []
      i = 0
      bind_ok = false
      each_trange(trng) { |t|
        t * @chunksize + offset
        io.seek(t * @chunksize + offset)
        buf.load_binary(io)      
        obj = buf[*argv].to_ca
        if obj.is_a?(CArray)
          outlist << obj
        else
          outlist << CA_FLOAT(obj)
          bind_ok = true
        end
        i += 1
      }
      if bind_ok
        out = CArray.bind(:float, outlist, 0)
      else
        out = CArray.merge(:float, outlist, 0)
      end
    end
  }
  out[:eq, @undef] = UNDEF
  return out
end

#each_trange(trng = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/grads/gridded.rb', line 57

def each_trange (trng=nil)
  case trng
  when NilClass, FalseClass
    (0...@tsize).each do |t|
      yield(t)
    end
  when Range
    trng.each do |t|
      yield(t)
    end
  else
    t = trng
    yield(t)
  end
end

#to_caObject



123
124
125
# File 'lib/grads/gridded.rb', line 123

def to_ca
  return self[]
end

#tsize(trng = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/grads/gridded.rb', line 73

def tsize (trng=nil)
  case trng
  when NilClass, FalseClass
    return @tsize
  when Range
    return trng.to_a.size
  else
    return 1
  end
end