Class: RFits::File
Overview
A class representing a FITS file.
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Class Method Summary collapse
-
.open(path, mode = "rw") ⇒ Object
Works as RFits::File#new but accepts a block.
Instance Method Summary collapse
-
#<<(hdu, *args) ⇒ Object
Append a new HDU to the file.
-
#[](extpos) ⇒ Object
Get the specified HDU associated with the FITS file.
-
#[]=(pos, *args) ⇒ Object
Insert a new HDU at the specified position.
-
#close ⇒ Object
Close the FITS file.
-
#delete(hdu) ⇒ Object
Delete the specified HDU.
-
#delete_at(extpos) ⇒ Object
Delete the HDU at the specified position.
-
#each ⇒ Object
Iterates through each HDU in the FITS file.
-
#first ⇒ Object
Get the first HDU in the file.
-
#initialize(path, mode = "rw") ⇒ File
constructor
Open an existing FITS file or create a new one.
-
#last ⇒ Object
Get the last HDU in the file.
-
#length ⇒ Object
The number of HDUs in the FITS file.
-
#mode ⇒ Object
The IO mode of the FITS file (i.e. IO::Proxy::READWRITE or IO::Proxy::READONLY) puts file.mode # 1 = IO::Proxy::READWRITE.
-
#path ⇒ Object
The filesystem path to the FITS file.
-
#set_position(extpos) ⇒ Object
:nodoc:.
-
#url_type ⇒ Object
The type of URL of the FITS file (i.e. file:// or ftp://).
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
#io ⇒ Object (readonly)
Returns the value of attribute io.
77 78 79 |
# File 'lib/rfits/rfits.rb', line 77 def io @io end |
Class Method Details
Instance Method Details
#<<(hdu, *args) ⇒ Object
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
202 203 204 205 |
# File 'lib/rfits/rfits.rb', line 202 def []=(pos, *args) hdu = args[0] hdu.create(pos, *args[1..-1]) end |
#close ⇒ Object
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 |
#each ⇒ Object
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 |
#first ⇒ Object
Get the first HDU in the file.
161 162 163 |
# File 'lib/rfits/rfits.rb', line 161 def first self[0] end |
#last ⇒ Object
Get the last HDU in the file.
156 157 158 |
# File 'lib/rfits/rfits.rb', line 156 def last self[-1] end |
#length ⇒ Object
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 |
#mode ⇒ Object
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 |
#path ⇒ Object
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_type ⇒ Object
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 |