Hide keyboard shortcuts

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 

9 

10 

11class TestSkyModel(unittest.TestCase): 

12 

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 

20 

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 

27 

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 

34 

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 

41 

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 

48 

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 

55 

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 """ 

61 

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) 

67 

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) 

73 

74 dummy, spec1 = sky1.returnWaveSpec() 

75 dummy, spec2 = sky2.returnWaveSpec() 

76 

77 np.testing.assert_almost_equal(spec1, spec2) 

78 

79 def testSetups(self): 

80 """ 

81 Check that things are the same if the model is set up with 

82 radecmjd or all the parameters independently 

83 """ 

84 

85 sm1 = self.sm_spec 

86 sm1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True) 

87 

88 sm2 = self.sm_spec2 

89 sm2.setParams(azs=sm1.azs, alts=sm1.alts, 

90 moonPhase=sm1.moonPhase, 

91 moonAlt=sm1.moonAlt, moonAz=sm1.moonAz, 

92 sunAlt=sm1.sunAlt, sunAz=sm1.sunAz, 

93 sunEclipLon=sm1.sunEclipLon, eclipLon=sm1.eclipLon, 

94 eclipLat=sm1.eclipLat, solarFlux=sm1.solarFlux, 

95 degrees=False) 

96 

97 dummy, spec1 = sm1.returnWaveSpec() 

98 dummy, spec2 = sm2.returnWaveSpec() 

99 

100 np.testing.assert_array_equal(spec1, spec2) 

101 

102 # Check that the degrees kwarg works 

103 sm2.setParams(azs=np.degrees(sm1.azs), alts=np.degrees(sm1.alts), 

104 moonPhase=sm1.moonPhase, 

105 moonAlt=np.degrees(sm1.moonAlt), moonAz=np.degrees(sm1.moonAz), 

106 sunAlt=np.degrees(sm1.sunAlt), sunAz=np.degrees(sm1.sunAz), 

107 sunEclipLon=np.degrees(sm1.sunEclipLon), eclipLon=np.degrees(sm1.eclipLon), 

108 eclipLat=np.degrees(sm1.eclipLat), solarFlux=sm1.solarFlux, 

109 degrees=True) 

110 

111 atList = ['azs', 'alts', 'moonPhase', 'moonAlt', 'moonAz', 'sunAlt', 'sunAz', 

112 'sunEclipLon', 'eclipLon', 'eclipLat', 'solarFlux'] 

113 

114 # Check each attribute that should match 

115 for attr in atList: 

116 np.testing.assert_allclose(getattr(sm1, attr), getattr(sm2, attr)) 

117 

118 # Check the interpolation points 

119 for name in sm1.points.dtype.names: 

120 np.testing.assert_allclose(sm1.points[name], sm2.points[name]) 

121 

122 # Check the final output spectra 

123 np.testing.assert_allclose(sm1.spec, sm2.spec) 

124 

125 def testMags(self): 

126 """ 

127 Test that the interpolated mags are similar to mags computed from interpolated spectra 

128 """ 

129 

130 throughPath = os.path.join(getPackageDir('throughputs'), 'baseline') 

131 filters = ['u', 'g', 'r', 'i', 'z', 'y'] 

132 

133 bps = {} 

134 for filterName in filters: 

135 bp = np.loadtxt(os.path.join(throughPath, 'total_%s.dat' % filterName), 

136 dtype=list(zip(['wave', 'trans'], [float]*2))) 

137 lsst_bp = Bandpass() 

138 lsst_bp.setBandpass(bp['wave'], bp['trans']) 

139 bps[filterName] = lsst_bp 

140 

141 sm1 = self.sm_spec 

142 sm1.setRaDecMjd([36.], [-68.], 49353.18, degrees=True) 

143 mags1 = sm1.returnMags(bandpasses=bps) 

144 

145 sm2 = self.sm_mags 

146 sm2.setRaDecMjd([36.], [-68.], 49353.18, degrees=True) 

147 mag2 = sm2.returnMags() 

148 

149 for i, filtername in enumerate(filters): 

150 np.testing.assert_allclose(mags1[filtername], mag2[filtername], rtol=1e-4) 

151 

152 def testGetComputed(self): 

153 """ 

154 Make sure we can recover computed values. 

155 """ 

156 

157 sm = self.sm_mags 

158 sm.setRaDecMjd([36., 36.], [-68., -70.], 49353.18, degrees=True) 

159 valDict = sm.getComputedVals() 

160 

161 attrToCheck = ['ra', 'dec', 'alts', 'azs', 'airmass', 'solarFlux', 'moonPhase', 

162 'moonAz', 'moonAlt', 'sunAlt', 'sunAz', 'azRelSun', 'moonSunSep', 

163 'azRelMoon', 'eclipLon', 'eclipLat', 'moonRA', 'moonDec', 'sunRA', 

164 'sunDec', 'sunEclipLon'] 

165 

166 for attr in attrToCheck: 

167 assert(attr in valDict) 

168 if np.size(valDict[attr]) > 1: 

169 np.testing.assert_array_equal(getattr(sm, attr), valDict[attr]) 

170 else: 

171 self.assertEqual(getattr(sm, attr), valDict[attr]) 

172 

173 # Check that things that should be radians are in radian range 

174 radList = ['ra', 'azs', 'moonAz', 'sunAz', 'azRelSun', 

175 'azRelMoon', 'eclipLon', 'moonRA', 'sunRA', 'sunEclipLon'] 

176 

177 for attr in radList: 

178 if valDict[attr] is not None: 

179 assert(np.min(valDict[attr]) >= 0) 

180 assert(np.max(valDict[attr]) <= 2.*np.pi) 

181 

182 # Radians in negative to positive pi range 

183 radList = ['moonAlt', 'sunAlt', 'alts', 'dec', 'moonDec', 

184 'sunDec', 'eclipLat'] 

185 for attr in radList: 

186 if valDict[attr] is not None: 

187 assert(np.min(valDict[attr]) >= -np.pi) 

188 assert(np.max(valDict[attr]) <= np.pi) 

189 

190 def test90Deg(self): 

191 """ 

192 Make sure we can look all the way to 90 degree altitude. 

193 """ 

194 mjd = 56973.268218 

195 sm = self.sm_mags 

196 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True) 

197 mags = sm.returnMags() 

198 for key in mags: 

199 assert(True not in np.isnan(mags[key])) 

200 assert(True not in np.isnan(sm.spec)) 

201 

202 def testAirglow(self): 

203 """ 

204 test that the airglow goes up with increasing SFU 

205 """ 

206 

207 mjd = 56973.268218 

208 sm = self.sm_mags 

209 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, solarFlux=130.) 

210 magNormal = sm.returnMags() 

211 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, solarFlux=50.) 

212 magFaint = sm.returnMags() 

213 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, solarFlux=200.) 

214 magBright = sm.returnMags() 

215 

216 assert(magNormal['r'][0] < magFaint['r'][0]) 

217 assert(magNormal['r'][0] > magBright['r'][0]) 

218 

219 def testFewerMags(self): 

220 """ 

221 Test that can call and only interpolate a few magnitudes. 

222 """ 

223 mjd = 56973.268218 

224 sm = self.sm_mags 

225 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True) 

226 all_mags = sm.returnMags() 

227 

228 filterNames = ['u', 'g', 'r', 'i', 'z', 'y'] 

229 for filterName in filterNames: 

230 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, filterNames=[filterName]) 

231 one_mag = sm.returnMags() 

232 self.assertEqual(all_mags[filterName], one_mag[filterName]) 

233 

234 # Test that I can do subset of mags 

235 subset = ['u', 'r', 'y'] 

236 sm.setRaDecMjd(0., 90., mjd, degrees=True, azAlt=True, filterNames=subset) 

237 sub_mags = sm.returnMags() 

238 for filterName in subset: 

239 self.assertEqual(all_mags[filterName], sub_mags[filterName]) 

240 

241 def test_setRaDecAltAzMjd(self): 

242 """ 

243 Make sure sending in self-computed alt, az works 

244 """ 

245 sm1 = self.sm_mags 

246 sm2 = self.sm_mags2 

247 ra = np.array([0., 0., 0.]) 

248 dec = np.array([-.1, -.2, -.3]) 

249 mjd = 5900 

250 sm1.setRaDecMjd(ra, dec, mjd) 

251 m1 = sm1.returnMags() 

252 sm2.setRaDecAltAzMjd(ra, dec, sm1.alts, sm1.azs, mjd) 

253 m2 = sm1.returnMags() 

254 

255 attrList = ['ra', 'dec', 'alts', 'azs'] 

256 for attr in attrList: 

257 np.testing.assert_equal(getattr(sm1, attr), getattr(sm2, attr)) 

258 

259 for key in m1: 

260 np.testing.assert_allclose(m1[key], m2[key], rtol=1e-6) 

261 

262 

263class TestMemory(lsst.utils.tests.MemoryTestCase): 

264 pass 

265 

266 

267def setup_module(module): 

268 lsst.utils.tests.init() 

269 

270if __name__ == "__main__": 270 ↛ 271line 270 didn't jump to line 271, because the condition on line 270 was never true

271 lsst.utils.tests.init() 

272 unittest.main()