Skip to content

Commit

Permalink
Merge 8664536 into 8a4b213
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Jan 16, 2020
2 parents 8a4b213 + 8664536 commit 0662347
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
10 changes: 10 additions & 0 deletions spectral_cube/io/casa_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@


def is_casa_image(origin, filepath, fileobj, *args, **kwargs):

# See note before StringWrapper definition
from .core import StringWrapper
if len(args) > 0 and isinstance(args[0], StringWrapper):
filepath = args[0].value

return filepath is not None and filepath.lower().endswith('.image')


Expand Down Expand Up @@ -181,6 +187,10 @@ def load_casa_image(filename, skipdata=False,
memory.
"""

from .core import StringWrapper
if isinstance(filename, StringWrapper):
filename = filename.value

try:
import casatools
iatool = casatools.image
Expand Down
21 changes: 18 additions & 3 deletions spectral_cube/io/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
"""


# Due to a bug in the astropy I/O infrastructure which causes an exception
# for directories (which we need for .image), we need to wrap the filenames in
# a custom string so that astropy doesn't try and call get_readable_fileobj on
# them.
class StringWrapper:
def __init__(self, value):
self.value = value


class SpectralCubeRead(registry.UnifiedReadWrite):

__doc__ = DOCSTRING_READ_TEMPLATE.format(clsname='SpectralCube',
Expand All @@ -105,7 +114,10 @@ def __call__(self, filename, *args, **kwargs):
if isinstance(filename, PosixPath):
filename = str(filename)
kwargs['target_cls'] = BaseSpectralCube
return registry.read(BaseSpectralCube, filename, *args, **kwargs)
try:
return registry.read(BaseSpectralCube, filename, *args, **kwargs)
except IsADirectoryError: # See note above StringWrapper
return registry.read(BaseSpectralCube, StringWrapper(filename), *args, **kwargs)


class SpectralCubeWrite(registry.UnifiedReadWrite):
Expand Down Expand Up @@ -134,7 +146,10 @@ def __call__(self, filename, *args, **kwargs):
if isinstance(filename, PosixPath):
filename = str(filename)
kwargs['target_cls'] = StokesSpectralCube
return registry.read(StokesSpectralCube, filename, *args, **kwargs)
try:
return registry.read(StokesSpectralCube, filename, *args, **kwargs)
except IsADirectoryError: # See note above StringWrapper
return registry.read(StokesSpectralCube, StringWrapper(filename), *args, **kwargs)


class StokesSpectralCubeWrite(registry.UnifiedReadWrite):
Expand Down Expand Up @@ -174,6 +189,6 @@ def normalize_cube_stokes(cube, target_cls=None):
raise ValueError("Spectral cube is a Stokes cube that "
"does not have an I component")
elif target_cls is StokesSpectralCube and isinstance(cube, BaseSpectralCube):
cube = StokesSpectralCube({'I': cube})
return StokesSpectralCube({'I': cube})
else:
return cube
2 changes: 1 addition & 1 deletion spectral_cube/tests/test_casafuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_casa_read(filename, tmp_path):

make_casa_testimage(filename, tmp_path / 'casa.image')

casacube = SpectralCube.read(tmp_path / 'casa.image', format='casa_image')
casacube = SpectralCube.read(tmp_path / 'casa.image')

assert casacube.shape == cube.shape
# what other equalities should we check?
Expand Down
9 changes: 9 additions & 0 deletions spectral_cube/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def test_4d_stokes(data_advs):
f.close()


def test_4d_stokes_read_3d(data_adv):
# 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)
assert isinstance(c, StokesSpectralCube)
f.close()


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

0 comments on commit 0662347

Please sign in to comment.