Module: RFits

Defined in:
lib/rfits/rfits.rb

Overview

RFits is a library for parsing the Flexible Image Transport System (FITS) files widely used in astronomy.

Installing

RFits requires the C library CFITSIO (heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html) to be installed. In most cases this is as simple as:

> tar zxvf cfitsioxxxx.tar.gz
> cd cfitsio
> ./configure
> make shared
> sudo make install

You can then install via rubygems in the usual way:

> gem install rfits

Using

require 'rubygems'
require 'rfits'

RFits::File.open('m31.fits', 'rw') do |fits|  # m31.fits exists
  # first extension is an image
  img = fits[0]

  # retrieve/write header values using hash syntax
  header = img.header
  puts header['TELESCOP']     #=> 'KPNO 4.0 meter telescope'
  puts header['TELEQUIN']     #=> 2000.0
  puts header['SIMPLE']       #=> true
  puts header['TELFOCUS']     #=> -9998
  header['MY_HDR1'] = 'A nice value'
  header['MY_HDR2'] = 10
  header['MY_HDR3'] = 9.1
  header['MY_HDR4'] = Complex.new(4, 1)  # yes, you can do this

  # retrieve/write pixel values using array syntax
  pixels = img.data
  first_pixel = pixels[0]                  # the first pixel
  pixel_list = pixels[0..10]               # pixels 0 through 10 as an array
  pixel_list = pixels[[0, 0], [10, 10]]    # pixels between the points [0, 0] and [10, 10]
  pixels[10] = 5                           # the ninth pixel value is set to 5
  pixels[3..7] = [1, 4, 2, 8]              # pixels 3 through 7 are set

  # second extension is a binary table
  tbl = fits[1]

  # access a table using array syntax
  data = tbl.data
  row = data[0]                                    # first row as a hetergeneous array
  col = data.column(2)                             # third column
  data[1] = [1, 1.2, 'blah', Complex.new(1, 1)]    # second row is set
  data.set_column(1, [1.3, 4.5, 7.8, 2.2])         # the second column is set
  col << [1, 1.2, 'blah', Complex.new(1, 1)]       # append a row

  # you can find out things about the table columns
   = tbl.column_information
  puts [0].data_type   #=> :short
  puts [0].name        #=> 'catalog_id'

  # and add new ones
   << {:name => 'new_column1', :format => 'A10'}  # append a new string column
  [3] = {:name => 'new_column2', :format => 'I'}  # insert an integer column as the 4th column
end

Defined Under Namespace

Modules: Compressible Classes: AsciiTable, BinaryTable, ColumnInformation, ColumnInformationList, File, HDU, Header, Image, ImageData, Table, TableData