Coverage for tests/testSkyModel.py : 14%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from builtins import zip
2import numpy as np
3import lsst.sims.skybrightness as sb
4import unittest
5import lsst.utils.tests
6import lsst.sims.photUtils.Bandpass as Bandpass
7from lsst.utils import getPackageDir
8import os
11class TestSkyModel(unittest.TestCase):
13 @classmethod
14 def setUpClass(cls):
15 # initalize the class with empty models
16 cls._sm_mags = None
17 cls._sm_mags2 = None
18 cls._sm_spec = None
19 cls._sm_spec2 = None
21 @classmethod
22 def tearDownClass(cls):
23 del cls._sm_mags
24 del cls._sm_mags2
25 del cls._sm_spec
26 del cls._sm_spec2
28 @property
29 def sm_mags(self):
30 cls = type(self)
31 if cls._sm_mags is None:
32 cls._sm_mags = sb.SkyModel(mags=True)
33 return cls._sm_mags
35 @property
36 def sm_mags2(self):
37 cls = type(self)
38 if cls._sm_mags2 is None:
39 cls._sm_mags2 = sb.SkyModel(mags=True)
40 return cls._sm_mags2
42 @property
43 def sm_spec(self):
44 cls = type(self)
45 if cls._sm_spec is None:
46 cls._sm_spec = sb.SkyModel(mags=False)
47 return cls._sm_spec
49 @property
50 def sm_spec2(self):
51 cls = type(self)
52 if cls._sm_spec2 is None:
53 cls._sm_spec2 = sb.SkyModel(mags=False)
54 return cls._sm_spec2
56 def testmergedComp(self):
57 """
58 Test that the 3 components that have been merged return the
59 same result if they are computed independently
60 """
62 sky1 = sb.SkyModel(twilight=False, zodiacal=False, moon=False,
63 lowerAtm=False, upperAtm=False,
64 airglow=False, scatteredStar=False,
65 mergedSpec=True)
66 sky1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
68 sky2 = sb.SkyModel(twilight=False, zodiacal=False, moon=False,
69 lowerAtm=True, upperAtm=True,
70 airglow=False, scatteredStar=True,
71 mergedSpec=False)
72 sky2.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
74 dummy, spec1 = sky1.returnWaveSpec()
75 dummy, spec2 = sky2.returnWaveSpec()
77 np.testing.assert_almost_equal(spec1, spec2)
79 # and then check for the mags
80 sky1 = sb.SkyModel(twilight=False, zodiacal=False, moon=False,
81 lowerAtm=False, upperAtm=False,
82 airglow=False, scatteredStar=False,
83 mergedSpec=True, mags=True)
84 sky1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
86 sky2 = sb.SkyModel(twilight=False, zodiacal=False, moon=False,
87 lowerAtm=True, upperAtm=True,
88 airglow=False, scatteredStar=True,
89 mergedSpec=False, mags=True)
90 sky2.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
92 m1 = sky1.returnMags()
93 m2 = sky2.returnMags()
94 for key in m1:
95 np.testing.assert_almost_equal(m1[key], m2[key], decimal=2)
98 def testSetups(self):
99 """
100 Check that things are the same if the model is set up with
101 radecmjd or all the parameters independently
102 """
104 sm1 = self.sm_spec
105 sm1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
107 sm2 = self.sm_spec2
108 sm2.setParams(azs=sm1.azs, alts=sm1.alts,
109 moonPhase=sm1.moonPhase,
110 moonAlt=sm1.moonAlt, moonAz=sm1.moonAz,
111 sunAlt=sm1.sunAlt, sunAz=sm1.sunAz,
112 sunEclipLon=sm1.sunEclipLon, eclipLon=sm1.eclipLon,
113 eclipLat=sm1.eclipLat, solarFlux=sm1.solarFlux,
114 degrees=False)
116 dummy, spec1 = sm1.returnWaveSpec()
117 dummy, spec2 = sm2.returnWaveSpec()
119 np.testing.assert_array_equal(spec1, spec2)
121 # Check that the degrees kwarg works
122 sm2.setParams(azs=np.degrees(sm1.azs), alts=np.degrees(sm1.alts),
123 moonPhase=sm1.moonPhase,
124 moonAlt=np.degrees(sm1.moonAlt), moonAz=np.degrees(sm1.moonAz),
125 sunAlt=np.degrees(sm1.sunAlt), sunAz=np.degrees(sm1.sunAz),
126 sunEclipLon=np.degrees(sm1.sunEclipLon), eclipLon=np.degrees(sm1.eclipLon),
127 eclipLat=np.degrees(sm1.eclipLat), solarFlux=sm1.solarFlux,
128 degrees=True)
130 atList = ['azs', 'alts', 'moonPhase', 'moonAlt', 'moonAz', 'sunAlt', 'sunAz',
131 'sunEclipLon', 'eclipLon', 'eclipLat', 'solarFlux']
133 # Check each attribute that should match
134 for attr in atList:
135 np.testing.assert_allclose(getattr(sm1, attr), getattr(sm2, attr))
137 # Check the interpolation points
138 for name in sm1.points.dtype.names:
139 np.testing.assert_allclose(sm1.points[name], sm2.points[name])
141 # Check the final output spectra
142 np.testing.assert_allclose(sm1.spec, sm2.spec)
144 def testMags(self):
145 """
146 Test that the interpolated mags are similar to mags computed from interpolated spectra
147 """
149 throughPath = os.path.join(getPackageDir('throughputs'), 'baseline')
150 filters = ['u', 'g', 'r', 'i', 'z', 'y']
152 bps = {}
153 for filterName in filters:
154 bp = np.loadtxt(os.path.join(throughPath, 'total_%s.dat' % filterName),
155 dtype=list(zip(['wave', 'trans'], [float]*2)))
156 lsst_bp = Bandpass()
157 lsst_bp.setBandpass(bp['wave'], bp['trans'])
158 bps[filterName] = lsst_bp
160 sm1 = self.sm_spec
161 sm1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
162 mags1 = sm1.returnMags(bandpasses=bps)
164 sm2 = self.sm_mags
165 sm2.setRaDecMjd([36.], [-68.], 49353.18, degrees=True)
166 mag2 = sm2.returnMags()
168 for i, filtername in enumerate(filters):
169 np.testing.assert_allclose(mags1[filtername], mag2[filtername], rtol=1e-4)
171 def testGetComputed(self):
172 """
173 Make sure we can recover computed values.
174 """
176 sm = self.sm_mags
177 sm.setRaDecMjd([36., 36.], [-68., -70.], 49353.18, degrees=True)
178 valDict = sm.getComputedVals()
180 attrToCheck = ['ra', 'dec', 'alts', 'azs', 'airmass', 'solarFlux', 'moonPhase',
181 'moonAz', 'moonAlt', 'sunAlt', 'sunAz', 'azRelSun', 'moonSunSep',
182 'azRelMoon', 'eclipLon', 'eclipLat', 'moonRA', 'moonDec', 'sunRA',
183 'sunDec', 'sunEclipLon']
185 for attr in attrToCheck:
186 assert(attr in valDict)
187 if np.size(valDict[attr]) > 1:
188 np.testing.assert_array_equal(getattr(sm, attr), valDict[attr])
189 else:
190 self.assertEqual(getattr(sm, attr), valDict[attr])
192 # Check that things that should be radians are in radian range
193 radList = ['ra', 'azs', 'moonAz', 'sunAz', 'azRelSun',
194 'azRelMoon', 'eclipLon', 'moonRA', 'sunRA', 'sunEclipLon']
196 for attr in radList:
197 if valDict[attr] is not None:
198 assert(np.min(valDict[attr]) >= 0)
199 assert(np.max(valDict[attr]) <= 2.*np.pi)
201 # Radians in negative to positive pi range
202 radList = ['moonAlt', 'sunAlt', 'alts', 'dec', 'moonDec',
203 'sunDec', 'eclipLat']
204 for attr in radList:
205 if valDict[attr] is not None:
206 assert(np.min(valDict[attr]) >= -np.pi)
207 assert(np.max(valDict[attr]) <= np.pi)
209 def test90Deg(self):
210 """
211 Make sure we can look all the way to 90 degree altitude.
212 """
213 mjd = 56973.268218
214 sm = self.sm_mags
215 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True)
216 mags = sm.returnMags()
217 for key in mags:
218 assert(True not in np.isnan(mags[key]))
219 assert(True not in np.isnan(sm.spec))
222 def testFewerMags(self):
223 """
224 Test that can call and only interpolate a few magnitudes.
225 """
226 mjd = 56973.268218
227 sm = self.sm_mags
228 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True)
229 all_mags = sm.returnMags()
231 filterNames = ['u', 'g', 'r', 'i', 'z', 'y']
232 for filterName in filterNames:
233 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, filterNames=[filterName])
234 one_mag = sm.returnMags()
235 self.assertEqual(all_mags[filterName], one_mag[filterName])
237 # Test that I can do subset of mags
238 subset = ['u', 'r', 'y']
239 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, filterNames=subset)
240 sub_mags = sm.returnMags()
241 for filterName in subset:
242 self.assertEqual(all_mags[filterName], sub_mags[filterName])
244 def test_setRaDecAltAzMjd(self):
245 """
246 Make sure sending in self-computed alt, az works
247 """
248 sm1 = self.sm_mags
249 sm2 = self.sm_mags2
250 ra = np.array([0., 0., 0.])
251 dec = np.array([-.1, -.2, -.3])
252 mjd = 5900
253 sm1.setRaDecMjd(ra, dec, mjd)
254 m1 = sm1.returnMags()
255 sm2.setRaDecAltAzMjd(ra, dec, sm1.alts, sm1.azs, mjd)
256 m2 = sm1.returnMags()
258 attrList = ['ra', 'dec', 'alts', 'azs']
259 for attr in attrList:
260 np.testing.assert_equal(getattr(sm1, attr), getattr(sm2, attr))
262 for key in m1:
263 np.testing.assert_allclose(m1[key], m2[key], rtol=1e-6)
266class TestMemory(lsst.utils.tests.MemoryTestCase):
267 pass
270def setup_module(module):
271 lsst.utils.tests.init()
273if __name__ == "__main__": 273 ↛ 274line 273 didn't jump to line 274, because the condition on line 273 was never true
274 lsst.utils.tests.init()
275 unittest.main()