Class: NetCDF::FileWriter

Inherits:
FileParent show all
Defined in:
lib/netcdf/file_writer.rb

Instance Attribute Summary collapse

Attributes inherited from FileParent

#file_name, #home_dir, #netcdf_elmt, #outside_scope, #root_group

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileParent

#global_attributes

Instance Attribute Details

#readerObject (readonly)

Opens the same file just for reading. I don't know it this will work properly. Needed as the API for FileWriter lacks some interesting features. Or maybe not! I don't know… I might just be confused!


32
33
34
# File 'lib/netcdf/file_writer.rb', line 32

def reader
  @reader
end

#versionObject (readonly)

Returns the value of attribute version


28
29
30
# File 'lib/netcdf/file_writer.rb', line 28

def version
  @version
end

Class Method Details

.existing_file(home_dir, name, outside_scope = nil) ⇒ Object


Writer for an existing NetCDF file



46
47
48
# File 'lib/netcdf/file_writer.rb', line 46

def self.existing_file(home_dir, name, outside_scope = nil)
  FileWriter.new(home_dir, name, nil, outside_scope)
end

.new_file(home_dir, name, version, outside_scope = nil) ⇒ Object


Writer for a new NetCDF file



38
39
40
# File 'lib/netcdf/file_writer.rb', line 38

def self.new_file(home_dir, name, version, outside_scope = nil)
  FileWriter.new(home_dir, name, version, outside_scope)
end

Instance Method Details

#add_dimension(name, size, is_shared = true) ⇒ Object


Add a Dimension to the file. Must be in define mode. dimName name of dimension (string) length size of dimension (int). If size == 0, dimension is unlimited <tt> is_shared<tt> if dimension is shared (boolean) if size == -1, then this is a variable_length dimension if size == 0, this is an unlimited dimension NetCDF3 only supports shared dimensions.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/netcdf/file_writer.rb', line 150

def add_dimension(name, size, is_shared = true)

  is_unlimited = false
  is_variable_length = false
  dim = nil

  if (size == -1)
    is_variable_length = true
  elsif (size == 0)
    is_unlimited = true
  end

  NetCDF::DimensionWriter
    .new(@netcdf_elmt.addDimension(@root_group.netcdf_elmt, "#{name}", size, 
                                  is_shared, is_unlimited, is_variable_length))

end

#add_global_att(name, value, type) ⇒ Object


Adds a global attribute



104
105
106
107
# File 'lib/netcdf/file_writer.rb', line 104

def add_global_att(name, value, type)
  attribute = NetCDF::AttributeWriter.build(name, value, type)
  add_group_att(@root_group, attribute)
end

#add_group(parent, name) ⇒ Object


Adds a new group to the file.



79
80
81
# File 'lib/netcdf/file_writer.rb', line 79

def add_group(parent, name)
  NetCDF::GroupWriter.new(@netcdf_elmt.addGroup(parent, name))
end

#add_group_att(group, attribute) ⇒ Object


Adds a group attribute



95
96
97
98
# File 'lib/netcdf/file_writer.rb', line 95

def add_group_att(group, attribute)
  group.add_attribute(attribute)
  attribute
end

#add_root_groupObject


Adds the root group to the file.



87
88
89
# File 'lib/netcdf/file_writer.rb', line 87

def add_root_group
  @root_group = add_group(nil, "root")
end

#add_variable(var_name, type, dims, *args) ⇒ Object


Add a variable to the file.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/netcdf/file_writer.rb', line 181

def add_variable(var_name, type, dims, *args)

  opts = Map.options(args)
  max_strlen = opts.getopt(:max_strlen, :default=>250)

  # if dims is an array then make a java dimension list
  dim_list = java.util.ArrayList.new
  if (dims.is_a? Array)
    dims.each do |dim|
      dim_list.add(dim.netcdf_elmt)
    end
  end
  
  case type
  when "string"
    NetCDF::VariableWriter.new(@netcdf_elmt
                                 .addStringVariable(@root_group.netcdf_elmt, var_name, 
                                                    dim_list, max_strlen))
  else
    NetCDF::VariableWriter.new(@netcdf_elmt
                                 .addVariable(@root_group.netcdf_elmt, var_name, 
                                              DataType.valueOf(type.upcase), dim_list))
  end
  
end

#add_variable_att(variable, name, value) ⇒ Object


Adds a variable attribute



229
230
231
232
233
# File 'lib/netcdf/file_writer.rb', line 229

def add_variable_att(variable, name, value)
  attribute = NetCDF::AttributeWriter.build(name, value)
  @netcdf_elmt.addVariableAttribute(variable.netcdf_elmt, attribute.netcdf_elmt)
  attribute
end

#closeObject


closes the file



316
317
318
# File 'lib/netcdf/file_writer.rb', line 316

def close
  @netcdf_elmt.close
end

#createObject


After you have added all of the Dimensions, Variables, and Attributes, call create to actually create the file. You must be in define mode. After this call, you are no longer in define mode.



301
302
303
304
305
306
307
308
309
310
# File 'lib/netcdf/file_writer.rb', line 301

def create
  
  begin
    @netcdf_elmt.create
  rescue java.io.IOException => ioe
    $stderr.print "Error accessing file: #{@file_name}"
    $stderr.print ioe
  end
  
end

#define_mode?Boolean


Returns true if the file is in define mode


Returns:

  • (Boolean)

340
341
342
# File 'lib/netcdf/file_writer.rb', line 340

def define_mode?
  @netcdf_elmt.isDefineMode()
end

#delete_global_att(attribute_name) ⇒ Object


Deletes a global attribute



249
250
251
# File 'lib/netcdf/file_writer.rb', line 249

def delete_global_att(attribute_name)
  @netcdf_elmt.deleteGroupAttribute(@root_group.netcdf_elmt, attribute_name)
end

#delete_variable_att(variable, att_name) ⇒ Object


Deletes a variable attribute



283
284
285
# File 'lib/netcdf/file_writer.rb', line 283

def delete_variable_att(variable, att_name)
  @netcdf_elmt.deleteVariableAttribute(variable.netcdf_elmt, att_name)
end

#dimension(name, size, is_shared = true) ⇒ Object


Adds a new dimension.



172
173
174
175
# File 'lib/netcdf/file_writer.rb', line 172

def dimension(name, size, is_shared = true)
  symbol = ("dim_" + name.gsub(/\s+/, "_").downcase).to_sym
  instance_variable_set("@#{symbol}", add_dimension(name, size, is_shared))
end

#fill=(bool) ⇒ Object


Set the fill flag. If fill flag is set then variable data is filled with fill value



349
350
351
# File 'lib/netcdf/file_writer.rb', line 349

def fill=(bool)
  @netcdf_elmt.setFill(bool)
end

#find_global_attribute(name, ignore_case = false) ⇒ Object




124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/netcdf/file_writer.rb', line 124

def find_global_attribute(name, ignore_case = false)

  if (ignore_case)
    att = @root_group.netcdf_elmt.findAttributeIgnoreCase(name)
  else
    att = @root_group.netcdf_elmt.findAttribute(name)
  end

  if (att != nil)
    return NetCDF::AttributeWriter.new(att)
  end

  nil

end

#find_variable(name) ⇒ Object


Finds a variable in the file



220
221
222
223
# File 'lib/netcdf/file_writer.rb', line 220

def find_variable(name)
  var = @netcdf_elmt.findVariable(name)
  var ? NetCDF::VariableWriter.new(var) : nil
end

#flushObject


Flush anything written to disk



332
333
334
# File 'lib/netcdf/file_writer.rb', line 332

def flush
  @netcdf_elmt.flush
end

#get_file_type_descriptionObject


Get a human-readable description for this file type.



357
358
359
# File 'lib/netcdf/file_writer.rb', line 357

def get_file_type_description
  @netcdf_elmt.getFileTypeDescription()
end

#global_att(name, value, type = "int") ⇒ Object


Adds new global attribute. A global attribute is a attribute added to the root group. In NetCDF 3 there is only the root group.



114
115
116
117
118
# File 'lib/netcdf/file_writer.rb', line 114

def global_att(name, value, type = "int")
  symbol = ("ga_" + name.gsub(/\s+/, "_").downcase).to_sym
  att = add_global_att(name, value, type)
  instance_variable_set("@#{symbol}", att)
end

#large_file=(bool) ⇒ Object


Set if this should be a “large file” (64-bit offset) format.



324
325
326
# File 'lib/netcdf/file_writer.rb', line 324

def large_file=(bool)
  @netcdf_elmt.setLargeFile(bool)
end

#openObject


Opens a netCDF file.

  • TODO: Check the case when reading should be shared

  • fill If true, or anything but false, the data is first written with fill

values. Default is fill = false. Leave false if you expect to write all data values, set to true if you want to be sure that unwritten data values have the fill value in it.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/netcdf/file_writer.rb', line 59

def open

  begin
    if (@version)
      @netcdf_elmt = NetcdfFileWriter.createNew(@version, @file_name)
      add_root_group
    else
      @netcdf_elmt = NetcdfFileWriter.openExisting(@file_name)
    end
  rescue java.io.IOException => ioe
    $stderr.print "Cannot open file: #{@file_name}"
    $stderr.print ioe
  end
  
end

#redefine=(bool) ⇒ Object


Switches redefine mode. if true allows data to be redefined, if false, redefine mode is closed. Returns true if it had to rewrite the entire file, false if it wrote the header in place



368
369
370
371
372
373
374
375
376
377
# File 'lib/netcdf/file_writer.rb', line 368

def redefine=(bool)

  begin
    @netcdf_elmt.setRedefineMode(bool)
  rescue java.io.IOException => ioe
    $stderr.print "Error accessing file: #{@file_name}"
    $stderr.print ioe
  end

end

#rename_dimension(old_name, new_name) ⇒ Object


Rename a Dimension.



265
266
267
# File 'lib/netcdf/file_writer.rb', line 265

def rename_dimension(old_name, new_name)
  @netcdf_elmt.renameDimension(@root_group.netcdf_elmt, old_name, new_name)
end

#rename_global_att(old_name, new_name) ⇒ Object


Rename a global Attribute. Does not seem to work on NetCDF-3 files.



257
258
259
# File 'lib/netcdf/file_writer.rb', line 257

def rename_global_att(old_name, new_name)
  @netcdf_elmt.renameGlobalAttribute(@root_group.netcdf_elmt, old_name, new_name)
end

#rename_variable(old_name, new_name) ⇒ Object


Rename a Variable.



275
276
277
# File 'lib/netcdf/file_writer.rb', line 275

def rename_variable(old_name, new_name)
  @netcdf_elmt.renameVariable(old_name, new_name)
end

#rename_variable_att(variable, att_name, new_name) ⇒ Object


Renames a variable attribute



291
292
293
# File 'lib/netcdf/file_writer.rb', line 291

def rename_variable_att(variable, att_name, new_name)
  @netcdf_elmt.renameVariableAttribute(variable.netcdf_elmt, att_name, new_name)
end

#variable(name, type, dims, *args) ⇒ Object


Adds new variable



211
212
213
214
# File 'lib/netcdf/file_writer.rb', line 211

def variable(name, type, dims, *args)
  symbol = ("var_" + name.gsub(/\s+/, "_").downcase).to_sym
  instance_variable_set("@#{symbol}", add_variable(name, type, dims, *args))
end

#variable_att(variable, att_name, value) ⇒ Object


Adds new variable attribute



239
240
241
242
243
# File 'lib/netcdf/file_writer.rb', line 239

def variable_att(variable, att_name, value)
  symbol = ("va_" + variable.name + "_" + att_name.gsub(/\s+/, "_").downcase).to_sym
  instance_variable_set("@#{symbol}", 
                        add_variable_att(variable, att_name, value))
end

#write(variable, values, origin = nil) ⇒ Object


writes the given data with the given layout on variable at origin @nc_i.write(<variable>, <layout>, <origin>, <data>) if data is not given, then it is assumed to be all zeroes @nc_i.write(<variable>, <layout>, <origin>) var_name Name of the variable in which to write type type of the data. layout layout of the data represented by a rank array origin origin in the var in which to write the data. If origin = nil, then origin is <0> data data to write. If data = nil, then an all zeroes data is assumed.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/netcdf/file_writer.rb', line 392

def write(variable, values, origin = nil)

  if (values.is_a? Numeric)
    if (variable.scalar?)
      type = variable.get_data_type
      val = MDArray.build(type, [])
      val.set_scalar(values)
      @netcdf_elmt.write(variable.netcdf_elmt, val.nc_array)
    else
      raise "Variable #{variable.name} is not a scalar variable"
    end
  elsif (origin)
    @netcdf_elmt.write(variable.netcdf_elmt, origin.to_java(:int), values.nc_array)
  else
    @netcdf_elmt.write(variable.netcdf_elmt, values.nc_array)
  end

end

#write_string(variable, values, origin = nil) ⇒ Object


writes a string data to the variable at origin if data is not given, then it is assumed to be all zeroes var_name Name of the variable in which to write type type of the data. layout layout of the data represented by a rank array origin origin in the var in which to write the data. If origin = nil, then origin is <0> data data to write. If data = nil, then an all zeroes data is assumed.



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/netcdf/file_writer.rb', line 422

def write_string(variable, values, origin = nil)

  if (values.is_a? String)
    val = MDArray.string([], [values])
    return @netcdf_elmt.writeStringData(variable.netcdf_elmt, val.nc_array)
  end

  if (origin)
    @netcdf_elmt.writeStringData(variable.netcdf_elmt, origin.to_java(:int), 
                                 values.nc_array)
  else
    @netcdf_elmt.writeStringData(variable.netcdf_elmt, values.nc_array)
  end
  
end

#write_time(var_name, layout, data, origin = nil) ⇒ Object




442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/netcdf/file_writer.rb', line 442

def write_time(var_name, layout, data, origin = nil)
  
  var = find_variable(var_name)

  write_data = Array.new
  data.each do |iso_date|
    write_data << var.to_msec(iso_date)
  end

  write(var_name, layout, write_data, origin)

end