Python Tip: Re-sampling spectra with pysynphot

by Jessica Lu on August 12, 2013

This post was inspired by a question from John Johnson.

Have you ever wanted to plot a model spectrum at lower resolution? Or compare a model spectrum with an observed spectrum? Have you ever wanted to shift several observed spectra to a common redshift to stack them up? In all these cases, you would need to resample the spectrum. Just such a scenario arose and I was asked:

What is the recommended way to resample a 1D spectrum?

I recalled using pysynphot, which is very useful for working with 1D spectra. However, the documentation is still a bit thin. After a bit of of exploring, here is an example that John Johnson and I wrote to illustrate our answer to the question. This package takes care to resample while preserving flux. We hope you find this useful! Or leave your alternative answer in the comments below.

from pysynphot import observation
from pysynphot import spectrum
 
def rebin_spec(wave, specin, wavnew):
    spec = spectrum.ArraySourceSpectrum(wave=wave, flux=specin)
    f = np.ones(len(wave))
    filt = spectrum.ArraySpectralElement(wave, f, waveunits='angstrom')
    obs = observation.Observation(spec, filt, binset=wavnew, force='taper')
 
    return obs.binflux

For those bleeding-edge python users out there, pysynphot is becoming an astropy affiliated package. You can download this new version from the pysynphot GitHub repository and test it out.

{ 3 comments… read them below or add one }

1 Christian Herenz August 13, 2013 at 1:40 pm

I don’t get why I would want to do it this complicated, not using some simple interpolation + scaling just using numpy packages? In the above example I would have no idea in the end what comes out….

If I know what units my input spectrum is both resampling and redshifting are straightforward mathematical expressions which can be entered as “pure” Numpy Python code.

Reply

2 Cristobal Sifon August 15, 2013 at 3:14 pm

Sorry for the self publicity, but for an example of what Christian Herenz said, check out
http://home.strw.leidenuniv.nl/~sifon/plotspec/
the program is given as stand alone but it’s python code which you can dig into (comments included).

Reply

3 Jessica Lu August 15, 2013 at 7:08 pm

Points in a spectrum are integrated fluxes over small wavelength bins, rather than flux densities. Pysynphot nicely preserves the integrated flux, while not all standard interpolation schemes do this.

Reply

Leave a Comment

Previous post:

Next post: