Method: NumRu::VArrayNetCDF.write

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

.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