mdarray 0.5.3-java → 0.5.4-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.md +325 -70
  2. data/doc/20130625 MDArray Internals.docx +0 -0
  3. data/lib/colt/colt.rb +2 -0
  4. data/lib/colt/matrix/colt_matrix.rb +365 -0
  5. data/lib/colt/matrix/matrix2D_floating_algebra.rb +325 -0
  6. data/lib/colt/matrix/matrix_hierarchy.rb +258 -0
  7. data/lib/mdarray.rb +54 -3
  8. data/lib/mdarray/access.rb +16 -0
  9. data/lib/mdarray/counter.rb +16 -0
  10. data/lib/mdarray/creation.rb +13 -1
  11. data/lib/mdarray/lazy_mdarray.rb +6 -2
  12. data/lib/mdarray/lazy_operators.rb +8 -0
  13. data/lib/mdarray/printing.rb +18 -3
  14. data/lib/mdarray/section.rb +101 -0
  15. data/lib/netcdf/attribute.rb +154 -0
  16. data/lib/netcdf/cdm_node.rb +71 -0
  17. data/lib/netcdf/dimension.rb +146 -0
  18. data/lib/netcdf/file.rb +253 -0
  19. data/lib/netcdf/file_writer.rb +474 -0
  20. data/lib/netcdf/group.rb +205 -0
  21. data/lib/netcdf/netcdf.rb +151 -0
  22. data/lib/netcdf/variable.rb +520 -0
  23. data/test/colt/test_complete.rb +1 -2
  24. data/test/colt/test_double_matrix2d.rb +186 -0
  25. data/test/colt/test_float_matrix2d.rb +171 -0
  26. data/test/colt/test_math.rb +21 -0
  27. data/test/colt/test_matrix.rb +172 -0
  28. data/test/complete.rb +9 -1
  29. data/test/env.rb +11 -1
  30. data/test/mdarray/test_complete.rb +2 -0
  31. data/test/mdarray/test_creation.rb +19 -28
  32. data/test/mdarray/test_non_numeric.rb +97 -0
  33. data/test/mdarray/test_sections.rb +94 -0
  34. data/test/mdarray/test_views.rb +23 -1
  35. data/test/netcdf/netcdfwriter.rb +197 -0
  36. data/test/netcdf/test_complete.rb +27 -0
  37. data/test/netcdf/test_netcdf.rb +331 -0
  38. data/test/netcdf/test_redefine.rb +120 -0
  39. data/vendor/incanter.jar +0 -0
  40. data/vendor/{netcdfAll-4.3.16.jar → netcdfAll-4.3.18.jar} +0 -0
  41. data/version.rb +1 -1
  42. metadata +44 -26
  43. data/vendor/parallelcolt-0.10.0.jar +0 -0
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ Dir.mkdir($TMP_TEST_DIR) unless File.exists?($TMP_TEST_DIR)
23
+
24
+ #=begin
25
+ require_relative 'test_netcdf'
26
+ require_relative 'netcdfwriter'
27
+ #=end
@@ -0,0 +1,331 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'mdarray'
27
+
28
+ class MDArrayTest < Test::Unit::TestCase
29
+
30
+ context "NetCDF implementation" do
31
+
32
+ setup do
33
+
34
+ @directory = $TMP_TEST_DIR
35
+
36
+ end
37
+
38
+ #-------------------------------------------------------------------------------------
39
+ #
40
+ #-------------------------------------------------------------------------------------
41
+
42
+ should "Allow definition of a NetCDF-3 file" do
43
+
44
+ # Opens a file for definition, passing the directory and file name.
45
+ # Creates a new scope for defining the NetCDF file. In order to access the
46
+ # ouside scope, that is, "here", we pass self as the third argument to define.
47
+ # When self is passed as argument, @outside_scope is available inside the block.
48
+ # NetCDF.define block is in define mode, so it is not possible to write data in
49
+ # this block. We need to open a new block with NetCDF.write for writing. The
50
+ # cost of doing this is that we close the file at the end of the define block and
51
+ # need to reopen the file for writing. However, since we probably only define the
52
+ # file once and write many times, this is not much of a problem. If one prefers,
53
+ # one could not use the define block and use the normal APIs.
54
+
55
+ NetCDF.define(cygpath(@directory), "nc_output", "netcdf3", self) do
56
+
57
+ #=================================================================================
58
+ # Add global attributes by adding a single valued attribute or an array of
59
+ # attributes. When adding an array of attributes all elements of the array must
60
+ # be of the same type, i.e, fixnum, floats or strings. Attributes with long numbers
61
+ # are not allowed. This seems to be a restriction of Java-NetCDF.
62
+ #
63
+ # Note that the attribute name can be used to make access to the attribute, for
64
+ # instance, on the first example "Values" can be accessed as @value later on.
65
+ #=================================================================================
66
+
67
+ global_att "Fixnum", 3
68
+ @outside_scope.assert_equal(3, @ga_fixnum.numeric_value)
69
+
70
+ global_att "Float", 3.45, "float"
71
+ @outside_scope.assert_equal("float", @ga_float.data_type)
72
+
73
+ global_att "Double", 3.45, "double"
74
+ @outside_scope.assert_equal("double", @ga_double.data_type)
75
+
76
+ global_att "Int", 3.45
77
+ @outside_scope.assert_equal("int", @ga_int.data_type)
78
+
79
+ global_att "Byte", 3, "byte"
80
+ @outside_scope.assert_equal("byte", @ga_byte.data_type)
81
+
82
+ global_att "Short", 3, "short"
83
+ @outside_scope.assert_equal("short", @ga_short.data_type)
84
+
85
+ global_att "String", "this is a string"
86
+ global_att "Description", "This is a test file created by MDArray"
87
+ @outside_scope.assert_equal("String", @ga_description.data_type)
88
+ @outside_scope.assert_equal("Description", @ga_description.name)
89
+ @outside_scope.assert_equal("This is a test file created by MDArray",
90
+ @ga_description.string_value)
91
+ @outside_scope.assert_equal(true, @ga_description.string?)
92
+ @outside_scope.assert_equal(false, @ga_description.unsigned?)
93
+
94
+ global_att "Values", [1, 2, 3, 4], "double"
95
+ @outside_scope.assert_equal(true, @ga_values.array?)
96
+ @outside_scope.assert_equal("Values", @ga_values.name)
97
+ @outside_scope.assert_equal(2.0, @ga_values.numeric_value(1))
98
+
99
+ global_att "Strings", ["abc", "efg"]
100
+ @outside_scope.assert_equal("abc", @ga_strings.string_value(0))
101
+
102
+ global_att "Floats", [1.34, 2.45], "float"
103
+
104
+ att = find_global_attribute("Fixnum")
105
+ @outside_scope.assert_equal("Fixnum", att.name)
106
+
107
+ att = find_global_attribute("doesnotexists")
108
+ @outside_scope.assert_equal(nil, att)
109
+
110
+ # print all global attributes
111
+ global_attributes.each do |att|
112
+ p att.name
113
+ end
114
+
115
+ #=================================================================================
116
+ # Adding dimensions
117
+ #=================================================================================
118
+
119
+ dimension "dim1", 5
120
+ @outside_scope.assert_equal(5, @dim_dim1.length)
121
+ @outside_scope.assert_equal("dim1", @dim_dim1.name)
122
+ @outside_scope.assert_equal(true, @dim_dim1.shared?)
123
+ @outside_scope.assert_equal(false, @dim_dim1.unlimited?)
124
+ @outside_scope.assert_equal(false, @dim_dim1.variable_length?)
125
+
126
+ dimension "dim2", 10
127
+ @outside_scope.assert_equal(true, @dim_dim2.shared?)
128
+
129
+ # create an unlimited dimension by setting the size to 0
130
+ dimension "dim3", 0
131
+ @outside_scope.assert_equal(true, @dim_dim3.shared?)
132
+ @outside_scope.assert_equal(true, @dim_dim3.unlimited?)
133
+ @outside_scope.assert_equal(false, @dim_dim3.variable_length?)
134
+
135
+ # Cannot create a variable_length dimension in NetCDF-3 file. Might
136
+ # work on NetCDF-4. Not tested.
137
+ # dimension "dim4", -1
138
+ # @outside_scope.assert_equal(true, @dim_dim4.variable_length?)
139
+
140
+ # Create a dimension that is not shared. Don't know exactly what happens
141
+ # in NetCDF-3 files. It does not give any bugs but the dimension does
142
+ # not appear on a write_cdl call.
143
+ dimension "dim5", 5, false
144
+ @outside_scope.assert_equal("dim5", @dim_dim5.name)
145
+ @outside_scope.assert_equal(false, @dim_dim5.shared?)
146
+
147
+ #=================================================================================
148
+ # Adding variables
149
+ #=================================================================================
150
+
151
+ # One dimensional variable
152
+ variable "arr1", "int", [@dim_dim1]
153
+ variable_att @var_arr1, "description", "this is a variable"
154
+ variable_att @var_arr1, "size", 5
155
+ variable_att @var_arr1, "date", 10
156
+ variable_att @var_arr1, "String List", ["abc", "def"]
157
+ variable_att @var_arr1, "Float list", [1.37, 5.18]
158
+
159
+ # two dimensional variable string variable is actually a three dimensional
160
+ # variable. NetCDF-3 does not support string variable, so this variable
161
+ # is transformed to a "char" variable with one extra dimension, the string
162
+ # size
163
+ variable "var3", "int", [@dim_dim3]
164
+ variable "var4", "string", [@dim_dim3, @dim_dim2]
165
+ variable "var5", "double", [@dim_dim3, @dim_dim2]
166
+
167
+ # controlling the size of the string variable
168
+ variable "Short", "string", [@dim_dim2], {:max_strlen => 20}
169
+ @outside_scope.assert_equal("Short", @var_short.name)
170
+
171
+ # double variable with fixed size dimensions
172
+ variable "Double", "double", [@dim_dim1, @dim_dim2]
173
+
174
+ # double variable with an unlimited dimension. Unlimited dimension must be the
175
+ # first dimension in NetCDF-3 files.
176
+ variable "Double3", "double", [@dim_dim3, @dim_dim1, @dim_dim2]
177
+ variable_att @var_double3, "_FillValue", -1
178
+
179
+ variable "Scalar", "double", []
180
+
181
+ #=================================================================================
182
+ # Add additional flags
183
+ #=================================================================================
184
+
185
+ # set fill to true... all elements should be filled with the fill_value
186
+ fill = true
187
+ large_file = true
188
+
189
+ end
190
+
191
+ end
192
+
193
+ #=begin
194
+ #-------------------------------------------------------------------------------------
195
+ # Opens a NetCDF file for writing data
196
+ #-------------------------------------------------------------------------------------
197
+
198
+ should "open a file for writing data" do
199
+
200
+ NetCDF.write(cygpath(@directory), "nc_output", self) do
201
+
202
+ # create some data to add to variable
203
+ array = MDArray.typed_arange("double", 0, 50)
204
+ array2 = MDArray.typed_arange("double", 0, 10)
205
+ array3 = MDArray.typed_arange("double", 0, 6)
206
+ string_array = MDArray.string([5], ["this", "is", "a", "string", "array"])
207
+
208
+ var = find_variable("Double")
209
+ @outside_scope.assert_equal("Double", var.name)
210
+ # reshape the first array to fit the variable shape
211
+ array.reshape!([5, 10])
212
+ # Fill variable Double with data. The whole variable gets data
213
+ write(var, array)
214
+
215
+ # writing data only to the last dimension
216
+ array2.reshape!([1, 10])
217
+ write(var, array2, [4, 0])
218
+
219
+ # writing data to a variable with unlimited dimension. Writing on the third
220
+ # index of the unlimited dimension. Array needs to be reshaped to the same
221
+ # shape as the variable
222
+ var3 = find_variable("Double3")
223
+ @outside_scope.assert_equal("Double3", var3.name)
224
+ array.reshape!([1, 5, 10])
225
+ write(var3, array, [2, 0, 0])
226
+
227
+ # adding data to a subsection of the variable
228
+ array3.reshape!([1, 1, 6])
229
+ write(var3, array3, [1, 1, 1])
230
+
231
+ # reshaping the data in another way
232
+ array3.reshape!([1, 3, 2])
233
+ write(var3, array3, [0, 2, 3])
234
+
235
+ # writing string data
236
+ short = find_variable("Short")
237
+ @outside_scope.assert_equal("Short", short.name)
238
+ write_string(short, string_array)
239
+
240
+ var4 = find_variable("var4")
241
+ string_array.reshape!([1, 5])
242
+ @outside_scope.assert_equal("var4", var4.name)
243
+ write_string(var4, string_array)
244
+
245
+ scalar = find_variable("Scalar")
246
+ @outside_scope.assert_equal("Scalar", scalar.name)
247
+ write(scalar, 5.34)
248
+
249
+ end
250
+
251
+ end
252
+ #=end
253
+ #-------------------------------------------------------------------------------------
254
+ # Opens a NetCDF file for reading only
255
+ #-------------------------------------------------------------------------------------
256
+ #=begin
257
+ should "open a file just for reading" do
258
+
259
+ # Opens a file for definition, passing the directory and file name.
260
+ # Creates a new scope for defining the NetCDF file. In order to access the
261
+ # ouside scope, that is, "here", we pass self as the third argument to define.
262
+ # When self is passed as argument, @outside_scope is available inside the block.
263
+
264
+ NetCDF.read(cygpath(@directory), "nc_output", self) do
265
+
266
+ # print all global attributes
267
+ global_attributes.each do |att|
268
+ p att.name
269
+ end
270
+
271
+ att = find_global_attribute("Fixnum")
272
+ @outside_scope.assert_equal("Fixnum", att.name)
273
+
274
+ # att
275
+
276
+ dim1 = find_dimension("dim1")
277
+ @outside_scope.assert_equal(5, dim1.length)
278
+ @outside_scope.assert_equal("dim1", dim1.name)
279
+ @outside_scope.assert_equal(true, dim1.shared?)
280
+ @outside_scope.assert_equal(false, dim1.unlimited?)
281
+ @outside_scope.assert_equal(false, dim1.variable_length?)
282
+
283
+ unlimited = find_unlimited_dimension
284
+ @outside_scope.assert_equal("dim3", unlimited.name)
285
+
286
+ # Although adding an unshared dimension seemed to work on NetCDF-3 file,
287
+ # trying to retrieve this dimension does not work.
288
+ # unshared = find_dimension("dim5")
289
+ # @outside_scope.assert_equal("dim5", unshared.name)
290
+
291
+ # reading the whole Double variable
292
+ var = find_variable("Double")
293
+ var.read
294
+
295
+ # reading a section of Double using origin and shape
296
+ var.read(:origin => [1, 1], :shape => [2, 3])
297
+
298
+ # reading a section of Double using a section specification
299
+ var.read(:spec => "0:4:2, 4:9:3")
300
+
301
+ # reading the whole Double3 variable
302
+ var2 = find_variable("Double3")
303
+ var2.read
304
+
305
+ # makes a new variable as a subsection of Double3
306
+ var3 = var2.section(:origin => [0, 1, 1], :shape => [1, 4, 9])
307
+
308
+ # reading a section of Double3 using origin and shape
309
+ var2.read(:origin => [0, 1, 1], :shape => [1, 4, 9])
310
+
311
+ write_cdl
312
+
313
+ p detail_info
314
+ p file_type_description
315
+ p file_type_id
316
+ p id
317
+ p last_modified
318
+ p location
319
+ p title
320
+ p unlimited_dimension?
321
+
322
+ end
323
+
324
+ end
325
+ #=end
326
+
327
+ end
328
+
329
+ end
330
+
331
+ # require_relative 'test_redefine'
@@ -0,0 +1,120 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'mdarray'
27
+
28
+ class MDArrayTest < Test::Unit::TestCase
29
+
30
+ context "NetCDF implementation" do
31
+
32
+ setup do
33
+
34
+ @directory = "/home/zxb3/tmp"
35
+
36
+ end
37
+
38
+
39
+ #-------------------------------------------------------------------------------------
40
+ #
41
+ #-------------------------------------------------------------------------------------
42
+
43
+ should "Open a file for redefinition" do
44
+
45
+ NetCDF.redefine(cygpath(@directory), "nc_output", self) do
46
+
47
+ # rename_global_att("Values", "New_values")
48
+ # delete_global_att("Values")
49
+ rename_dimension("dim1", "new_dim1")
50
+ # rename_variable("arr1", "new_arr1")
51
+
52
+ var1 = find_variable("arr1")
53
+ @outside_scope.assert_equal("arr1", var1.name)
54
+ delete_variable_att(var1, "description")
55
+ rename_variable_att(var1, "size", "new_size")
56
+
57
+ p var1.extra_info
58
+ att = var1.find_attribute("date")
59
+ @outside_scope.assert_equal("date", att.name)
60
+ @outside_scope.assert_equal(0, var1.find_dimension_index("new_dim1"))
61
+ atts = var1.find_attributes
62
+ atts.each do |att|
63
+ p att.name
64
+ end
65
+ @outside_scope.assert_equal("int", var1.get_data_type)
66
+ p var1.get_description
67
+ dim = var1.get_dimension(0)
68
+ p dim.name
69
+ p var1.find_dimensions
70
+ p var1.get_dimensions_string
71
+ p var1.get_element_size
72
+ p var1.get_name_and_dimensions
73
+ p var1.rank
74
+ p var1.shape
75
+ p var1.size
76
+ p var1.get_units_string
77
+
78
+ end
79
+
80
+ end
81
+
82
+ #-------------------------------------------------------------------------------------
83
+ # Opens a NetCDF file for reading only
84
+ #-------------------------------------------------------------------------------------
85
+
86
+ should "open a file just for reading" do
87
+
88
+ # Opens a file for definition, passing the directory and file name.
89
+ # Creates a new scope for defining the NetCDF file. In order to access the
90
+ # ouside scope, that is, "here", we pass self as the third argument to define.
91
+ # When self is passed as argument, @outside_scope is available inside the block.
92
+
93
+ reader = NetCDF.read(cygpath(@directory), "nc_output", self) do
94
+ =begin
95
+ # print all global attributes
96
+ global_attributes.each do |att|
97
+ p att.name
98
+ end
99
+ =end
100
+ write_cdl
101
+
102
+ =begin
103
+ p detail_info
104
+ p file_type_description
105
+ p file_type_id
106
+ p id
107
+ p last_modified
108
+ p location
109
+ p title
110
+ p unlimited_dimension?
111
+ =end
112
+
113
+ end
114
+
115
+ end
116
+
117
+
118
+ end
119
+
120
+ end