astropy/specutils

Name: specutils

Owner: The Astropy Project

Description: Affiliated package for 1D spectral operations. Maintainer: @crawfordsm @keflavich @nmearl

Created: 2011-10-14 16:11:38.0

Updated: 2017-12-09 11:10:07.0

Pushed: 2017-11-02 19:48:28.0

Homepage:

Size: 4393

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Specutils

Specutils is an Astropy affiliated package with the goal of providing a shared set of Python representations of astronomical spectra and basic tools to operate on these spectra. The effort is also meant to be a “hub”, helping to unite the Python astronomical spectroscopy community around shared effort, much as Astropy is meant to for the wider astronomy Python ecosystem.

Note that Specutils is not intended as an all-in-one spectroscopic analysis or reduction tool. While it provides some basic analysis (following the Python philosophy of “batteries included”), it is also meant to facilitate connecting together disparate reduction pipelines and analysis tools through shared data representations.

Installation

There are several ways to install the package, the most direct is using pip

p install git+https://github.com/astropy/specutils

Otherwise, you may simply clone the repo and install the package

t clone https://github.com/astropy/specutils
 specutils
thon setup.py install

or, if you'd like to easily be able to pip uninstall, use the following commands

t clone https://github.com/astropy/specutils
 specutils
p install .
Quickstart

Defining a spectrum is straightforward

 astropy.units import Quantity
 specutils.spectra import Spectrum1D
rt numpy as np

ing Astropy `Quantity`s
 = Spectrum1D(flux=Quantity(np.random.sample(100), "erg/Angstrom/cm2/s"),
              dispersion=Quantity(np.arange(100), "Angstrom"))

thout `Quantity`s
 = Spectrum1D(flux=np.random.sample(100), unit="erg/Angstrom/cm2/s",
              dispersion=np.arange(100), disp_unit="Angstrom")

Converting to different units

.to_flux("Jy")

om Angstrom to Hz
.to_dispersion("Hz", rest=Quantity(202, "Angstrom"))

om Hz to cm/s
.to_dispersion("cm/s", rest=Quantity(1.48412108e+16, "Hz"))

om cm/s to Angstrom
.to_dispersion("Angstrom", rest=Quantity(1.48412108e+16, "Hz"))

Basic analysis usage

 specutils.analysis import equivalent_width

t(equivalent_width(spec))
Custom loaders

Define a custom loader in a separate python file and place the file in your ~/.specutils directory. Upon importing specutils, the loader will be added to the registry.

.specutils/my_custom_loader.py
rt os
rt six

 astropy.io import fits
 astropy.nddata import StdDevUncertainty
 astropy.table import Table
 astropy.units import Unit
 astropy.wcs import WCS

 specutils.io.registers import data_loader
 specutils.spectra import Spectrum1D

fine an optional identifier. If made specific enough, this circumvents the
ed to add `format="my-format"` in the `Spectrum1D.read` call.
identify_generic_fits(origin, *args, **kwargs):
return (isinstance(args[0], six.string_types) and
        os.path.splitext(args[0].lower())[1] == '.fits')


a_loader("my-format", identifier=identify_generic_fits)
generic_fits(file_name, **kwargs):
name = os.path.basename(file_name.rstrip(os.sep)).rsplit('.', 1)[0]

with fits.open(file_name, **kwargs) as hdulist:
    header = hdulist[0].header

    tab = Table.read(file_name)

    meta = {'header': header}
    wcs = WCS(hdulist[0].header)
    uncertainty = StdDevUncertainty(tab["err"])
    data = tab["flux"] * Unit("Jy")

return Spectrum1D(flux=data, wcs=wcs, uncertainty=uncertainty, meta=meta)

Using your custom loader:

 specutils import Spectrum1D

 = Spectrum1D.read("path/to/data", format="my-format")
Custom writer

Define a custom writer in a separate python file and place the file in your ~/.specutils directory. Upon importing specutils, the writer will be added to the registry.

.spectacle/my_writer.py
 astropy.table import Table
 specutils.io.registers import custom_writer


tom_writer("fits-writer")
generic_fits(spectrum, file_name, **kwargs):
flux = spectrum.flux.value
disp = spectrum.dispersion.value
meta = spectrum.meta

tab = Table([disp, flux], names=("dispersion", "flux"), meta=meta)

tab.write(file_name, format="fits")

Using your custom writer:

 = Spectrum1D(flux=Quantity(np.random.sample(100), "erg/Angstrom/cm2/s"),
              dispersion=Quantity(np.arange(100), "Angstrom"))

.write("my_output.fits", format="fits-writer")

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.