Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute apparent magnitudes from input catalog info #61

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions python/desc/imsim/imsim_truth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Tools to compute the apparent magnitudes from the SED, mag_norm,
redshift, and reddening info in the input imsim object catalogs.
"""
from __future__ import absolute_import, print_function
import os
import copy
from collections import OrderedDict
import lsst.sims.photUtils as photUtils
import lsst.utils as lsstUtils

__all__ = ['ApparentMagnitudes']

# Create class-level attributes.
_bandpasses = dict()
for band_name in 'ugrizy':
throughput_dir = os.path.join(lsstUtils.getPackageDir('throughputs'),
'baseline', 'total_%s.dat' % band_name)
_bandpasses[band_name] = photUtils.Bandpass()
_bandpasses[band_name].readThroughput(throughput_dir)
_control_bandpass = photUtils.Bandpass()
_control_bandpass.imsimBandpass()

class ApparentMagnitudes(object):
"""
Class to compute apparent magnitudes for a given rest-frame SED.
"""
bps = _bandpasses
control_bandpass = _control_bandpass
def __init__(self, sed_name, max_mag=1000.):
"""
Read in the unnormalized SED.
"""
sed_dir = lsstUtils.getPackageDir('sims_sed_library')
self.sed_unnormed = photUtils.Sed()
self.sed_unnormed.readSED_flambda(os.path.join(sed_dir, sed_name))
self.max_mag = max_mag

def __call__(self, obj_pars, bands='ugrizy'):
sed = copy.deepcopy(self.sed_unnormed)
fnorm = sed.calcFluxNorm(obj_pars.magNorm, self.control_bandpass)
sed.multiplyFluxNorm(fnorm)

a_int, b_int = sed.setupCCMab()
if obj_pars.internalAv != 0 or obj_pars.internalRv != 0:
# Apply internal dust extinction.
sed.addCCMDust(a_int, b_int, A_v=obj_pars.internalAv,
R_v=obj_pars.internalRv)

if obj_pars.redshift > 0:
sed.redshiftSED(obj_pars.redshift, dimming=True)

if obj_pars.galacticAv != 0 or obj_pars.galacticRv != 0:
# Apply Galactic extinction.
sed.addCCMDust(a_int, b_int, A_v=obj_pars.galacticAv,
R_v=obj_pars.galacticRv)

mags = OrderedDict()
for band in bands:
try:
mags[band] = sed.calcMag(self.bps[band])
except Exception as eObj:
if str(eObj).startswith('This SED has no flux'):
mags[band] = self.max_mag
else:
raise eObj

return mags
34 changes: 34 additions & 0 deletions tests/test_imsim_truth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import print_function
import os
import unittest
import desc.imsim
import desc.imsim.imsim_truth as imsim_truth

class ApparentMagnitudesTestCase(unittest.TestCase):
"Test case class for ApparentMagnitudes class."
def setUp(self):
pass

def tearDown(self):
pass

def test_magnitudes(self):
instcat = os.path.join(os.environ['IMSIM_DIR'], 'tests',
'tiny_instcat.txt')
commands, objects = desc.imsim.parsePhoSimInstanceFile(instcat)


obj = objects.iloc[0]
app_mags = imsim_truth.ApparentMagnitudes(obj.sedFilepath)
mags = app_mags(obj)
self.assertAlmostEqual(mags['u'], 25.948024179976176)
self.assertAlmostEqual(mags['r'], 22.275029634051265)

obj = objects.iloc[8]
app_mags = imsim_truth.ApparentMagnitudes(obj.sedFilepath)
mags = app_mags(obj)
for band in mags:
self.assertAlmostEqual(mags[band], 1000.)

if __name__ == '__main__':
unittest.main()