Read/write 1d spectrum from/to fits file

The routines read1dFitsSpec() and write1dFitsSpec() provide simple interfaces for reading and writing one-dimensional fits spectra with pyfits (

Reading fits spectrum

PyAstronomy.pyasl.read1dFitsSpec(fn, hdu=0, fullout=False, CRPIX1=None, keymap={})

Read a simple 1d spectrum from fits file.

Reads a 1d-spectrum from a file and constructs the associated wavelength axis. To this end, the expression: wvl = ((np.arange(N) + 1.0) - CRPIX1) * CDELT1 + CRVAL1 will be evaluated, where N is the number of bins and CRPIX1, CDELT1, and CRVAL1 are header keywords.



hduint, optional

The number of the HDU to be used. The default is 0, i.e., the primary HDU.

fulloutboolean, optional

If True, the header keywords used to construct the wavelength axis will be returned. The default is False.

CRPIX1int, optional

Can be used to circumvent missing CRPIX1 entry.

keymapdict, optional

Can be used to map header keywords


The wavelength array.


The flux array.


from PyAstronomy import pyasl
wvl, flx = pyasl.read1dFitsSpec("mySpectrum.fits")

Writing fits spectrum

PyAstronomy.pyasl.write1dFitsSpec(fn, flux, wvl=None, waveParams=None, fluxErr=None, header=None, clobber=False, refFileName=None, refFileExt=0)

Write a 1d spectrum with equidistant binning to a fits file.

Write a 1d-spectrum to a file. Wavelength axis and header keywords are related through the following expression: wvl = ((np.arange(N) + 1.0) - CRPIX1) * CDELT1 + CRVAL1, where CRPIX1, CDELT1, and CRVAL1 are the relevant header keywords.

The function allows to specify an existing fits extension, from which the header will be cloned. Alternatively, an arbitrary, user-defined header may be given.




Flux array

wvlarray, optional

Wavelength array. Either the wavelength array or the header keywords have to be provided (see waveParams).

waveParamsdict, optional

Wavelength information required in the fits-header. Required keys are CDELT, CRVAL, CRPIX or (CDELT1, CRVAL1, CRPIX1).

fluxErrarray, optional

Flux errors. If given, the error will be stored in an additional extension.

headerdict, optional

Dictionary with header information to be transfered to the new file. Note that the wavelength information will be overwritten by the information given to this routine. If both, a reference file to clone the header from and the header parameters are given, the cloned header from the reference file will be overwritten and extended by the keywords specified here.

refFileNamestring, optional

Clone header keywords from a reference file. Note that the wavelength information will be overwritten by the information by the information given to this routine.

refFileExtint, optional

Reference-file extension to be used for cloning the header keywords. The default is 0.

Example of usage

import numpy as np
from PyAstronomy import pyasl

# Generate a "spectrum"
wvl = np.arange(5000., 5010., 0.01)
flux = np.random.normal(1.0, 0.01, wvl.size)

# Write spectrum providing wavelength array
pyasl.write1dFitsSpec("test1.fits", flux, wvl=wvl, clobber=True)

# Write spectrum specifying wavelength-related header keywords
# manually
wp = {"CRVAL1": 5000., "CDELT1": 0.01, "CRPIX1": 1}
pyasl.write1dFitsSpec("test2.fits", flux, waveParams=wp, clobber=True)