Class: NCFileWriter::Var
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
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[]=(*argv) ⇒ Object
- #get_varm(start, count, stride, imap, value) ⇒ Object
-
#initialize(ncfile, name, definition, compression = 0, define_mode = true) ⇒ Var
constructor
A new instance of Var.
- #put(*argv) ⇒ Object
- #put_var(value) ⇒ Object
- #put_var1(index, value) ⇒ Object
- #put_vara(start, count, value) ⇒ Object
- #put_vars(start, count, stride, value) ⇒ Object
- #to_i ⇒ Object
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
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 542 543 |
# File 'lib/io/netcdf.rb', line 502 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).len } dim_ids = @dims.map{|key| @ncfile.dim(key).to_i } 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. + " {#{name}: #{value}}") end end end @attributes.freeze end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
545 546 547 |
# File 'lib/io/netcdf.rb', line 545 def attributes @attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
545 546 547 |
# File 'lib/io/netcdf.rb', line 545 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
545 546 547 |
# File 'lib/io/netcdf.rb', line 545 def type @type end |
Instance Method Details
#[]=(*argv) ⇒ Object
551 552 553 |
# File 'lib/io/netcdf.rb', line 551 def []= (*argv) put(*argv) end |
#get_varm(start, count, stride, imap, value) ⇒ Object
615 616 617 |
# File 'lib/io/netcdf.rb', line 615 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
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 592 593 594 595 596 597 |
# File 'lib/io/netcdf.rb', line 555 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
603 604 605 |
# File 'lib/io/netcdf.rb', line 603 def put_var (value) return nc_put_var(@file_id, @var_id, value) end |
#put_var1(index, value) ⇒ Object
599 600 601 |
# File 'lib/io/netcdf.rb', line 599 def put_var1 (index, value) return nc_put_var1(@file_id, @var_id, index, value) end |
#put_vara(start, count, value) ⇒ Object
607 608 609 |
# File 'lib/io/netcdf.rb', line 607 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
611 612 613 |
# File 'lib/io/netcdf.rb', line 611 def put_vars (start, count, stride, value) return nc_put_vars(@file_id, @var_id, start, count, stride, value) end |
#to_i ⇒ Object
547 548 549 |
# File 'lib/io/netcdf.rb', line 547 def to_i return @var_id end |