Skip to content

Instantly share code, notes, and snippets.

@nmearl
Last active December 8, 2017 19:59
Show Gist options
  • Save nmearl/0748088345dba34663af1d75386618f2 to your computer and use it in GitHub Desktop.
Save nmearl/0748088345dba34663af1d75386618f2 to your computer and use it in GitHub Desktop.
Example of how to create a custom python loader function for reading data into SpecViz.
import os
from astropy.io import fits
from astropy.wcs import WCS
from astropy.units import Unit
from astropy.nddata import StdDevUncertainty
from specviz.interfaces.decorators import data_loader
from specviz.core.data import Spectrum1DRef
def fits_identify(file_name, *args, **kwargs):
# This function ensures that the file that's trying to be read by this
# loader is in fact a fits file, or something that can be understood
# by Astropy's fits loader.
return (isinstance(file_name, str) and
args[0].lower().split('.')[-1] in ['fits', 'fit', 'fits.gz'])
@data_loader(label="Example Fits", identifier=fits_identify, extensions=["fits", "fit"])
def simple_generic_loader(file_name, **kwargs):
# Get a name to use for the spectra object that's created when the data is loaded.
# Here, we'll just get the name of the file itself.
name = os.path.basename(file_name.rstrip(os.sep)).rsplit('.', 1)[0]
# Now, open the fits file
with fits.open(file_name, **kwargs) as hdulist:
# We grab the entire header object. We'll stick this in the spectrum's meta
# data dictionary so we can always have it inside SpecViz. This is useful
# when we want to export a spectrum created using this data, for example.
header = hdulist[0].header
err = hdulist[1].data['err']
flux = hdulist[1].data['flux']
# Dump the header into the meta dictionary
meta = {'header': header}
# Try and parse the WCS information from the header
wcs = WCS(header)
# Here, I set the unit explicitly, but you can imagine passing in a string from
# the fits file's header instead.
unit = Unit('erg / (Angstrom cm2 s)')
# Uncertainties should be explicitly defined. Currently, only standard deviation
# uncertainties are supported. All this means is that uncertainties will be
# correctly handled when doing spectrum arithmetic (e.g. propagation, etc). You
# can get more information about this from http://docs.astropy.org/en/stable/nddata/ccddata.html#uncertainty
uncertainty = StdDevUncertainty(err)
# A new spectrum object is returned, which specviz understands
return Spectrum1DRef(data=flux, name=name, wcs=wcs,
uncertainty=uncertainty, unit=unit, meta=meta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment