Hyperspectral Cube

This class is a very simple (understand : not fully-featured nor exhaustive) model of a hyperspectral cube.

class galpak.HyperspectralCube(data=None, header=None, verbose=False, filename=None, variance=None)

A hyperspectral cube has 3 dimensions : 2 spatial, and 1 spectral. It is basically a rectangular region of the sky seen on a discrete subset of the light spectrum. It is heavily used in spectral imaging, and is provided by spectroscopic explorers such as MUSE.

This class is essentially a convenience wrapper for data values and header specs of a single hyperspectral cube.

It understands the basic arithmetic operations + - * / **, which should behave much like with numpy’s ndarrays, and update the header accordingly. An operation between a cube and a number will behave as expected, by applying the operation voxel-by-voxel to the cube. (it broadcasts the number to the shape of the cube) An operation between a cube and an image (a slice along the spectral axis, z) will behave as expected, by applying the operation image-by-image to the cube. (it broadcasts the image to the shape of the cube)

It also understands indexation of the form cube[zmin:zmax, ymin:ymax, xmin:xmax].

See also http://en.wikipedia.org/wiki/Hyperspectral_imaging .

Note : no FSCALE management is implemented yet.

You can load from a single HDU of a FITS (Flexible Image Transport System) file :

HyperspectralCube.from_file(filename, hdu_index=None, verbose=False)
data: numpy.ndarray|None
3D numpy ndarray holding the raw data. The indices are the pixel coordinates in the cube. Note that in order to be consistent with astropy.io.fits, data is indexed in that order : λ, y, x.
header: fits.Header|None
http://docs.astropy.org/en/latest/io/fits/api/headers.html Note: this might become its own wrapper class in the future.
verbose: boolean
Set to True to log everything and anything.
First reads the header for
xy_step (CDELT1 or CD1_1) z_step (CDELT3 or CD3_3) z_cunit (CUNIT3) crpix (CRPIX3) crval (CRVAL3).

If some of these are None, get the default values from the instrument and then reset the header :

xy_step: Instrument.xy_step –> self.xy_step z_step: Instrument.z_step –> self.z_step –> CDELT3 z_cunit: Instrument.z_cunit –> self.z_cunit –> CUNIT3 crpix: shape[0]/2 –> CRPIX3 crval: Instrument.z_central –> self.z_central –> CRVAL3


static from_file(filename, hdu_index=None, verbose=False)

Factory to create a HyperspectralCube from one HDU in a FITS file. http://fits.gsfc.nasa.gov/fits_standard.html

No other file formats are supported at the moment. You may specify the index of the HDU you want. If you don’t, it will try to guess by searching for a HDU with EXTNAME=DATA, or it will pick the first HDU in the list that has 3 dimensions.

filename: string
An absolute or relative path to the FITS file we want to load. The astropy.io.fits module is used to open the file.
hdu_index: integer|None
Index of the HDU to load. If you set this, guessing is not attempted.
verbose: boolean
Should we fill up the log with endless chatter ?
Return type:HyperspectralCube

Returns a list of the 3 steps [λ,y,x]. The units are the ones specified in the header.


Does this cube have a header ?

return boolean


Initialize steps xy_steps z_steps z_cunnit and z_central z_central requires CRPIX3 CRVAL3 CDELT3


Initialize steps xy_steps z_steps z_cunnit and z_central z_central requires CRPIX3 CRVAL3 CDELT3


Is this cube void of any data ?

return boolean

sanitize(header=None, data=None)

Procedurally apply various sanitization tasks : - http://docs.astropy.org/en/latest/io/fits/usage/verification.html (todo) - Fix spectral step keyword :

  • CDEL_3 –> CDELT3
  • CD3_3 –> CDELT3
  • Fix blatantly illegal units :
    • DEG –> deg
    • MICRONS –> um

Sanitizes this cube’s header and data, or provided header and data.


header and data are mutated, not copied, so this method returns nothing.


Get the wavelength (in the unit specified in the header) of the specified pixel index along z. The pixel index should start at 0.

Return type:float
write_to(filename, clobber=False)

Write this cube to a FITS file.

filename: string
The filename (absolute or relative) of the file we want to write to. The astropy.io.fits module is used to write to the file.
clobber: bool
When set to True, will overwrite the output file if it exists.


This class is not fully formed yet and its API may evolve as it moves to its own module.