ruby-netcdf 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/doc/to_html ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/csh
2
+ nkf -e Ref_man_jp.rd | rd2 | \
3
+ sed -e 's/<dt>/<dt><h4>/' -e 's/<\/dt>/<\/h4><\/dt>/' \
4
+ >! Ref_man_jp.html
5
+ rd2 Ref_man.rd | \
6
+ sed -e 's/<dt>/<dt><h4>/' -e 's/<\/dt>/<\/h4><\/dt>/' \
7
+ >! Ref_man.html
data/extconf.rb ADDED
@@ -0,0 +1,137 @@
1
+ require "mkmf"
2
+
3
+ hoge = Gem::GemPathSearcher.new.find("narray")
4
+ narray_fullname = hoge.full_name
5
+ narray_installpath = hoge.installation_path
6
+ narray_include = "#{narray_installpath}/gems/#{narray_fullname}/"
7
+ narray_lib = "#{narray_installpath}/gems/#{narray_fullname}/"
8
+
9
+ dir_config('narray',narray_include,narray_lib)
10
+ dir_config('netcdf','/usr/local')
11
+
12
+ if ( ! ( have_header("narray.h") && have_header("narray_config.h") ) ) then
13
+ print <<EOS
14
+ ** configure error **
15
+ Header narray.h or narray_config.h is not found. If you have these files in
16
+ /narraydir/include, try the following:
17
+
18
+ % ruby extconf.rb --with-narray-include=/narraydir/include
19
+
20
+ EOS
21
+ exit(-1)
22
+ end
23
+
24
+ if xsystem("ncdap-config --libs")
25
+ libncdods = "nc-dap"
26
+ cflags = `ncdap-config --cflags`.gsub(/\n/, " ")
27
+ libs = `ncdap-config --libs`.gsub(/\n/, " ")
28
+ prefix_dods = `ncdap-config --prefix`.gsub(/\n/, "")
29
+ elsif xsystem("opendap-config --libs")
30
+ libncdods = "nc-dods"
31
+ cflags = `opendap-config --cflags`.gsub(/\n/, " ")
32
+ libs = `opendap-config --libs-nc`.gsub(/\n/, " ")
33
+ prefix_dods = `opendap-config --prefix`.gsub(/\n/, "")
34
+ end
35
+
36
+ if File.directory?("/usr/include/netcdf") #-- for Vine linux
37
+ cflags = "-I/usr/include/netcdf"
38
+ libs = "-L/usr/lib"
39
+ $CFLAGS += ' ' + cflags
40
+ $LOCAL_LIBS += ' ' + libs
41
+ end
42
+
43
+ if (enable_config('opendap',true) && ( xsystem("opendap-config --libs") ||
44
+ xsystem("ncdap-config --libs") ) )
45
+
46
+ dir_config(libncdods,prefix_dods)
47
+
48
+ if (!have_library(libncdods))
49
+ print <<-EOS
50
+ ** ERROR ** Library not found: nc-dods (OPeNDAP/DODS-enabled NetCDF lib)
51
+ Install it, or run extconf.rb with option --disable-opendap.
52
+ ^^^^^^^^^^^^^^^^^
53
+ EOS
54
+ exit(-1)
55
+ else
56
+ print <<-EOS
57
+ ** Message ** Compiling with OPeNDAP/DODS-enabled NetCDF library.
58
+
59
+ This is because the command opendap-config is found in your system.
60
+ If you want to use the ordinary (non-DODS) version of NetCDF,
61
+ run extconf.rb with option --disable-opendap.
62
+ ^^^^^^^^^^^^^^^^^
63
+ EOS
64
+ end
65
+
66
+ $CFLAGS += ' '+cflags
67
+ $LOCAL_LIBS += ' ' + libs
68
+
69
+ # non portable treatments: should be improved (by Horinouchi)
70
+ CONFIG['LDSHARED'].sub!(/gcc/,'g++')
71
+ $LIBS.sub!(/-lc\s/,'') ; $LIBS.sub!(/-lc$/,'')
72
+ print <<-EOS
73
+ ** Warning ** non-portable treatments are made,
74
+ which was sucessfull redhat linux 9:
75
+ * gcc was replaced with g++ in CONFIG['LDSHARED']
76
+ * -lc library was removed if in $LIBS
77
+
78
+ EOS
79
+ # p '@@@'
80
+ # ary = []
81
+ # CONFIG.each{|k,v| ary.push([k,v])}
82
+ # ary.sort.each{|x| p x}
83
+ else
84
+ if xsystem("nc-config --libs") # for NetCDF 4
85
+ cflags = `nc-config --cflags`.gsub(/\n/, " ")
86
+ libs = `nc-config --libs`.gsub(/\n/, " ")
87
+ prefix_nc = `nc-config --prefix`.gsub(/\n/, "")
88
+
89
+ dir_config("netcdf",prefix_nc)
90
+ $CFLAGS += ' ' + cflags
91
+ $LOCAL_LIBS += ' ' + libs
92
+ end
93
+ if ( ! ( have_header("netcdf.h") && have_library("netcdf") ) )then
94
+ print <<-EOS
95
+ ** configure error **
96
+ Header netcdf.h or the compiled netcdf library is not found.
97
+ If you have the library installed under /netcdfdir (that is, netcdf.h is
98
+ in /netcdfdir/include and the library in /netcdfdir/lib/),
99
+ try the following:
100
+
101
+ % ruby extconf.rb --with-netcdf-dir=/netcdfdir
102
+
103
+ Alternatively, you can specify the two directory separately
104
+ with --with-netcdf-include and --with-netcdf-lib.
105
+ EOS
106
+ exit(-1)
107
+ end
108
+ end
109
+
110
+ if /cygwin|mingw/ =~ RUBY_PLATFORM
111
+ have_library("narray") || raise("ERROR: narray library is not found")
112
+ end
113
+
114
+ create_makefile "numru/netcdfraw"
115
+
116
+ ###### Modify Makefile: #######
117
+ File.rename("Makefile","Makefile.orig")
118
+ oldmkfl = File.open("Makefile.orig")
119
+ newmkfl = File.open("Makefile","w")
120
+ oldmkfl.each_line{ |line|
121
+ case(line)
122
+ when /^distclean:/
123
+ newmkfl.puts(line)
124
+ newmkfl.puts("\t\t@$(RM) *.nc demo/*.nc demo/*~ lib/*~ doc/*~ test/*.nc test/*~ Makefile.orig")
125
+ when /^all:/
126
+ newmkfl.puts(line)
127
+ newmkfl.puts("")
128
+ newmkfl.puts("test: all") # insert the "test" target
129
+ newmkfl.puts("\t\t@cd test && ruby test.rb && echo 'test did not fail :-p (please ignore the warnings)' && cd ..")
130
+ when /lib\/netcdf/
131
+ line = line.chomp! + "/"
132
+ newmkfl.puts(line)
133
+ else
134
+ newmkfl.puts(line)
135
+ end
136
+ }
137
+ newmkfl.close
data/extconf.rb.orig ADDED
@@ -0,0 +1,124 @@
1
+ require "mkmf"
2
+
3
+ dir_config('narray',$sitearchdir,$sitearchdir)
4
+ dir_config('netcdf','/usr/local')
5
+
6
+ if ( ! ( have_header("narray.h") && have_header("narray_config.h") ) ) then
7
+ print <<EOS
8
+ ** configure error **
9
+ Header narray.h or narray_config.h is not found. If you have these files in
10
+ /narraydir/include, try the following:
11
+
12
+ % ruby extconf.rb --with-narray-include=/narraydir/include
13
+
14
+ EOS
15
+ exit(-1)
16
+ end
17
+
18
+ if xsystem("ncdap-config --libs")
19
+ libncdods = "nc-dap"
20
+ cflags = `ncdap-config --cflags`.gsub(/\n/, " ")
21
+ libs = `ncdap-config --libs`.gsub(/\n/, " ")
22
+ prefix_dods = `ncdap-config --prefix`.gsub(/\n/, "")
23
+ elsif xsystem("opendap-config --libs")
24
+ libncdods = "nc-dods"
25
+ cflags = `opendap-config --cflags`.gsub(/\n/, " ")
26
+ libs = `opendap-config --libs-nc`.gsub(/\n/, " ")
27
+ prefix_dods = `opendap-config --prefix`.gsub(/\n/, "")
28
+ end
29
+
30
+ if (enable_config('opendap',true) && ( xsystem("opendap-config --libs") ||
31
+ xsystem("ncdap-config --libs") ) )
32
+
33
+ dir_config(libncdods,prefix_dods)
34
+
35
+ if (!have_library(libncdods))
36
+ print <<-EOS
37
+ ** ERROR ** Library not found: nc-dods (OPeNDAP/DODS-enabled NetCDF lib)
38
+ Install it, or run extconf.rb with option --disable-opendap.
39
+ ^^^^^^^^^^^^^^^^^
40
+ EOS
41
+ exit(-1)
42
+ else
43
+ print <<-EOS
44
+ ** Message ** Compiling with OPeNDAP/DODS-enabled NetCDF library.
45
+
46
+ This is because the command opendap-config is found in your system.
47
+ If you want to use the ordinary (non-DODS) version of NetCDF,
48
+ run extconf.rb with option --disable-opendap.
49
+ ^^^^^^^^^^^^^^^^^
50
+ EOS
51
+ end
52
+
53
+ $CFLAGS += ' '+cflags
54
+ $LOCAL_LIBS += ' ' + libs
55
+
56
+ # non portable treatments: should be improved (by Horinouchi)
57
+ CONFIG['LDSHARED'].sub!(/gcc/,'g++')
58
+ $LIBS.sub!(/-lc\s/,'') ; $LIBS.sub!(/-lc$/,'')
59
+ print <<-EOS
60
+ ** Warning ** non-portable treatments are made,
61
+ which was sucessfull redhat linux 9:
62
+ * gcc was replaced with g++ in CONFIG['LDSHARED']
63
+ * -lc library was removed if in $LIBS
64
+
65
+ EOS
66
+ # p '@@@'
67
+ # ary = []
68
+ # CONFIG.each{|k,v| ary.push([k,v])}
69
+ # ary.sort.each{|x| p x}
70
+ else
71
+ if xsystem("nc-config --libs") # for NetCDF 4
72
+ cflags = `nc-config --cflags`.gsub(/\n/, " ")
73
+ libs = `nc-config --libs`.gsub(/\n/, " ")
74
+ prefix_nc = `nc-config --prefix`.gsub(/\n/, "")
75
+
76
+ dir_config("netcdf",prefix_nc)
77
+ $CFLAGS += ' ' + cflags
78
+ $LOCAL_LIBS += ' ' + libs
79
+ end
80
+ if ( ! ( have_header("netcdf.h") && have_library("netcdf") ) )then
81
+ print <<-EOS
82
+ ** configure error **
83
+ Header netcdf.h or the compiled netcdf library is not found.
84
+ If you have the library installed under /netcdfdir (that is, netcdf.h is
85
+ in /netcdfdir/include and the library in /netcdfdir/lib/),
86
+ try the following:
87
+
88
+ % ruby extconf.rb --with-netcdf-dir=/netcdfdir
89
+
90
+ Alternatively, you can specify the two directory separately
91
+ with --with-netcdf-include and --with-netcdf-lib.
92
+ EOS
93
+ exit(-1)
94
+ end
95
+ end
96
+
97
+ if /cygwin|mingw/ =~ RUBY_PLATFORM
98
+ have_library("narray") || raise("ERROR: narray library is not found")
99
+ end
100
+
101
+ create_makefile "numru/netcdfraw"
102
+
103
+ ###### Modify Makefile: #######
104
+ File.rename("Makefile","Makefile.orig")
105
+ oldmkfl = File.open("Makefile.orig")
106
+ newmkfl = File.open("Makefile","w")
107
+ oldmkfl.each_line{ |line|
108
+ case(line)
109
+ when /^distclean:/
110
+ newmkfl.puts(line)
111
+ newmkfl.puts("\t\t@$(RM) *.nc demo/*.nc demo/*~ lib/*~ doc/*~ test/*.nc test/*~ Makefile.orig")
112
+ when /^all:/
113
+ newmkfl.puts(line)
114
+ newmkfl.puts("")
115
+ newmkfl.puts("test: all") # insert the "test" target
116
+ newmkfl.puts("\t\t@cd test && ruby test.rb && echo 'test did not fail :-p (please ignore the warnings)' && cd ..")
117
+ when /lib\/netcdf/
118
+ line = line.chomp! + "/"
119
+ newmkfl.puts(line)
120
+ else
121
+ newmkfl.puts(line)
122
+ end
123
+ }
124
+ newmkfl.close
data/extconf.rb.rej ADDED
@@ -0,0 +1,16 @@
1
+ --- extconf.rb 2011-02-04 13:30:55.000000000 +0900
2
+ +++ extconf.rb 2011-02-14 15:57:48.000000000 +0900
3
+ @@ -33,6 +39,13 @@
4
+ prefix_dods = `opendap-config --prefix`.gsub(/\n/, "")
5
+ end
6
+
7
+ +if File.directory?("/usr/include/netcdf") #-- for Vine linux
8
+ + cflags = "-I/usr/include/netcdf"
9
+ + libs = "-L/usr/lib"
10
+ + $CFLAGS += ' ' + cflags
11
+ + $LOCAL_LIBS += ' ' + libs
12
+ +end
13
+ +
14
+ if (enable_config('opendap',true) && ( xsystem("opendap-config --libs")) ||
15
+ xsystem("ncdap-config --libs") )
16
+
data/lib/netcdf.rb ADDED
@@ -0,0 +1,791 @@
1
+ require 'narray'
2
+ require 'numru/netcdfraw'
3
+
4
+ # NetCDF$B%/%i%9$K4X$7$F(B
5
+ module NumRu
6
+ class NetCDF
7
+
8
+ Max_Try = 100
9
+
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
43
+
44
+ class << NetCDF
45
+ alias new open
46
+ end
47
+
48
+
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
70
+
71
+ class << NetCDF
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
81
+ protected :clean_tmpfile
82
+ end
83
+
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
108
+
109
+
110
+ def put_att(attname,val,atttype=nil)
111
+ put_attraw(attname,val,atttype)
112
+ end
113
+
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
137
+
138
+ # Iterators:
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
146
+
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
154
+
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
162
+
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
173
+
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
184
+
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
194
+
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
204
+
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
214
+
215
+ def inspect
216
+ "NetCDF:"+path
217
+ end
218
+
219
+ end
220
+
221
+ #NetCDFVar class $B$K4X$7$F(B
222
+ class NetCDFVar
223
+
224
+ class << NetCDFVar
225
+ def new(file,varname,mode="r",share=false)
226
+ if(file.is_a?(String))
227
+ file = NetCDF.open(file,mode,share)
228
+ elsif(!file.is_a?(NetCDF))
229
+ raise TypeError, "1st arg must be a NetCDF (file object) or a String (path)"
230
+ end
231
+ file.var(varname)
232
+ end
233
+
234
+ alias open new
235
+ end
236
+
237
+ alias :rank :ndims
238
+
239
+ def each_att
240
+ num_att=natts()
241
+ for attnum in 0..num_att-1
242
+ obj_Att=id2att(attnum)
243
+ yield(obj_Att)
244
+ end
245
+ end
246
+
247
+ def dim_names
248
+ ary = Array.new()
249
+ dims.each{|dim| ary.push(dim.name)}
250
+ ary
251
+ end
252
+
253
+ def att_names
254
+ num_att=natts()
255
+ names=[]
256
+ for attnum in 0..num_att-1
257
+ obj_Att=id2att(attnum)
258
+ names=names+[obj_Att.name]
259
+ end
260
+ return names
261
+ end
262
+
263
+ def put_att(attname,val,atttype=nil)
264
+ put_attraw(attname,val,atttype)
265
+ end
266
+
267
+ def shape_ul0
268
+ sh = []
269
+ dims.each{|d|
270
+ if d.unlimited? then
271
+ sh.push(0)
272
+ else
273
+ sh.push(d.length)
274
+ end
275
+ }
276
+ sh
277
+ end
278
+
279
+ def shape_current
280
+ sh = []
281
+ dims.each{|d|
282
+ sh.push(d.length)
283
+ }
284
+ sh
285
+ end
286
+
287
+ # The put and get methods in the NetCDFVar class
288
+
289
+ def pack(na)
290
+ sf = att('scale_factor')
291
+ ao = att('add_offset')
292
+ if ( sf == nil && ao == nil ) then
293
+ na
294
+ else
295
+ na = NArray.to_na(na) if na.is_a?(Array)
296
+ if sf
297
+ csf = sf.get
298
+ raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String)
299
+ raise NetcdfError, "scale_factor is not unique" if csf.length != 1
300
+ raise NetcdfError, "zero scale_factor" if csf[0] == 0
301
+ else
302
+ csf = nil
303
+ end
304
+ if ao
305
+ cao = ao.get
306
+ raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String)
307
+ raise NetcdfError, "add_offset is not unique" if cao.length != 1
308
+ else
309
+ cao = nil
310
+ end
311
+ if csf and cao
312
+ packed = (na - cao) / csf
313
+ elsif csf
314
+ packed = na / csf
315
+ elsif cao
316
+ packed = na - cao
317
+ end
318
+ if self.typecode <= NArray::LINT
319
+ packed = packed.round
320
+ end
321
+ end
322
+ end
323
+
324
+ def scaled_put(var,hash=nil)
325
+ simple_put( pack(var), hash)
326
+ end
327
+
328
+ @@unpack_type = nil
329
+ class << NetCDFVar
330
+ def unpack_type
331
+ @@unpack_type
332
+ end
333
+ def unpack_type=(na_type)
334
+ if [NArray::BYTE, NArray::SINT, NArray::INT,
335
+ NArray::SFLOAT, NArray::FLOAT, nil].include?(na_type)
336
+ @@unpack_type = na_type
337
+ else
338
+ raise ArgumentError, "Arg must be one of NArray::BYTE, NArray::SINT, NArray::INT, NArray::SFLOAT, NArray::FLOAT"
339
+ end
340
+ end
341
+
342
+ end
343
+
344
+ def unpack(na)
345
+ sf = att('scale_factor')
346
+ ao = att('add_offset')
347
+ if ( sf == nil && ao == nil ) then
348
+ na
349
+ else
350
+ if sf
351
+ csf = sf.get
352
+ raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String)
353
+ raise NetcdfError, "scale_factor is not unique" if csf.length != 1
354
+ raise NetcdfError, "zero scale_factor" if csf[0] == 0
355
+ else
356
+ csf =nil
357
+ end
358
+ if ao
359
+ cao = ao.get
360
+ raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String)
361
+ raise NetcdfError, "add_offset is not unique" if cao.length != 1
362
+ else
363
+ cao = nil
364
+ end
365
+ if csf and cao
366
+ una = na * csf + cao # csf & cao are NArray -> coerced to their types
367
+ elsif csf
368
+ una = na * csf
369
+ elsif cao
370
+ una = na + cao
371
+ end
372
+ una = una.to_type(@@unpack_type) if @@unpack_type
373
+ una
374
+ end
375
+ end
376
+
377
+ def scaled_get(hash=nil)
378
+ unpack( simple_get(hash) )
379
+ end
380
+
381
+ def simple_put(var,hash=nil)
382
+ if hash==nil
383
+ if self.vartype == "char"
384
+ put_var_char(var)
385
+ elsif self.vartype == "byte"
386
+ put_var_byte(var)
387
+ elsif self.vartype == "sint"
388
+ put_var_sint(var)
389
+ elsif self.vartype == "int"
390
+ put_var_int(var)
391
+ elsif self.vartype == "sfloat"
392
+ put_var_sfloat(var)
393
+ elsif self.vartype == "float"
394
+ put_var_float(var)
395
+ else
396
+ raise NetcdfError,"variable type isn't supported in netCDF"
397
+ end
398
+ elsif hash.key?("index")==true
399
+ if self.vartype == "char"
400
+ put_var1_char(var,hash["index"])
401
+ elsif self.vartype=="byte"
402
+ put_var1_byte(var,hash["index"])
403
+ elsif self.vartype=="sint"
404
+ put_var1_sint(var,hash["index"])
405
+ elsif self.vartype == "int"
406
+ put_var1_int(var,hash["index"])
407
+ elsif self.vartype == "sfloat"
408
+ put_var1_sfloat(var,hash["index"])
409
+ elsif self.vartype == "float"
410
+ put_var1_float(var,hash["index"])
411
+ else
412
+ raise NetcdfError,"variable type isn't supported in netCDF"
413
+ end
414
+ elsif hash.key?("start")==true
415
+ if hash.key?("end")==false && hash.key?("stride")==false
416
+ if self.vartype == "char"
417
+ put_vars_char(var,hash["start"],nil,nil)
418
+ elsif self.vartype=="byte"
419
+ put_vars_byte(var,hash["start"],nil,nil)
420
+ elsif self.vartype=="sint"
421
+ put_vars_sint(var,hash["start"],nil,nil)
422
+ elsif self.vartype=="int"
423
+ put_vars_int(var,hash["start"],nil,nil)
424
+ elsif self.vartype=="sfloat"
425
+ put_vars_sfloat(var,hash["start"],nil,nil)
426
+ elsif self.vartype=="float"
427
+ put_vars_float(var,hash["start"],nil,nil)
428
+ else
429
+ raise NetcdfError, "variable type isn't supported in netCDF"
430
+ end
431
+ elsif hash.key?("end")==true && hash.key?("stride") == false
432
+ if self.vartype == "char"
433
+ put_vars_char(var,hash["start"],hash["end"],nil)
434
+ elsif self.vartype=="byte"
435
+ put_vars_byte(var,hash["start"],hash["end"],nil)
436
+ elsif self.vartype=="sint"
437
+ put_vars_sint(var,hash["start"],hash["end"],nil)
438
+ elsif self.vartype=="int"
439
+ put_vars_int(var,hash["start"],hash["end"],nil)
440
+ elsif self.vartype == "sfloat"
441
+ put_vars_sfloat(var,hash["start"],hash["end"],nil)
442
+ elsif self.vartype =="float"
443
+ put_vars_float(var,hash["start"],hash["end"],nil)
444
+ else
445
+ raise NetcdfError, "variable type isn't supported in netCDF"
446
+ end
447
+ elsif hash.key?("end")==false && hash.key?("stride")==true
448
+ if self.vartype == "char"
449
+ put_vars_char(var,hash["start"],nil,hash["stride"])
450
+ elsif self.vartype=="byte"
451
+ put_vars_byte(var,hash["start"],nil,hash["stride"])
452
+ elsif self.vartype=="sint"
453
+ put_vars_sint(var,hash["start"],nil,hash["stride"])
454
+ elsif self.vartype=="int"
455
+ put_vars_int(var,hash["start"],nil,hash["stride"])
456
+ elsif self.vartype=="sfloat"
457
+ put_vars_sfloat(var,hash["start"],nil,hash["stride"])
458
+ elsif self.vartype=="float"
459
+ put_vars_float(var,hash["start"],nil,hash["stride"])
460
+ else
461
+ raise NetcdfError, "variable type isn't supported in netCDF"
462
+ end
463
+ else hash.key?("end")==true && hash.key?("stride")==true
464
+ if self.vartype == "char"
465
+ put_vars_char(var,hash["start"],hash["end"],hash["stride"])
466
+ elsif self.vartype=="byte"
467
+ put_vars_byte(var,hash["start"],hash["end"],hash["stride"])
468
+ elsif self.vartype=="sint"
469
+ put_vars_sint(var,hash["start"],hash["end"],hash["stride"])
470
+ elsif self.vartype=="int"
471
+ put_vars_int(var,hash["start"],hash["end"],hash["stride"])
472
+ elsif self.vartype=="sfloat"
473
+ put_vars_sfloat(var,hash["start"],hash["end"],hash["stride"])
474
+ elsif self.vartype=="float"
475
+ put_vars_float(var,hash["start"],hash["end"],hash["stride"])
476
+ else
477
+ raise NetcdfError, "variable type isn't supported in netCDF"
478
+ end
479
+ end
480
+ else
481
+ raise ArgumentError,"{'start'}=>[ARRAY] or {'index'}=>[ARRAY] is needed"
482
+ end
483
+ end
484
+
485
+ alias put simple_put
486
+
487
+ def simple_get(hash=nil)
488
+ t_var = self.vartype
489
+ if hash == nil
490
+ if t_var == "char"
491
+ get_var_char
492
+ elsif t_var == "byte"
493
+ get_var_byte
494
+ elsif t_var == "sint"
495
+ get_var_sint
496
+ elsif t_var == "int"
497
+ get_var_int
498
+ elsif t_var == "sfloat"
499
+ get_var_sfloat
500
+ elsif t_var == "float"
501
+ get_var_float
502
+ else
503
+ raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
504
+ end
505
+ elsif hash.key?("index")==true
506
+ ind = hash["index"]
507
+ if t_var == "char"
508
+ get_var1_char(ind)
509
+ elsif t_var == "byte"
510
+ get_var1_byte(ind)
511
+ elsif t_var == "sint"
512
+ get_var1_sint(ind)
513
+ elsif t_var == "int"
514
+ get_var1_int(ind)
515
+ elsif t_var == "sfloat"
516
+ get_var1_sfloat(ind)
517
+ elsif t_var == "float"
518
+ get_var1_float(ind)
519
+ else
520
+ raise NetcdfError,"variable type #{t_var} isn't supported in netCDF"
521
+ end
522
+ elsif hash.key?("start")==true
523
+ h_sta = hash["start"]
524
+ endq = hash.key?("end")
525
+ strq = hash.key?("stride")
526
+ if endq == false && strq == false
527
+ if t_var == "char"
528
+ get_vars_char(h_sta,nil,nil)
529
+ elsif t_var == "byte"
530
+ get_vars_byte(h_sta,nil,nil)
531
+ elsif t_var == "sint"
532
+ get_vars_sint(h_sta,nil,nil)
533
+ elsif t_var == "int"
534
+ get_vars_int(h_sta,nil,nil)
535
+ elsif t_var == "sfloat"
536
+ get_vars_sfloat(h_sta,nil,nil)
537
+ elsif t_var == "float"
538
+ get_vars_float(h_sta,nil,nil)
539
+ else
540
+ raise NetcdfError, "varialbe type #{t_var} isn't supported in netCDF"
541
+ end
542
+ elsif endq == true && strq == false
543
+ h_end = hash["end"]
544
+ if t_var == "char"
545
+ get_vars_char(h_sta,h_end,nil)
546
+ elsif t_var == "byte"
547
+ get_vars_byte(h_sta,h_end,nil)
548
+ elsif t_var == "sint"
549
+ get_vars_sint(h_sta,h_end,nil)
550
+ elsif t_var == "int"
551
+ get_vars_int(h_sta,h_end,nil)
552
+ elsif t_var == "sfloat"
553
+ get_vars_sfloat(h_sta,h_end,nil)
554
+ elsif t_var == "float"
555
+ get_vars_float(h_sta,h_end,nil)
556
+ else
557
+ raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
558
+ end
559
+ elsif endq == false && strq == true
560
+ h_str = hash["stride"]
561
+ if t_var == "char"
562
+ get_vars_char(h_sta,nil,h_str)
563
+ elsif t_var == "byte"
564
+ get_vars_byte(h_sta,nil,h_str)
565
+ elsif t_var == "sint"
566
+ get_vars_sint(h_sta,nil,h_str)
567
+ elsif t_var == "int"
568
+ get_vars_int(h_sta,nil,h_str)
569
+ elsif t_var == "sfloat"
570
+ get_vars_sfloat(h_sta,nil,h_str)
571
+ elsif t_var == "float"
572
+ get_vars_float(h_sta,nil,h_str)
573
+ else
574
+ raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
575
+ end
576
+ else endq == true && strq == true
577
+ h_end = hash["end"]
578
+ h_str = hash["stride"]
579
+ if t_var == "char"
580
+ get_vars_char(h_sta,h_end,h_str)
581
+ elsif t_var == "byte"
582
+ get_vars_byte(h_sta,h_end,h_str)
583
+ elsif t_var == "sint"
584
+ get_vars_sint(h_sta,h_end,h_str)
585
+ elsif t_var == "int"
586
+ get_vars_int(h_sta,h_end,h_str)
587
+ elsif t_var == "sfloat"
588
+ get_vars_sfloat(h_sta,h_end,h_str)
589
+ elsif t_var == "float"
590
+ get_vars_float(h_sta,h_end,h_str)
591
+ else
592
+ raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
593
+ end
594
+ end
595
+ else
596
+ raise ArgumentError,"{'start'}=>{ARRAY} or {'index'}=>{ARRAY} is needed"
597
+ end
598
+ end
599
+
600
+ alias get simple_get
601
+
602
+ def __rubber_expansion( args )
603
+ if (id = args.index(false)) # substitution into id
604
+ # false is incuded
605
+ alen = args.length
606
+ if args.rindex(false) != id
607
+ raise ArguemntError,"only one rubber dimension is permitted"
608
+ elsif alen > rank+1
609
+ raise ArgumentError, "too many args"
610
+ end
611
+ ar = ( id!=0 ? args[0..id-1] : [] )
612
+ args = ar + [true]*(rank-alen+1) + args[id+1..-1]
613
+ elsif args.length == 0 # to support empty [], []=
614
+ args = [true]*rank
615
+ end
616
+ args
617
+ end
618
+ private :__rubber_expansion
619
+
620
+ def [](*a)
621
+ if a.length == 0
622
+ return self.get
623
+ end
624
+ a = __rubber_expansion(a)
625
+ first = Array.new
626
+ last = Array.new
627
+ stride = Array.new
628
+ set_stride = false
629
+ a.each{|i|
630
+ if(i.is_a?(Fixnum))
631
+ first.push(i)
632
+ last.push(i)
633
+ stride.push(1)
634
+ elsif(i.is_a?(Range))
635
+ first.push(i.first)
636
+ last.push(i.exclude_end? ? i.last-1 : i.last)
637
+ stride.push(1)
638
+ elsif(i.is_a?(Hash))
639
+ r = (i.to_a[0])[0]
640
+ s = (i.to_a[0])[1]
641
+ if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
642
+ raise TypeError, "Hash argument must be {a_Range, step}"
643
+ end
644
+ first.push(r.first)
645
+ last.push(r.exclude_end? ? r.last-1 : r.last)
646
+ stride.push(s)
647
+ set_stride = true
648
+ elsif(i.is_a?(TrueClass))
649
+ first.push(0)
650
+ last.push(-1)
651
+ stride.push(1)
652
+ elsif( i.is_a?(Array) || i.is_a?(NArray))
653
+ a_new = a.dup
654
+ at = a.index(i)
655
+ i = NArray.to_na(i) if i.is_a?(Array)
656
+ for n in 0..i.length-1
657
+ a_new[at] = i[n]..i[n]
658
+ na_tmp = self[*a_new]
659
+ if n==0 then
660
+ k = at
661
+ if at > 0
662
+ a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end}
663
+ end
664
+ shape_tmp = na_tmp.shape
665
+ shape_tmp[k] = i.length
666
+ na = na_tmp.class.new(na_tmp.typecode,*shape_tmp)
667
+ index_tmp = Array.new(shape_tmp.length,true)
668
+ end
669
+ index_tmp[k] = n..n
670
+ na[*index_tmp] = na_tmp
671
+ end
672
+ return na
673
+ else
674
+ raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray"
675
+ end
676
+ }
677
+
678
+ if(set_stride)
679
+ na = self.get({"start"=>first, "end"=>last, "stride"=>stride})
680
+ else
681
+ na = self.get({"start"=>first, "end"=>last})
682
+ end
683
+ shape = na.shape
684
+ (a.length-1).downto(0){ |i|
685
+ shape.delete_at(i) if a[i].is_a?(Fixnum)
686
+ }
687
+ na.reshape!( *shape )
688
+ na
689
+ end
690
+
691
+ def []=(*a)
692
+ val = a.pop
693
+ a = __rubber_expansion(a)
694
+ first = Array.new
695
+ last = Array.new
696
+ stride = Array.new
697
+ set_stride = false
698
+ a.each{|i|
699
+ if(i.is_a?(Fixnum))
700
+ first.push(i)
701
+ last.push(i)
702
+ stride.push(1)
703
+ elsif(i.is_a?(Range))
704
+ first.push(i.first)
705
+ last.push(i.exclude_end? ? i.last-1 : i.last)
706
+ stride.push(1)
707
+ elsif(i.is_a?(Hash))
708
+ r = (i.to_a[0])[0]
709
+ s = (i.to_a[0])[1]
710
+ if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
711
+ raise ArgumentError, "Hash argument must be {first..last, step}"
712
+ end
713
+ first.push(r.first)
714
+ last.push(r.exclude_end? ? r.last-1 : r.last)
715
+ stride.push(s)
716
+ set_stride = true
717
+ elsif(i.is_a?(TrueClass))
718
+ first.push(0)
719
+ last.push(-1)
720
+ stride.push(1)
721
+ elsif(i.is_a?(Array) || i.is_a?(NArray))
722
+ a_new = a.dup
723
+ at = a.index(i)
724
+ i = NArray.to_na(i) if i.is_a?(Array)
725
+ val = NArray.to_na(val) if val.is_a?(Array)
726
+ rank_of_subset = a.dup.delete_if{|v| v.is_a?(Fixnum)}.length
727
+ if val.rank != rank_of_subset
728
+ raise "rank of the rhs (#{val.rank}) is not equal to the rank "+
729
+ "of the subset specified by #{a.inspect} (#{rank_of_subset})"
730
+ end
731
+ k = at
732
+ a[0..at-1].each{|x| if x.is_a?(Fixnum) then k -= 1 end}
733
+ if i.length != val.shape[k]
734
+ raise "length of the #{k+1}-th dim of rhs is incorrect "+
735
+ "(#{i.length} for #{val.shape[k]})"
736
+ end
737
+ index_tmp = Array.new(val.rank,true) if !val.is_a?(Numeric) #==>Array-like
738
+ for n in 0..i.length-1
739
+ a_new[at] = i[n]..i[n]
740
+ if !val.is_a?(Numeric) then
741
+ index_tmp[k] = n..n
742
+ self[*a_new] = val[*index_tmp]
743
+ else
744
+ self[*a_new] = val
745
+ end
746
+ end
747
+ return self
748
+ else
749
+ raise TypeError, "argument must be Fixnum, Range, Hash, TrueClass, Array, or NArray"
750
+ end
751
+ }
752
+
753
+ if(set_stride)
754
+ self.put(val, {"start"=>first, "end"=>last, "stride"=>stride})
755
+ else
756
+ self.put(val, {"start"=>first, "end"=>last})
757
+ end
758
+ end
759
+
760
+ def inspect
761
+ 'NetCDFVar:'+file.path+'?var='+name
762
+ end
763
+
764
+ end
765
+
766
+ class NetCDFAtt
767
+
768
+ def put(val,atttype=nil)
769
+ putraw(val,atttype)
770
+ end
771
+
772
+ def inspect
773
+ 'NetCDFAtt:'+name
774
+ end
775
+ end
776
+
777
+ class NetCDFDim
778
+ def inspect
779
+ 'NetCDFDim:'+name
780
+ end
781
+
782
+ def length_ul0
783
+ if unlimited?
784
+ 0
785
+ else
786
+ length
787
+ end
788
+ end
789
+
790
+ end
791
+ end