Class: NumRu::NetCDF

Inherits:
Object
  • Object
show all
Defined in:
lib/netcdf.rb

Constant Summary

Max_Try =
100
NCVERSION =
NetCDF.libvers
@@nc4 =
false
@@cr_format =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_tmpfile(path) ⇒ Object



127
128
129
130
131
132
133
134
135
# 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

.create(filename, noclobber = false, share = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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_formatObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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?Boolean



28
29
30
# File 'lib/netcdf.rb', line 28

def NetCDF.nc4?
  @@nc4
end

.open(filename, mode = "r", share = false) ⇒ Object Also known as: new



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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

Instance Method Details

#att_namesObject



260
261
262
263
264
265
266
267
268
# 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) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# 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_namesObject



240
241
242
243
244
245
246
247
248
# 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) ⇒ Object

return all if names==nil



218
219
220
221
222
223
224
225
226
227
# 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_attObject



210
211
212
213
214
215
216
# 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_dimObject

Iterators:



194
195
196
197
198
199
200
# 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_varObject



202
203
204
205
206
207
208
# 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

#inspectObject



270
271
272
# File 'lib/netcdf.rb', line 270

def inspect
  "NetCDF:"+path
end

#put_att(attname, val, atttype = nil) ⇒ Object



165
166
167
# File 'lib/netcdf.rb', line 165

def put_att(attname,val,atttype=nil)
   put_attraw(attname,val,atttype)
end

#var_namesObject



250
251
252
253
254
255
256
257
258
# 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) ⇒ Object

return all if names==nil



229
230
231
232
233
234
235
236
237
238
# 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