Skip to content

Commit

Permalink
Add support for loading Spectrum1D cubes
Browse files Browse the repository at this point in the history
  • Loading branch information
javerbukh committed May 3, 2021
1 parent 4a6a755 commit 87b413f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 231 deletions.
37 changes: 31 additions & 6 deletions jdaviz/configs/cubeviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ def _parse_hdu(app, hdulist, file_name=None):

wcs = WCS(hdu.header)

if 'BUNIT' in hdu.header:
flux = hdu.data * u.Unit(hdu.header['BUNIT'])
else:
# TODO Add warning that actual units were not found and using fake ones
try:
flux_unit = u.Unit(hdu.header['BUNIT'])
except KeyError as e:
logging.warn("No flux units found in hdu, using Jansky as a stand-in")
flux = hdu.data * u.Jy
flux_unit = u.Jy
finally:
flux = hdu.data * flux_unit

flux = np.moveaxis(flux, 1, 2)
flux = np.moveaxis(flux, 0, 1)
Expand Down Expand Up @@ -132,7 +133,31 @@ def _parse_spectral_cube(app, file_obj, data_type='flux', data_label=None):

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

for attr in ["flux", "mask", "uncertainty"]:
if attr == "mask":
flux = getattr(file_obj, attr) * file_obj.flux.unit
elif attr == "uncertainty":
flux = getattr(file_obj, attr)
flux = u.Quantity(flux.array) * file_obj.flux.unit
else:
flux = getattr(file_obj, attr)

flux = np.moveaxis(flux, 1, 2)
flux = np.moveaxis(flux, 0, 1)

s1d = Spectrum1D(flux=flux, wcs=file_obj.wcs)

data_label = f"Unknown spectrum object[{attr}]"
app.data_collection[data_label] = s1d

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


def _parse_spectrum1d(app, file_obj):
Expand Down
226 changes: 1 addition & 225 deletions notebooks/CubevizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,230 +29,6 @@
"fn = download_file('https://stsci.box.com/shared/static/28a88k1qfipo4yxc4p4d40v4axtlal8y.fits', cache=True)\n",
"viz.load_data(fn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Begin development code for SpectralCube to Specutils transition"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load Specutils1D cube into jdaviz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from specutils import Spectrum1D\n",
"from astropy.io import fits\n",
"from spectral_cube.io.fits import FITSReadError\n",
"from astropy.io.registry import IORegistryError\n",
"from astropy.wcs import WCS\n",
"from astropy import units as u\n",
"\n",
"\n",
"temp = Spectrum1D.read(fn)\n",
"viz.app.data_collection[\"temp\"] = temp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why is shape different?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temp.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"viz.app.data_collection[0].data.get_object().shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternate way of loading"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with fits.open(fn) as hdulist:\n",
" hdulist = fits.open(fn)\n",
" wcs = WCS(hdulist[1].header)\n",
" flux = hdulist[1].data * u.Unit(\"Jy\")\n",
" print(wcs.world_axis_names)\n",
" #wcs.pixel_n_dim = 3\n",
" #wcs.world_axis_names = ['', '', '']\n",
"# print(wcs)\n",
" sc = Spectrum1D(flux=flux, wcs=wcs)\n",
"# sc.wcs = None\n",
"# sc.wcs = wcs\n",
"# sc.flux.wcs = wcs\n",
" viz.app.data_collection[\"temp\"] = sc\n",
"\n",
" \n",
"# print(dir(wcs))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# viz.load_data(temp)\n",
"temp.wcs.world_axis_names[0].title()\n",
"#print(0 in temp.wcs.world_axis_names)\n",
"#print(len(temp.wcs.world_axis_names))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"viz.app.data_collection[\"temp_flux\"] = temp.flux"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temp.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(temp.wcs.world_axis_names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temp.wcs.pixel_n_dim"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temp.ndim = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from glue.core import Data\n",
"from glue_astronomy.spectral_coordinates import SpectralCoordinates\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# temp.ndim = lambda: None\n",
"# setattr(temp.ndim, 'somefield', 'somevalue')\n",
"wcs = temp.wcs\n",
"temp.wcs = WCS(wcs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"coords = SpectralCoordinates(temp.spectral_axis)\n",
"data = Data(coords=coords)\n",
"data['flux'] = temp.flux.value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from astropy.wcs import WCS\n",
"\n",
"print(isinstance(wcs,WCS))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(type(temp.wcs))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"viz.app.data_collection[0].data.get_object().wcs.world_axis_names"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"viz.app.data_collection[0].data.get_object().wcs.pixel_n_dim"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -271,7 +47,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 87b413f

Please sign in to comment.