File: test_io.py

package info (click to toggle)
spectral-cube 0.4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,236 kB
  • sloc: python: 15,520; makefile: 154; ansic: 88
file content (102 lines) | stat: -rw-r--r-- 3,141 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import print_function, absolute_import, division

import os
import tempfile

import numpy as np
from astropy.io import fits as pyfits
from astropy import units as u
from ..io import class_lmv, fits
from .. import SpectralCube, StokesSpectralCube
from ..lower_dimensional_structures import (OneDSpectrum,
                                            VaryingResolutionOneDSpectrum)
from . import path

from radio_beam import Beam

def test_lmv_fits():
    c1 = SpectralCube.read(path('example_cube.fits'))
    c2 = SpectralCube.read(path('example_cube.lmv'))

    assert c1.shape == c2.shape

    # should be able to do this, but it is not true:
    #assert c1.header == c2.header

    # should also be able to do this, but it is again false:
    #assert c1.wcs==c2.wcs

def test_3d_4d_stokes():
    f3 = pyfits.open(path('adv.fits'))
    f4 = pyfits.open(path('advs.fits'))
    f3b = pyfits.PrimaryHDU(data=f3[0].data, header=f4[0].header)

    c1 = SpectralCube.read(f3)
    c2 = SpectralCube.read(f4)
    c3 = SpectralCube.read(f3b)

    assert c1.shape == c3.shape
    # c2 has a different shape on disk...

def test_4d_stokes():
    f = pyfits.open(path('advs.fits'))
    c = StokesSpectralCube.read(f)
    assert isinstance(c, StokesSpectralCube)

def test_3d_beams():
    c = SpectralCube.read(path('vda_beams.fits'))
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)

def test_4d_beams():
    c = SpectralCube.read(path('sdav_beams.fits'))
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)


def test_3d_beams_roundtrip():
    c = SpectralCube.read(path('vda_beams.fits'))
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)
    tmp = tempfile.mkstemp(suffix='.fits')[1]
    try:
        c.write(tmp, overwrite = True)
        c2 = SpectralCube.read(tmp)
    finally:
        os.remove(tmp)

    # assert c==c2 # this is not implemented?
    assert np.all(c.filled_data[:] == c2.filled_data[:])
    #assert c.wcs == c2.wcs # not implemented correctly?

    np.testing.assert_almost_equal(c2.beams[0].major.value, 0.1)


def test_4d_beams_roundtrip():
    # not sure if 4d can round-trip...
    c = SpectralCube.read(path('sdav_beams.fits'))
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)
    tmp = tempfile.mkstemp(suffix='.fits')[1]
    try:
        c.write(tmp, overwrite=True)
        c2 = SpectralCube.read(tmp)
    finally:
        os.remove(tmp)

    # assert c==c2 # this is not implemented?
    assert np.all(c.filled_data[:] == c2.filled_data[:])
    #assert c.wcs == c2.wcs # not implemented correctly?

    np.testing.assert_almost_equal(c2.beams[0].major.value, 0.1)

def test_1d():
    hdu = pyfits.open(path('5_spectral.fits'))[0]
    spec = OneDSpectrum.from_hdu(hdu)

    np.testing.assert_almost_equal(spec, np.arange(5, dtype='float'))

def test_1d_beams():
    hdu = pyfits.open(path('5_spectral_beams.fits'))
    spec = OneDSpectrum.from_hdu(hdu)

    np.testing.assert_almost_equal(spec, np.arange(5, dtype='float'))
    assert isinstance(spec, VaryingResolutionOneDSpectrum)
    assert hasattr(spec, 'beams')
    assert len(spec.beams) == 5