Class: NumRu::VArrayNetCDF

Inherits:
VArray
  • Object
show all
Defined in:
lib/numru/gphys/varraynetcdf.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, #transpose, #units, #units=

Constructor Details

#initialize(aNetCDFVar) ⇒ VArrayNetCDF

< initialization redefined > ##

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/numru/gphys/varraynetcdf.rb', line 131

def initialize(aNetCDFVar)
	 @name = aNetCDFVar.name
	 @mapping = nil
	 @varray = nil
	 raise ArgumentError,"Not a NetCDVAr" if ! aNetCDFVar.is_a?(NetCDFVar)
	 @ary = aNetCDFVar
	 @ary.extend(NetCDFVarDeferred)
	 @ary.file.extend(NetCDFDeferred)
	 @attr = AttributeNetCDF.new(aNetCDFVar)
	 @convention = NetCDF_Conventions.find(aNetCDFVar.file)
	 extend( @convention::VArray_Mixin )
end

Class Method Details

.new2(file, name, ntype, dimensions, vary = nil) ⇒ Object Also known as: def_var

< redefined class methods > ##



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/numru/gphys/varraynetcdf.rb', line 151

def new2(file, name, ntype, dimensions, vary=nil)
	   dimensions = dimensions.collect{|dim| 
	     if dim.is_a?(String)
	       # specification by name is available for existing dimensions
	       file.dim(dim) || raise("dimension "+dim+" is not in "+file.path)
	     else
	       dim
	     end
	   }
	    va = new( file.def_var(name, ntype, dimensions) )
   if vary
	      vary.att_names.each{|name| va.set_att(name, vary.get_att(name))}
	    end 
   va
end

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

< additional class methods > ##

Raises:

  • (ArgumentError)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/numru/gphys/varraynetcdf.rb', line 171

def write(file, vary, rename=nil, dimnames=nil)
   raise ArgumentError, "1st arg: not a NetCDF" if !file.is_a?(NetCDF)
   raise ArgumentError, "2nd arg: not a VArray" if !vary.is_a?(VArray)
   rank=vary.rank
   if dimnames == nil
      if vary.is_a?(VArrayNetCDF)
  dimnames = vary.dim_names
      else
  dimnames=Array.new
  for i in 0...rank
     dimnames[i]='x'+i.to_s
  end
      end
   elsif( rank != dimnames.length)
      raise ArgumentError, 
                  "# of dim names does not agree with the rank of the VArray"
   end
   fdimnms = file.dim_names
   begin
      shape = vary.shape
   rescue StandardError, NameError
      shape = vary.shape_ul0
   end
   dims = Array.new
   mode_switched = file.redef
   for i in 0...rank
      nm = dimnames[i]
      if fdimnms.include?(nm)
  dims[i] = dm = file.dim(nm)
  if dm.length != shape[i] && shape[i] != 0 && dm.length != 0
     raise "Length of the dimension #{nm} is #{dims[i].length}, while the #{i}-th dimension of the VArray #{vary.name} is #{shape[i]}"
                end
      else
  dims[i] = file.def_dim(nm,shape[i])
      end
   end
   nm = ( rename || vary.name )
   val = vary.val
   newvary = new2(file, nm, vary.typecode, dims, vary)
   newvary.val = val
   if  mode_switched; file.enddef; end
   newvary
end

Instance Method Details

#dim_namesObject

< additional instance methods > ##



298
299
300
# File 'lib/numru/gphys/varraynetcdf.rb', line 298

def dim_names
	 @ary.dim_names
end

#fileObject



292
293
294
# File 'lib/numru/gphys/varraynetcdf.rb', line 292

def file
	 @ary.file
end

#inspectObject



144
145
146
# File 'lib/numru/gphys/varraynetcdf.rb', line 144

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

#name=(nm) ⇒ Object



255
256
257
258
# File 'lib/numru/gphys/varraynetcdf.rb', line 255

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

#ntypeObject



272
273
274
# File 'lib/numru/gphys/varraynetcdf.rb', line 272

def ntype
	@ary.ntype
end

#rankObject



283
284
285
# File 'lib/numru/gphys/varraynetcdf.rb', line 283

def rank
	shape_current.length
end

#rename(nm) ⇒ Object



268
269
270
# File 'lib/numru/gphys/varraynetcdf.rb', line 268

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

#rename!(nm) ⇒ Object



259
260
261
262
263
# File 'lib/numru/gphys/varraynetcdf.rb', line 259

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

#rename_no_file_change(nm) ⇒ Object



264
265
266
267
# File 'lib/numru/gphys/varraynetcdf.rb', line 264

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:



251
252
253
# File 'lib/numru/gphys/varraynetcdf.rb', line 251

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

#shape_currentObject



304
305
306
# File 'lib/numru/gphys/varraynetcdf.rb', line 304

def shape_current
	 @ary.shape_current
end

#shape_ul0Object



301
302
303
# File 'lib/numru/gphys/varraynetcdf.rb', line 301

def shape_ul0
	 @ary.shape_ul0
end

#totalObject Also known as: length



276
277
278
279
280
# File 'lib/numru/gphys/varraynetcdf.rb', line 276

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

#valObject

< redefined instance methods > ##



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/numru/gphys/varraynetcdf.rb', line 218

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

#val=(narray) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/numru/gphys/varraynetcdf.rb', line 231

def val=(narray)
	if narray.is_a?(Numeric)
	  @ary.put( narray )
	else
	  if shape_ul0.include?(0)
	    # has unlimited dimension
	    narray = __check_ary_class(narray)
	    slicer = (0...rank).collect{|i|
 (shape_ul0[i] != 0) ? true : 0...narray.shape[i]
	    }
	    @ary[*slicer] = narray
	  else
	    @ary.put( __check_ary_class(narray) )
	  end
	  narray
	end
end