Class: RFits::TableData

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rfits/rfits.rb

Overview

Represents tabular data in a FITS file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ TableData

Create a TableData object that is associated with the specifed table.

data = TableData.new(table)


614
615
616
# File 'lib/rfits/rfits.rb', line 614

def initialize(table)
  @table = table
end

Instance Attribute Details

#tableObject (readonly)

The table the data lives in.



610
611
612
# File 'lib/rfits/rfits.rb', line 610

def table
  @table
end

Instance Method Details

#<<(values) ⇒ Object

Append a new row.

data << ['new', 2.2, 7, Complex.new(5, 5)]


701
702
703
# File 'lib/rfits/rfits.rb', line 701

def <<(values)
  self[self.size] = values
end

#[](*args) ⇒ Object

Retrieve the ith row of data.

data[2]  # ['a1', 1.1, 3, Complex.new(3, 4)]

or the value of the cell at the specified row and column.

data[2, 3] # Complex.new(3, 4)  # the 4th cell in the 3rd row


641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/rfits/rfits.rb', line 641

def [](*args)
  self.table.reset_position
  
  result = case args.length
    when 1 # retrieve the whole row
      row = []
      (0...self.table.column_information.size).each do |col|
        row << IO::Proxy.fits_read_col(self.table.file.io,
          self.table.column_information[col].data_type(true),
          col+1, (args.first.to_i+1), 1, 1, nil).first.first
      end
      row
    when 2 # retrieve the value of the cell
      IO::Proxy.fits_read_col(self.table.file.io,
        self.table.column_information[args.last].data_type(true),
        (args.last.to_i)+1, (args.first.to_i)+1, 1, 1, nil).first.first
    else
      raise ArgumentError, "Selector should be an integer indicating the row, or two integers indicating the row and column."
  end
  
  result
end

#[]=(*args) ⇒ Object

Set the ith row to the specified values

data[4] = ['new', 2.2, 7, Complex.new(5, 5)]

or specify the value of a particular cell

data[4, 1] = 2.2  # the second column of the 5th row


673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/rfits/rfits.rb', line 673

def []=(*args)
  self.table.reset_position
  
  i = args.first
  
  case args.length
    when 2
      values = args[1..-1].first
      
      (0...values.size).each do |col|
        IO::Proxy.fits_write_colnull(self.table.file.io,
          self.table.column_information[col].data_type(true), col+1, i+1, 1, 1,
          [values[col]], nil)
      end
    when 3
      col = args[1]
      values = args[2..-1]
      
      IO::Proxy.fits_write_colnull(self.table.file.io,
        self.table.column_information[col].data_type(true), col+1, i+1, 1, 1,
        values, nil)
    else
      raise ArgumentError, "Selector should be an integer indicating the row, or two integers indicating the row and column."
  end
end

#column(i) ⇒ Object

Retrieve the ith column.

data.column(2)  # [1, 2, 3, 4, 5]


630
631
632
633
634
635
# File 'lib/rfits/rfits.rb', line 630

def column(i)
  self.table.reset_position
  
  IO::Proxy.fits_read_col(self.table.file.io,
    self.table.column_information[i].data_type(true), i+1, 1, 1, self.size, nil).first
end

#delete(i) ⇒ Object

Delete the ith row.

table.delete(2)  # delete the 3rd row


724
725
726
727
# File 'lib/rfits/rfits.rb', line 724

def delete(i)
  self.table.reset_position
  IO::Proxy.fits_delete_rows(self.table.file.io, i+1, 1)
end

#eachObject

Iterate through each row in the table.



737
738
739
740
741
742
743
# File 'lib/rfits/rfits.rb', line 737

def each
  self.table.reset_position
  
  (0...self.size).each do |i|
    yield self[i]
  end
end

#each_columnObject

Iterate through each column in the table.



746
747
748
749
750
# File 'lib/rfits/rfits.rb', line 746

def each_column
  (0...self.table.column_information.size).each do |i|
    yield self.column(i)
  end
end

#each_rowObject

Alias for TableData#each.



730
731
732
733
734
# File 'lib/rfits/rfits.rb', line 730

def each_row
  self.each do |row|
    yield row
  end
end

#firstObject

Retrieve the first row in the table.

table.first  # ['blah', 23.1, 9, Complex.new(4, 5)]


754
755
756
# File 'lib/rfits/rfits.rb', line 754

def first
  self[0]
end

#lastObject

Retrieve the last row in the table.

table.last  # ['new', 2.2, 7, Complex.new(5, 5)]


760
761
762
# File 'lib/rfits/rfits.rb', line 760

def last
  self[self.size - 1]
end

#lengthObject

The number of rows in the table.

table.length  # 5


712
713
714
715
# File 'lib/rfits/rfits.rb', line 712

def length
  self.table.reset_position
  IO::Proxy.fits_get_num_rows(self.table.file.io)
end

#row(i) ⇒ Object

Alias for TableData#[]



665
666
667
# File 'lib/rfits/rfits.rb', line 665

def row(i)
  self[i]
end

#set_column(i, values) ⇒ Object

Set the ith columns to the specified values.

data.set_column(2, [1, 2, 3, 4, 5])  # set the 3rd column (which is an integer column)


620
621
622
623
624
625
626
# File 'lib/rfits/rfits.rb', line 620

def set_column(i, values)
  self.table.reset_position
  
  IO::Proxy.fits_write_colnull(self.table.file.io,
    self.table.column_information[i].data_type(true), i+1, 1, 1, values.size,
    values, nil)
end

#set_row(i, values) ⇒ Object

Alias for TableData#[]=



706
707
708
# File 'lib/rfits/rfits.rb', line 706

def set_row(i, values)
  self[i] = values
end

#sizeObject

Alias for TableData#length



718
719
720
# File 'lib/rfits/rfits.rb', line 718

def size
  self.length
end

#to_csvObject

Convert the rows in the table to CSV.



770
771
772
773
774
775
776
777
778
# File 'lib/rfits/rfits.rb', line 770

def to_csv
  buffer = ''

  self.each_row do |row|
    CSV.generate_row(row.collect{ |cell| cell.to_s }, row.size, buffer)
  end
  
  buffer
end

#to_sObject

Convert the rows to a string. Currently an alias to TableData#to_csv



765
766
767
# File 'lib/rfits/rfits.rb', line 765

def to_s
  self.to_csv
end