Class: RFits::Header

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

Overview

Represents the header keyword information of an HDU. Behaves much like a hash.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hdu) ⇒ Header

Initialize the header. This doesn’t actually write bytes to file–use Header#create for that.

header = Header.new(hdu)


222
223
224
# File 'lib/rfits/rfits.rb', line 222

def initialize(hdu)
  @hdu = hdu
end

Instance Attribute Details

#hduObject (readonly)

The HDU associated with the header.



218
219
220
# File 'lib/rfits/rfits.rb', line 218

def hdu
  @hdu
end

Instance Method Details

#[](keyname, include_comment = false, as = nil) ⇒ Object

Retrieve the value of the specified keyword. If include_comment is true, the returned value will be a two-element array with the value as the first element, and the comment as the second. The return type of the value will be determined automatically–this is almost always what you want. However, if necessary, you may specify the type using as, which may be one of: “C” (string), “L” (logical), “I” (integer), “F” (float) or “C” (complex) and the library will try to do the right thing.

puts header['OBJNAME']             # M31
puts header['OBJNAME', true]       # ['M31', 'The name of the object']
puts header['BITPIX']              # 8
puts header['BITPIX', false, 'F']  # 8.0


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rfits/rfits.rb', line 238

def [](keyname, include_comment=false, as=nil)
  self.hdu.reset_position()
  IO::Proxy.fits_read_keyn(self.hdu.file.io, 0)
 
  begin
    keytype = as ? as : IO::Proxy.fits_get_keytype(IO::Proxy.fits_read_keyword(self.hdu.file.io, keyname).first) rescue IO::Exception
    key = case keytype
      when 'C'          then IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TSTRING, keyname)
      when 'L'          then IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TLOGICAL, keyname)
      when 'I'          then IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TLONG, keyname)
      when 'F'          then IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TDOUBLE, keyname)
      when 'X'          then IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TDBLCOMPLEX, keyname)
      else  IO::Proxy.fits_read_key(self.hdu.file.io, IO::Proxy::TSTRING, keyname)
    end
    include_comment ? key : key.first
  rescue IO::Exception
    nil
  end
end

#[]=(keyname, keyvalue) ⇒ Object

Set the value of the keyword. If keyvalue is an array, the first element of that array is assumed to be the value, and the second the comment. The value may be a: String, boolean, Fixnum, Float or Complex object. Anything else will be serialized using it’s to_s method and saved as a string.

header['TEST1'] = 123                    # set keyword value to 123
header['TEST1'] = [123, 'a test integer'] # set keyword value to 123 with "a test integer" as the comment


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rfits/rfits.rb', line 266

def []=(keyname, keyvalue)
  self.hdu.reset_position()
  
  value, comment = keyvalue.is_a?(Array) ? keyvalue : [keyvalue, nil]
  
  dtype = case value
    when String     then IO::Proxy::TSTRING
    when TrueClass  then IO::Proxy::TLOGICAL
    when FalseClass then IO::Proxy::TLOGICAL
    when Fixnum     then IO::Proxy::TLONG
    when Float      then IO::Proxy::TDOUBLE
    when Complex    then IO::Proxy::TDBLCOMPLEX
    else
      value = value.to_s
      IO::Proxy::TSTRING
  end

  IO::Proxy.fits_update_key(self.hdu.file.io, dtype, keyname, value, comment)
end

#commentObject

Retrieve COMMENT keywords as a string.

puts header.comments   # This file is part of the EUVE Science Archive. It contains...


327
328
329
330
331
332
333
334
335
336
# File 'lib/rfits/rfits.rb', line 327

def comment
  self.hdu.reset_position()
  
  statements = []
  self.each(false, true) do |keyname, keyvalue, keycomment|
    statements << keycomment.strip.gsub(/^\'/, '').gsub(/\'$/, '') if keyname == 'COMMENT'
  end
  
  statements.join("\n")
end

#comment=(comment) ⇒ Object

Append a COMMENT keyword.

header.comment = "My comment"


340
341
342
343
# File 'lib/rfits/rfits.rb', line 340

def comment=(comment)
  self.hdu.reset_position()
  IO::Proxy.fits_write_comment(self.hdu.file.io, comment)
end

#delete(keyname) ⇒ Object

Delete the specified keyword.

header.delete('OBJNAME')


313
314
315
316
# File 'lib/rfits/rfits.rb', line 313

def delete(keyname)
  self.hdu.reset_position()
  IO::Proxy.fits_delete_key(self.hdu.file.io, keyname)
end

#each(typecast = true, comment_keywords = false) ⇒ Object

Iterate through each keyword pair in the header. By default, values will be converted to their appropriate types, but this can be overridden by setting typecast to false. Additionally, COMMENT and HISTORY keywords are ignored unless comment_keywords is set to true.

header.each do |name, value, comment|
 puts "#{name} = #{value}"
end


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rfits/rfits.rb', line 295

def each(typecast=true, comment_keywords=false)
  self.hdu.reset_position()
  
  (1..self.length).each do |i|
    keyname = value = comment = nil
    if typecast
      keyname = IO::Proxy.fits_read_keyn(self.hdu.file.io, i).first
      value, comment = self[keyname, true]
    else
      keyname, value, comment = IO::Proxy.fits_read_keyn(self.hdu.file.io, i)
    end
    
    yield(keyname, value, comment) unless (keyname == "COMMENT" or keyname == "HISTORY") and !comment_keywords
  end
end

#historyObject

Retrieve HISTORY keywords as a strong.

puts header.history


347
348
349
350
351
352
353
354
355
356
# File 'lib/rfits/rfits.rb', line 347

def history
  self.hdu.reset_position()
  
  statements = []
  self.each(false, true) do |keyname, keyvalue, keycomment|
    statements << keycomment.strip.gsub(/^\'/, '').gsub(/\'$/, '') if keyname == 'HISTORY'
  end
  
  statements.join("\n")
end

#history=(history) ⇒ Object

Append a HISTORY comment.

header.history = "My history"


360
361
362
363
# File 'lib/rfits/rfits.rb', line 360

def history=(history)
  self.hdu.reset_position()
  IO::Proxy.fits_write_history(self.hdu.file.io, history)
end

#lengthObject

The number of keyword pairs in the header.

puts header.length   # 62


320
321
322
323
# File 'lib/rfits/rfits.rb', line 320

def length
  self.hdu.reset_position()
  IO::Proxy.fits_get_hdrspace(self.hdu.file.io).first
end

#to_sObject

Convert the header to a string.



366
367
368
369
# File 'lib/rfits/rfits.rb', line 366

def to_s
  self.hdu.reset_position()
  IO::Proxy.fits_hdr2str(self.hdu.file.io, false, [], 0)
end