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,205 @@
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
+ class NetCDF
23
+
24
+ ########################################################################################
25
+ # A Group is a logical collection of Variables. The Groups in a Dataset form a
26
+ # hierarchical tree, like directories on a disk. A Group has a name and optionally a
27
+ # set of Attributes. There is always at least one Group in a dataset, the root Group,
28
+ # whose name is the empty string.
29
+ #
30
+ # Immutable if setImmutable() was called.
31
+ ########################################################################################
32
+
33
+ class Group < CDMNode
34
+
35
+ #------------------------------------------------------------------------------------
36
+ #
37
+ #------------------------------------------------------------------------------------
38
+
39
+ def common_parent(group)
40
+ @netcdf_elmt.commomParent(group.netcdf_elmt)
41
+ end
42
+
43
+ #------------------------------------------------------------------------------------
44
+ #
45
+ #------------------------------------------------------------------------------------
46
+
47
+ def find_attribute(name, ignore_case = false)
48
+ if (ignore_case)
49
+ NetCDF::Attribute.new(@netcdf_elmt.findAttributeIgnoreCase(name))
50
+ else
51
+ NetCDF::Attribute.new(@netcdf_elmt.findAttribute(name))
52
+ end
53
+ end
54
+
55
+ #------------------------------------------------------------------------------------
56
+ # Retrieve a Dimension using its (short) name. If local is true, then
57
+ # retrieve a Dimension using its (short) name, in this group only.
58
+ #------------------------------------------------------------------------------------
59
+
60
+ def find_dimension(name, local = false)
61
+ if (local)
62
+ NetCDF::Dimension.new(@netcdf_elmt.findDimensionLocal(name))
63
+ else
64
+ NetCDF::Dimension.new(@netcdf_elmt.findDimension(name))
65
+ end
66
+ end
67
+
68
+ #------------------------------------------------------------------------------------
69
+ # Retrieve the Group with the specified (short) name.
70
+ #------------------------------------------------------------------------------------
71
+
72
+ def find_group(name)
73
+ NetCDF::Group.new(@netcdf_elmt.findGroup(name))
74
+ end
75
+
76
+ #------------------------------------------------------------------------------------
77
+ #
78
+ #------------------------------------------------------------------------------------
79
+
80
+ def find_variable(name, local = true)
81
+
82
+ if (local)
83
+ NetCDF::Variable.new(@netcdf_elmt.findVariable(name))
84
+ else
85
+ NetCDF::Variable.new(@netcdf_elmt.findVariableOrInParent(name))
86
+ end
87
+
88
+ end
89
+
90
+ #------------------------------------------------------------------------------------
91
+ #
92
+ #------------------------------------------------------------------------------------
93
+
94
+ def attributes
95
+
96
+ java_attributes = @netcdf_elmt.getAttributes()
97
+ attributes = Array.new
98
+ java_attributes.each do |att|
99
+ attributes << NetCDF::Attribute.new(att)
100
+ end
101
+ attributes
102
+
103
+ end
104
+
105
+ #------------------------------------------------------------------------------------
106
+ #
107
+ #------------------------------------------------------------------------------------
108
+
109
+ def to_string
110
+ @netcdf_elmt.toString()
111
+ end
112
+
113
+ end
114
+
115
+ #=======================================================================================
116
+ # GroupWriter is a group that allows for writing data on it.
117
+ #=======================================================================================
118
+
119
+ class GroupWriter < Group
120
+
121
+ #------------------------------------------------------------------------------------
122
+ #
123
+ #------------------------------------------------------------------------------------
124
+
125
+ def add_attribute(attribute)
126
+ @netcdf_elmt.addAttribute(attribute.netcdf_elmt)
127
+ end
128
+
129
+ #------------------------------------------------------------------------------------
130
+ #
131
+ #------------------------------------------------------------------------------------
132
+
133
+ def add_dimension(dimension)
134
+ @netcdf_elmt.addDimension(dimension.netcdf_elmt)
135
+ end
136
+
137
+ #------------------------------------------------------------------------------------
138
+ #
139
+ #------------------------------------------------------------------------------------
140
+
141
+ def add_dimension_if_not_exists(dimension)
142
+ @netcdf_elmt.addDimensionIfNotExists(dimension.netcdf_elmt)
143
+ end
144
+
145
+ #------------------------------------------------------------------------------------
146
+ #
147
+ #------------------------------------------------------------------------------------
148
+
149
+ def add_group(group)
150
+ @netcdf_elmt.addGroup(group.netcdf_elmt)
151
+ end
152
+
153
+ #------------------------------------------------------------------------------------
154
+ #
155
+ #------------------------------------------------------------------------------------
156
+
157
+ def add_variable(variable)
158
+ @netcdf_elmt.addVariable(variable.netcdf_elmt)
159
+ end
160
+
161
+ #------------------------------------------------------------------------------------
162
+ #
163
+ #------------------------------------------------------------------------------------
164
+
165
+ def remove(elmt)
166
+ @netcdf_elmt.remove(elmt.netcdf_elmt)
167
+ end
168
+
169
+ #------------------------------------------------------------------------------------
170
+ # remove a Dimension using its name, in this group only
171
+ #------------------------------------------------------------------------------------
172
+
173
+ def remove_dimension(name)
174
+ @netcdf_elmt.removeDimension(name)
175
+ end
176
+
177
+ #------------------------------------------------------------------------------------
178
+ # remove a Variable using its (short) name, in this group only
179
+ #------------------------------------------------------------------------------------
180
+
181
+ def remove_variable(name)
182
+ @netcdf_elmt.removeVariable(name)
183
+ end
184
+
185
+ #------------------------------------------------------------------------------------
186
+ #
187
+ #------------------------------------------------------------------------------------
188
+
189
+ def name=(name)
190
+ @netcdf_elmt.setName(name)
191
+ end
192
+
193
+ #------------------------------------------------------------------------------------
194
+ # Set the Group's parent Group
195
+ #------------------------------------------------------------------------------------
196
+
197
+ def parent_group=(parent)
198
+ @netcdf_elmt.setParentGroup(parent.netcdf_elmt)
199
+ end
200
+
201
+
202
+ end # GroupWriter
203
+
204
+
205
+ end
@@ -0,0 +1,151 @@
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
+
23
+ #======================================================================================
24
+ #
25
+ #======================================================================================
26
+
27
+ class NetCDF
28
+ include_package "ucar.nc2"
29
+ include_package "ucar.ma2"
30
+
31
+ #------------------------------------------------------------------------------------
32
+ #
33
+ #------------------------------------------------------------------------------------
34
+
35
+ def self.define(home_dir, file_name, version, outside_scope = nil, &block)
36
+
37
+ writer = NetCDF::FileWriter.new_file(home_dir, file_name, version, outside_scope)
38
+ begin
39
+ writer.open
40
+ writer.instance_eval(&block)
41
+ writer.create
42
+ writer.close
43
+ writer
44
+ rescue
45
+ writer.close
46
+ raise "Illegal opperation occured on file: #{writer.file_name}"
47
+ end
48
+
49
+ end
50
+
51
+ #------------------------------------------------------------------------------------
52
+ #
53
+ #------------------------------------------------------------------------------------
54
+
55
+ def self.redefine(home_dir, file_name, outside_scope = nil, &block)
56
+
57
+ writer = NetCDF::FileWriter.existing_file(home_dir, file_name, outside_scope)
58
+ begin
59
+ writer.open
60
+ writer.redefine = true
61
+ # we are not really adding a root group, but actually getting the root group
62
+ # can only be done in define mode
63
+ writer.add_root_group
64
+ writer.instance_eval(&block)
65
+ writer.redefine = false
66
+ writer.close
67
+ writer
68
+ rescue Exception =>e
69
+ writer.close
70
+ p e.message
71
+ # p e.backtrace.inspect
72
+ # raise "Illegal opperation occured on file: #{writer.file_name}"
73
+ raise e
74
+ end
75
+
76
+ end
77
+
78
+ #------------------------------------------------------------------------------------
79
+ #
80
+ #------------------------------------------------------------------------------------
81
+
82
+ def self.write(home_dir, file_name, outside_scope = nil, &block)
83
+
84
+ writer = NetCDF::FileWriter.existing_file(home_dir, file_name, outside_scope)
85
+
86
+ begin
87
+ writer.open
88
+ writer.instance_eval(&block)
89
+ writer.close
90
+ writer
91
+ rescue Exception => e
92
+ writer.close
93
+ # raise "Illegal opperation occured on file: #{file_name}"
94
+ raise e
95
+ end
96
+
97
+ end
98
+
99
+ #------------------------------------------------------------------------------------
100
+ #
101
+ #------------------------------------------------------------------------------------
102
+
103
+ def self.read(home_dir, file_name, outside_scope = nil, &block)
104
+
105
+ reader = NetCDF::File.new(home_dir, file_name, outside_scope)
106
+ begin
107
+ reader.open
108
+ reader.instance_eval(&block)
109
+ reader.close
110
+ reader
111
+ rescue Exception => e
112
+ reader.close
113
+ p e.message
114
+ # raise "Illegal opperation occured on file: #{file_name}"
115
+ raise e
116
+ end
117
+
118
+ end
119
+
120
+ #------------------------------------------------------------------------------------
121
+ # Outputs the data format
122
+ #------------------------------------------------------------------------------------
123
+
124
+ def self.output(file_name)
125
+ print `ncdump #{file_name}`
126
+ end
127
+
128
+ #------------------------------------------------------------------------------------
129
+ #
130
+ #------------------------------------------------------------------------------------
131
+
132
+ def self.get_dtype(type)
133
+ DataType.valueOf(type.upcase)
134
+ end
135
+
136
+ #------------------------------------------------------------------------------------
137
+ #
138
+ #------------------------------------------------------------------------------------
139
+
140
+ def self.writer_version(name)
141
+ NetcdfFileWriter::Version.valueOf(name)
142
+ end
143
+
144
+ #------------------------------------------------------------------------------------
145
+ #
146
+ #------------------------------------------------------------------------------------
147
+
148
+ end # NetCDF
149
+
150
+ require_relative 'cdm_node'
151
+ require_relative 'file'
@@ -0,0 +1,520 @@
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
+ class NetCDF
23
+
24
+ #=======================================================================================
25
+ # A Variable is a logical container for data. It has a dataType, a set of Dimensions
26
+ # that define its array shape, and optionally a set of Attributes. The data is a
27
+ # multidimensional array of primitive types, Strings, or Structures. Data access is
28
+ # done through the read() methods, which return a memory resident Array.
29
+ # Immutable if setImmutable() was called.
30
+ #=======================================================================================
31
+
32
+ class Variable < CDMNode
33
+
34
+ attr_reader :attributes
35
+ attr_reader :dimensions
36
+ attr_reader :index_iterator
37
+
38
+ #------------------------------------------------------------------------------------
39
+ #
40
+ #------------------------------------------------------------------------------------
41
+
42
+ def extra_info
43
+ @netcdf_elmt.extraInfo()
44
+ end
45
+
46
+ #------------------------------------------------------------------------------------
47
+ # Create a new data cache, use this when you dont want to share the cache.
48
+ #------------------------------------------------------------------------------------
49
+
50
+ def create_new_cache
51
+ @netcdf_elmt.createNewCache()
52
+ end
53
+
54
+ #------------------------------------------------------------------------------------
55
+ # Finds an attribute by name
56
+ #------------------------------------------------------------------------------------
57
+
58
+ def find_attribute(name, ignore_case = false)
59
+
60
+ if (ignore_case)
61
+ @netcdf_elmt.findAttributeIgnoreCase(name)
62
+ else
63
+ @netcdf_elmt.findAttribute(name)
64
+ end
65
+
66
+ end
67
+
68
+ #------------------------------------------------------------------------------------
69
+ # Find the index of the named Dimension in this Variable.
70
+ #------------------------------------------------------------------------------------
71
+
72
+ def find_dimension_index(name)
73
+ @netcdf_elmt.findDimensionIndex(name)
74
+ end
75
+
76
+ #------------------------------------------------------------------------------------
77
+ # Returns the set of attributes for this variable.
78
+ #------------------------------------------------------------------------------------
79
+
80
+ def find_attributes
81
+
82
+ attributes = @netcdf_elmt.getAttributes()
83
+ atts = Array.new
84
+ attributes.each do |att|
85
+ atts << NetCDF::Attribute.new(att)
86
+ end
87
+ atts
88
+
89
+ end
90
+
91
+ #------------------------------------------------------------------------------------
92
+ # Gets the variable data_type
93
+ #------------------------------------------------------------------------------------
94
+
95
+ def get_data_type
96
+ @netcdf_elmt.getDataType().toString()
97
+ end
98
+
99
+ #------------------------------------------------------------------------------------
100
+ # Get the description of the Variable.
101
+ #------------------------------------------------------------------------------------
102
+
103
+ def get_description
104
+ @netcdf_elmt.getDescription()
105
+ end
106
+
107
+ #------------------------------------------------------------------------------------
108
+ # Get the ith dimension.
109
+ #------------------------------------------------------------------------------------
110
+
111
+ def get_dimension(index)
112
+ @netcdf_elmt.getDimension(index)
113
+ end
114
+
115
+ #------------------------------------------------------------------------------------
116
+ # Get the list of dimensions used by this variable.
117
+ #------------------------------------------------------------------------------------
118
+
119
+ def find_dimensions
120
+
121
+ dimensions = @netcdf_elmt.getDimensions()
122
+ dims = Array.new
123
+ dimensions.each do |dim|
124
+ dims << NetCDF::Dimension.new(dim)
125
+ end
126
+ dims
127
+
128
+ end
129
+
130
+ #------------------------------------------------------------------------------------
131
+ # Get the list of Dimension names, space delineated.
132
+ #------------------------------------------------------------------------------------
133
+
134
+ def get_dimensions_string
135
+ @netcdf_elmt.getDimensionsString()
136
+ end
137
+
138
+ #------------------------------------------------------------------------------------
139
+ # Get the number of bytes for one element of this Variable.
140
+ #------------------------------------------------------------------------------------
141
+
142
+ def get_element_size
143
+ @netcdf_elmt.getElementSize()
144
+ end
145
+
146
+ #------------------------------------------------------------------------------------
147
+ # Get the display name plus the dimensions, eg 'float name(dim1, dim2)'
148
+ #------------------------------------------------------------------------------------
149
+
150
+ def get_name_and_dimensions
151
+ @netcdf_elmt.getNameAndDimensions()
152
+ end
153
+
154
+ #------------------------------------------------------------------------------------
155
+ # Get the number of dimensions of the Variable.
156
+ #------------------------------------------------------------------------------------
157
+
158
+ def get_rank
159
+ @netcdf_elmt.getRank()
160
+ end
161
+
162
+ alias :rank :get_rank
163
+
164
+ #------------------------------------------------------------------------------------
165
+ # Get the shape: length of Variable in each dimension.
166
+ #------------------------------------------------------------------------------------
167
+
168
+ def get_shape(index = nil)
169
+ if (index)
170
+ @netcdf_elmt.getShape(index)
171
+ else
172
+ @netcdf_elmt.getShape().to_a
173
+ end
174
+ end
175
+
176
+ alias :shape :get_shape
177
+
178
+ #------------------------------------------------------------------------------------
179
+ # Get the total number of elements in the Variable.
180
+ #------------------------------------------------------------------------------------
181
+
182
+ def get_size
183
+ @netcdf_elmt.getSize()
184
+ end
185
+
186
+ alias :size :get_size
187
+
188
+ #------------------------------------------------------------------------------------
189
+ # Get the Unit String for the Variable.
190
+ #------------------------------------------------------------------------------------
191
+
192
+ def get_units_string
193
+ @netcdf_elmt.getUnitsString()
194
+ end
195
+
196
+ #------------------------------------------------------------------------------------
197
+ # Has data been read and cached.
198
+ #------------------------------------------------------------------------------------
199
+
200
+ def cached_data?
201
+ @netcdf_elmt.hasCachedData()
202
+ end
203
+
204
+ #------------------------------------------------------------------------------------
205
+ # Invalidate the data cache
206
+ #------------------------------------------------------------------------------------
207
+
208
+ def invalidate_cache
209
+ @netcdf_elmt.invalidateCache()
210
+ end
211
+
212
+ #------------------------------------------------------------------------------------
213
+ # Will this Variable be cached when read.
214
+ #------------------------------------------------------------------------------------
215
+
216
+ def caching?
217
+ @netcdf_elmt.isCaching()
218
+ end
219
+
220
+ #------------------------------------------------------------------------------------
221
+ # Calculate if this is a classic coordinate variable: has same name as its first
222
+ # dimension.
223
+ #------------------------------------------------------------------------------------
224
+
225
+ def coordinate_variable?
226
+ @netcdf_elmt.isCoordinateVariable()
227
+ end
228
+
229
+ #------------------------------------------------------------------------------------
230
+ # Is this Variable immutable
231
+ #------------------------------------------------------------------------------------
232
+
233
+ def immutable?
234
+ @netcdf_elmt.isImmutable()
235
+ end
236
+
237
+ #------------------------------------------------------------------------------------
238
+ # Is this variable metadata?.
239
+ #------------------------------------------------------------------------------------
240
+
241
+ def metadata?
242
+ @netcdf_elmt.isMetadata()
243
+ end
244
+
245
+ #------------------------------------------------------------------------------------
246
+ # Whether this is a scalar Variable (rank == 0).
247
+ #------------------------------------------------------------------------------------
248
+
249
+ def scalar?
250
+ @netcdf_elmt.isScalar()
251
+ end
252
+
253
+ #------------------------------------------------------------------------------------
254
+ # Can this variable's size grow?.
255
+ #------------------------------------------------------------------------------------
256
+
257
+ def unlimited?
258
+ @netcdf_elmt.isUnlimited()
259
+ end
260
+
261
+ #------------------------------------------------------------------------------------
262
+ # Is this Variable unsigned?.
263
+ #------------------------------------------------------------------------------------
264
+
265
+ def unsigned?
266
+ @netcdf_elmt.isUnsigned()
267
+ end
268
+
269
+ #------------------------------------------------------------------------------------
270
+ # Does this variable have a variable length dimension? If so, it has as one of its
271
+ # mensions Dimension.VLEN.
272
+ #------------------------------------------------------------------------------------
273
+
274
+ def variable_length?
275
+ @netcdf_elmt.isVariableLength()
276
+ end
277
+
278
+ #------------------------------------------------------------------------------------
279
+ # Reads data for this Variable and sets the variable @data to the memory resident
280
+ # array.
281
+ # <tt>:origin</tt> int array with the origin of data to be read
282
+ # <tt>:shape</tt> int array with the shape of the data to be read. If origin or
283
+ #------------------------------------------------------------------------------------
284
+
285
+ def read(*args)
286
+
287
+ opts = Map.options(args)
288
+ spec = opts.getopt(:spec)
289
+ origin = opts.getopt(:origin)
290
+ shape = opts.getopt(:shape)
291
+
292
+ if (origin || shape)
293
+ MDArray.build_from_nc_array(nil, @netcdf_elmt.read(origin.to_java(:int),
294
+ shape.to_java(:int)))
295
+ elsif (spec)
296
+ MDArray.build_from_nc_array(nil, @netcdf_elmt.read(spec))
297
+ else
298
+ MDArray.build_from_nc_array(nil, @netcdf_elmt.read())
299
+ end
300
+
301
+ end
302
+
303
+ #------------------------------------------------------------------------------------
304
+ #
305
+ #------------------------------------------------------------------------------------
306
+
307
+ def read_scalar(type = nil)
308
+
309
+ if (!type)
310
+ type = get_data_type
311
+ end
312
+
313
+ case type
314
+ when "double"
315
+ @netcdf_elmt.readScalarDouble()
316
+ when "float"
317
+ @netcdf_elmt.readScalarFloat()
318
+ when "long"
319
+ @netcdf_elmt.readScalarLong()
320
+ when "int"
321
+ @netcdf_elmt.readScalarInt()
322
+ when "short"
323
+ @netcdf_elmt.readScalarShort()
324
+ when "byte"
325
+ @netcdf_elmt.readScalarByte()
326
+ when "string"
327
+ @netcdf_elmt.readScalarString()
328
+ else
329
+ raise "Unknown type: #{type}"
330
+ end
331
+
332
+ end
333
+
334
+ #------------------------------------------------------------------------------------
335
+ # Set the data cache
336
+ #------------------------------------------------------------------------------------
337
+
338
+ def set_cached_data(array, metadata = nil)
339
+ @netcdf_elmt.setCachedData(array.nc_array, metadata)
340
+ end
341
+
342
+ #------------------------------------------------------------------------------------
343
+ #
344
+ #------------------------------------------------------------------------------------
345
+
346
+ def caching=(boolean)
347
+ @netcdf_elmt.setCaching(boolean)
348
+ end
349
+
350
+ #------------------------------------------------------------------------------------
351
+ # Create a new Variable that is a logical subsection of this Variable. The
352
+ # subsection can be specified passing the following arguments:
353
+ # shape
354
+ # origin
355
+ # size
356
+ # stride
357
+ # range
358
+ # section
359
+ # spec
360
+ #------------------------------------------------------------------------------------
361
+
362
+ def section(*args)
363
+
364
+ sec = MDArray::Section.build(*args)
365
+ NetCDF::Variable.new(@netcdf_elmt.section(sec.netcdf_elmt))
366
+
367
+ end
368
+
369
+ #------------------------------------------------------------------------------------
370
+ #
371
+ #------------------------------------------------------------------------------------
372
+
373
+ def set_cached_data(array, metadata)
374
+ @netcdf_elmt.setCachedData(array.nc_array, metadata)
375
+ end
376
+
377
+ #------------------------------------------------------------------------------------
378
+ # Prints the content of the current data slice
379
+ #------------------------------------------------------------------------------------
380
+
381
+ def to_string
382
+ @netcdf_elmt.toString()
383
+ end
384
+
385
+ alias :to_s :to_string
386
+
387
+ #------------------------------------------------------------------------------------
388
+ #
389
+ #------------------------------------------------------------------------------------
390
+
391
+ def to_string_debug
392
+ @netcdf_elmt.toSringDebug()
393
+ end
394
+
395
+ #------------------------------------------------------------------------------------
396
+ # Prints the content of the current data slice
397
+ #------------------------------------------------------------------------------------
398
+
399
+ def print
400
+ p to_string
401
+ end
402
+
403
+ end # Variable
404
+
405
+ #=======================================================================================
406
+ #
407
+ #=======================================================================================
408
+
409
+ class VariableWriter < Variable
410
+
411
+ #------------------------------------------------------------------------------------
412
+ # Remove an Attribute : uses the attribute hashCode to find it.
413
+ #------------------------------------------------------------------------------------
414
+
415
+ def remove_attribute(attribute, ignore_case = false)
416
+
417
+ if (attribute.is_a? String)
418
+ if (ignore_case)
419
+ @netcdf_elmt.removeAttributeIgnoreCase(attribute)
420
+ else
421
+ @netcdf_elmt.removeAttribute(attribute)
422
+ end
423
+ elsif (attribute.is_a? NetCDF::Attribute)
424
+ @netcdf_elmt.remove(attribute.netcdf_elmt)
425
+ else
426
+ raise "Given parameter is not an attribute: #{attribute}"
427
+ end
428
+
429
+ end
430
+
431
+ #------------------------------------------------------------------------------------
432
+ # Reset the dimension array.
433
+ #------------------------------------------------------------------------------------
434
+
435
+ def reset_dimensions
436
+ @netcdf_elmt.resetDimensions()
437
+ end
438
+
439
+ #------------------------------------------------------------------------------------
440
+ # Use when dimensions have changed, to recalculate the shape.
441
+ #------------------------------------------------------------------------------------
442
+
443
+ def reset_shape
444
+ @netcdf_elmt.resetShape()
445
+ end
446
+
447
+ #------------------------------------------------------------------------------------
448
+ #
449
+ #------------------------------------------------------------------------------------
450
+ #------------------------------------------------------------------------------------
451
+ #
452
+ #------------------------------------------------------------------------------------
453
+ #------------------------------------------------------------------------------------
454
+ #
455
+ #------------------------------------------------------------------------------------
456
+ #------------------------------------------------------------------------------------
457
+ #
458
+ #------------------------------------------------------------------------------------
459
+ #------------------------------------------------------------------------------------
460
+ #
461
+ #------------------------------------------------------------------------------------
462
+ #------------------------------------------------------------------------------------
463
+ #
464
+ #------------------------------------------------------------------------------------
465
+
466
+ end # VariableWriter
467
+
468
+
469
+ #=======================================================================================
470
+ #
471
+ #=======================================================================================
472
+
473
+ class TimeVariable < Variable
474
+ include_package "ucar.nc2.time"
475
+
476
+ attr_reader :calendar
477
+ attr_reader :units
478
+ attr_reader :base_date
479
+
480
+ #------------------------------------------------------------------------------------
481
+ # Initializes the Variable by giving a java netcdf_variable
482
+ #------------------------------------------------------------------------------------
483
+
484
+ def initialize(netcdf_variable)
485
+
486
+ super(netcdf_variable)
487
+ @calendar = @netcdf_elmt.findAttribute("calendar").getStringValue()
488
+ @units = @netcdf_elmt.findAttribute("units").getStringValue()
489
+ date_unit = CalendarDateUnit.of(@calendar, @units)
490
+ @base_date = date_unit.getBaseCalendarDate()
491
+
492
+ end
493
+
494
+ #------------------------------------------------------------------------------------
495
+ # Returns the number of milliseconds elapsed from the base_date to the given date.
496
+ # The given date should be in iso_format.
497
+ #------------------------------------------------------------------------------------
498
+
499
+ def to_msec(iso_date)
500
+
501
+ date = CalendarDate.parseISOformat(@calendar, iso_date)
502
+ date.getDifferenceInMsecs(@base_date)
503
+
504
+ end
505
+
506
+ #------------------------------------------------------------------------------------
507
+ #
508
+ #------------------------------------------------------------------------------------
509
+
510
+ def next
511
+
512
+ millisec_date = super
513
+ @base_date.add(millisec_date, CalendarPeriod.fromUnitString("Millisec")).toString()
514
+
515
+ end
516
+
517
+ end # TimeVariable
518
+
519
+
520
+ end # NetCDFInterface