Class: NCFileWriter::Var

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

Constant Summary collapse

TYPEMAP =
{
  "char"   => NC_CHAR,
  "byte"   => NC_BYTE,
  "short"  => NC_SHORT,
  "int"    => NC_INT,
  "float"  => NC_FLOAT,
  "double" => NC_DOUBLE,
}

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(ncfile, name, definition, compression = 0, define_mode = true) ⇒ Var

Returns a new instance of Var.



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/io/netcdf.rb', line 500

def initialize (ncfile, name, definition, compression = 0, define_mode = true)
  @ncfile  = ncfile
  @file_id = ncfile.file_id
  @name    = name
  @type    = definition["type"] || NC_FLOAT
  if @type.is_a?(String)
    if TYPEMAP.include?(@type)
      @type = TYPEMAP[@type]
    else
      raise "invalid variable data type"
    end
  end
  @dims    = definition["dim"]
  if @dims
    @shape   = @dims.map{|key| @ncfile.dim(key).to_i }
    dim_ids  = @dims.map{|key| @ncfile.dim(key).dim_id }
  else
    dim_ids  = []
  end
  if define_mode
    @var_id  = nc_def_var(@file_id, @name, @type, dim_ids)
    if compression != 0
      nc_def_var_deflate(@file_id, @var_id, 1, 1, compression)
    end
  else
    @var_id  = nc_inq_varid(@file_id, @name)        
  end
  @attributes = definition.dup
  @attributes.delete("type")
  @attributes.delete("dim")
  if define_mode
    @attributes.each do |name, value|
      value = NCFileWriter.convert_attribute_value(value)
      begin
        nc_put_att(@file_id, @var_id, name, value)
      rescue => e
        raise(e.class, e.message + " {#{name}: #{value}}")
      end
    end
  end
  @attributes.freeze
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#[]=(*argv) ⇒ Object



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

def []= (*argv)
  put(*argv)      
end

#get_varm(start, count, stride, imap, value) ⇒ Object



609
610
611
# File 'lib/io/netcdf.rb', line 609

def get_varm (start, count, stride, imap, value)
  return nc_put_varm(@file_id, @var_id, start, count, stride, imap, value)
end

#put(*argv) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/io/netcdf.rb', line 549

def put (*argv)
  value = argv.pop
  info = CArray.scan_index(@shape, argv)
  case info.type
  when CA_REG_ADDRESS
    addr  = info.index[0]
    index = []
    (0..@shape.size-1).reverse_each do |i|
      index[i] = addr % @shape[i]
      addr /= @shape[i]
    end
    put_var1(index, value)
  when CA_REG_FLATTEN
    put_var(value)
  when CA_REG_POINT
    put_var1(info.index, value)
  when CA_REG_ALL
    put_var(value)
  when CA_REG_BLOCK
    start = []
    count = []
    stride = []
    info.index.each do |idx|
      case idx
      when Array
        start << idx[0]
        count << idx[1]
        stride << idx[2]
      else
        start << idx
        count << 1
        stride << 1
      end
    end
    if stride.all?{|x| x == 1 }
      put_vara(start, count, value)
    else
      put_vars(start, count, stride, value)
    end
  else
    raise "invalid index"
  end
end

#put_var(value) ⇒ Object



597
598
599
# File 'lib/io/netcdf.rb', line 597

def put_var (value)
  return nc_put_var(@file_id, @var_id, value)
end

#put_var1(index, value) ⇒ Object



593
594
595
# File 'lib/io/netcdf.rb', line 593

def put_var1 (index, value)
  return nc_put_var1(@file_id, @var_id, index, value)
end

#put_vara(start, count, value) ⇒ Object



601
602
603
# File 'lib/io/netcdf.rb', line 601

def put_vara (start, count, value)
  return nc_put_vara(@file_id, @var_id, start, count, value)
end

#put_vars(start, count, stride, value) ⇒ Object



605
606
607
# File 'lib/io/netcdf.rb', line 605

def put_vars (start, count, stride, value)
  return nc_put_vars(@file_id, @var_id, start, count, stride, value)
end