ruby-netcdf 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/test/aref_aset.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'numru/netcdf'
2
+ 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
+
24
+ f.close
25
+
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
data/test/char_var.rb ADDED
@@ -0,0 +1,25 @@
1
+ ## test of "char" variables
2
+
3
+ require 'numru/netcdf'
4
+ 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
+
24
+
25
+
data/test/clone.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'numru/netcdf'
2
+ 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
+
@@ -0,0 +1,15 @@
1
+ require 'numru/netcdf'
2
+
3
+ $DEBUG = true
4
+ 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"
@@ -0,0 +1,14 @@
1
+ require 'numru/netcdf'
2
+ 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
10
+
11
+ p var1 == nil
12
+ p file == nil
13
+ p att == nil
14
+ p att == att, var1 ==var1, file == file
@@ -0,0 +1,53 @@
1
+ require 'numru/netcdf'
2
+ include NumRu
3
+
4
+ s = 'tmp.nc'
5
+ f = NetCDF.create(s)
6
+
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
23
+
24
+
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
32
+
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
data/test/putatt.cdl ADDED
@@ -0,0 +1,23 @@
1
+ netcdf tmp {
2
+ dimensions:
3
+ x = 5 ;
4
+ variables:
5
+ float x(x) ;
6
+ x:long_name = "test variable" ;
7
+ x:int_att = 123s ;
8
+ x:float_att = 0.333333333333333 ;
9
+ x:float_array = 0.1, 0.2, 30. ;
10
+ x:sfloat_narray = 0.f, 0.3333333f, 0.6666667f ;
11
+ x:float_narray = 0., 1., 2. ;
12
+ x:sint_narray = 0s, 1s, 2s ;
13
+ x:int2float = 10. ;
14
+ x:changed = "changed to text" ;
15
+
16
+ // global attributes:
17
+ :history = "2001-12-23" ;
18
+ :int_att = 123s ;
19
+ :sfloat_att = 0.3333333f ;
20
+ data:
21
+
22
+ x = _, _, _, _, _ ;
23
+ }
data/test/putatt.rb ADDED
@@ -0,0 +1,56 @@
1
+ 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
+ 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
38
+
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
45
+
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
52
+
53
+
54
+ f.close
55
+
56
+ print `ncdump tmp.nc`
data/test/test.cdl ADDED
@@ -0,0 +1,31 @@
1
+ netcdf test {
2
+ dimensions:
3
+ x = 15 ;
4
+ y = 10 ;
5
+ z = 10 ;
6
+ variables:
7
+ short test_sint(x) ;
8
+ byte test_byte(y) ;
9
+ byte test_byte2(z, y) ;
10
+ int test_int(y) ;
11
+ float test_sfloat(z) ;
12
+ double test_float(y) ;
13
+ byte test_byte_vara(z, y) ;
14
+ test_byte_vara:min to max of byte_vara = 0b, 20b ;
15
+ short test_sint_vara(z, y) ;
16
+ test_sint_vara:string = "%%%%%" ;
17
+ int test_int_vara(z, y) ;
18
+ test_int_vara:min to max of int_vara = 0, 60 ;
19
+ float test_sfloat_vara(z, y) ;
20
+ test_sfloat_vara:min to max of float_vara = 0.01f, 5.5f ;
21
+ double test_float_vara(z, y) ;
22
+ test_float_vara:min_to max of double_vara = 0.0001, 5.5555 ;
23
+
24
+ // global attributes:
25
+ :type_byte = 6b ;
26
+ :type_short = 222s, 333s, 444s ;
27
+ :type_int = 2222s, 3333s, 4444s ;
28
+ :type_float = 2.22f, 3.33f, 4.44f ;
29
+ :type_double = 2.222, 3.333, 4.444 ;
30
+ :string = "netCDF for Ruby" ;
31
+ }
data/test/test.rb ADDED
@@ -0,0 +1,192 @@
1
+ ##require 'numru/netcdf'
2
+ ## // to test before make install -->
3
+ require 'narray'
4
+ require '../netcdfraw'
5
+ require '../lib/netcdf'
6
+ ## <-- to test before make install //
7
+
8
+ include NumRu
9
+
10
+ filename = "test.nc"
11
+ print "creating ",filename,"...\n"
12
+ file=NetCDF.create(filename,false,false)
13
+
14
+ dimx=file.def_dim("x",15)
15
+ dimy=file.def_dim("y",10)
16
+ dimz=file.def_dim("z",10)
17
+
18
+ batt = file.put_att("type_byte",5,"byte")
19
+ file.put_att("type_short",[222,333,444],"sint")
20
+ file.put_att("type_int",[2222,3333,4444])
21
+ file.put_att("type_float",[2.22,3.33,4.44],"sfloat")
22
+ file.put_att("type_double",[2.222,3.333,4.444])
23
+ string = file.put_attraw("string","netCDF for Ruby","string")
24
+ batt.put(6,"byte")
25
+
26
+ sint_var=file.def_var("test_sint","sint",["x"])
27
+ byte_var=file.def_var("test_byte","byte",["y"])
28
+ byte_var2=file.def_var("test_byte2","byte",[dimy,dimz])
29
+ int_var=file.def_var("test_int","int",["y"])
30
+ sfloat_var=file.def_var("test_sfloat","sfloat",["z"])
31
+ float_var=file.def_var("test_float","float",["y"])
32
+
33
+ a=NArray.sint(10).indgen
34
+ b=NArray.sint(10).fill(7)
35
+ c=NArray.byte(10).indgen
36
+ d=NArray.int(10).indgen
37
+ e=NArray.sfloat(10).fill(1.111)
38
+ f=NArray.float(10).fill(5.5555555)
39
+ file.enddef
40
+ file2 = file
41
+ (file2 == file)
42
+
43
+ byte_var.put_var_byte(c)
44
+ int_var.put_var_int(d)
45
+ sfloat_var.put_var_sfloat(e)
46
+ float_var.put_var_float(f)
47
+
48
+ file.redef
49
+ byte_vara=file.def_var("test_byte_vara","byte",[dimy,dimz]);
50
+ sint_vara=file.def_var("test_sint_vara","sint",["y","z"]);
51
+ int_vara=file.def_var("test_int_vara","int",["y","z"]);
52
+ sfloat_vara=file.def_var("test_sfloat_vara","sfloat",["y","z"]);
53
+ float_vara=file.def_var("test_float_vara","float",["y","z"]);
54
+ file.enddef
55
+
56
+ byte_vara2 = byte_vara
57
+ (byte_vara2 == byte_vara)
58
+
59
+ g=NArray.byte(10,10).indgen
60
+ h=NArray.byte(2,3).indgen
61
+ gh=NArray.byte(1,1).fill(33)
62
+ k=NArray.sint(10,10).indgen
63
+ l=NArray.sint(2,3).indgen
64
+ kl=NArray.sint(1,1).fill(44)
65
+ m=NArray.int(10,10).indgen
66
+ n=NArray.int(2,3).indgen
67
+ mn=NArray.int(1,1).fill(55)
68
+ o=NArray.sfloat(10,10).fill(1.234567)
69
+ p=NArray.sfloat(2,3).fill(2.345678)
70
+ op=NArray.int(1,1).fill(3.4)
71
+ q=NArray.float(10,10).fill(1.234)
72
+ r=NArray.float(2,3).fill(2.345)
73
+ qr=NArray.float(1,1).fill(4.5)
74
+ s=NArray.float(2,2).fill(10.0)
75
+
76
+
77
+ byte_vara.put(g)
78
+ byte_vara.put(h,{"start"=>[3,5]})
79
+ byte_vara.put(g,{"start"=>[0,0],"end"=>[9,9]})
80
+ byte_vara.put(h,{"start"=>[-8,2]})
81
+ byte_vara.put(h,{"start"=>[0,0],"stride"=>[2,3]})
82
+ byte_vara.put(h,{'start'=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
83
+ byte_vara.put(gh,{"index"=>[4,7]})
84
+
85
+ sint_vara.put(k)
86
+ sint_vara.put(l,{"start"=>[3,5]})
87
+ sint_vara.put(k,{"start"=>[0,0],"end"=>[9,9]})
88
+ sint_vara.put(l,{"start"=>[-8,2]})
89
+ sint_vara.put(l,{"start"=>[0,0],"stride"=>[2,3]})
90
+ sint_vara.put(l,{"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
91
+ sint_vara.put(kl,{"index"=>[4,7]})
92
+
93
+ int_vara.put(m)
94
+ int_vara.put(n,{"start"=>[3,5]})
95
+ int_vara.put(m,{"start"=>[0,0],"end"=>[9,9]})
96
+ int_vara.put(n,{"start"=>[-8,2]})
97
+ int_vara.put(n,{"start"=>[0,0],"stride"=>[2,3]})
98
+ int_vara.put(n,{"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
99
+ int_vara.put(mn,{"index"=>[4,7]})
100
+
101
+ sfloat_vara.put(o)
102
+ sfloat_vara.put(p,{"start"=>[3,5]})
103
+ sfloat_vara.put(o,{"start"=>[0,0],"end"=>[9,9]})
104
+ sfloat_vara.put(p,{"start"=>[-8,2]})
105
+ sfloat_vara.put(p,{"start"=>[0,0],"stride"=>[2,3]})
106
+ sfloat_vara.put(p,{"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
107
+ sfloat_vara.put(op,{"index"=>[4,7]})
108
+
109
+ float_vara.put(q)
110
+ float_vara.put(r,{"start"=>[3,5]})
111
+ float_vara.put(q,{"start"=>[0,0],"end"=>[9,9]})
112
+ float_vara.put(r,{"start"=>[-8,2]})
113
+ float_vara.put(r,{"start"=>[0,0],"stride"=>[2,3]})
114
+ float_vara.put(r,{"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
115
+ float_vara.put(qr,{"index"=>[4,7]})
116
+
117
+ float_vara.dim_names
118
+
119
+ na_aaa=byte_vara.get({"start"=>[3,5]})
120
+ na_aab=byte_vara.get({"start"=>[0,0],"end"=>[9,9]})
121
+ na_aac=byte_vara.get({"start"=>[-8,2]})
122
+ na_aad=byte_vara.get({"start"=>[0,0],"stride"=>[2,3]})
123
+ na_aae=byte_vara.get({'start'=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
124
+ na_aaf=byte_vara.get({"index"=>[4,7]})
125
+ na_aag=byte_vara.get
126
+
127
+ na_bba=sint_vara.get({"start"=>[3,5]})
128
+ na_bbb=sint_vara.get({"start"=>[0,0],"end"=>[9,9]})
129
+ na_bbc=sint_vara.get({"start"=>[-8,2]})
130
+ na_bbd=sint_vara.get({"start"=>[0,0],"stride"=>[2,3]})
131
+ na_bbf=sint_vara.get({"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
132
+ na_bbg=sint_vara.get({"index"=>[4,7]})
133
+ na_bbh=sint_vara.get
134
+
135
+ na_cca=int_vara.get({"start"=>[3,5]})
136
+ na_ccb=int_vara.get({"start"=>[0,0],"end"=>[9,9]})
137
+ na_ccc=int_vara.get({"start"=>[-8,2]})
138
+ na_ccd=int_vara.get({"start"=>[0,0],"stride"=>[2,3]})
139
+ na_cce=int_vara.get({"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
140
+ na_ccf=int_vara.get({"index"=>[4,7]})
141
+ na_ccg=int_vara.get
142
+
143
+ na_dda=sfloat_vara.get({"start"=>[3,5]})
144
+ na_ddb=sfloat_vara.get({"start"=>[0,0],"end"=>[9,9]})
145
+ na_ddc=sfloat_vara.get({"start"=>[-8,2]})
146
+ na_ddd=sfloat_vara.get({"start"=>[0,0],"stride"=>[2,3]})
147
+ na_dde=sfloat_vara.get({"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
148
+ na_ddf=sfloat_vara.get({"index"=>[4,7]})
149
+ na_ddg=sfloat_vara.get
150
+
151
+ na_eea=float_vara.get({"start"=>[3,5]})
152
+ na_eeb=float_vara.get({"start"=>[0,0],"end"=>[9,9]})
153
+ na_eec=float_vara.get({"start"=>[-8,2]})
154
+ na_eed=float_vara.get({"start"=>[0,0],"stride"=>[2,3]})
155
+ na_eee=float_vara.get({"start"=>[1,1],"end"=>[3,7],"stride"=>[2,3]})
156
+ na_eef=float_vara.get({"index"=>[4,7]})
157
+ na_eeg=float_vara.get
158
+
159
+ file.redef
160
+
161
+ copy_byte = string.copy(byte_vara)
162
+ copy_byte.put([0,20],"byte")
163
+ copy_byte.name="new_name"
164
+
165
+ copy_byte2 = copy_byte
166
+ (copy_byte2 == copy_byte)
167
+
168
+ copy_sint = string.copy(sint_vara)
169
+ copy_sint.put("%%%%%")
170
+ copy_int = string.copy(int_vara)
171
+ copy_int.put([0,60],"int")
172
+ copy_sfloat = string.copy(sfloat_vara)
173
+ copy_sfloat.put([0.01,5.5],"sfloat")
174
+ copy_float = string.copy(float_vara)
175
+ copy_float.put([0.0001,5.5555],"float")
176
+ file.enddef
177
+ nm = copy_byte.name
178
+ att0 = string.get
179
+ att1 = copy_byte.get
180
+ att2 = copy_sint.get
181
+ att3 = copy_int.get
182
+ att4 = copy_sfloat.get
183
+ att5 = copy_float.get
184
+ file.fill(true)
185
+ file.fill(false)
186
+
187
+ float_vara.dim_names
188
+ float_vara.att_names
189
+
190
+ file.close
191
+ exit(0)
192
+