Class | NumRu::NetCDF |
In: |
lib/netcdf.rb
|
Parent: | Object |
Max_Try | = | 100 |
open | -> | new |
# File lib/netcdf.rb, line 49 49: def NetCDF.create(filename,noclobber=false,share=false) 50: case(noclobber) 51: when false 52: noclobber=NC_CLOBBER 53: when true 54: noclobber=NC_NOCLOBBER 55: else 56: raise NetcdfError,"noclobber (2nd argument) must be true or false" 57: end 58: case(share) 59: when false 60: share=0 61: when true 62: share=NC_SHARE 63: else 64: raise NetcdfError,"share (3rd argument) must be true or false" 65: end 66: 67: cmode=noclobber | share 68: nc_create(filename,cmode) 69: end
# File lib/netcdf.rb, line 84 84: def NetCDF.create_tmp(tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', 85: share=false) 86: basename = 'temp' 87: if $SAFE > 0 and tmpdir.tainted? 88: tmpdir = '.' 89: end 90: 91: n = 0 92: while true 93: begin 94: tmpname = sprintf('%s/%s%d_%d.nc', tmpdir, basename, $$, n) 95: unless File.exist?(tmpname) 96: netcdf = NetCDF.create(tmpname, true, share) 97: ObjectSpace.define_finalizer(netcdf, 98: NetCDF.clean_tmpfile(tmpname)) 99: break 100: end 101: rescue 102: raise NetcdfError, "cannot generate tempfile `%s'" % tmpname if n >= Max_Try 103: end 104: n += 1 105: end 106: netcdf 107: end
# File lib/netcdf.rb, line 10 10: def NetCDF.open(filename,mode="r",share=false) 11: call_create=false # false-> nc_open; true->nc_create 12: case(mode) 13: when "r","rb" # read only 14: mode=NC_NOWRITE 15: when "w","w+","wb","w+b" # overwrite if exits 16: call_create=true 17: mode=NC_CLOBBER 18: when "a","a+","r+","ab","a+b","r+b" # append if exits 19: if( File.exists?(filename) ) 20: mode=NC_WRITE 21: else 22: call_create=true #(nonexsitent --> create) 23: mode=NC_CLOBBER 24: end 25: else 26: raise NetcdfError, "Mode #{mode} is not supported" 27: end 28: case(share) 29: when false 30: share=0 31: when true 32: share=NC_SHARE 33: else 34: raise NetcdfError, "We can't use the sharing mode you typed" 35: end 36: omode = mode | share 37: if(!call_create) 38: nc_open(filename,omode) 39: else 40: nc_create(filename,omode) 41: end 42: end
# File lib/netcdf.rb, line 72 72: def clean_tmpfile(path) 73: proc { 74: print "removing ", path, "..." if $DEBUG 75: if File.exist?(path) 76: File.unlink(path) 77: end 78: print "done\n" if $DEBUG 79: } 80: end
# File lib/netcdf.rb, line 205 205: def att_names 206: num_att=natts() 207: names=[] 208: for attnum in 0..num_att-1 209: obj_Att=id2att(attnum) 210: names=names+[obj_Att.name] 211: end 212: return names 213: end
# File lib/netcdf.rb, line 114 114: def def_var_with_dim(name, vartype, shape_ul0, dimnames) 115: # Same as def_var but defines dimensions first if needed. 116: # Use zero in shape to define an unlimited dimension. 117: if (shape_ul0.length != dimnames.length ) then 118: raise ArgumentError, 'lengths of shape and dimnames do not agree' 119: end 120: dims = [] 121: dimnames.each_index{ |i| 122: dim = self.dim( dimnames[i] ) 123: if ( dim != nil ) then 124: # dim exists --> check the length 125: if (shape_ul0[i] != dim.length_ul0 ) then 126: raise ArgumentError, "dimension length do not agree: #{i}th dim: "+\ 127: "#{shape_ul0[i]} and #{dim.length_ul0}" 128: end 129: dims.push(dim) 130: else 131: # dim does not exist --> define it 132: dims.push( def_dim( dimnames[i], shape_ul0[i] ) ) 133: end 134: } 135: def_var(name, vartype, dims) 136: end
# File lib/netcdf.rb, line 185 185: def dim_names 186: num_dim=ndims() 187: names=[] 188: for dimid in 0..num_dim-1 189: obj_Dim=id2dim(dimid) 190: names=names+[obj_Dim.name] 191: end 192: return names 193: end
# File lib/netcdf.rb, line 163 163: def dims( names=nil ) # return all if names==nil 164: if names == nil 165: dims = (0..ndims()-1).collect{|dimid| id2dim(dimid)} 166: else 167: raise TypeError, "names is not an array" if ! names.is_a?(Array) 168: dims = names.collect{|name| dim(name)} 169: raise ArgumentError, "One or more dimensions do not exist" if dims.include?(nil) 170: end 171: dims 172: end
# File lib/netcdf.rb, line 155 155: def each_att 156: num_att=natts() 157: for attnum in 0..num_att-1 158: obj_Att=id2att(attnum) 159: yield(obj_Att) 160: end 161: end
Iterators:
# File lib/netcdf.rb, line 139 139: def each_dim 140: num_dim=ndims() 141: for dimid in 0..num_dim-1 142: obj_Dim=id2dim(dimid) 143: yield(obj_Dim) 144: end 145: end
# File lib/netcdf.rb, line 147 147: def each_var 148: num_var=nvars() 149: for varid in 0..num_var-1 150: obj_Var=id2var(varid) 151: yield(obj_Var) 152: end 153: end
# File lib/netcdf.rb, line 110 110: def put_att(attname,val,atttype=nil) 111: put_attraw(attname,val,atttype) 112: end
# File lib/netcdf.rb, line 195 195: def var_names 196: num_var=nvars() 197: names=[] 198: for varid in 0..num_var-1 199: obj_Var=id2var(varid) 200: names=names+[obj_Var.name] 201: end 202: return names 203: end
# File lib/netcdf.rb, line 174 174: def vars( names=nil ) # return all if names==nil 175: if names == nil 176: vars = (0..nvars()-1).collect{ |varid| id2var(varid) } 177: else 178: raise TypeError, "names is not an array" if ! names.is_a?(Array) 179: vars = names.collect{|name| var(name)} 180: raise ArgumentError, "One or more variables do not exist" if vars.include?(nil) 181: end 182: vars 183: end