class AgwxGrids::Grid

Constants

DAILY
HOURLY

Attributes

mD[R]
period[R]
xDim[R]
yDim[R]

Public Class Methods

nearest(coord,start,incr) click to toggle source

Find the nearest index to the given coord (params in real space, return is a zero-based index)

# File lib/agwx_grids/grid.rb, line 163
def self.nearest(coord,start,incr)
  # convert coord to a lower-bound index (i.e. truncating integer conversion)
  trunc = ((coord - start)/incr).to_i
  # Does "trunc" the index of the closest real-space coord, or the next one up?
  coord - (start + incr*trunc) > incr / 2.0 ? trunc + 1 : trunc
end
new(path,period) click to toggle source
# File lib/agwx_grids/grid.rb, line 145
def initialize(path,period)
  @layers = {}
  File.open(path) do |gridFile|
    @mD = GridMetaData.new(gridFile)
    if @mD == nil
      raise "nil metadata"
    end
    @xDim = @mD.xDim
    @yDim = @mD.yDim
    @mD.zStart.step(@mD.zEnd,@mD.zIncr) do |layer_index|
      if (!(gridFile.eof) && (layer = GridLayer.new(gridFile,@mD)))
        @layers[layer.zIndex] = layer
      end
    end
  end
end

Public Instance Methods

at_by_index(longitude_index,latitude_index) click to toggle source
# File lib/agwx_grids/grid.rb, line 228
def at_by_index(longitude_index,latitude_index)
  @layers.inject({}) do |hash,arr|
    doy = arr[0]
    layer = arr[1]
    hash.merge doy => layer.get(longitude_index,latitude_index)
  end
end
at_by_long_lat(long,lat) click to toggle source
# File lib/agwx_grids/grid.rb, line 236
def at_by_long_lat(long,lat)
  @layers.keys.inject({}) do |hash,doy|
    hash.merge doy => get(long,lat,doy)
  end
end
each() { |layer| ... } click to toggle source
# File lib/agwx_grids/grid.rb, line 220
def each
  @layers.each { |layer| yield layer }
end
each_value(lat,long) { |nil| ... } click to toggle source
# File lib/agwx_grids/grid.rb, line 205
def each_value(lat,long)
  # note switch here -- grids do longitude as X
  realToIndex(long,lat,0)
  @layers.keys.sort.each do |doy|
    layer = @layers[doy]
    if layer == nil
        yield nil
    else
      val = layer.get(@my_ii,@my_jj)
      val = nil if val == mD.badVal
      yield val
    end
  end 
end
each_with_doy() { |layer(doy),doy| ... } click to toggle source
# File lib/agwx_grids/grid.rb, line 224
def each_with_doy
  @layers.keys.sort.each {|doy| yield [layer(doy),doy]}
end
get(x,y,z) click to toggle source
# File lib/agwx_grids/grid.rb, line 180
def get(x,y,z)
  # puts "get #{x},#{y},#{z}"
  # puts "xStart=#{@mD.xStart}, yStart=#{@mD.yStart}, zStart=#{@mD.zStart}"
  realToIndex(x,y,z)
  # puts "@my_ii=#{@my_ii}, @my_jj=#{@my_jj}, @my_doy=#{@my_doy}"
  # puts "#{@layers[@my_doy]}"
  if @layers[@my_doy] == nil
      nil
  else
      val = @layers[@my_doy].get(@my_ii,@my_jj)
      val == mD.badVal ? nil : val
  end
end
get_by_index(longitude_index,latitude_index,doy) click to toggle source

Grids are stored from low to high latitude, so as you look at the text file, it’s mirrored vertically. The first line in a given grid layer is the LOWEST latitude, the last is the highest.

# File lib/agwx_grids/grid.rb, line 196
def get_by_index(longitude_index,latitude_index,doy)
  if @layers[doy]
    val = @layers[doy].get(longitude_index,latitude_index)
    val == mD.badVal ? nil : val
  else
    nil
  end
end
latitude_for(index) click to toggle source
# File lib/agwx_grids/grid.rb, line 250
def latitude_for(index)
  mD.yStart + (index * mD.yIncr)
end
layer(doy) click to toggle source
# File lib/agwx_grids/grid.rb, line 246
def layer(doy)
  @layers[doy]
end
layer_list() click to toggle source
# File lib/agwx_grids/grid.rb, line 242
def layer_list
  @layers.keys
end
longitude_for(index) click to toggle source
# File lib/agwx_grids/grid.rb, line 254
def longitude_for(index)
  mD.xStart + (index * mD.xIncr)
end
realToIndex(x,y,z) click to toggle source

Convert coordinates in real XY space to indices. Note that Z is sort-of a real-space coord, but represents a quantized there-or-not value like a DOY, not a scalar.

# File lib/agwx_grids/grid.rb, line 172
def realToIndex(x,y,z) 
  @my_ii = Grid.nearest(x,@mD.xStart,@mD.xIncr)
  @my_jj = Grid.nearest(y,@mD.yStart,@mD.yIncr)
  @my_doy = z
  # puts "realToIndex: x #{x}, xStart #{@mD.xStart}, xIncr #{@mD.xIncr} myX #{@my_ii}; y #{y}, myY #{@my_jj} z #{z}, myZ #{@my_doy}"
  [@my_ii,@my_jj,@my_doy]
end