Spectral Extraction

A commonly required operation is extracting a spectrum from a part of a cube.

The simplest way to get a spectrum from the cube is simply to slice it along a single pixel:

>>> spectrum = cube[:, 50, 60]  

Slicing along the first dimension will create a OneDSpectrum object, which has a few useful capabilities.

Aperture Extraction

Going one level further, you can extract a spectrum from an aperture. We’ll start with the simplest variant: a square aperture. The cube can be sliced in pixel coordinates to produce a sub-cube which we then average spatially to get the spectrum:

>>> subcube = cube[:, 50:53, 60:63]  
>>> spectrum = subcube.mean(axis=(1,2))  

The spectrum can be obtained using any mathematical operation, such as the max or std, e.g., if you wanted to obtain the noise spectrum.

Slightly more sophisticated aperture extraction

To get the flux in a circular aperture, you need to mask the data. In this example, we don’t use any external libraries, but show how to create a circular mask from scratch and apply it to the data.:

>>> yy, xx = np.indices([5,5], dtype='float')  
>>> radius = ((yy-2)**2 + (xx-2)**2)**0.5  
>>> mask = radius <= 2  
>>> subcube = cube[:, 50:55, 60:65]  
>>> maskedsubcube = subcube.with_mask(mask)  
>>> spectrum = maskedsubcube.mean(axis=(1,2))  

Aperture and spectral extraction using regions

Spectral-cube supports ds9 and crtf regions, so you can use them to create a mask. The ds9/crtf region support relies on regions, which supports most shapes in ds9 and crtf, so you are not limited to circular apertures.

In this example, we’ll extract a subcube from ds9 region string using subcube_from_ds9region():

>>> ds9_str = 'fk5; circle(19:23:43.907, +14:30:34.66, 3")'  
>>> subcube = cube.subcube_from_ds9region(ds9_str)  
>>> spectrum = subcube.mean(axis=(1, 2))  

Similarly, one can extract a subcube from a crtf region string using subcube_from_crtfregion():

>>> crtf_str = 'circle[[19:23:43.907, +14:30:34.66], 3"], coord=fk5, range=[150km/s, 300km/s]'  
>>> subcube = cube.subcube_from_crtfregion(crtf_str)  
>>> spectrum = subcube.mean(axis=(1, 2))  

You can also use a _list_ of Region objects to extract a subcube using subcube_from_regions():

>>> import regions 
>>> regpix = regions.RectanglePixelRegion(regions.PixCoord(0.5, 1), width=4, height=2)  
>>> subcube = cube.subcube_from_regions([regpix])  
>>> spectrum = subcube.mean(axis=(1, 2))  

To learn more, go to Extracting a subcube from a DS9/CRTF region.