Class: NCFile

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

Instance Attribute Summary collapse

Attributes inherited from NCObject

#attributes

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.



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

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

Instance Attribute Details

#dimsObject (readonly)

Returns the value of attribute dims.



390
391
392
# File 'lib/io/netcdf.rb', line 390

def dims
  @dims
end

#file_idObject (readonly)

Returns the value of attribute file_id.



390
391
392
# File 'lib/io/netcdf.rb', line 390

def file_id
  @file_id
end

#varsObject (readonly)

Returns the value of attribute vars.



390
391
392
# File 'lib/io/netcdf.rb', line 390

def vars
  @vars
end

Class Method Details

.convert_attribute_value(value) ⇒ Object



415
416
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
# File 'lib/io/netcdf.rb', line 415

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_BYTE
        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_BYTE
        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



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

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



465
466
467
# File 'lib/io/netcdf.rb', line 465

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

#closeObject



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

def close ()
  nc_close(@file_id)
end

#definitionObject



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

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



469
470
471
# File 'lib/io/netcdf.rb', line 469

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

#has_dim?(name) ⇒ Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/io/netcdf.rb', line 473

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

#has_var?(name) ⇒ Boolean

Returns:

  • (Boolean)


477
478
479
# File 'lib/io/netcdf.rb', line 477

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

#parse_metadataObject



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

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