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,474 @@
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
+ class FileWriter < FileParent
25
+ include_package "ucar.ma2"
26
+ include_package "ucar.nc2"
27
+
28
+ attr_reader :version
29
+ # Opens the same file just for reading. I don't know it this will work properly.
30
+ # Needed as the API for FileWriter lacks some interesting features. Or maybe not!
31
+ # I don't know... I might just be confused!
32
+ attr_reader :reader
33
+
34
+ #------------------------------------------------------------------------------------
35
+ # Writer for a new NetCDF file
36
+ #------------------------------------------------------------------------------------
37
+
38
+ def self.new_file(home_dir, name, version, outside_scope = nil)
39
+ FileWriter.new(home_dir, name, version, outside_scope)
40
+ end
41
+
42
+ #------------------------------------------------------------------------------------
43
+ # Writer for an existing NetCDF file
44
+ #------------------------------------------------------------------------------------
45
+
46
+ def self.existing_file(home_dir, name, outside_scope = nil)
47
+ FileWriter.new(home_dir, name, nil, outside_scope)
48
+ end
49
+
50
+ #------------------------------------------------------------------------------------
51
+ # Opens a netCDF file.
52
+ # * TODO: Check the case when reading should be shared
53
+ # * <tt>fill</tt> If true, or anything but false, the data is first written with fill
54
+ # values. Default is fill = false. Leave false if you expect to write all data values,
55
+ # set to true if you want to be sure that unwritten data values have the fill value
56
+ # in it.
57
+ #------------------------------------------------------------------------------------
58
+
59
+ def open
60
+
61
+ begin
62
+ if (@version)
63
+ @netcdf_elmt = NetcdfFileWriter.createNew(@version, @file_name)
64
+ add_root_group
65
+ else
66
+ @netcdf_elmt = NetcdfFileWriter.openExisting(@file_name)
67
+ end
68
+ rescue java.io.IOException => ioe
69
+ $stderr.print "Cannot open file: #{@file_name}"
70
+ $stderr.print ioe
71
+ end
72
+
73
+ end
74
+
75
+ #------------------------------------------------------------------------------------
76
+ # Adds a new group to the file.
77
+ #------------------------------------------------------------------------------------
78
+
79
+ def add_group(parent, name)
80
+ NetCDF::GroupWriter.new(@netcdf_elmt.addGroup(parent, name))
81
+ end
82
+
83
+ #------------------------------------------------------------------------------------
84
+ # Adds the root group to the file.
85
+ #------------------------------------------------------------------------------------
86
+
87
+ def add_root_group
88
+ @root_group = add_group(nil, "root")
89
+ end
90
+
91
+ #------------------------------------------------------------------------------------
92
+ # Adds a group attribute
93
+ #------------------------------------------------------------------------------------
94
+
95
+ def add_group_att(group, attribute)
96
+ group.add_attribute(attribute)
97
+ attribute
98
+ end
99
+
100
+ #------------------------------------------------------------------------------------
101
+ # Adds a global attribute
102
+ #------------------------------------------------------------------------------------
103
+
104
+ def add_global_att(name, value, type)
105
+ attribute = NetCDF::AttributeWriter.build(name, value, type)
106
+ add_group_att(@root_group, attribute)
107
+ end
108
+
109
+ #------------------------------------------------------------------------------------
110
+ # Adds new global attribute. A global attribute is a attribute added to the root
111
+ # group. In NetCDF 3 there is only the root group.
112
+ #------------------------------------------------------------------------------------
113
+
114
+ def global_att(name, value, type = "int")
115
+ symbol = ("ga_" + name.gsub(/\s+/, "_").downcase).to_sym
116
+ att = add_global_att(name, value, type)
117
+ instance_variable_set("@#{symbol}", att)
118
+ end
119
+
120
+ #------------------------------------------------------------------------------------
121
+ #
122
+ #------------------------------------------------------------------------------------
123
+
124
+ def find_global_attribute(name, ignore_case = false)
125
+
126
+ if (ignore_case)
127
+ att = @root_group.netcdf_elmt.findAttributeIgnoreCase(name)
128
+ else
129
+ att = @root_group.netcdf_elmt.findAttribute(name)
130
+ end
131
+
132
+ if (att != nil)
133
+ return NetCDF::AttributeWriter.new(att)
134
+ end
135
+
136
+ nil
137
+
138
+ end
139
+
140
+ #------------------------------------------------------------------------------------
141
+ # Add a Dimension to the file. Must be in define mode.
142
+ # <tt>dimName</tt> name of dimension (string)
143
+ # <tt>length</tt> size of dimension (int). If size == 0, dimension is unlimited
144
+ # <tt> is_shared<tt> if dimension is shared (boolean)
145
+ # if size == -1, then this is a variable_length dimension
146
+ # if size == 0, this is an unlimited dimension
147
+ # NetCDF3 only supports shared dimensions.
148
+ #------------------------------------------------------------------------------------
149
+
150
+ def add_dimension(name, size, is_shared = true)
151
+
152
+ is_unlimited = false
153
+ is_variable_length = false
154
+ dim = nil
155
+
156
+ if (size == -1)
157
+ is_variable_length = true
158
+ elsif (size == 0)
159
+ is_unlimited = true
160
+ end
161
+
162
+ NetCDF::DimensionWriter
163
+ .new(@netcdf_elmt.addDimension(@root_group.netcdf_elmt, "#{name}", size,
164
+ is_shared, is_unlimited, is_variable_length))
165
+
166
+ end
167
+
168
+ #------------------------------------------------------------------------------------
169
+ # Adds a new dimension.
170
+ #------------------------------------------------------------------------------------
171
+
172
+ def dimension(name, size, is_shared = true)
173
+ symbol = ("dim_" + name.gsub(/\s+/, "_").downcase).to_sym
174
+ instance_variable_set("@#{symbol}", add_dimension(name, size, is_shared))
175
+ end
176
+
177
+ #------------------------------------------------------------------------------------
178
+ # Add a variable to the file.
179
+ #------------------------------------------------------------------------------------
180
+
181
+ def add_variable(var_name, type, dims, *args)
182
+
183
+ opts = Map.options(args)
184
+ max_strlen = opts.getopt(:max_strlen, :default=>250)
185
+
186
+ # if dims is an array then make a java dimension list
187
+ dim_list = java.util.ArrayList.new
188
+ if (dims.is_a? Array)
189
+ dims.each do |dim|
190
+ dim_list.add(dim.netcdf_elmt)
191
+ end
192
+ end
193
+
194
+ case type
195
+ when "string"
196
+ NetCDF::VariableWriter.new(@netcdf_elmt
197
+ .addStringVariable(@root_group.netcdf_elmt, var_name,
198
+ dim_list, max_strlen))
199
+ else
200
+ NetCDF::VariableWriter.new(@netcdf_elmt
201
+ .addVariable(@root_group.netcdf_elmt, var_name,
202
+ DataType.valueOf(type.upcase), dim_list))
203
+ end
204
+
205
+ end
206
+
207
+ #------------------------------------------------------------------------------------
208
+ # Adds new variable
209
+ #------------------------------------------------------------------------------------
210
+
211
+ def variable(name, type, dims, *args)
212
+ symbol = ("var_" + name.gsub(/\s+/, "_").downcase).to_sym
213
+ instance_variable_set("@#{symbol}", add_variable(name, type, dims, *args))
214
+ end
215
+
216
+ #------------------------------------------------------------------------------------
217
+ # Finds a variable in the file
218
+ #------------------------------------------------------------------------------------
219
+
220
+ def find_variable(name)
221
+ var = @netcdf_elmt.findVariable(name)
222
+ var ? NetCDF::VariableWriter.new(var) : nil
223
+ end
224
+
225
+ #------------------------------------------------------------------------------------
226
+ # Adds a variable attribute
227
+ #------------------------------------------------------------------------------------
228
+
229
+ def add_variable_att(variable, name, value)
230
+ attribute = NetCDF::AttributeWriter.build(name, value)
231
+ @netcdf_elmt.addVariableAttribute(variable.netcdf_elmt, attribute.netcdf_elmt)
232
+ attribute
233
+ end
234
+
235
+ #------------------------------------------------------------------------------------
236
+ # Adds new variable attribute
237
+ #------------------------------------------------------------------------------------
238
+
239
+ def variable_att(variable, att_name, value)
240
+ symbol = ("va_" + variable.name + "_" + att_name.gsub(/\s+/, "_").downcase).to_sym
241
+ instance_variable_set("@#{symbol}",
242
+ add_variable_att(variable, att_name, value))
243
+ end
244
+
245
+ #------------------------------------------------------------------------------------
246
+ # Deletes a global attribute
247
+ #------------------------------------------------------------------------------------
248
+
249
+ def delete_global_att(attribute_name)
250
+ @netcdf_elmt.deleteGroupAttribute(@root_group.netcdf_elmt, attribute_name)
251
+ end
252
+
253
+ #------------------------------------------------------------------------------------
254
+ # Rename a global Attribute. Does not seem to work on NetCDF-3 files.
255
+ #------------------------------------------------------------------------------------
256
+
257
+ def rename_global_att(old_name, new_name)
258
+ @netcdf_elmt.renameGlobalAttribute(@root_group.netcdf_elmt, old_name, new_name)
259
+ end
260
+
261
+ #------------------------------------------------------------------------------------
262
+ # Rename a Dimension.
263
+ #------------------------------------------------------------------------------------
264
+
265
+ def rename_dimension(old_name, new_name)
266
+ @netcdf_elmt.renameDimension(@root_group.netcdf_elmt, old_name, new_name)
267
+ end
268
+
269
+
270
+
271
+ #------------------------------------------------------------------------------------
272
+ # Rename a Variable.
273
+ #------------------------------------------------------------------------------------
274
+
275
+ def rename_variable(old_name, new_name)
276
+ @netcdf_elmt.renameVariable(old_name, new_name)
277
+ end
278
+
279
+ #------------------------------------------------------------------------------------
280
+ # Deletes a variable attribute
281
+ #------------------------------------------------------------------------------------
282
+
283
+ def delete_variable_att(variable, att_name)
284
+ @netcdf_elmt.deleteVariableAttribute(variable.netcdf_elmt, att_name)
285
+ end
286
+
287
+ #------------------------------------------------------------------------------------
288
+ # Renames a variable attribute
289
+ #------------------------------------------------------------------------------------
290
+
291
+ def rename_variable_att(variable, att_name, new_name)
292
+ @netcdf_elmt.renameVariableAttribute(variable.netcdf_elmt, att_name, new_name)
293
+ end
294
+
295
+ #------------------------------------------------------------------------------------
296
+ # After you have added all of the Dimensions, Variables, and Attributes, call create
297
+ # to actually create the file. You must be in define mode. After this call, you are
298
+ # no longer in define mode.
299
+ #------------------------------------------------------------------------------------
300
+
301
+ def create
302
+
303
+ begin
304
+ @netcdf_elmt.create
305
+ rescue java.io.IOException => ioe
306
+ $stderr.print "Error accessing file: #{@file_name}"
307
+ $stderr.print ioe
308
+ end
309
+
310
+ end
311
+
312
+ #------------------------------------------------------------------------------------
313
+ # closes the file
314
+ #------------------------------------------------------------------------------------
315
+
316
+ def close
317
+ @netcdf_elmt.close
318
+ end
319
+
320
+ #------------------------------------------------------------------------------------
321
+ # Set if this should be a "large file" (64-bit offset) format.
322
+ #------------------------------------------------------------------------------------
323
+
324
+ def large_file=(bool)
325
+ @netcdf_elmt.setLargeFile(bool)
326
+ end
327
+
328
+ #------------------------------------------------------------------------------------
329
+ # Flush anything written to disk
330
+ #------------------------------------------------------------------------------------
331
+
332
+ def flush
333
+ @netcdf_elmt.flush
334
+ end
335
+
336
+ #------------------------------------------------------------------------------------
337
+ # Returns true if the file is in define mode
338
+ #------------------------------------------------------------------------------------
339
+
340
+ def define_mode?
341
+ @netcdf_elmt.isDefineMode()
342
+ end
343
+
344
+ #------------------------------------------------------------------------------------
345
+ # Set the fill flag. If fill flag is set then variable data is filled with fill
346
+ # value
347
+ #------------------------------------------------------------------------------------
348
+
349
+ def fill=(bool)
350
+ @netcdf_elmt.setFill(bool)
351
+ end
352
+
353
+ #------------------------------------------------------------------------------------
354
+ # Get a human-readable description for this file type.
355
+ #------------------------------------------------------------------------------------
356
+
357
+ def get_file_type_description
358
+ @netcdf_elmt.getFileTypeDescription()
359
+ end
360
+
361
+ #------------------------------------------------------------------------------------
362
+ # Switches redefine mode. if true allows data to be redefined, if false, redefine
363
+ # mode is closed.
364
+ # <tt>Returns</tt> true if it had to rewrite the entire file, false if it wrote the
365
+ # header in place
366
+ #------------------------------------------------------------------------------------
367
+
368
+ def redefine=(bool)
369
+
370
+ begin
371
+ @netcdf_elmt.setRedefineMode(bool)
372
+ rescue java.io.IOException => ioe
373
+ $stderr.print "Error accessing file: #{@file_name}"
374
+ $stderr.print ioe
375
+ end
376
+
377
+ end
378
+
379
+ #------------------------------------------------------------------------------------
380
+ # writes the given data with the given layout on variable at origin
381
+ # @nc_i.write(<variable>, <layout>, <origin>, <data>)
382
+ # if data is not given, then it is assumed to be all zeroes
383
+ # @nc_i.write(<variable>, <layout>, <origin>)
384
+ # <tt>var_name</tt> Name of the variable in which to write
385
+ # <tt>type</tt> type of the data.
386
+ # <tt>layout</tt> layout of the data represented by a rank array
387
+ # <tt>origin</tt> origin in the var in which to write the data. If origin = nil,
388
+ # then origin is <0>
389
+ # <tt>data</tt> data to write. If data = nil, then an all zeroes data is assumed.
390
+ #------------------------------------------------------------------------------------
391
+
392
+ def write(variable, values, origin = nil)
393
+
394
+ if (values.is_a? Numeric)
395
+ if (variable.scalar?)
396
+ type = variable.get_data_type
397
+ val = MDArray.build(type, [])
398
+ val.set_scalar(values)
399
+ @netcdf_elmt.write(variable.netcdf_elmt, val.nc_array)
400
+ else
401
+ raise "Variable #{variable.name} is not a scalar variable"
402
+ end
403
+ elsif (origin)
404
+ @netcdf_elmt.write(variable.netcdf_elmt, origin.to_java(:int), values.nc_array)
405
+ else
406
+ @netcdf_elmt.write(variable.netcdf_elmt, values.nc_array)
407
+ end
408
+
409
+ end
410
+
411
+ #------------------------------------------------------------------------------------
412
+ # writes a string data to the variable at origin
413
+ # if data is not given, then it is assumed to be all zeroes
414
+ # <tt>var_name</tt> Name of the variable in which to write
415
+ # <tt>type</tt> type of the data.
416
+ # <tt>layout</tt> layout of the data represented by a rank array
417
+ # <tt>origin</tt> origin in the var in which to write the data. If origin = nil,
418
+ # then origin is <0>
419
+ # <tt>data</tt> data to write. If data = nil, then an all zeroes data is assumed.
420
+ #------------------------------------------------------------------------------------
421
+
422
+ def write_string(variable, values, origin = nil)
423
+
424
+ if (values.is_a? String)
425
+ val = MDArray.string([], [values])
426
+ return @netcdf_elmt.writeStringData(variable.netcdf_elmt, val.nc_array)
427
+ end
428
+
429
+ if (origin)
430
+ @netcdf_elmt.writeStringData(variable.netcdf_elmt, origin.to_java(:int),
431
+ values.nc_array)
432
+ else
433
+ @netcdf_elmt.writeStringData(variable.netcdf_elmt, values.nc_array)
434
+ end
435
+
436
+ end
437
+
438
+ #------------------------------------------------------------------------------------
439
+ #
440
+ #------------------------------------------------------------------------------------
441
+
442
+ def write_time(var_name, layout, data, origin = nil)
443
+
444
+ var = find_variable(var_name)
445
+
446
+ write_data = Array.new
447
+ data.each do |iso_date|
448
+ write_data << var.to_msec(iso_date)
449
+ end
450
+
451
+ write(var_name, layout, write_data, origin)
452
+
453
+ end
454
+
455
+ #------------------------------------------------------------------------------------
456
+ #
457
+ #------------------------------------------------------------------------------------
458
+
459
+ private
460
+
461
+ #------------------------------------------------------------------------------------
462
+ #
463
+ #------------------------------------------------------------------------------------
464
+
465
+ def initialize(home_dir, name, version = nil, outside_scope = nil)
466
+
467
+ super(home_dir, name, outside_scope)
468
+ @version = NetCDF.writer_version(version) if version
469
+
470
+ end
471
+
472
+ end # FileWriteable
473
+
474
+ end