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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

from builtins import zip 

import numpy as np 

import lsst.sims.skybrightness as sb 

import unittest 

import lsst.utils.tests 

import lsst.sims.photUtils.Bandpass as Bandpass 

from lsst.utils import getPackageDir 

import os 

 

 

class TestSkyModel(unittest.TestCase): 

 

@classmethod 

def setUpClass(cls): 

# initalize the class with empty models 

cls._sm_mags = None 

cls._sm_mags2 = None 

cls._sm_spec = None 

cls._sm_spec2 = None 

 

@classmethod 

def tearDownClass(cls): 

del cls._sm_mags 

del cls._sm_mags2 

del cls._sm_spec 

del cls._sm_spec2 

 

@property 

def sm_mags(self): 

cls = type(self) 

if cls._sm_mags is None: 

cls._sm_mags = sb.SkyModel(mags=True) 

return cls._sm_mags 

 

@property 

def sm_mags2(self): 

cls = type(self) 

if cls._sm_mags2 is None: 

cls._sm_mags2 = sb.SkyModel(mags=True) 

return cls._sm_mags2 

 

@property 

def sm_spec(self): 

cls = type(self) 

if cls._sm_spec is None: 

cls._sm_spec = sb.SkyModel(mags=False) 

return cls._sm_spec 

 

@property 

def sm_spec2(self): 

cls = type(self) 

if cls._sm_spec2 is None: 

cls._sm_spec2 = sb.SkyModel(mags=False) 

return cls._sm_spec2 

 

def testmergedComp(self): 

""" 

Test that the 3 components that have been merged return the 

same result if they are computed independently 

""" 

 

sky1 = sb.SkyModel(twilight=False, zodiacal=False, moon=False, 

lowerAtm=False, upperAtm=False, 

airglow=False, scatteredStar=False, 

mergedSpec=True) 

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

 

sky2 = sb.SkyModel(twilight=False, zodiacal=False, moon=False, 

lowerAtm=True, upperAtm=True, 

airglow=False, scatteredStar=True, 

mergedSpec=False) 

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

 

dummy, spec1 = sky1.returnWaveSpec() 

dummy, spec2 = sky2.returnWaveSpec() 

 

np.testing.assert_almost_equal(spec1, spec2) 

 

def testSetups(self): 

""" 

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

radecmjd or all the parameters independently 

""" 

 

sm1 = self.sm_spec 

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

 

sm2 = self.sm_spec2 

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

moonPhase=sm1.moonPhase, 

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

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

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

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

degrees=False) 

 

dummy, spec1 = sm1.returnWaveSpec() 

dummy, spec2 = sm2.returnWaveSpec() 

 

np.testing.assert_array_equal(spec1, spec2) 

 

# Check that the degrees kwarg works 

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

moonPhase=sm1.moonPhase, 

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

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

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

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

degrees=True) 

 

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

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

 

# Check each attribute that should match 

for attr in atList: 

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

 

# Check the interpolation points 

for name in sm1.points.dtype.names: 

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

 

# Check the final output spectra 

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

 

def testMags(self): 

""" 

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

""" 

 

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

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

 

bps = {} 

for filterName in filters: 

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

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

lsst_bp = Bandpass() 

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

bps[filterName] = lsst_bp 

 

sm1 = self.sm_spec 

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

mags1 = sm1.returnMags(bandpasses=bps) 

 

sm2 = self.sm_mags 

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

mag2 = sm2.returnMags() 

 

for i, filtername in enumerate(filters): 

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

 

def testGetComputed(self): 

""" 

Make sure we can recover computed values. 

""" 

 

sm = self.sm_mags 

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

valDict = sm.getComputedVals() 

 

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

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

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

'sunDec', 'sunEclipLon'] 

 

for attr in attrToCheck: 

assert(attr in valDict) 

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

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

else: 

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

 

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

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

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

 

for attr in radList: 

if valDict[attr] is not None: 

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

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

 

# Radians in negative to positive pi range 

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

'sunDec', 'eclipLat'] 

for attr in radList: 

if valDict[attr] is not None: 

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

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

 

def test90Deg(self): 

""" 

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

""" 

mjd = 56973.268218 

sm = self.sm_mags 

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

mags = sm.returnMags() 

for key in mags: 

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

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

 

def testAirglow(self): 

""" 

test that the airglow goes up with increasing SFU 

""" 

 

mjd = 56973.268218 

sm = self.sm_mags 

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

magNormal = sm.returnMags() 

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

magFaint = sm.returnMags() 

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

magBright = sm.returnMags() 

 

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

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

 

def testFewerMags(self): 

""" 

Test that can call and only interpolate a few magnitudes. 

""" 

mjd = 56973.268218 

sm = self.sm_mags 

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

all_mags = sm.returnMags() 

 

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

for filterName in filterNames: 

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

one_mag = sm.returnMags() 

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

 

# Test that I can do subset of mags 

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

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

sub_mags = sm.returnMags() 

for filterName in subset: 

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

 

def test_setRaDecAltAzMjd(self): 

""" 

Make sure sending in self-computed alt, az works 

""" 

sm1 = self.sm_mags 

sm2 = self.sm_mags2 

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

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

mjd = 5900 

sm1.setRaDecMjd(ra, dec, mjd) 

m1 = sm1.returnMags() 

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

m2 = sm1.returnMags() 

 

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

for attr in attrList: 

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

 

for key in m1: 

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

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

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

lsst.utils.tests.init() 

unittest.main()