Class: NCFile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NC

nc_decode, nc_put_att_simple, nc_put_var_all

Methods inherited from NCObject

#attribute, #get_attributes, #get_attributes!

Constructor Details

#initialize(file_id) ⇒ NCFile

Returns a new instance of NCFile.



381
382
383
384
385
386
387
388
389
390
# File 'lib/io/netcdf.rb', line 381

def initialize (file_id)
  @file_id  = file_id
  @dims     = []
  @vars     = []
  @name2dim = {}
  @name2var = {}
  @attributes = get_attributes(@file_id, NC_GLOBAL)
  @attributes.freeze
  ()
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



392
393
394
# File 'lib/io/netcdf.rb', line 392

def attributes
  @attributes
end

#dimsObject (readonly)

Returns the value of attribute dims.



392
393
394
# File 'lib/io/netcdf.rb', line 392

def dims
  @dims
end

#file_idObject (readonly)

Returns the value of attribute file_id.



392
393
394
# File 'lib/io/netcdf.rb', line 392

def file_id
  @file_id
end

#varsObject (readonly)

Returns the value of attribute vars.



392
393
394
# File 'lib/io/netcdf.rb', line 392

def vars
  @vars
end

Class Method Details

.convert_attribute_value(value) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/io/netcdf.rb', line 417

def self.convert_attribute_value (value)
  case value
  when CArray
    if value.length == 1
      case value.data_type
      when CA_UINT8
        value = { "char" => value[0] }
      when CA_INT8
        value = { "byte" => value[0] }
      when CA_SHORT
        value = { "short" => value[0] }
      when CA_INT
        value = { "int" => value[0] }
      when CA_FLOAT
        value = { "float" => value[0] }
      when CA_DOUBLE
        value = { "double" => value[0] }
      end        
    else
      case value.data_type
      when CA_UINT8
        value = { "char" => value.to_a }
      when CA_INT8
        value = { "byte" => value.to_a }
      when CA_SHORT
        value = { "short" => value.to_a }
      when CA_INT
        value = { "int" => value.to_a }
      when CA_FLOAT
        value = { "float" => value.to_a }
      when CA_DOUBLE
        value = { "double" => value.to_a }
      end
    end
  end
  return value
end

.open(filename) ⇒ Object



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

def self.open (filename)
  file_id = NC.open(filename)
  if block_given?
    begin
      nc = NCFile.new(file_id)    
      yield(nc)
    ensure
      nc.close
    end
  else
    return NCFile.new(file_id)    
  end
end

Instance Method Details

#[](name) ⇒ Object



467
468
469
# File 'lib/io/netcdf.rb', line 467

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

#closeObject



394
395
396
# File 'lib/io/netcdf.rb', line 394

def close ()
  nc_close(@file_id)
end

#definitionObject



455
456
457
458
459
460
461
462
463
464
465
# File 'lib/io/netcdf.rb', line 455

def definition
  atts = {}
  get_attributes!(@file_id, NC_GLOBAL).each do |k,v|
    atts[k] = NCFile.convert_attribute_value(v)
  end
  {
    "dimensions" => @dims.map{|x| [x.name, x.definition] }.to_h,
    "variables" => @vars.map{|x| [x.name, x.definition] }.to_h,
    "attributes" => atts
  }
end

#dim(name) ⇒ Object



471
472
473
# File 'lib/io/netcdf.rb', line 471

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

#has_dim?(name) ⇒ Boolean

Returns:

  • (Boolean)


475
476
477
# File 'lib/io/netcdf.rb', line 475

def has_dim?(name)
  return @name2dim.has_key?(name)
end

#has_var?(name) ⇒ Boolean

Returns:

  • (Boolean)


479
480
481
# File 'lib/io/netcdf.rb', line 479

def has_var?(name)
  return @name2var.has_key?(name)
end

#parse_metadataObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/io/netcdf.rb', line 398

def  ()
  ndims = nc_inq_ndims(@file_id)
  ndims.times do |i|
    dim = NCDim.new(self, i)
    @dims[i] = dim
    @name2dim[dim.name] = dim
  end
  @dims.freeze
  @name2dim.freeze
  nvars = nc_inq_nvars(@file_id)
  nvars.times do |i|
    var = NCVar.new(self, i)
    @vars[i] = var
    @name2var[var.name] = var
  end
  @vars.freeze
  @name2var.freeze
end