class NumRu::NetCDF
Public Class Methods
create(filename,noclobber=false,share=false)
click to toggle source
# File lib/netcdf.rb, line 104 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 | @@cr_format nc_create(filename,cmode) end
create_tmp(tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', share=false)
click to toggle source
# File lib/netcdf.rb, line 139 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
creation_format()
click to toggle source
# File lib/netcdf.rb, line 51 def NetCDF.creation_format raise("This method is available only for NetCDF >= 4") unless @@nc4 case @@cr_format when 0 "TRADITIONAL" when NC_64BIT_OFFSET "64BIT_OFFSET" when NC_NETCDF4 "NETCDF4" when NC_NETCDF4 | NC_CLASSIC_MODEL "NETCDF4_CLASSIC" end end
creation_format=(cmode)
click to toggle source
# File lib/netcdf.rb, line 34 def NetCDF.creation_format=(cmode) raise("This method is available only for NetCDF >= 4") unless @@nc4 case cmode when 0, nil, NC_CLASSIC_MODEL, /^CLASSIC$/i # classic netcdf ver 3 fmt @@cr_format = 0 when NC_64BIT_OFFSET, /^64BIT_OFFSET$/i @@cr_format = NC_64BIT_OFFSET when NC_NETCDF4, /^NETCDF4$/i @@cr_format = NC_NETCDF4 when ( NC_NETCDF4 | NC_CLASSIC_MODEL), /^NETCDF4_CLASSIC$/i # NetCDF4 but disabling new data models @@cr_format = NC_NETCDF4 | NC_CLASSIC_MODEL else raise ArgumentError, "Unsupported creation mode: #{cmod.to_s}" end end
nc4?()
click to toggle source
# File lib/netcdf.rb, line 28 def NetCDF.nc4? @@nc4 end
open(filename,mode="r",share=false)
click to toggle source
# File lib/netcdf.rb, line 65 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/netcdf.rb, line 127 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/netcdf.rb, line 260 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/netcdf.rb, line 169 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/netcdf.rb, line 240 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/netcdf.rb, line 218 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() { |obj_Att| ... }
click to toggle source
# File lib/netcdf.rb, line 210 def each_att num_att=natts() for attnum in 0..num_att-1 obj_Att=id2att(attnum) yield(obj_Att) end end
each_dim() { |obj_Dim| ... }
click to toggle source
Iterators:
# File lib/netcdf.rb, line 194 def each_dim num_dim=ndims() for dimid in 0..num_dim-1 obj_Dim=id2dim(dimid) yield(obj_Dim) end end
each_var() { |obj_Var| ... }
click to toggle source
# File lib/netcdf.rb, line 202 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/netcdf.rb, line 270 def inspect "NetCDF:"+path end
put_att(attname,val,atttype=nil)
click to toggle source
# File lib/netcdf.rb, line 165 def put_att(attname,val,atttype=nil) put_attraw(attname,val,atttype) end
var_names()
click to toggle source
# File lib/netcdf.rb, line 250 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/netcdf.rb, line 229 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