Skip to content

Commit

Permalink
POC: Load MAST s3d files
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Sep 27, 2021
1 parent a8aeb8a commit ebb29b0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
42 changes: 18 additions & 24 deletions jdaviz/configs/cubeviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import numpy as np
from astropy.io import fits
from spectral_cube import SpectralCube
from specutils import Spectrum1D

from astropy.wcs import WCS
Expand Down Expand Up @@ -51,13 +50,25 @@ def parse_data(app, file_obj, data_type=None, data_label=None):
file_name = os.path.basename(file_obj)

with fits.open(file_obj) as hdulist:
_parse_hdu(app, hdulist, file_name=data_label or file_name)
prihdr = hdulist[0].header
telescop = prihdr.get('TELESCOP', '').lower()
filetype = prihdr.get('FILETYPE', '').lower()
if telescop == 'jwst' and filetype == '3d ifu cube':
from glue.core import Data
unit = u.Unit(hdulist[1].header.get('BUNIT', 'count'))
flux = hdulist[1].data << unit
wcs = WCS(hdulist[1].header, hdulist)
data = Data(flux=flux, coords=wcs) # Spectrum1D too slow
data_label = f'{file_obj}[SCI]'
app.add_data(data, data_label)
app.add_data_to_viewer('flux-viewer', data_label)
app.add_data_to_viewer('spectrum-viewer', data_label)
else:
_parse_hdu(app, hdulist, file_name=data_label or file_name)

# If the data types are custom data objects, use explicit parsers. Note
# that this relies on the glue-astronomy machinery to turn the data object
# into something glue can understand.
elif isinstance(file_obj, SpectralCube):
_parse_spectral_cube(app, file_obj, data_type or 'flux', data_label)
elif isinstance(file_obj, Spectrum1D) and len(file_obj.shape) == 3:
_parse_spectrum1d_3d(app, file_obj)
elif isinstance(file_obj, Spectrum1D):
Expand All @@ -80,12 +91,12 @@ def _parse_hdu(app, hdulist, file_name=None):
if hdu.data is None or not hdu.is_image or len(hdu.data.shape) != 3:
continue

wcs = WCS(hdu.header)
wcs = WCS(hdu.header, hdulist)

try:
flux_unit = u.Unit(hdu.header['BUNIT'])
except KeyError:
logging.warn("No flux units found in hdu, using u.count as a stand-in")
logging.warning("No flux units found in hdu, using u.count as a stand-in")
flux_unit = u.count
finally:
flux = hdu.data * flux_unit
Expand All @@ -96,7 +107,7 @@ def _parse_hdu(app, hdulist, file_name=None):
sc = Spectrum1D(flux=flux, wcs=wcs)
app.data_collection[data_label] = sc
except Exception as e:
logging.warn(e)
logging.warning(e)
continue

# If the data type is some kind of integer, assume it's the mask/dq
Expand Down Expand Up @@ -130,23 +141,6 @@ def _fix_axes(app):
app.get_viewer("spectrum-viewer").viewer_options.x_att_selected = 5


def _parse_spectral_cube(app, file_obj, data_type='flux', data_label=None):
data_label = data_label or f"Unknown spectral cube[{data_type.upper()}]"

app.add_data(file_obj, data_label)

if data_type == 'flux':
app.add_data_to_viewer('flux-viewer', f"{data_label}")
app.add_data_to_viewer('spectrum-viewer', f"{data_label}")
elif data_type == 'mask':
app.add_data_to_viewer('mask-viewer', f"{data_label}")
elif data_type == 'uncert':
app.add_data_to_viewer('uncert-viewer', f"{data_label}")

# TODO: SpectralCube does not store mask information
# TODO: SpectralCube does not store data quality information


def _parse_spectrum1d_3d(app, file_obj):
# Load spectrum1d as a cube

Expand Down
17 changes: 1 addition & 16 deletions jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from astropy.io import fits
from astropy.nddata import StdDevUncertainty
from astropy.wcs import WCS
from spectral_cube import SpectralCube
from specutils import Spectrum1D

from jdaviz.app import Application
Expand Down Expand Up @@ -49,28 +48,14 @@ def image_hdu_obj():
return fits.HDUList([fits.PrimaryHDU(), flux_hdu, mask_hdu, uncert_hdu])


@pytest.mark.filterwarnings('ignore:.* contains multiple slashes')
@pytest.mark.filterwarnings('ignore')
def test_fits_image_hdu_parse(image_hdu_obj, cubeviz_app):
cubeviz_app.load_data(image_hdu_obj)

assert len(cubeviz_app.data_collection) == 3
assert cubeviz_app.data_collection[0].label.endswith('[FLUX]')


@pytest.mark.filterwarnings('ignore:.* contains multiple slashes')
def test_spectral_cube_parse(tmpdir, image_hdu_obj, cubeviz_app):
f = tmpdir.join("test_fits_image.fits")
path = os.path.join(f.dirname, f.basename)
image_hdu_obj.writeto(path)

sc = SpectralCube.read(path, hdu=1)

cubeviz_app.load_data(sc)

assert len(cubeviz_app.data_collection) == 1
assert cubeviz_app.data_collection[0].label.endswith('[FLUX]')


def test_spectrum1d_parse(image_hdu_obj, cubeviz_app):
spec = Spectrum1D(flux=np.random.sample(10) * u.Jy,
spectral_axis=np.arange(10) * u.nm,
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/default/plugins/line_lists/line_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _on_viewer_data_changed(self, msg=None):

try:
viewer_data = self.app.get_viewer('spectrum-viewer').data()
except TypeError:
except Exception:
warn_message = SnackbarMessage("Line list plugin could not retrieve data from viewer",
sender=self, color="error")
self.hub.broadcast(warn_message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def _on_viewer_data_changed(self, msg=None):
if msg is not None and msg.viewer_id != self._viewer_id:
return

self._viewer_data = self.app.get_data_from_viewer('spectrum-viewer',
include_subsets=False)
try:
self._viewer_data = self.app.get_data_from_viewer('spectrum-viewer',
include_subsets=False)
except Exception: # Some data cannot be parsed as Spectrum1D
return

self.dc_items = [data.label
for data in self.app.data_collection
Expand Down

0 comments on commit ebb29b0

Please sign in to comment.