Class: NCFileWriter

Inherits:
Object
  • Object
show all
Includes:
NC
Defined in:
lib/io/netcdf.rb

Defined Under Namespace

Classes: Dim, Var

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NC

nc_decode, nc_put_att_simple, nc_put_var_all

Constructor Details

#initialize(file) ⇒ NCFileWriter

Returns a new instance of NCFileWriter.



474
475
476
477
478
479
480
481
# File 'lib/io/netcdf.rb', line 474

def initialize (file)
  @file_id  = nc_create(file)
  @dims     = []
  @name2dim = {}
  @vars     = []
  @name2var = {}
  @attributes = nil
end

Instance Attribute Details

#file_idObject (readonly)

Returns the value of attribute file_id.



483
484
485
# File 'lib/io/netcdf.rb', line 483

def file_id
  @file_id
end

Instance Method Details

#[](name) ⇒ Object



536
537
538
# File 'lib/io/netcdf.rb', line 536

def [] (name)
  return @name2var[name]
end

#[]=(name, value) ⇒ Object



540
541
542
# File 'lib/io/netcdf.rb', line 540

def []= (name, value)
  return @name2var[name].put(value)
end

#closeObject



544
545
546
# File 'lib/io/netcdf.rb', line 544

def close
  nc_close(@file_id)
end

#copy_dims(nc, name = nil) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/io/netcdf.rb', line 518

def copy_dims (nc, name = nil)
  if name
    if @name2dim.has_key?(name) and @name2var.has_key?(name)
      @name2var[name].put(nc[name].get)
    end
  else
    nc.dims.each do |dim|
      if @name2dim.has_key?(dim.name) and @name2var.has_key?(dim.name)
        @name2var[dim.name].put(nc[dim.name].get)
      end
    end
  end
end

#define(definition) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/io/netcdf.rb', line 496

def define (definition)
  definition = normalize_definition(definition)
  definition["dimensions"].each do |name, len|
    dim = Dim.new(self, name, len.to_i)
    @dims.push dim
    @name2dim[name] = dim
  end
  definition["variables"].each do |name, info|
    var = Var.new(self, name, info)
    @vars.push var
    @name2var[name] = var
  end
  @attributes = definition.dup
  @attributes.delete("dimensions")
  @attributes.delete("variables")
  @attributes.each do |name, value|
    nc_put_att(@file_id, NC_GLOBAL, name, value)
  end
  @attributes.freeze
  nc_enddef(@file_id)    
end

#dim(name) ⇒ Object



532
533
534
# File 'lib/io/netcdf.rb', line 532

def dim (name)
  return @name2dim[name]
end

#normalize_definition(definition) ⇒ Object



485
486
487
488
489
490
491
492
493
494
# File 'lib/io/netcdf.rb', line 485

def normalize_definition (definition)
  out = {}
  definition.each do |key, value|
    if value.is_a?(Hash)
      value = normalize_definition(value)
    end
    out[key.to_s] = value
  end
  return out
end