File: test_io.py

package info (click to toggle)
spectral-cube 0.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,136 kB
  • sloc: python: 13,236; makefile: 154
file content (166 lines) | stat: -rw-r--r-- 5,867 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
import pytest

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

from radio_beam import Beam


from .. import SpectralCube, VaryingResolutionSpectralCube
from ..dask_spectral_cube import DaskSpectralCube


@pytest.mark.skipif(
    sys.byteorder!="little",
    reason="https://github.com/radio-astro-tools/spectral-cube/issues/903",
)
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(data_adv, data_advs, use_dask):
    f3 = pyfits.open(data_adv)
    f4 = pyfits.open(data_advs)
    f3b = pyfits.PrimaryHDU(data=f3[0].data, header=f4[0].header)

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

    assert c1.shape == c3.shape
    # c2 has a different shape on disk...
    f3.close()
    f4.close()


def test_4d_stokes(data_advs, use_dask):
    f = pyfits.open(data_advs)
    c = StokesSpectralCube.read(f, use_dask=use_dask)
    assert isinstance(c, StokesSpectralCube)
    if use_dask:
        assert isinstance(c.I, DaskSpectralCube)
    else:
        assert isinstance(c.I, SpectralCube)
    f.close()


def test_4d_stokes_read_3d(data_adv, use_dask):
    # Regression test for a bug that caused StokesSpectralCube.read to not work
    # correctly when reading in a 3D FITS file.
    f = pyfits.open(data_adv)
    c = StokesSpectralCube.read(f, use_dask=use_dask)
    assert isinstance(c, StokesSpectralCube)
    f.close()


def test_3d_beams(data_vda_beams, use_dask):
    c = SpectralCube.read(data_vda_beams, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1)


def test_4d_beams(data_sdav_beams, use_dask):
    c = SpectralCube.read(data_sdav_beams, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1)


def test_4d_beams_nounits(data_sdav_beams_nounits, use_dask):
    c = SpectralCube.read(data_sdav_beams_nounits, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1)
    assert c.beams[0].major.unit == u.arcsec
    assert c.beams[0].minor.unit == u.arcsec


def test_3d_beams_roundtrip(tmpdir, data_vda_beams, use_dask):
    c = SpectralCube.read(data_vda_beams, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1)
    c.write(tmpdir.join('vda_beams_out.fits').strpath)
    c2 = SpectralCube.read(tmpdir.join('vda_beams_out.fits').strpath, use_dask=use_dask)

    # 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.4)
    np.testing.assert_almost_equal(c2.beams[0].minor.value, 0.1)
    assert c2.beams[0].major.unit == u.arcsec
    assert c2.beams[0].minor.unit == u.arcsec


def test_4d_beams_roundtrip(tmpdir, data_sdav_beams, use_dask):
    # not sure if 4d can round-trip...
    c = SpectralCube.read(data_sdav_beams, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1)
    c.write(tmpdir.join('sdav_beams_out.fits').strpath)
    c2 = SpectralCube.read(tmpdir.join('sdav_beams_out.fits').strpath, use_dask=use_dask)

    # 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.4)
    np.testing.assert_almost_equal(c2.beams[0].minor.value, 0.1)
    assert c2.beams[0].major.unit == u.arcsec
    assert c2.beams[0].minor.unit == u.arcsec


def test_1d(data_5_spectral):
    hdul = pyfits.open(data_5_spectral)
    hdu = hdul[0]
    spec = OneDSpectrum.from_hdu(hdu)

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

def test_1d_beams(data_5_spectral_beams, use_dask):
    hdu = pyfits.open(data_5_spectral_beams)
    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
    hdu.close()



def test_aips_beams_units(tmpdir, data_455_degree_beams, use_dask):
    """
    regression test for #737, AIPS beam unit specs (degrees)
    """
    c = SpectralCube.read(data_455_degree_beams, use_dask=use_dask)
    np.testing.assert_almost_equal(c.beams[0].major.value, 0.4/3600)
    np.testing.assert_almost_equal(c.beams[0].minor.value, 0.1/3600)
    np.testing.assert_almost_equal(c.beams[0].major.to(u.arcsec).value, 0.4)
    np.testing.assert_almost_equal(c.beams[0].minor.to(u.arcsec).value, 0.1)



def test_vrsc_fullstokes_read_fits(data_advs_beams_fullstokes):
    '''
    Test reading a spectral line data cube with full stokes and a beam table.
    '''
    c = StokesSpectralCube.read(data_advs_beams_fullstokes)

    for component in ['I', 'Q', 'U', 'V']:
        assert isinstance(c[component], VaryingResolutionSpectralCube)
        assert hasattr(c[component], 'beams')