Class: NumRu::NetCDF

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

Constant Summary collapse

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



116
117
118
119
120
121
122
123
124
# File 'lib/netcdf.rb', line 116

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/netcdf.rb', line 93

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/netcdf.rb', line 128

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/netcdf.rb', line 40

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/netcdf.rb', line 23

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

Returns:

  • (Boolean)


17
18
19
# File 'lib/netcdf.rb', line 17

def NetCDF.nc4?
  @@nc4
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/netcdf.rb', line 54

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



249
250
251
252
253
254
255
256
257
# File 'lib/netcdf.rb', line 249

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/netcdf.rb', line 158

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



229
230
231
232
233
234
235
236
237
# File 'lib/netcdf.rb', line 229

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/netcdf.rb', line 207

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



199
200
201
202
203
204
205
# File 'lib/netcdf.rb', line 199

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:



183
184
185
186
187
188
189
# File 'lib/netcdf.rb', line 183

def each_dim
  num_dim=ndims()    
  for dimid in 0..num_dim-1
    obj_Dim=id2dim(dimid)
    yield(obj_Dim)
  end
end

#each_varObject



191
192
193
194
195
196
197
# File 'lib/netcdf.rb', line 191

def each_var
  num_var=nvars()
  for varid in 0..num_var-1
    obj_Var=id2var(varid)
    yield(obj_Var)
  end
end

#inspectObject



259
260
261
# File 'lib/netcdf.rb', line 259

def inspect
  "NetCDF:"+path
end

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



154
155
156
# File 'lib/netcdf.rb', line 154

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

#var_namesObject



239
240
241
242
243
244
245
246
247
# File 'lib/netcdf.rb', line 239

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



218
219
220
221
222
223
224
225
226
227
# File 'lib/netcdf.rb', line 218

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