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

1import unittest 

2import lsst.sims.skybrightness as sb 

3import lsst.sims.skybrightness_pre as sbp 

4import lsst.utils.tests 

5import healpy as hp 

6import numpy as np 

7import lsst.sims.utils as utils 

8import ephem 

9from lsst.sims.skybrightness.utils import mjd2djd 

10from lsst.sims.utils import _angularSeparation, raDec2Hpid, angularSeparation 

11import lsst.sims.survey.fields as sf 

12import warnings 

13 

14 

15class TestSkyPre(unittest.TestCase): 

16 

17 @classmethod 

18 def setUpClass(cls): 

19 try: 

20 cls.sm = sbp.SkyModelPre(speedLoad=True) 

21 # cls.sm_fields = sbp.SkyModelPre(speedLoad=True, opsimFields=False, useSimsData=False) 

22 mjd = cls.sm.info['mjds'][1]+4./60./24. 

23 tmp = cls.sm.returnMags(mjd) 

24 cls.data_present = True 

25 except: 

26 cls.data_present = False 

27 warnings.warn('Data files not found, skipping tests. Check data/ for instructions to pull data.') 

28 

29 @unittest.skip("24 Aug 2018--Depriciating using Fields ") 

30 def testFieldsMatch(self): 

31 """Test that the field based maps match the healpix maps 

32 """ 

33 if self.data_present: 

34 decs = [-60., -40., -20.] 

35 mjds = [] 

36 ra = 280 

37 

38 # Load up the fields 

39 selection = sf.FieldSelection() 

40 all_fields = selection.get_all_fields() 

41 fdb = sf.FieldsDatabase() 

42 field_ras, field_decs = fdb.get_ra_dec_arrays(all_fields) 

43 

44 for mjd in self.sm.info['mjds'][10:12]: 

45 mjds.append(mjd) 

46 mjds.append(mjd+.0002) 

47 for dec in decs: 

48 healID = raDec2Hpid(self.sm.nside, ra, dec) 

49 # do the slow brute force lookup since I'm too lazy to import kdtree 

50 dist = angularSeparation(field_ras, field_decs, ra, dec) 

51 fieldID = np.max(np.where(dist == dist.min())) 

52 for mjd in mjds: 

53 field_mag = self.sm_fields.returnMags(mjd, indx=[fieldID]) 

54 heal_mag = self.sm.returnMags(mjd, indx=[healID]) 

55 for key in field_mag: 

56 np.testing.assert_almost_equal(field_mag[key], heal_mag[key], decimal=3) 

57 

58 def testReturnMags(self): 

59 """ 

60 Test all the ways ReturnMags can be used 

61 """ 

62 # Check both the healpix and opsim fields 

63 if self.data_present: 

64 sms = [self.sm] 

65 mjds = [] 

66 for mjd in sms[0].info['mjds'][100:102]: 

67 mjds.append(mjd) 

68 mjds.append(mjd+.0002) 

69 

70 # Make sure there's an mjd that is between sunrise/set that gets tested 

71 diff = sms[0].info['mjds'][1:] - sms[0].info['mjds'][0:-1] 

72 between = np.where(diff >= sms[0].header['timestep_max'])[0][0] 

73 mjds.append(sms[0].info['mjds'][between+1] + sms[0].header['timestep_max']) 

74 

75 indxes = [None, [100, 101]] 

76 apply_masks = [True, False] 

77 apply_planets = [True, False] 

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

79 

80 for sm in sms: 

81 for mjd in mjds: 

82 for indx in indxes: 

83 for am in apply_masks: 

84 for planet in apply_planets: 

85 for filt in filters: 

86 mags = sm.returnMags(mjd, indx=indx, airmass_mask=am, filters=filt, 

87 planet_mask=planet) 

88 # Check the filters returned are correct 

89 self.assertEqual(len(filt), len(list(mags.keys()))) 

90 self.assertEqual(set(filt), set(mags.keys())) 

91 airmasses = sm.returnAirmass(mjd, indx=indx) 

92 # Check the magnitudes are correct 

93 if indx is not None: 

94 self.assertEqual(np.size(mags[list(mags.keys())[0]]), np.size(indx)) 

95 self.assertEqual(np.size(airmasses), np.size(indx)) 

96 

97 @unittest.skip("13 March 2017--Takes a long time to load the data") 

98 def testCrazyDate(self): 

99 """ 

100 Test date that falls at akward time 

101 """ 

102 if self.data_present: 

103 mjd = 60291.35423611111 

104 sm = self.sm 

105 mags = sm.returnMags(mjd) 

106 sunmoon = sm.returnSunMoon(mjd) 

107 airmass = sm.returnAirmass(mjd) 

108 

109 goodVals = np.where(mags['g'] != hp.UNSEEN)[0] 

110 assert(len(goodVals) > 0) 

111 for key in sunmoon: 

112 goodVals = np.where(sunmoon[key] != hp.UNSEEN)[0] 

113 assert(len(goodVals) > 0) 

114 goodVals = np.where(airmass != hp.UNSEEN)[0] 

115 assert(len(goodVals) > 0) 

116 

117 def testSunMoon(self): 

118 """ 

119 Test that the sun moon interpolation is good enough 

120 """ 

121 if self.data_present: 

122 sm = self.sm 

123 telescope = utils.Site('LSST') 

124 Observatory = ephem.Observer() 

125 Observatory.lat = telescope.latitude_rad 

126 Observatory.lon = telescope.longitude_rad 

127 Observatory.elevation = telescope.height 

128 

129 sun = ephem.Sun() 

130 moon = ephem.Moon() 

131 

132 mjd1 = sm.info['mjds'][0] 

133 mjd2 = sm.info['mjds'][3] 

134 

135 mjds = np.linspace(mjd1, mjd2, 20) 

136 

137 # Demand Moon and Sun Positions match to within 3 arcmin 

138 arcmin_places = np.abs(np.floor(np.log10(3/60./180.*np.pi))).astype(int) 

139 

140 for mjd in mjds: 

141 Observatory.date = mjd2djd(mjd) 

142 sun.compute(Observatory) 

143 moon.compute(Observatory) 

144 pre_calced = sm.returnSunMoon(mjd) 

145 

146 self.assertLess(np.abs(pre_calced['sunAlt']-sun.alt), arcmin_places) 

147 sun_dist = _angularSeparation(sun.ra, sun.dec, pre_calced['sunRA'], pre_calced['sunDec']) 

148 self.assertAlmostEqual(sun_dist, 0., places=arcmin_places) 

149 

150 self.assertLess(np.abs(pre_calced['moonAlt']-moon.alt), arcmin_places) 

151 moon_dist = _angularSeparation(moon.ra, moon.dec, pre_calced['moonRA'], pre_calced['moonDec']) 

152 self.assertAlmostEqual(moon_dist, 0., places=arcmin_places) 

153 

154 self.assertAlmostEqual(np.radians(pre_calced['moonSunSep']), 

155 np.radians(moon.phase/100.*180.), places=arcmin_places) 

156 

157 def testSBP(self): 

158 """ 

159 Check that values are similar enough 

160 """ 

161 if self.data_present: 

162 original_model = sb.SkyModel(mags=True) 

163 pre_calc_model = self.sm 

164 

165 hpindx = np.arange(hp.nside2npix(pre_calc_model.header['nside'])) 

166 ra, dec = utils.hpid2RaDec(pre_calc_model.header['nside'], hpindx) 

167 

168 # Run through a number of mjd values 

169 step = 30. / 60. / 24. # 30 minute timestep 

170 nmjd = 48 

171 mjds = np.arange(nmjd)*step + self.sm.info['mjds'][10]+0.1 

172 

173 # Tolerance for difference between true and interpolated airmass 

174 am_tol = 0.05 

175 am_limit = 3. 

176 

177 # Where to check the magnitudes match 

178 mag_am_limit = 1.5 

179 mag_tol = 0.27 # mags 

180 

181 for mjd in mjds: 

182 original_model.setRaDecMjd(ra, dec, mjd, degrees=True) 

183 if original_model.sunAlt < np.radians(-12.): 

184 sky1 = original_model.returnMags() 

185 sky2 = pre_calc_model.returnMags(mjd) 

186 am1 = original_model.airmass 

187 am2 = pre_calc_model.returnAirmass(mjd) 

188 good_am = np.where((am1 >= 1.) & (am1 <= am_limit)) 

189 diff = am1[good_am] - am2[good_am] 

190 # Check that the interpolated airmass is close 

191 assert(np.max(np.abs(diff)) < am_tol) 

192 

193 for filtername in sky1: 

194 good = np.where((am1 < mag_am_limit) & (sky2[filtername] != hp.UNSEEN) & 

195 (np.isfinite(sky1[filtername]))) 

196 diff = sky1[filtername][good] - sky2[filtername][good] 

197 assert(np.max(np.abs(diff)) <= mag_tol) 

198 

199 @unittest.skip("Don't want to add sims_data as dependency, and this does a large file load too") 

200 def test_various(self): 

201 """ 

202 Test some various loading things 

203 """ 

204 # check that the sims_data stuff loads 

205 sm = sbp.SkyModelPre(speedLoad=True) 

206 mjd = self.sm.info['mjds'][10]+0.1 

207 mags = sm.returnMags(mjd) 

208 # check that it'll load up something later properly 

209 mags = sm.returnMags(60000) 

210 

211 

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

213 pass 

214 

215 

216def setup_module(module): 

217 lsst.utils.tests.init() 

218 

219 

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

221 lsst.utils.tests.init() 

222 unittest.main()