File: spectral_cube.py

package info (click to toggle)
glueviz 0.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,180 kB
  • ctags: 6,728
  • sloc: python: 37,111; makefile: 134; sh: 60
file content (56 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import absolute_import, division, print_function

from spectral_cube import SpectralCube, StokesSpectralCube

from glue.core import Data
from glue.config import data_factory, qglue_parser
from glue.core.data_factories.fits import is_fits
from glue.core.coordinates import coordinates_from_wcs

__all__ = ['read_spectral_cube', 'parse_spectral_cube']


def is_spectral_cube(filename, **kwargs):
    """
    Check that the file is a 3D or 4D FITS spectral cube
    """

    if not is_fits(filename):
        return False

    try:
        StokesSpectralCube.read(filename)
    except Exception:
        return False
    else:
        return True


def spectral_cube_to_data(cube, label=None):

    if isinstance(cube, SpectralCube):
        cube = StokesSpectralCube({'I': cube})

    result = Data(label=label)
    result.coords = coordinates_from_wcs(cube.wcs)

    for component in cube.components:
        data = getattr(cube, component).unmasked_data[...]
        result.add_component(data, label='STOKES {0}'.format(component))

    return result


@data_factory(label='FITS Spectral Cube', identifier=is_spectral_cube)
def read_spectral_cube(filename, **kwargs):
    """
    Read in a FITS spectral cube. If multiple Stokes components are present,
    these are split into separate glue components.
    """
    cube = StokesSpectralCube.read(filename)
    return spectral_cube_to_data(cube)


@qglue_parser((SpectralCube, StokesSpectralCube))
def parse_spectral_cube(cube, label):
    return [spectral_cube_to_data(cube, label=label)]