Parent

Class/Module Index [+]

Quicksearch

NumRu::NetCDF

Constants

Max_Try

Public Class Methods

create(filename,noclobber=false,share=false) click to toggle source
# File lib/numru/netcdf.rb, line 49
def NetCDF.create(filename,noclobber=false,share=false)
  case(noclobber)
  when false
    noclobber=NC_CLOBBER
  when true
    noclobber=NC_NOCLOBBER
  else
    raise NetcdfError,"noclobber (2nd argument) must be true or false"
  end
  case(share)
  when false
    share=0
  when true
    share=NC_SHARE
  else
    raise NetcdfError,"share (3rd argument) must be true or false"
  end
  
  cmode=noclobber | share
  nc_create(filename,cmode)
end
create_tmp(tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', share=false) click to toggle source
# File lib/numru/netcdf.rb, line 84
def NetCDF.create_tmp(tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', 
                      share=false)
   basename = 'temp'
   if $SAFE > 0 and tmpdir.tainted?
      tmpdir = '.'
   end

   n = 0
   while true
     begin
       tmpname = sprintf('%s/%s%d_%d.nc', tmpdir, basename, $$, n)
       unless File.exist?(tmpname)
          netcdf = NetCDF.create(tmpname, true, share)
          ObjectSpace.define_finalizer(netcdf, 
                                       NetCDF.clean_tmpfile(tmpname))
          break
       end
     rescue
       raise NetcdfError, "cannot generate tempfile `%s'" % tmpname if n >= Max_Try
     end
     n += 1
   end
   netcdf
end
new(filename,mode="r",share=false) click to toggle source
Alias for: open
open(filename,mode="r",share=false) click to toggle source
# File lib/numru/netcdf.rb, line 10
def NetCDF.open(filename,mode="r",share=false)
   call_create=false   # false-> nc_open; true->nc_create
   case(mode)
   when "r","rb"                          # read only
      mode=NC_NOWRITE
   when "w","w+","wb","w+b"               # overwrite if exits
      call_create=true
      mode=NC_CLOBBER
   when "a","a+","r+","ab","a+b","r+b"    # append if exits
      if( File.exists?(filename) )
         mode=NC_WRITE
      else
         call_create=true   #(nonexsitent --> create)
         mode=NC_CLOBBER
      end
   else
      raise NetcdfError, "Mode #{mode} is not supported"
   end
   case(share)
   when false
      share=0
   when true
      share=NC_SHARE
   else
      raise NetcdfError, "We can't use the sharing mode you typed"
   end
   omode = mode | share
   if(!call_create)
      nc_open(filename,omode)
   else
      nc_create(filename,omode)
   end
end
Also aliased as: new, new

Protected Class Methods

clean_tmpfile(path) click to toggle source
# File lib/numru/netcdf.rb, line 72
def clean_tmpfile(path)
   proc {
      print "removing ", path, "..." if $DEBUG
      if File.exist?(path)
         File.unlink(path) 
      end
      print "done\n" if $DEBUG
   }
end

Public Instance Methods

att_names() click to toggle source
# File lib/numru/netcdf.rb, line 205
def att_names
  num_att=natts()
  names=[]
  for attnum in 0..num_att-1
    obj_Att=id2att(attnum)    
    names=names+[obj_Att.name]
  end
  return names
end
def_var_with_dim(name, vartype, shape_ul0, dimnames) click to toggle source
# File lib/numru/netcdf.rb, line 114
def def_var_with_dim(name, vartype, shape_ul0, dimnames)
   # Same as def_var but defines dimensions first if needed.
   # Use zero in shape to define an unlimited dimension.
   if (shape_ul0.length != dimnames.length ) then
      raise ArgumentError, 'lengths of shape and dimnames do not agree'
   end
   dims = []
   dimnames.each_index{ |i|
      dim = self.dim( dimnames[i] )
      if ( dim != nil ) then
         # dim exists --> check the length
         if (shape_ul0[i] != dim.length_ul0 ) then
            raise ArgumentError, "dimension length do not agree: #{i}th dim: "+                 "#{shape_ul0[i]} and #{dim.length_ul0}"
         end
         dims.push(dim)
      else
         # dim does not exist --> define it
         dims.push( def_dim( dimnames[i], shape_ul0[i] ) )
      end
   }
   def_var(name, vartype, dims)
end
dim_names() click to toggle source
# File lib/numru/netcdf.rb, line 185
def dim_names
  num_dim=ndims()
  names=[]
  for dimid in 0..num_dim-1
    obj_Dim=id2dim(dimid)    
    names=names+[obj_Dim.name]
  end
  return names
end
dims( names=nil ) click to toggle source
# File lib/numru/netcdf.rb, line 163
def dims( names=nil )   # return all if names==nil
   if names == nil
      dims = (0..ndims()-1).collect{|dimid| id2dim(dimid)}
   else
      raise TypeError, "names is not an array" if ! names.is_a?(Array)
      dims = names.collect{|name| dim(name)}
      raise ArgumentError, "One or more dimensions do not exist" if dims.include?(nil)
   end
   dims
end
each_att() click to toggle source
# File lib/numru/netcdf.rb, line 155
def each_att
  num_att=natts()
  for attnum in 0..num_att-1
    obj_Att=id2att(attnum)
     yield(obj_Att)
  end
end
each_dim() click to toggle source

Iterators:

# File lib/numru/netcdf.rb, line 139
def each_dim
  num_dim=ndims()    
  for dimid in 0..num_dim-1
    obj_Dim=id2dim(dimid)
     yield(obj_Dim)
  end
end
each_var() click to toggle source
# File lib/numru/netcdf.rb, line 147
def each_var
  num_var=nvars()
  for varid in 0..num_var-1
    obj_Var=id2var(varid)
    yield(obj_Var)
  end
end
inspect() click to toggle source
# File lib/numru/netcdf.rb, line 215
def inspect
  "NetCDF:"+path
end
put_att(attname,val,atttype=nil) click to toggle source
# File lib/numru/netcdf.rb, line 110
def put_att(attname,val,atttype=nil)
   put_attraw(attname,val,atttype)
end
var_names() click to toggle source
# File lib/numru/netcdf.rb, line 195
def var_names
  num_var=nvars()
  names=[]
  for varid in 0..num_var-1
    obj_Var=id2var(varid)
    names=names+[obj_Var.name]
  end
  return names
end
vars( names=nil ) click to toggle source
# File lib/numru/netcdf.rb, line 174
def vars( names=nil )   # return all if names==nil
   if names == nil
      vars = (0..nvars()-1).collect{ |varid| id2var(varid) }
   else
      raise TypeError, "names is not an array" if ! names.is_a?(Array)
      vars = names.collect{|name| var(name)}
      raise ArgumentError, "One or more variables do not exist" if vars.include?(nil)
   end
   vars
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.