Module: NumRu::GPhys::Grib_IO

Defined in:
lib/numru/gphys/gphys_grib_io.rb

Class Method Summary collapse

Class Method Details

.is_a_Grib?(filename) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/numru/gphys/gphys_grib_io.rb', line 74

def is_a_Grib?(filename)
  VArrayGrib.grib.is_a_Grib?(filename)
end

.open(file, varname) ⇒ Object



78
79
80
81
82
83
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/numru/gphys/gphys_grib_io.rb', line 78

def open(file, varname)
  if file.is_a?(String)
    file = VArrayGrib.grib.open(file)
  elsif ! VArrayGrib.grib===file
    raise ArgumentError, "1st arg must be a Grib or a file name"
  end

  var = file.var(varname)
  var.nil? && raise("variable '#{varname}' not found in '#{file.path}'")
  data = VArrayGrib.new(var)

  rank = data.rank
  bare_index = [ false ] * rank # will be true if coord var is not found


  axes = Array.new
  var_names = file.var_names
  for i in 0...rank
    dim = var.dim(i)
    val = dim.get
    if Array===val && val.length!=1
      bare_index[i] = true
    end
    axpos = VArrayGrib.new( dim )
    cell_center = true
    cell = false

    axis = Axis.new(cell,bare_index[i])
    if !cell
      axis.set_pos( axpos )
    else
      if cell_center
        if varray_cell_bounds
          axis.set_cell(axpos, varray_cell_bounds).set_pos_to_center
        else
          p "cell bounds are guessed"
          axis.set_cell_guess_bounds(axpos).set_pos_to_center
        end
      else  # then it is cell_bounds
        if varray_cell_center
          axis.set_cell(varray_cell_center, axpos).set_pos_to_bounds
        else
          p "cell center is guessed"
          axis.set_cell_guess_center(axpos).set_pos_to_bounds
        end
      end
    end
    if Array===val && val.length!=1
      val[1..-1].each{|hash|
        va = 
 va = VArray.new(hash["value"],nil,hash["name"])
 hash.each{|k,v|
		next if k=="value"||k=="name"
		va.put_att(k,v)
        }
        axis.set_aux(hash["name"],va)
      }
    end
    
    #p "yet-to-be-defined: method to define aux coord vars"
    
    axes[i] = axis
  end

  grid = Grid.new( *axes )

  GPhys.new(grid,data)
end

.var_names(file) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/numru/gphys/gphys_grib_io.rb', line 156

def var_names(file)
  opened = false
  case file
  when String
    file = VArrayGrib.grib.open(file)
  when VArrayGrib.grib
    opened = true
  else
    raise ArgumentError, "arg must be a Grib or a file name"
  end
  var_names = file.var_names
  file.close unless opened
  return var_names
end

.var_names_except_coordinates(file) ⇒ Object



170
171
172
# File 'lib/numru/gphys/gphys_grib_io.rb', line 170

def var_names_except_coordinates(file)
	var_names(file)
end

.write(file, gphys, name_dummy = nil) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/numru/gphys/gphys_grib_io.rb', line 147

def write(file, gphys, name_dummy=nil)
  dims = Array.new
  gphys.rank.times{|n|
    dims[n] = gphys.coord(n)
  }
  VArrayGrib.write(file,gphys.data,dims)
  nil
end