Class: RFits::Image

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

Overview

An Image HDU.

Constant Summary collapse

IMG_TYPE_MAP =
{
  IO::Proxy::BYTE_IMG => :byte,
  IO::Proxy::SHORT_IMG => :short,
  IO::Proxy::LONG_IMG => :long,
  IO::Proxy::LONGLONG_IMG => :longlong,
  IO::Proxy::FLOAT_IMG => :float,
  IO::Proxy::DOUBLE_IMG => :double
}

Constants included from Compressible

Compressible::COMPRESSION_DIM_KEY, Compressible::COMPRESSION_NOISE_BIT_NAME_VALUE, Compressible::COMPRESSION_SCALE_NAME_VALUE, Compressible::COMPRESSION_SMOOTH_NAME_VALUE, Compressible::COMPRESSION_TILE_KEY_ROOT, Compressible::COMPRESSION_TYPE_KEY, Compressible::COMPRESSION_TYPE_MAP, Compressible::COMPRESSION_VAR_KEY_ROOT, Compressible::COMPRESSION_VAR_VAL_ROOT

Constants inherited from HDU

HDU::HDU_TYPE_MAP

Instance Attribute Summary collapse

Attributes inherited from HDU

#data, #file, #header

Instance Method Summary collapse

Methods included from Compressible

#activate_compression, #compression_type, #compression_type=, #deactivate_compression, #hcomp_scale, #hcomp_scale=, #hcomp_smooth, #hcomp_smooth=, #noise_bits, #noise_bits=, #tile_dim, #tile_dim=

Methods inherited from HDU

#hdu_type, #position, #reset_position

Constructor Details

#initialize(file, extpos, bitpix, naxes, coptions = nil) ⇒ Image

Instantiate a new image in the specified file at the specified position, of a certain data type and size. “coptions” is optional, but if present see Compressible#activate_compression for allowed format.

If you do activate compression for an image there is one “gotcha” that might take you by surprise: if there are no existing HDUs in the file and you add a compressed image, an extra image HDU at the start of the file will be created. In other words, your new compressed image will be at fits not fits. This is because cfitsio compresses the image by wrapping it in a binary table, but the FITS specification mandates that the primary HDU must always be an image array.



980
981
982
983
984
985
986
987
# File 'lib/rfits/rfits.rb', line 980

def initialize(file, extpos, bitpix, naxes, coptions=nil)
  super(file, extpos)
  
  @bitpix = bitpix
  @naxes = naxes
  
  @compression_options = coptions
end

Instance Attribute Details

#compression_optionsObject (readonly)

Returns the value of attribute compression_options.



960
961
962
# File 'lib/rfits/rfits.rb', line 960

def compression_options
  @compression_options
end

Instance Method Details

#bitpix(equiv = true) ⇒ Object

Number of bits per data pixel. By default returns the “equivalent type” unless equiv is set to false.



1044
1045
1046
1047
# File 'lib/rfits/rfits.rb', line 1044

def bitpix(equiv=true)
  reset_position()
  equiv ? IO::Proxy.fits_get_img_equivtype(self.file.io) : IO::Proxy.fits_get_img_type(self.file.io)
end

#bitpix=(bitpix) ⇒ Object

Alter the datatype of the image.

img.bitpix = IO::Proxy::LONG_IMG


1051
1052
1053
1054
# File 'lib/rfits/rfits.rb', line 1051

def bitpix=(bitpix)
  reset_position()
  IO::Proxy.fits_resize_img(self.file.io, bitpix, self.naxis, self.naxes)
end

#compressed?Boolean

Returns true if the image is compressed, false otherwise.

Returns:

  • (Boolean)


1064
1065
1066
1067
# File 'lib/rfits/rfits.rb', line 1064

def compressed?
  reset_position()
  IO::Proxy.fits_is_compressed_image(self.file.io) == 1 ? true : false
end

#create(at) ⇒ Object

Actually create a physical image (i.e. write bytes). “at” is the position at which to insert the image.



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/rfits/rfits.rb', line 991

def create(at)
  self.deactivate_compression
  
  # A dummy image gets added if to the top of the file if
  # the first image added is compressed.
  @extpos = (self.file.length == 0 and self.compression_options) ? at + 1 : at
  
  self.file.set_position(at - 1 < 0 ? 0 : at - 1)
  self.activate_compression() if self.compression_options
  IO::Proxy.fits_insert_img(self.file.io, @bitpix, @naxes.size, @naxes)

  # This just makes sure something is written out right away.
  self.header['RFITS1234'] = 'temp'
  self.header.delete('RFITS1234')
end

#dimObject

The number of dimensions of the image (i.e. 2).



1013
1014
1015
1016
# File 'lib/rfits/rfits.rb', line 1013

def dim
  reset_position()
  IO::Proxy.fits_get_img_dim(self.file.io)
end

#image_typeObject

The type of image. Possibilities include: :byte, :short, :long, :longlong, :float, :double



1037
1038
1039
1040
1041
# File 'lib/rfits/rfits.rb', line 1037

def image_type
  reset_position()
  etype = IO::Proxy.fits_get_img_equivtype(self.file.io)
  IMG_TYPE_MAP[etype] || etype
end

#naxesObject

Alias for Image#size



1025
1026
1027
# File 'lib/rfits/rfits.rb', line 1025

def naxes
  self.size
end

#naxes=(naxes) ⇒ Object

Alter image dimensions to the specified size.

img.naxes = [1000, 200]


1031
1032
1033
1034
# File 'lib/rfits/rfits.rb', line 1031

def naxes=(naxes)
  reset_position()
  IO::Proxy.fits_resize_img(self.file.io, self.bitpix, naxes.size, naxes)
end

#naxisObject

Alias for Image#dim



1008
1009
1010
# File 'lib/rfits/rfits.rb', line 1008

def naxis
  self.dim
end

#resize(bitpix, naxes) ⇒ Object

Resize the image to the specified dimensions and data type.

img.resize(IO::Proxy::LONG_IMG, [1000, 200])


1058
1059
1060
1061
# File 'lib/rfits/rfits.rb', line 1058

def resize(bitpix, naxes)
  reset_position()
  IO::Proxy.fits_resize_img(self.file.io, bitpix, naxes.size, naxes)
end

#sizeObject

The size of each dimension (i.e. [512, 512]).



1019
1020
1021
1022
# File 'lib/rfits/rfits.rb', line 1019

def size
  reset_position()
  IO::Proxy.fits_get_img_size(self.file.io, self.naxis)
end