lib/numru/gphys/varraygrib.rb in gphys-1.1.1 vs lib/numru/gphys/varraygrib.rb in gphys-1.2.2

- old
+ new

@@ -2,10 +2,18 @@ =Status * only reading =end require "numru/gphys/grib" + +### to be made below in the definition of VArrayGrib in order to record +### whetehr its successfull or not +#begin +# require "numru/grib" +#rescue LoadError +#end + require "numru/gphys/varray" module NumRu =begin @@ -14,23 +22,67 @@ ---new(GribVar) ==Methods =end class VArrayGrib < VArray + begin + require "numru/grib" + @@rbgribloaded = true + rescue LoadError + @@rbgribloaded = false + end + + @@GribLib = :gphys_grib + + class << self + def use_gphys_grib + @@GribLib = :gphys_grib + set_grib_lib + end + def use_rb_grib + if !@@rbgribloaded + raise LoadError, "Loading rb-GRIB has been failed. You probably need to install it." + end + @@GribLib = :rb_grib + set_grib_lib + end + def grib_lib + @@GribLib.dup + end + def grib + @@Grib + end + def set_grib_lib + case @@GribLib + when :gphys_grib + @@GribDim = GPhys::GribDim + @@GribVar = GPhys::GribVar + @@Grib = GPhys::Grib + when :rb_grib + @@GribDim = GribDim + @@GribVar = GribVar + @@Grib = Grib + end + end + end + set_grib_lib + def initialize(aGribVar) - if !GribVar===aGribVar && !GribDim===aGribVar + if !@@GribVar===aGribVar && !@@GribDim===aGribVar raise ArgumentError,"Not a GribVar or GribDim" end @name = aGribVar.name @mapping = nil @varray = nil @ary = aGribVar @attr = Attribute.new aGribVar.att_names.each{|name| - @attr[name] = aGribVar.att(name) + val = aGribVar.att(name) + val = [val] if val.is_a?(Numeric) + @attr[name] = val } - if GribDim===@ary + if @@GribDim===@ary class << @ary def rank return 1 end def shape @@ -39,13 +91,13 @@ end end end def inspect - if GribVar===@ary + if @@GribVar===@ary "<'#{@name}' in '#{@ary.file.path}' [#{@ary.shape.join(", ")}]>" - elsif GribDim===@ary + elsif @@GribDim===@ary "<'#{@name}' in '#{@ary.var.name}' #{@ary.length}>" end end class << self @@ -53,11 +105,11 @@ def new2(file, name, dims, vary) v = file.def_var(name) dims.length.times{|n| d = dims[n] - if GribDim===d + if @@GribDim===d gd = v.def_dim(d.name,n) gd.put(d.get) d.att_names.each{|name| gd.put_att(name,d.att(name)) } elsif VArray===d gd = v.def_dim(d.name,n) @@ -78,11 +130,11 @@ alias def_var new2 ## < additional class methods > ## def write(file,vary,dims=nil) - Grib===file || raise(ArgumentError,"1st arg: not a Grib") + @@Grib===file || raise(ArgumentError,"1st arg: not a Grib") VArray===vary || raise(ArgumentError,"2st arg: not a VArray") rank = vary.rank if !dims dims = vary.dim_names if VArrayGrib===vary @@ -155,11 +207,11 @@ if $0 == __FILE__ $DEBUG = true include NumRu - grib = Grib.open("../../../testdata/T.jan.grib") + grib = VArrayGrib.grib.open("../../../testdata/T.jan.grib") gv = grib.var("TMP") va = VArrayGrib.new(gv) p va.dim_names p va.shape_ul0 @@ -171,7 +223,17 @@ va2 = va[3..9,5..15,0] p va2.shape p va2.val + + begin + VArrayGrib.use_rb_grib + grib = VArrayGrib.grib.open("../../../testdata/T.jan.grib") + p grib.var_names + gv = grib.var("t") + p gv.val + rescue LoadError + print "...error rescued & test skipped\n\n" + end end