Skip to content

Commit

Permalink
Fix insturment broadening.
Browse files Browse the repository at this point in the history
There was a fixed resolution.

Added a test with variable R.
  • Loading branch information
jason-neal committed Aug 6, 2018
1 parent 3faa152 commit ad5eaee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
20 changes: 15 additions & 5 deletions spectrum_overload/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,24 @@ def normalize(self, method: str = "scalar", degree: Optional[int] = None, **kwar
def instrument_broaden(self, R, **pya_kwargs):
"""Broaden spectrum by instrumental resolution R.
Uses the pyastronomy instrBroadGaussFast function.
Uses the PyAstronomy instrBroadGaussFast function.
:param R:
:param pya_kwargs: kwarg parameters for pyasl.instrBroadGaussFast()
:return s: broadened spectrum
Parameters
----------
R: int
Instrumental Resolution
pya_kwargs: dict
kwarg parameters for pyasl.instrBroadGaussFast()
Returns
-------
s: ndarray
Broadened spectrum array.
"""
s = self.copy()
new_flux = pyasl.instrBroadGaussFast(s.xaxis, s.flux, 50000, **pya_kwargs)
new_flux = pyasl.instrBroadGaussFast(
s.xaxis, s.flux, resolution=R, **pya_kwargs
)
s.flux = new_flux
return s

Expand Down
28 changes: 23 additions & 5 deletions spectrum_overload/test/test_Spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"""
from __future__ import division, print_function

from PyAstronomy import pyasl
import hypothesis.strategies as st
import numpy as np
import pytest
from astropy.io import fits

# Test using hypothesis
from hypothesis import example, given
from pkg_resources import resource_filename
Expand All @@ -19,12 +21,9 @@

@pytest.fixture
def phoenix_spectrum():
# Get a phoenix spectrum in test data to load in and get th
spec_1 = resource_filename('spectrum_overload', 'data/spec_1.fits')
# phoenix_file = resource_filename('spectrum_overload', 'data/spec_1.fits')

# Get a phoenix spectrum in test
spec_1 = resource_filename("spectrum_overload", "data/spec_1.fits")
flux = fits.getdata(spec_1)
# wave = fits.getdata("")
wave = np.arange(len(flux))
header = fits.getheader(spec_1)
return Spectrum(xaxis=wave, flux=flux, header=header)
Expand Down Expand Up @@ -482,3 +481,22 @@ def test_normalization_method_match_degree(method, degree):
poly_deg = s.normalize(method='poly', degree=degree)
poly_deg = poly_deg.remove_nans() # hack for getting to pass on < py 34
assert np.allclose(named_method.flux, poly_deg.flux)


@pytest.mark.parametrize(
"R", [5, 10, 99, 500]
) # Small resolutions for easy testing (measureable differences)
def test_instrument_broaden(phoenix_spectrum, R):
"""Test instrument_broadening same as pyastronomy."""
spec = phoenix_spectrum

new_flux = pyasl.instrBroadGaussFast(spec.xaxis, spec.flux, R)

# There is a change due to broadening
assert not np.allclose(spec.flux, new_flux)
new_spec = spec.instrument_broaden(R)

# There is a change due to broadening
assert not np.allclose(new_spec.flux, spec.flux)
# Spectrum result equals correct pyasl value.
assert np.allclose(new_spec.flux, new_flux)

0 comments on commit ad5eaee

Please sign in to comment.