Class: NumRu::NetCDF
- Inherits:
-
Object
- Object
- NumRu::NetCDF
- Defined in:
- lib/netcdf.rb
Constant Summary collapse
- Max_Try =
100
Class Method Summary collapse
- .clean_tmpfile(path) ⇒ Object
- .create(filename, noclobber = false, share = false) ⇒ Object
- .create_tmp(tmpdir = ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', share = false) ⇒ Object
- .open(filename, mode = "r", share = false) ⇒ Object (also: new)
Instance Method Summary collapse
- #att_names ⇒ Object
- #def_var_with_dim(name, vartype, shape_ul0, dimnames) ⇒ Object
- #dim_names ⇒ Object
-
#dims(names = nil) ⇒ Object
return all if names==nil.
- #each_att ⇒ Object
-
#each_dim ⇒ Object
Iterators:.
- #each_var ⇒ Object
- #inspect ⇒ Object
- #put_att(attname, val, atttype = nil) ⇒ Object
- #var_names ⇒ Object
-
#vars(names = nil) ⇒ Object
return all if names==nil.
Class Method Details
.clean_tmpfile(path) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/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 |
.create(filename, noclobber = false, share = false) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/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 |
.open(filename, mode = "r", share = false) ⇒ Object Also known as: new
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/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 |
Instance Method Details
#att_names ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/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) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/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 ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/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) ⇒ Object
return all if names==nil
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/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 ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/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 ⇒ Object
Iterators:
139 140 141 142 143 144 145 |
# File 'lib/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 ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/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 ⇒ Object
215 216 217 |
# File 'lib/netcdf.rb', line 215 def inspect "NetCDF:"+path end |
#put_att(attname, val, atttype = nil) ⇒ Object
110 111 112 |
# File 'lib/netcdf.rb', line 110 def put_att(attname,val,atttype=nil) put_attraw(attname,val,atttype) end |
#var_names ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/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) ⇒ Object
return all if names==nil
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/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 |