Class: RFits::File

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

Overview

A class representing a FITS file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = "rw") ⇒ File

Open an existing FITS file or create a new one. The mode parameter may be on of:

  • r opens a file for reading. If it doesn’t exist, it creates it.

  • rw opens afile for reading and writing. If it doesn’t exist, it creates it.

  • rw+ opens a file for reading and writing. If it exists, it truncates it.

fits = RFits::File.new('m31.fits', 'rw')  # this is probably what you want


86
87
88
89
90
91
92
93
94
95
# File 'lib/rfits/rfits.rb', line 86

def initialize(path, mode="rw")
  iomode = mode.match('w') ? IO::Proxy::READWRITE : IO::Proxy::READONLY
  truncate = mode.match('\+') ? true : false
  
  if truncate and mode.match('w')
    @io = IO::Proxy::fits_create_file("!#{path}")
  else
    @io = ::File.exists?(path) ? IO::Proxy.fits_open_file(path, iomode) : IO::Proxy::fits_create_file(path)
  end
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



77
78
79
# File 'lib/rfits/rfits.rb', line 77

def io
  @io
end

Class Method Details

.open(path, mode = "rw") ⇒ Object

Works as RFits::File#new but accepts a block. The FITS file is automatically closed after execution of the block.

RFits::File.open('m31.fits', 'rw') do |fits|
  # do things to the FITS file, fits.close gets called  automatically
end


103
104
105
106
107
108
109
110
111
# File 'lib/rfits/rfits.rb', line 103

def self.open(path, mode="rw")
  fits = File.new(path, mode)
  if block_given?
    yield fits
    fits.close
  else
    fits
  end
end

Instance Method Details

#<<(hdu, *args) ⇒ Object

Append a new HDU to the file.

fits << RFits::Image.new(@fits, nil, RFits::IO::Proxy::LONG_IMG, [4, 4])  # notice you can leave the position nil


196
197
198
# File 'lib/rfits/rfits.rb', line 196

def <<(hdu, *args)
  hdu.create(self.length, *args)
end

#[](extpos) ⇒ Object

Get the specified HDU associated with the FITS file. Contrary to FITS conventions this is zero-based access. So:

primary_hdu = fits[0]  # primary array
hdu = fits[2]  # third HDU


141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rfits/rfits.rb', line 141

def [](extpos)
  extpos = extpos < 0 ? (self.length + extpos) : extpos
  
  set_position(extpos)
  hdu = case IO::Proxy.fits_get_hdu_type(self.io)
    when IO::Proxy::IMAGE_HDU         then  Image.new(self, extpos, nil, nil)
    when IO::Proxy::ASCII_TBL         then  AsciiTable.new(self, extpos, nil, nil)
    when IO::Proxy::BINARY_TBL        then  BinaryTable.new(self, extpos, nil, nil)
    else  HDU.new(self, extpos)
  end
  
  hdu
end

#[]=(pos, *args) ⇒ Object

Insert a new HDU at the specified position.

fits[1] = RFits::Image.new(@fits, nil, RFits::IO::Proxy::LONG_IMG, [2, 3])  # the old hdu at pos 1 gets pushed down


202
203
204
205
# File 'lib/rfits/rfits.rb', line 202

def []=(pos, *args)
  hdu = args[0]
  hdu.create(pos, *args[1..-1])
end

#closeObject

Close the FITS file.

file.close()


115
116
117
# File 'lib/rfits/rfits.rb', line 115

def close
  IO::Proxy::fits_close_file(self.io)
end

#delete(hdu) ⇒ Object

Delete the specified HDU.

fits.delete(hdu)


190
191
192
# File 'lib/rfits/rfits.rb', line 190

def delete(hdu)
  delete_at(hdu.position)
end

#delete_at(extpos) ⇒ Object

Delete the HDU at the specified position. Again, this is zero-based access.

fits.delete_at(2)  # delete the third HDU


183
184
185
186
# File 'lib/rfits/rfits.rb', line 183

def delete_at(extpos)
  set_position(extpos)
  Proxy.fits_delete_hdu(self.io)
end

#eachObject

Iterates through each HDU in the FITS file.

fits.each do |hdu|
  # do something...
end


169
170
171
172
173
# File 'lib/rfits/rfits.rb', line 169

def each
  (0...self.length).each do |i|
    yield self[i]
  end
end

#firstObject

Get the first HDU in the file.



161
162
163
# File 'lib/rfits/rfits.rb', line 161

def first
  self[0]
end

#lastObject

Get the last HDU in the file.



156
157
158
# File 'lib/rfits/rfits.rb', line 156

def last
  self[-1]
end

#lengthObject

The number of HDUs in the FITS file.



176
177
178
# File 'lib/rfits/rfits.rb', line 176

def length
  IO::Proxy.fits_get_num_hdus(self.io)
end

#modeObject

The IO mode of the FITS file (i.e. IO::Proxy::READWRITE or IO::Proxy::READONLY)

puts file.mode   # 1 = IO::Proxy::READWRITE


127
128
129
# File 'lib/rfits/rfits.rb', line 127

def mode
  IO::Proxy.fits_file_mode(self.io)
end

#pathObject

The filesystem path to the FITS file.

puts file.path   # m31.fits


121
122
123
# File 'lib/rfits/rfits.rb', line 121

def path
  IO::Proxy::fits_file_name(self.io)
end

#set_position(extpos) ⇒ Object

:nodoc:



207
208
209
# File 'lib/rfits/rfits.rb', line 207

def set_position(extpos) #:nodoc:
  IO::Proxy.fits_movabs_hdu(self.io, extpos + 1)
end

#url_typeObject

The type of URL of the FITS file (i.e. file:// or ftp://).

puts file.url_type   # file://


133
134
135
# File 'lib/rfits/rfits.rb', line 133

def url_type
  IO::Proxy.fits_url_type(self.io)
end