ruby-netcdf 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.required_ruby_version = Gem::Requirement.new(">= 1.8")
25
25
  spec.add_runtime_dependency(%q<narray-bigmem>, [">= 0"])
26
26
  spec.add_runtime_dependency(%q<narray_miss-bigmem>, [">= 0"])
27
+ spec.add_development_dependency "test-unit"
27
28
 
28
29
  end
data/ruby-netcdf.gemspec CHANGED
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.required_ruby_version = Gem::Requirement.new(">= 1.8")
25
25
  spec.add_runtime_dependency(%q<narray>, [">= 0"])
26
26
  spec.add_runtime_dependency(%q<narray_miss>, [">= 0"])
27
+ spec.add_development_dependency "test-unit"
27
28
 
28
29
  end
data/test/aref_aset.rb CHANGED
@@ -1,37 +1,67 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
3
  include NumRu
3
- s = 'tmp.nc'
4
- f = NetCDF.create(s)
5
- f.redef
6
- dx = f.def_dim('x',5)
7
- dy = f.def_dim('y',3)
8
- v = f.def_var('x','sfloat',[dx])
9
- v2 = f.def_var('x2',NArray::SFLOAT,[dx])
10
- vxy = f.def_var('xy','sfloat',[dx,dy])
11
- f.enddef
12
- v.put([1,2,3,4,5])
13
- p 'testing []= ...'
14
- v[{0..3=>2}]=[100,500]
15
- v[1]=999
16
- v[3..4]=[-99,-99]
17
- v2.put(666)
18
- v2[0..2]=777
19
- vxy.put(NArray.sfloat(5,3).indgen!)
20
- vxy[[2,0],[0,2,1]] = [[1,2],[3,4],[5,6]]
21
- vxy[1,[2,0,1]] = [10,20,30]
22
- vxy[[4,3],2] = [100,200]
23
4
 
24
- f.close
5
+ class TestArefAset < Test::Unit::TestCase
6
+ def startup # call once before tests
7
+ end
8
+ def setup # call before each test
9
+ @s = 'tmp.nc'
10
+ end
11
+ def teardown # call after each test
12
+ end
13
+ def shutdown # call once after tests
14
+ end
25
15
 
26
- f = NetCDF.open(s)
27
- v = f.var('x')
28
- p 'testing [] ...'
29
- p '*0*',v[{0..3=>2}]
30
- p '*1*',v[1]
31
- p '*2*',v[3..4],v.rank
32
- p '*3*',v[[2,0,0]]
33
- vxy = f.var('xy')
34
- p '*4*',vxy[[2,0],[0,2,1]]
35
- p '*5*',vxy[1,[2,0,1]]
36
- p '*6*',vxy[[4,3],2]
37
- f.close
16
+ def test_netcdf_create
17
+ f = NetCDF.create(@s)
18
+ f.redef
19
+ dx = f.def_dim('x',5)
20
+ dy = f.def_dim('y',3)
21
+ v = f.def_var('x','sfloat',[dx])
22
+ v2 = f.def_var('x2',NArray::SFLOAT,[dx])
23
+ vxy = f.def_var('xy','sfloat',[dx,dy])
24
+ f.enddef
25
+ v.put([1,2,3,4,5])
26
+ v[{0..3=>2}]=[100,500]
27
+ v[1]=999
28
+ v[3..4]=[-99,-99]
29
+ v2.put(666)
30
+ v2[0..2]=777
31
+ vxy.put(NArray.sfloat(5,3).indgen!)
32
+ vxy[[2,0],[0,2,1]] = [[1,2],[3,4],[5,6]]
33
+ vxy[1,[2,0,1]] = [10,20,30]
34
+ vxy[[4,3],2] = [100,200]
35
+ f.close
36
+ end
37
+
38
+ def test_netcdf_open
39
+ unless File.exist?(@s)
40
+ test_netcdf_create
41
+ else
42
+ f = NetCDF.open(@s)
43
+ v = f.var('x')
44
+ assert_equal v[{0..3=>2}], NArray[100.0,500.0].to_f
45
+ assert_equal v[1], NArray[999.0].to_f
46
+ assert_equal v[3..4], NArray[-99.0,-99.0].to_f
47
+ assert_equal v.rank, 1
48
+ assert_equal v[[2,0,0]], NArray[500.0,100.0,100.0].to_f
49
+ vxy = f.var('xy')
50
+ assert_equal vxy[[2,0],[0,2,1]],
51
+ NArray[[1.0,2.0],[3.0,4.0],[5.0,6.0]].to_f
52
+ assert_equal vxy[1,[2,0,1]],
53
+ NArray[10.0,20.0,30.0].to_f
54
+ assert_equal vxy[[4,3],2],
55
+ NArray[100.0,200.0].to_f
56
+ f.close
57
+ end
58
+ if File.exist?(@s)
59
+ begin
60
+ File.delete(@s)
61
+ rescue
62
+ p $!
63
+ end
64
+ end
65
+ end
66
+
67
+ end
data/test/char_var.rb CHANGED
@@ -1,25 +1,36 @@
1
- ## test of "char" variables
2
-
1
+ require 'test/unit'
3
2
  require 'numru/netcdf'
4
3
  include NumRu
5
- s = 'tmp.nc'
6
- f = NetCDF.create(s)
7
- d = f.def_dim('x',5)
8
- v = f.def_var('x','char',[d])
9
- tv = f.def_var('text','char',[d])
10
- f.enddef
11
- v.put( NArray.byte(5).indgen! )
12
- tv.put( NArray.to_na("hello","byte",5) )
13
- tv.put( NArray.to_na("LO","byte",2), 'start'=>[3] )
14
- tv.put( NArray.to_na("H","byte",1), 'index'=>[0] )
15
- f.close
16
-
17
- f = NetCDF.open(s)
18
- v = f.var('x')
19
- p v.get
20
- tv = f.var('text')
21
- p tv.get
22
- p tv.get.to_s
23
-
4
+ class TestCharVar < Test::Unit::TestCase
5
+ def setup
6
+ @s = 'tmp.nc'
7
+ f = NetCDF.create(@s)
8
+ d = f.def_dim('x',5)
9
+ v = f.def_var('x','char',[d])
10
+ tv = f.def_var('text','char',[d])
11
+ f.enddef
12
+ v.put( NArray.byte(5).indgen! )
13
+ tv.put( NArray.to_na("hello","byte",5) )
14
+ tv.put( NArray.to_na("LO","byte",2), 'start'=>[3] )
15
+ tv.put( NArray.to_na("H","byte",1), 'index'=>[0] )
16
+ f.close
17
+ end
18
+ def teardown
19
+ if File.exist?(@s)
20
+ begin
21
+ File.delete(@s)
22
+ rescue
23
+ p $!
24
+ end
25
+ end
26
+ end
24
27
 
28
+ def test_char_var
29
+ f = NetCDF.open(@s)
30
+ v = f.var('x')
31
+ assert_equal v.get, NArray.byte(5).indgen!
32
+ tv = f.var('text')
33
+ assert_equal tv.get.to_s, "HelLO"
34
+ end
25
35
 
36
+ end
data/test/clone.rb CHANGED
@@ -1,54 +1,49 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
3
  include NumRu
3
- s = 'tmp.nc'
4
- #f = NetCDF.new(s,'rw')
5
- f = NetCDF.create(s)
6
- f.redef
7
- d = f.def_dim('x',3)
8
- v = f.def_var('x','sfloat',[d])
9
- a = f.put_att('long_name','xx')
10
- f.enddef
11
- v.put([1,2,3])
12
- f.taint
13
- f.freeze
14
- f2 = f.clone
15
- p 'netcdf clone'
16
- p f.path, f2.path
17
- p f.tainted?, f2.tainted?, f.frozen?, f2.frozen?
18
-
19
- p 'netcdf dup'
20
- f3 = f.dup
21
- p f.tainted?, f3.tainted?, f.frozen?, f3.frozen?
22
-
23
- p 'netcdfdim'
24
- d.taint
25
- d2 = d.clone
26
- d3 = d.dup
27
- p d.tainted?, d2.tainted?, d3.tainted?
28
-
29
- p 'netcdfvar'
30
- v.taint
31
- v2 = v.clone
32
- v3 = v.dup
33
- p v.tainted?, v2.tainted?, v3.tainted?
34
-
35
- p 'netcdfatt'
36
- a.taint
37
- a2 = a.clone
38
- a3 = a.dup
39
- p a.tainted?, a2.tainted?, a3.tainted?
40
-
41
- f.close
42
-
43
- p 'narray (for reference)'
44
- a = NArray.float(3)
45
- a.taint
46
- b = a.clone
47
- p a.tainted?, b.tainted?
48
- b = a.dup
49
- p a.tainted?, b.tainted?
50
-
51
-
52
-
53
-
54
4
 
5
+ class TestClone < Test::Unit::TestCase
6
+ def setup
7
+ @s = 'tmp.nc'
8
+ @f = NetCDF.create(@s)
9
+ @f.redef
10
+ @d = @f.def_dim('x',3)
11
+ @v = @f.def_var('x','sfloat',[@d])
12
+ @a = @f.put_att('long_name','xx')
13
+ @f.enddef
14
+ @v.put([1,2,3])
15
+ @f.freeze
16
+ end
17
+
18
+ def teardown
19
+ @f.close
20
+ if File.exist?(@s)
21
+ begin
22
+ File.delete(@s)
23
+ rescue
24
+ p $!
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_clone_netcdf_path
30
+ f = @f.clone
31
+ assert_equal @f.path, f.path
32
+ assert(@f.frozen? == f.frozen?)
33
+ d = @d.clone
34
+ assert_equal @d, d
35
+ v = @v.clone
36
+ assert_equal @v, v
37
+ a = @a.clone
38
+ assert_equal @a, a
39
+ end
40
+
41
+ def test_clone_netcdf_dup
42
+ f = @f.dup
43
+ assert(@f.frozen? != f.frozen?)
44
+ # d = @d.dup
45
+ # v = @v.dup
46
+ # a = @a.dup
47
+ end
48
+
49
+ end
data/test/create_tmp.rb CHANGED
@@ -1,15 +1,19 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
-
3
- $DEBUG = true
4
3
  include NumRu
5
- file = NetCDF.create_tmp
6
- file.def_dim('x',5)
7
- file.put_att("history", __FILE__ )
8
- p file.path
9
- p file.att("history").get
10
- print "environment variable TEMP ="+(ENV['TEMP'] || '')+"\n"
11
- file2=file
12
- print "000\n"
13
- file.close
14
- GC.start
15
- print "aaa\n"
4
+ class CreateTMP < Test::Unit::TestCase
5
+ def setup
6
+ @file = NetCDF.create_tmp
7
+ @file.def_dim('x',5)
8
+ @file.put_att("history", __FILE__ )
9
+ end
10
+ def teardown
11
+ @file.close
12
+ GC.start
13
+ end
14
+ def test_create_tmp
15
+ assert_equal @file.att("history").get, __FILE__
16
+ # print "environment variable TEMP ="+(ENV['TEMP'] || '')+"\n"
17
+ # file2 = @file
18
+ end
19
+ end
@@ -1,14 +1,32 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
3
  include NumRu
3
- file = NetCDF.create("tmp.nc")
4
- var1 = file.def_var_with_dim("var1","sfloat",[6],["x"])
5
- var2 = file.def_var_with_dim("var2","sfloat",[6,3],["x","y"])
6
- var3 = file.def_var_with_dim("var3","sfloat",[3],["y"])
7
- var3 = file.def_var_with_dim("var4","sfloat",[0],["t"])
8
- att = var1.put_att("long_name","test")
9
- file.close
4
+ class TestDefVarWithDim < Test::Unit::TestCase
5
+ def setup
6
+ @fname = "tmp.nc"
7
+ @file = NetCDF.create(@fname)
8
+ @var1 = @file.def_var_with_dim("var1","sfloat",[6],["x"])
9
+ @var2 = @file.def_var_with_dim("var2","sfloat",[6,3],["x","y"])
10
+ @var3 = @file.def_var_with_dim("var3","sfloat",[3],["y"])
11
+ @var3 = @file.def_var_with_dim("var4","sfloat",[0],["t"])
12
+ @att = @var1.put_att("long_name","test")
13
+ @file.close
14
+ end
15
+ def teardown
16
+ if File.exist?(@fname)
17
+ begin
18
+ File.delete(@fname)
19
+ rescue
20
+ p $!
21
+ end
22
+ end
23
+ end
10
24
 
11
- p var1 == nil
12
- p file == nil
13
- p att == nil
14
- p att == att, var1 ==var1, file == file
25
+ def test_def_var_with_dim
26
+ assert_not_nil @var1
27
+ assert_not_nil @var2
28
+ assert_not_nil @var3
29
+ assert_not_nil @file
30
+ assert_not_nil @att
31
+ end
32
+ end
@@ -1,53 +1,84 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
3
  include NumRu
3
4
 
4
- s = 'tmp.nc'
5
- f = NetCDF.create(s)
5
+ class TestFactorOffset < Test::Unit::TestCase
6
+ def setup
7
+ @s = 'tmp.nc'
8
+ f = NetCDF.create(@s)
9
+ nx = 10
10
+ d = f.def_dim('x',nx)
11
+ v1 = f.def_var('v1','sint',[d])
12
+ v2 = f.def_var('v2','sint',[d])
13
+ v3 = f.def_var('v3','int',[d])
14
+ v1.put_att('scale_factor',0.1,'sfloat')
15
+ v1.put_att('add_offset',100.0,'sfloat')
16
+ v2.put_att('scale_factor',0.1,'sfloat')
17
+ v2.put_att('add_offset',100.0,'sfloat')
18
+ v3.put_att('scale_factor',0.1,'sfloat')
19
+ v3.put_att('add_offset',100.0,'sfloat')
20
+ f.enddef
21
+ v1.put( NArray.sint(nx).indgen!+100 )
22
+ v2.scaled_put( NArray.float(nx).indgen!+100 )
23
+ v3.scaled_put( NArray.float(nx).indgen!+100 )
24
+ f.close
25
+ end
6
26
 
7
- nx = 10
8
- d = f.def_dim('x',nx)
9
- v1 = f.def_var('v1','sint',[d])
10
- v2 = f.def_var('v2','sint',[d])
11
- v3 = f.def_var('v3','int',[d])
12
- v1.put_att('scale_factor',0.1,'sfloat')
13
- v1.put_att('add_offset',100.0,'sfloat')
14
- v2.put_att('scale_factor',0.1,'sfloat')
15
- v2.put_att('add_offset',100.0,'sfloat')
16
- v3.put_att('scale_factor',0.1,'sfloat')
17
- v3.put_att('add_offset',100.0,'sfloat')
18
- f.enddef
19
- v1.put( NArray.sint(nx).indgen!+100 )
20
- v2.scaled_put( NArray.float(nx).indgen!+100 )
21
- v3.scaled_put( NArray.float(nx).indgen!+100 )
22
- f.close
27
+ def teardown
28
+ if File.exist?(@s)
29
+ begin
30
+ File.delete(@s)
31
+ rescue
32
+ p $!
33
+ end
34
+ end
35
+ end
36
+ def test_originally_uscaled
37
+ f = NetCDF.open(@s)
38
+ v1 = f.var('v1')
39
+ v11 = v1.get
40
+ assert_equal v11,
41
+ NArray[ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 ].to_i
42
+ v12 = v1.scaled_get
43
+ assert_equal v12,
44
+ NArray[ 110.0, 110.1, 110.2, 110.3, 110.4, 110.5, 110.6, 110.7, 110.8, 110.9 ].to_type(NArray::SFLOAT)
45
+ f.close
46
+ end
23
47
 
48
+ def test_originally_scaled
49
+ f = NetCDF.open(@s)
50
+ v2 = f.var('v2')
51
+ v21 = v2.get
52
+ assert_equal v21,
53
+ NArray[ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ].to_i
54
+ v22 = v2.scaled_get
55
+ assert_equal v22,
56
+ NArray[ 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0 ].to_type(NArray::SFLOAT)
57
+ f.close
58
+ end
24
59
 
25
- print "** originally unscaled\n"
26
- f = NetCDF.open(s)
27
- v1 = f.var('v1')
28
- v11 = v1.get
29
- v12 = v1.scaled_get
30
- p v11
31
- p v12
60
+ def test_originally_scaled_int_to_double
61
+ f = NetCDF.open(@s)
62
+ v3 = f.var('v3')
63
+ v31 = v3.get
64
+ assert_equal v31,
65
+ NArray[ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ].to_i
66
+ v32 = v3.scaled_get
67
+ assert_equal v32,
68
+ NArray[ 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0 ].to_type(NArray::SFLOAT)
69
+ f.close
70
+ end
32
71
 
33
- print "** originally scaled\n"
34
- v2 = f.var('v2')
35
- v21 = v2.get
36
- v22 = v2.scaled_get
37
- p v21
38
- p v22
39
-
40
- print "** originally sclaed (int --> double)\n"
41
- v3 = f.var('v3')
42
- v31 = v3.get
43
- v32 = v3.scaled_get
44
- p v31
45
- p v32
46
-
47
- print "** unpack type fixed to sfloat\n"
48
- NetCDFVar.unpack_type = NArray::SFLOAT
49
- v33 = v3.scaled_get
50
- p v33
51
- NetCDFVar.unpack_type = NArray::INT
52
- v33 = v3.scaled_get
53
- p v33
72
+ def test_unpack_type_fixed_to_sfloat
73
+ f = NetCDF.open(@s)
74
+ v3 = f.var('v3')
75
+ NetCDFVar.unpack_type = NArray::SFLOAT
76
+ v33 = v3.scaled_get
77
+ assert_equal v33,
78
+ NArray[ 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0 ].to_type(NArray::SFLOAT)
79
+ NetCDFVar.unpack_type = NArray::INT
80
+ v33 = v3.scaled_get
81
+ assert_equal v33,
82
+ NArray[ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 ].to_i
83
+ end
84
+ end
data/test/putatt.rb CHANGED
@@ -1,56 +1,78 @@
1
+ require 'test/unit'
1
2
  require 'numru/netcdf'
2
- include NumRu
3
- s = 'tmp.nc'
4
- f = NetCDF.create(s)
5
- d = f.def_dim('x',5)
6
- v = f.def_var('x','sfloat',[d])
7
-
8
3
  require 'date'
9
- att = f.put_att("history", Date.today.to_s )
10
- p att.get, f.att("history").get
11
- att = f.put_att("int_att",123)
12
- att = f.put_att("sfloat_att",1.0/3.0,'sfloat')
13
- att = f.put_att("sfloat_att2",2.0/3.0,NArray::SFLOAT)
14
- att = v.put_att("long_name",'test variable')
15
- att = v.put_att("int_att",123)
16
- p att.get, v.att("int_att").get
17
- att = v.put_att("float_att",1.0/3.0)
18
- att = v.put_att("float_array",[0.1, 0.2, 30])
19
- att = v.put_att("sfloat_narray",NArray.sfloat(3).indgen!/3)
20
- att = v.put_att("float_narray",NArray.float(3).indgen!)
21
- att = v.put_att("sint_narray",NArray.sint(3).indgen!)
22
- att = v.put_att("int2float",10,'float')
23
- att = att = v.put_att("dummy",10,'float')
24
- att.put('changed to text')
25
- att.name = 'changed'
26
- begin
27
- v.put_att("destined_to_fail",9.8,'complex')
28
- rescue
29
- print "*1* exception raised as expected -- (",
30
- __FILE__,":",__LINE__,") ", $!,"\n"
31
- end
32
- begin
33
- v.put_att("destined_to_fail",9.8,'string')
34
- rescue
35
- print "*2* exception raised as expected -- (",
36
- __FILE__,":",__LINE__,") ", $!,"\n"
37
- end
4
+ include NumRu
5
+ class TestPutAttr < Test::Unit::TestCase
6
+ def setup
7
+ @s = 'tmp.nc'
8
+ @f = NetCDF.create(@s)
9
+ @d = @f.def_dim('x',5)
10
+ @v = @f.def_var('x','sfloat',[@d])
11
+ end
12
+ def teardown
13
+ @f.close
14
+ if File.exist?(@s)
15
+ begin
16
+ File.delete(@s)
17
+ rescue
18
+ p $!
19
+ end
20
+ end
21
+ end
22
+ def test_att_history_get
23
+ att = @f.put_att("history", Date.today.to_s )
24
+ assert_equal att.get,
25
+ @f.att("history").get
26
+ end
27
+ def test_att_int_att_get
28
+ att = @f.put_att("int_att",123)
29
+ att = @f.put_att("sfloat_att",1.0/3.0,'sfloat')
30
+ att = @f.put_att("sfloat_att2",2.0/3.0,NArray::SFLOAT)
31
+ att = @v.put_att("long_name",'test variable')
32
+ att = @v.put_att("int_att",123)
33
+ assert_equal att.get,
34
+ @v.att("int_att").get
35
+ end
38
36
 
39
- begin
40
- v.put_att("destined_to_fail",'hello','int')
41
- rescue
42
- print "*3* exception raised as expected -- (",
43
- __FILE__,":",__LINE__,") ", $!,"\n"
44
- end
37
+ def test_att_changed_to_text
38
+ att = @v.put_att("float_att",1.0/3.0)
39
+ att = @v.put_att("float_array",[0.1, 0.2, 30])
40
+ tmp = NArray.sfloat(3).indgen!
41
+ tmp = tmp/3
42
+ att = @v.put_att("sfloat_narray",tmp)
43
+ att = @v.put_att("float_narray",NArray.float(3).indgen!)
44
+ att = @v.put_att("sint_narray",NArray.sint(3).indgen!)
45
+ att = @v.put_att("int2float",10,'float')
46
+ att = att = @v.put_att("dummy",10,'float')
47
+ assert att.put('changed to text')
48
+ att.name = 'changed'
49
+ end
45
50
 
46
- begin
47
- v.put_att("destined_to_fail",[10,30,'sss'])
48
- rescue
49
- print "*4* exception raised as expected -- (",
50
- __FILE__,":",__LINE__,") ", $!,"\n"
51
- end
51
+ def test_destined_to_fail_complex
52
+ assert_raise(NetcdfError) do
53
+ @v.put_att("destined_to_fail", 9.8, 'complex')
54
+ # "unsupported type. code = 7"
55
+ end
56
+ end
52
57
 
58
+ def test_destined_to_fail_string
59
+ assert_raise(ArgumentError) do
60
+ @v.put_att("destined_to_fail", 9.8, 'string')
61
+ # Unrecognized NArray type>
62
+ end
63
+ end
53
64
 
54
- f.close
65
+ def test_destined_to_fail_int
66
+ assert_raise(NetcdfError) do
67
+ @v.put_att("destined_to_fail", 'hello', 'int')
68
+ # attribute type must be 'char' (or nil) for a String value
69
+ end
70
+ end
55
71
 
56
- print `ncdump tmp.nc`
72
+ def test_destined_to_fail_mixed_array
73
+ assert_raise(TypeError) do
74
+ @v.put_att("destined_to_fail",[10,30,'sss'])
75
+ # no implicit conversion of String into Integer
76
+ end
77
+ end
78
+ end