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

273

274

275

276

from builtins import zip 

import unittest 

import numpy as np 

import lsst.utils.tests 

 

from lsst.sims.utils import raDecFromNativeLonLat, nativeLonLatFromRaDec 

from lsst.sims.utils import _raDecFromNativeLonLat, _nativeLonLatFromRaDec 

from lsst.sims.utils import observedFromICRS, icrsFromObserved 

from lsst.sims.utils import ObservationMetaData, haversine 

from lsst.sims.utils import arcsecFromRadians, raDecFromAltAz, Site 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class NativeLonLatTest(unittest.TestCase): 

 

def testNativeLonLat(self): 

""" 

Test that nativeLonLatFromRaDec works by considering stars and pointings 

at intuitive locations 

""" 

 

mjd = 53855.0 

 

raList_obs = [0.0, 0.0, 0.0, 270.0] 

decList_obs = [90.0, 90.0, 0.0, 0.0] 

 

raPointList_obs = [0.0, 270.0, 270.0, 0.0] 

decPointList_obs = [0.0, 0.0, 0.0, 0.0] 

 

lonControlList = [180.0, 180.0, 90.0, 270.0] 

latControlList = [0.0, 0.0, 0.0, 0.0] 

 

for rr_obs, dd_obs, rp_obs, dp_obs, lonc, latc in \ 

zip(raList_obs, decList_obs, raPointList_obs, decPointList_obs, 

lonControlList, latControlList): 

 

obsTemp = ObservationMetaData(mjd=mjd) 

 

rr, dd = icrsFromObserved(np.array([rr_obs, rp_obs]), 

np.array([dd_obs, dp_obs]), 

obs_metadata=obsTemp, 

epoch=2000.0, includeRefraction=True) 

 

obs = ObservationMetaData( 

pointingRA=rr[1], pointingDec=dd[1], mjd=mjd) 

lon, lat = nativeLonLatFromRaDec(rr[0], dd[0], obs) 

distance = arcsecFromRadians(haversine(lon, lat, lonc, latc)) 

self.assertLess(distance, 1.0) 

 

def testNativeLongLatComplicated(self): 

""" 

Test that nativeLongLatFromRaDec works by considering stars and pointings 

at non-intuitive locations. 

""" 

 

rng = np.random.RandomState(42) 

nPointings = 10 

raPointingList_icrs = rng.random_sample(nPointings) * 360.0 

decPointingList_icrs = rng.random_sample( 

nPointings) * 180.0 - 90.0 

mjdList = rng.random_sample(nPointings) * 10000.0 + 43000.0 

 

nStars = 10 

for raPointing_icrs, decPointing_icrs, mjd in \ 

zip(raPointingList_icrs, decPointingList_icrs, mjdList): 

 

obs = ObservationMetaData(pointingRA=raPointing_icrs, pointingDec=decPointing_icrs, mjd=mjd) 

raList_icrs = rng.random_sample(nStars) * 360.0 

decList_icrs = rng.random_sample(nStars) * 180.0 - 90.0 

raList_obs, decList_obs = observedFromICRS(raList_icrs, decList_icrs, obs_metadata=obs, 

epoch=2000.0, includeRefraction=True) 

 

obsTemp = ObservationMetaData(mjd=mjd) 

raPointing_obs, decPointing_obs = observedFromICRS(raPointing_icrs, 

decPointing_icrs, 

obs_metadata=obsTemp, epoch=2000.0, 

includeRefraction=True) 

 

for ra_obs, dec_obs, ra_icrs, dec_icrs in \ 

zip(raList_obs, decList_obs, raList_icrs, decList_icrs): 

 

raRad = np.radians(ra_obs) 

decRad = np.radians(dec_obs) 

sinRa = np.sin(raRad) 

cosRa = np.cos(raRad) 

sinDec = np.sin(decRad) 

cosDec = np.cos(decRad) 

 

# the three dimensional position of the star 

controlPosition = np.array([-cosDec * sinRa, cosDec * cosRa, sinDec]) 

 

# calculate the rotation matrices needed to transform the 

# x, y, and z axes into the local x, y, and z axes 

# (i.e. the axes with z lined up with raPointing_obs, decPointing_obs) 

alpha = 0.5 * np.pi - np.radians(decPointing_obs) 

ca = np.cos(alpha) 

sa = np.sin(alpha) 

rotX = np.array([[1.0, 0.0, 0.0], 

[0.0, ca, sa], 

[0.0, -sa, ca]]) 

 

cb = np.cos(np.radians(raPointing_obs)) 

sb = np.sin(np.radians(raPointing_obs)) 

rotZ = np.array([[cb, -sb, 0.0], 

[sb, cb, 0.0], 

[0.0, 0.0, 1.0]]) 

 

# rotate the coordinate axes into the local basis 

xAxis = np.dot(rotZ, np.dot(rotX, np.array([1.0, 0.0, 0.0]))) 

yAxis = np.dot(rotZ, np.dot(rotX, np.array([0.0, 1.0, 0.0]))) 

zAxis = np.dot(rotZ, np.dot(rotX, np.array([0.0, 0.0, 1.0]))) 

 

# calculate the local longitude and latitude of the star 

lon, lat = nativeLonLatFromRaDec(ra_icrs, dec_icrs, obs) 

cosLon = np.cos(np.radians(lon)) 

sinLon = np.sin(np.radians(lon)) 

cosLat = np.cos(np.radians(lat)) 

sinLat = np.sin(np.radians(lat)) 

 

# the x, y, z position of the star in the local coordinate 

# basis 

transformedPosition = np.array([-cosLat * sinLon, 

cosLat * cosLon, 

sinLat]) 

 

# convert that position back into the un-rotated bases 

testPosition = transformedPosition[0] * xAxis + \ 

transformedPosition[1] * yAxis + \ 

transformedPosition[2] * zAxis 

 

# assert that testPosition and controlPosition should be equal 

distance = np.sqrt(np.power(controlPosition - testPosition, 2).sum()) 

self.assertLess(distance, 1.0e-12) 

 

def testNativeLonLatVector(self): 

""" 

Test that nativeLonLatFromRaDec works in a vectorized way; we do this 

by performing a bunch of tansformations passing in ra and dec as numpy arrays 

and then comparing them to results computed in an element-wise way 

""" 

 

obs = ObservationMetaData(pointingRA=123.0, pointingDec=43.0, mjd=53467.2) 

 

nSamples = 100 

rng = np.random.RandomState(42) 

raList = rng.random_sample(nSamples) * 360.0 

decList = rng.random_sample(nSamples) * 180.0 - 90.0 

 

lonList, latList = nativeLonLatFromRaDec(raList, decList, obs) 

 

for rr, dd, lon, lat in zip(raList, decList, lonList, latList): 

lonControl, latControl = nativeLonLatFromRaDec(rr, dd, obs) 

distance = arcsecFromRadians(haversine(np.radians(lon), np.radians(lat), 

np.radians(lonControl), np.radians(latControl))) 

 

self.assertLess(distance, 0.0001) 

 

def testRaDec(self): 

""" 

Test that raDecFromNativeLonLat does invert 

nativeLonLatFromRaDec 

""" 

rng = np.random.RandomState(42) 

nSamples = 100 

# because raDecFromNativeLonLat is only good 

rrList = rng.random_sample(nSamples) * 50.0 

# out to a zenith distance of ~ 70 degrees 

 

thetaList = rng.random_sample(nSamples) * 2.0 * np.pi 

 

rrPointingList = rng.random_sample(10) * 50.0 

thetaPointingList = rng.random_sample(10) * 2.0 * np.pi 

mjdList = rng.random_sample(nSamples) * 10000.0 + 43000.0 

 

for rrp, thetap, mjd in \ 

zip(rrPointingList, thetaPointingList, mjdList): 

 

site = Site(name='LSST') 

raZenith, decZenith = raDecFromAltAz(180.0, 0.0, 

ObservationMetaData(mjd=mjd, site=site)) 

 

rp = raZenith + rrp * np.cos(thetap) 

dp = decZenith + rrp * np.sin(thetap) 

obs = ObservationMetaData(pointingRA=rp, pointingDec=dp, mjd=mjd, site=site) 

 

raList_icrs = (raZenith + rrList * np.cos(thetaList)) % 360.0 

decList_icrs = decZenith + rrList * np.sin(thetaList) 

 

raList_obs, decList_obs = observedFromICRS(raList_icrs, decList_icrs, 

obs_metadata=obs, 

epoch=2000.0, includeRefraction=True) 

 

# calculate the distance between the ICRS position and the observed 

# geocentric position 

dd_icrs_obs_list = arcsecFromRadians(haversine(np.radians(raList_icrs), 

np.radians(decList_icrs), 

np.radians(raList_obs), 

np.radians(decList_obs))) 

 

for rr, dd, dd_icrs_obs in zip(raList_icrs, decList_icrs, dd_icrs_obs_list): 

lon, lat = nativeLonLatFromRaDec(rr, dd, obs) 

r1, d1 = raDecFromNativeLonLat(lon, lat, obs) 

 

# the distance between the input RA, Dec and the round-trip output 

# RA, Dec 

distance = arcsecFromRadians(haversine(np.radians(r1), np.radians(d1), 

np.radians(rr), np.radians(dd))) 

 

rr_obs, dec_obs = observedFromICRS(rr, dd, 

obs_metadata=obs, epoch=2000.0, includeRefraction=True) 

 

# verify that the round trip through nativeLonLat only changed 

# RA, Dec by less than an arcsecond 

self.assertLess(distance, 1.0) 

 

# verify that any difference in the round trip is much less 

# than the distance between the ICRS and the observed geocentric 

# RA, Dec 

self.assertLess(distance, dd_icrs_obs * 0.01) 

 

def testRaDecVector(self): 

""" 

Test that raDecFromNativeLonLat does invert 

nativeLonLatFromRaDec (make sure it works in a vectorized way) 

""" 

rng = np.random.RandomState(42) 

nSamples = 100 

latList = rng.random_sample(nSamples) * 360.0 

lonList = rng.random_sample(nSamples) * 180.0 - 90.0 

raPoint = 95.0 

decPoint = 75.0 

 

obs = ObservationMetaData( 

pointingRA=raPoint, pointingDec=decPoint, mjd=53467.89) 

 

raList, decList = raDecFromNativeLonLat(lonList, latList, obs) 

 

for lon, lat, ra0, dec0 in zip(lonList, latList, raList, decList): 

ra1, dec1 = raDecFromNativeLonLat(lon, lat, obs) 

distance = arcsecFromRadians(haversine(np.radians(ra0), np.radians(dec0), 

np.radians(ra1), np.radians(dec1))) 

self.assertLess(distance, 0.1) 

 

def testDegreesVersusRadians(self): 

""" 

Test that the radian and degree versions of nativeLonLatFromRaDec 

and raDecFromNativeLonLat are consistent with each other 

""" 

 

rng = np.random.RandomState(873) 

nSamples = 1000 

obs = ObservationMetaData( 

pointingRA=45.0, pointingDec=-34.5, mjd=54656.76) 

raList = rng.random_sample(nSamples) * 360.0 

decList = rng.random_sample(nSamples) * 180.0 - 90.0 

 

lonDeg, latDeg = nativeLonLatFromRaDec(raList, decList, obs) 

lonRad, latRad = _nativeLonLatFromRaDec(np.radians(raList), np.radians(decList), obs) 

np.testing.assert_array_almost_equal(np.radians(lonDeg), lonRad, 15) 

np.testing.assert_array_almost_equal(np.radians(latDeg), latRad, 15) 

 

raDeg, decDeg = raDecFromNativeLonLat(raList, decList, obs) 

raRad, decRad = _raDecFromNativeLonLat(np.radians(raList), np.radians(decList), obs) 

np.testing.assert_array_almost_equal(np.radians(raDeg), raRad, 15) 

np.testing.assert_array_almost_equal(np.radians(decDeg), decRad, 15) 

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()