Class: NumRu::VArrayGrADS

Inherits:
VArray
  • Object
show all
Defined in:
lib/numru/gphys/varraygrads.rb

Constant Summary

Constants inherited from VArray

NumRu::VArray::Binary_operators, NumRu::VArray::Binary_operatorsL, NumRu::VArray::Binary_operatorsL_comp, NumRu::VArray::Binary_operatorsL_other, NumRu::VArray::Binary_operators_Uconv, NumRu::VArray::Binary_operators_Unone, NumRu::VArray::Binary_operators_Uop, NumRu::VArray::Math_funcs, NumRu::VArray::Math_funcs_nondim, NumRu::VArray::Math_funcs_radian, NumRu::VArray::NArray_type1_methods, NumRu::VArray::NArray_type2_methods, NumRu::VArray::NArray_type2_methods1, NumRu::VArray::NArray_type2_methods2, NumRu::VArray::NArray_type2_methods3, NumRu::VArray::NArray_type3_methods, NumRu::VArray::Unary_operators

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VArray

#+@, #[], #[]=, #att_names, #attr_copy, #axis_cyclic?, #axis_cyclic_extendible?, #axis_draw_positive, #axis_modulo, #bin_mean, #bin_sum, #coerce, #convert_units, #copy, #del_att, #get_att, #long_name, #long_name=, #mapped?, #marshal_dump, #marshal_load, #name, #replace_val, #reshape, #reshape!, #set_att, #sqrt, #to_g1D, #transpose, #units, #units=

Constructor Details

#initialize(aGrADSVar) ⇒ VArrayGrADS

< initialization redefined > ##

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/numru/gphys/varraygrads.rb', line 15

def initialize(aGrADSVar)
   @name = aGrADSVar.name
   @mapping = nil
   @varray = nil
   raise ArgumentError,"Not a GrADSVar" if ! aGrADSVar.is_a?(GrADSVar)
   @ary = aGrADSVar
   @attr = aGrADSVar.attr
end

Class Method Details

.new2(file, name, ntype, dimensions, attr = nil) ⇒ Object

< redefined class methods > ##



32
33
34
35
36
# File 'lib/numru/gphys/varraygrads.rb', line 32

def new2(file, name, ntype, dimensions, attr=nil)
   va = new( file.def_var(name, ntype, dimensions) )
   if attr; attr.each{|key,val| va.attr[key]=val}; end 
   va
end

.write_binary(file, vary, rename = nil, dimnames = nil) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
# File 'lib/numru/gphys/varraygrads.rb', line 106

def write_binary(file, vary, rename=nil, dimnames=nil)
   raise ArgumentError, "1st arg: not a GrADS_Gridded" if !file.is_a?(GrADS_Gridded)
   raise ArgumentError, "2nd arg: not a VArray" if !vary.is_a?(VArray)
   rank=vary.rank
   raise ArgumentError, "only 4D data is supported" if rank != 4

   file.put(vary.val)
end

.write_control(file, gphys) ⇒ Object

< additional class methods > ##

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
# File 'lib/numru/gphys/varraygrads.rb', line 40

def write_control(file, gphys)
  raise ArgumentError, "1st arg: not a GrADS_Gridded" if !file.is_a?(GrADS_Gridded)
  rank = gphys.data.rank
  raise ArgumentError, "only 4D data is supported" if rank != 4

  for i in 0..2 # lon, lat, lev
    c = ""
    if   ( i==1 && (gphys.coord(i).length > 1) && 
        (gphys.coord(i).val[1]-gphys.coord(i).val[0] < 0) )
      file.put_att("yrev",true)
      gphys.axis(i).pos.val[-1..0].each{|j| c += (j.to_s + " ")}
    elsif( i==2 && (gphys.coord(i).length > 1) && 
        (gphys.coord(i)[1].val-gphys.coord(i)[0].val > 0) )
      file.put_att("zrev",true)
      gphys.axis(i).pos.val[-1..0].each{|j| c += (j.to_s + " ")}
    else
      gphys.axis(i).pos.val.each{|j| c += (j.to_s + " ")}
    end
    file.dimensions[i] = {
      :len=>gphys.axis(i).pos.length, 
      :flag=> "LEVELS", 
      :spec=> c.gsub(/(-*\d+\.\d+ ){5,5}/,'\0'+"\n     ")
    }
  end

  i = 3 # time
    sunits = gphys.coord(i).get_att("units")
    /(.*) *since *(.*)/ =~ sunits
    if (!$1 or !$2)
      raise("Units mismatch. Requires time units that includes 'since'")
    end
    increment_units = $1
    startdatetime = DateTime.parse($2)

    if( gphys.coord(i).length > 1 )
      increment = gphys.coord(i)[1].val - gphys.coord(i)[0].val
    else
      increment = 1.0 # anything is possible
    end

    file.dimensions[i] = {
      :len=>gphys.axis(i).pos.length, 
      :flag=> "LINEAR", 
      :startdatetime=>startdatetime, 
      :increment=>increment, 
      :increment_units=>increment_units
    }

  nlev1 = gphys.coord(2).length
  if( nlev = gphys.data.get_att("nlev") )
    nlev = nlev.to_i
    if( (nlev1 != nlev) && !(nlev1==1 && nlev==0) )
      raise "inconsistent attribute nlev (#{nlev} and #{nlev1})"
    end
  else
    nlev = nlev1
  end
  file.def_var(gphys.name, nlev, 99, gphys.data.get_att("long_name") )

  ctlfile = File.open(file.path,"w")
  ctlfile << file.to_ctl
  ctlfile.close

end

Instance Method Details

#dim_namesObject

< additional instance methods > ##



176
177
178
# File 'lib/numru/gphys/varraygrads.rb', line 176

def dim_names
   @ary.dim_names
end

#fileObject



185
186
187
# File 'lib/numru/gphys/varraygrads.rb', line 185

def file
   @ary.file
end

#inspectObject



24
25
26
27
# File 'lib/numru/gphys/varraygrads.rb', line 24

def inspect
#         "<'#{@name}' in '#{@ary.file.path}'  #{@ary.ntype}#{shape_current.inspect}>"
   "<'#{@name}'  #{shape_current.inspect}>"
end

#name=(nm) ⇒ Object



136
137
138
139
# File 'lib/numru/gphys/varraygrads.rb', line 136

def name=(nm)
   @ary.name = nm
   @name = nm
end

#ntypeObject



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

def ntype
  @ary.ntype
end

#rankObject



164
165
166
# File 'lib/numru/gphys/varraygrads.rb', line 164

def rank
  shape_current.length
end

#rename(nm) ⇒ Object



149
150
151
# File 'lib/numru/gphys/varraygrads.rb', line 149

def rename(nm)
  self.dup.rename_no_file_change(nm)
end

#rename!(nm) ⇒ Object



140
141
142
143
144
# File 'lib/numru/gphys/varraygrads.rb', line 140

def rename!(nm)
   @ary.name = nm
   @name = nm
   self
end

#rename_no_file_change(nm) ⇒ Object



145
146
147
148
# File 'lib/numru/gphys/varraygrads.rb', line 145

def rename_no_file_change(nm)
   @name = nm
   self
end

#shapeObject

It is safer not to have the method “shape” to avoid misconfusion of shape_ul0 and shape_current:



132
133
134
# File 'lib/numru/gphys/varraygrads.rb', line 132

def shape
   raise "The shape method is not available. Use shape_ul0 or shape_current instead."
end

#shape_currentObject



182
183
184
# File 'lib/numru/gphys/varraygrads.rb', line 182

def shape_current
   @ary.shape_current
end

#shape_ul0Object



179
180
181
# File 'lib/numru/gphys/varraygrads.rb', line 179

def shape_ul0
   @ary.shape_ul0
end

#totalObject Also known as: length



157
158
159
160
161
# File 'lib/numru/gphys/varraygrads.rb', line 157

def total
   len = 1
   @ary.shape_current.each{|i| len *= i}
   len
end

#valObject

< redefined instance methods > ##



118
119
120
121
122
123
# File 'lib/numru/gphys/varraygrads.rb', line 118

def val
#         mode_switched = @ary.file.enddef
   v = @ary.get
#         if mode_switched; @ary.file.redef; end
#         v
end

#val=(narray) ⇒ Object



125
126
127
128
# File 'lib/numru/gphys/varraygrads.rb', line 125

def val=(narray)
   @ary.put( __check_ary_class2(narray) )
   narray
end