Skip to content

Commit

Permalink
Merge pull request #12 from karllark/generic_ext
Browse files Browse the repository at this point in the history
Extinguish function to class
Travis build failing due to scipy docs being offline.  All other builds working.
  • Loading branch information
karllark authored Mar 15, 2017
2 parents dd752c8 + d3776fa commit aacc0a1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
47 changes: 43 additions & 4 deletions dust_extinction/dust_extinction.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CCM89(Model):
Returns
-------
elvebv: float
elvebv: np.array(dtype=float)
E(x-V)/E(B-V) extinction curve [mag]
Raises
Expand All @@ -50,8 +50,6 @@ class CCM89(Model):
F99 should be used instead as it is based on 10x more observations
and a better treatment of the optical/NIR photometry based portion
of the curves.
CCM89 is mainly for historical puposes
"""
inputs = ('x',)
outputs = ('elvebv',)
Expand Down Expand Up @@ -146,6 +144,47 @@ def evaluate(in_x, Rv):
a[fuv_indxs] = np.polyval((-.070, .137, -.628, -1.073), y)
b[fuv_indxs] = np.polyval((.374, -.42, 4.257, 13.67), y)

# return Al/Av
# return E(lambda-V)/E(B-V)
return a + b/Rv

def extinguish(self, x, Av=None, Ebv=None):
"""
Calculate the extinction as a fraction
Parameters
----------
x: float
expects either x in units of wavelengths or frequency
or assumes wavelengths in wavenumbers [1/micron]
internally wavenumbers are used
Av: float
A(V) value of dust column
Av or Ebv must be set
Ebv: float
E(B-V) value of dust column
Av or Ebv must be set
Returns
-------
frac_ext: np.array(dtype=float)
extinction as a function of x
"""
# get the extinction curve
elvebv = self(x)

# check that av or ebv is set
if (Av is None) and (Ebv is None):
raise InputParameterError('neither Av or Ebv passed, one required')

# if Av is not set and Ebv set, convert to Av
if Av is None:
Av = self.Rv*Ebv

# convert to A(lambda)/A(V)
alav = elvebv/self.Rv + 1

# return fractional extinction
return np.power(10.0,-0.4*alav*Av)

5 changes: 4 additions & 1 deletion dust_extinction/tests/coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ omit =
{packagename}/tests/*
{packagename}/*/tests/*
{packagename}/*/*/tests/*
{packagename}/plots/*
{packagename}/*/plots/*
{packagename}/*/*/plots/*
{packagename}/version*

[report]
Expand All @@ -28,4 +31,4 @@ exclude_lines =
def main\(.*\):

# Ignore branches that don't pertain to this version of Python
pragma: py{ignore_python_version}
pragma: py{ignore_python_version}
50 changes: 47 additions & 3 deletions dust_extinction/tests/test_ccm89.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ def test_invalid_micron(x_invalid_angstrom):
+ ' <= x <= ' \
+ str(tmodel.x_range[1]) \
+ ', x has units 1/micron]'

@pytest.mark.parametrize("Rv", [2.0, 3.0, 3.1, 4.0, 5.0, 6.0])
def test_extinction_CCM89_values(Rv):

def get_elvebv_cor_vals(Rv):
# testing wavenumbers
x = np.array([ 10. , 9. , 8. , 7. ,
6. , 5. , 4.6 , 4. ,
Expand Down Expand Up @@ -115,9 +114,54 @@ def test_extinction_CCM89_values(Rv):
else:
cor_vals = np.array([ 0.0 ])

return (x, cor_vals)


@pytest.mark.parametrize("Rv", [2.0, 3.0, 3.1, 4.0, 5.0, 6.0])
def test_extinction_CCM89_values(Rv):
# get the correct values
x, cor_vals = get_elvebv_cor_vals(Rv)

# initialize extinction model
tmodel = CCM89(Rv=Rv)

# test
np.testing.assert_allclose(tmodel(x), cor_vals)

def test_extinguish_no_av_or_ebv():
tmodel = CCM89()
with pytest.raises(InputParameterError) as exc:
tmodel.extinguish([1.0])
assert exc.value.args[0] == 'neither Av or Ebv passed, one required'

@pytest.mark.parametrize("Rv", [2.0, 3.0, 3.1, 4.0, 5.0, 6.0])
def test_extinction_CCM89_extinguish_values_Av(Rv):
# get the correct values
x, cor_vals = get_elvebv_cor_vals(Rv)

# calculate the cor_vals in fractional units
Av = 1.0
cor_vals = np.power(10.0,-0.4*(cor_vals/Rv+1)*Av)

# initialize extinction model
tmodel = CCM89(Rv=Rv)

# test
np.testing.assert_allclose(tmodel.extinguish(x, Av=Av), cor_vals)

@pytest.mark.parametrize("Rv", [2.0, 3.0, 3.1, 4.0, 5.0, 6.0])
def test_extinction_CCM89_extinguish_values_Ebv(Rv):
# get the correct values
x, cor_vals = get_elvebv_cor_vals(Rv)

# calculate the cor_vals in fractional units
Ebv = 1.0
Av = Ebv*Rv
cor_vals = np.power(10.0,-0.4*(cor_vals/Rv+1)*Av)

# initialize extinction model
tmodel = CCM89(Rv=Rv)

# test
np.testing.assert_allclose(tmodel.extinguish(x, Ebv=Ebv), cor_vals)

0 comments on commit aacc0a1

Please sign in to comment.