Class: NumRu::VArrayNetCDF

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

Constant Summary collapse

@@deflation_level =

< NetCDF4 deflation (compression) settings for variable creation >

nil
@@deflate_all =
false
@@shuffle =
false

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(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

.deflate_allObject



194
195
196
# File 'lib/numru/gphys/varraynetcdf.rb', line 194

def deflate_all
  @@deflate_all
end

.deflate_all=(t_or_f) ⇒ Object

If false, @@deflation_level is applied to variables of 2D or with a greater rank. If false, @@deflation_level is applied even to 1D variables.



191
192
193
# File 'lib/numru/gphys/varraynetcdf.rb', line 191

def deflate_all=(t_or_f)
  @@deflate_all = t_or_f
end

.deflation_levelObject

Must be an integer from 0 to 9 (usually 1 is good). Use 0 to use only the shuffle filter.



183
184
185
# File 'lib/numru/gphys/varraynetcdf.rb', line 183

def deflation_level
  @@deflation_level
end

.deflation_level=(level) ⇒ Object

if integer, use deflation for NetCDF-4



178
179
180
181
182
# File 'lib/numru/gphys/varraynetcdf.rb', line 178

def deflation_level=(level)
  @@deflation_level = level   # Must be an integer from 0 to 9
                              # (usually 1 is good).
                              # Use 0 to use only the shuffle filter.
end

.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
166
167
168
169
170
171
# 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 ( @@deflation_level &&
        ## file.respond_to?(:format) &&  # supported by ruby-netcdf 0.7.2 or later
        file.format >= NetCDF::NC_FORMAT_NETCDF4 &&
        (dimensions.length >=2 || @@deflate_all) )
     file.var(name).deflate(@@deflation_level, @@shuffle)
   end
   if vary
	      vary.att_names.each{|name| va.set_att(name, vary.get_att(name))}
	    end 
   va
end

.shuffleObject



203
204
205
# File 'lib/numru/gphys/varraynetcdf.rb', line 203

def shuffle
  @@shuffle
end

.shuffle=(t_or_f) ⇒ Object

Use the shuffle filter for integer variables. Effective only when deflation_level is non-nil.



200
201
202
# File 'lib/numru/gphys/varraynetcdf.rb', line 200

def shuffle=(t_or_f)
  @@shuffle = t_or_f
end

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

< additional class methods > ##

Raises:

  • (ArgumentError)


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/numru/gphys/varraynetcdf.rb', line 209

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 > ##



336
337
338
# File 'lib/numru/gphys/varraynetcdf.rb', line 336

def dim_names
	 @ary.dim_names
end

#fileObject



330
331
332
# File 'lib/numru/gphys/varraynetcdf.rb', line 330

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



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

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

#ntypeObject



310
311
312
# File 'lib/numru/gphys/varraynetcdf.rb', line 310

def ntype
	@ary.ntype
end

#rankObject



321
322
323
# File 'lib/numru/gphys/varraynetcdf.rb', line 321

def rank
	shape_current.length
end

#rename(nm) ⇒ Object



306
307
308
# File 'lib/numru/gphys/varraynetcdf.rb', line 306

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

#rename!(nm) ⇒ Object



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

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

#rename_no_file_change(nm) ⇒ Object



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

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:



289
290
291
# File 'lib/numru/gphys/varraynetcdf.rb', line 289

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

#shape_currentObject



342
343
344
# File 'lib/numru/gphys/varraynetcdf.rb', line 342

def shape_current
	 @ary.shape_current
end

#shape_ul0Object



339
340
341
# File 'lib/numru/gphys/varraynetcdf.rb', line 339

def shape_ul0
	 @ary.shape_ul0
end

#totalObject Also known as: length



314
315
316
317
318
# File 'lib/numru/gphys/varraynetcdf.rb', line 314

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

#valObject

< redefined instance methods > ##



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/numru/gphys/varraynetcdf.rb', line 256

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/numru/gphys/varraynetcdf.rb', line 269

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