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 numpy as np 

3import lsst.utils.tests 

4 

5from lsst.sims.utils import ObservationMetaData 

6from lsst.sims.utils import raDecFromAltAz 

7from lsst.sims.coordUtils import pixelCoordsFromRaDec 

8from lsst.sims.coordUtils import _pixelCoordsFromRaDec 

9from lsst.sims.coordUtils import raDecFromPixelCoords 

10from lsst.sims.coordUtils import _raDecFromPixelCoords 

11from lsst.sims.coordUtils import pupilCoordsFromPixelCoords 

12from lsst.sims.coordUtils import pixelCoordsFromPupilCoords 

13 

14from lsst.sims.GalSimInterface import GalSimCameraWrapper 

15from lsst.sims.GalSimInterface import LSSTCameraWrapper 

16 

17import lsst.afw.cameraGeom.testUtils as camTestUtils 

18from lsst.afw.cameraGeom import FOCAL_PLANE 

19from lsst.afw.cameraGeom import TAN_PIXELS, FIELD_ANGLE, PIXELS 

20 

21from lsst.sims.coordUtils import pupilCoordsFromPixelCoordsLSST 

22from lsst.sims.coordUtils import pixelCoordsFromPupilCoordsLSST 

23from lsst.sims.coordUtils import raDecFromPixelCoordsLSST 

24from lsst.sims.coordUtils import lsst_camera 

25 

26from lsst.sims.coordUtils import clean_up_lsst_camera 

27 

28def setup_module(module): 

29 lsst.utils.tests.init() 

30 

31 

32class Camera_Wrapper_Test_Class(unittest.TestCase): 

33 

34 longMessage = True 

35 

36 @classmethod 

37 def tearDownClass(cls): 

38 clean_up_lsst_camera() 

39 

40 def test_generic_camera_wrapper(self): 

41 """ 

42 Test that GalSimCameraWrapper wraps its methods as expected. 

43 This is mostly to catch changes in afw API. 

44 """ 

45 camera = camTestUtils.CameraWrapper().camera 

46 camera_wrapper = GalSimCameraWrapper(camera) 

47 

48 obs_mjd = ObservationMetaData(mjd=60000.0) 

49 ra, dec = raDecFromAltAz(35.0, 112.0, obs_mjd) 

50 obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, 

51 mjd=obs_mjd.mjd, 

52 rotSkyPos=22.4) 

53 

54 rng = np.random.RandomState(8124) 

55 

56 for detector in camera: 

57 name = detector.getName() 

58 bbox = camera[name].getBBox() 

59 bbox_wrapper = camera_wrapper.getBBox(name) 

60 self.assertEqual(bbox.getMinX(), bbox_wrapper.getMinX()) 

61 self.assertEqual(bbox.getMaxX(), bbox_wrapper.getMaxX()) 

62 self.assertEqual(bbox.getMinY(), bbox_wrapper.getMinY()) 

63 self.assertEqual(bbox.getMaxY(), bbox_wrapper.getMaxY()) 

64 

65 center_point = camera[name].getCenter(FOCAL_PLANE) 

66 pixel_system = camera[name].makeCameraSys(PIXELS) 

67 center_pix = camera.transform(center_point, FOCAL_PLANE, pixel_system) 

68 center_pix_wrapper = camera_wrapper.getCenterPixel(name) 

69 self.assertEqual(center_pix.getX(), center_pix_wrapper.getX()) 

70 self.assertEqual(center_pix.getY(), center_pix_wrapper.getY()) 

71 

72 pupil_system = camera[name].makeCameraSys(FIELD_ANGLE) 

73 center_pupil = camera.transform(center_point, FOCAL_PLANE, pupil_system) 

74 center_pupil_wrapper = camera_wrapper.getCenterPupil(name) 

75 self.assertEqual(center_pupil.getX(), center_pupil_wrapper.getX()) 

76 self.assertEqual(center_pupil.getY(), center_pupil_wrapper.getY()) 

77 

78 corner_pupil_wrapper = camera_wrapper.getCornerPupilList(name) 

79 corner_point_list = camera[name].getCorners(FOCAL_PLANE) 

80 for point in corner_point_list: 

81 point_pupil = camera.transform(point, FOCAL_PLANE, pupil_system) 

82 dd_min = 1.0e10 

83 for wrapper_point in corner_pupil_wrapper: 

84 dd = np.sqrt((point_pupil.getX()-wrapper_point.getX())**2 + 

85 (point_pupil.getY()-wrapper_point.getY())**2) 

86 

87 if dd < dd_min: 

88 dd_min = dd 

89 self.assertLess(dd_min, 1.0e-20) 

90 

91 xpix_min = None 

92 xpix_max = None 

93 ypix_min = None 

94 ypix_max = None 

95 focal_to_tan_pix = camera[name].getTransform(FOCAL_PLANE, TAN_PIXELS) 

96 for point in corner_point_list: 

97 pixel_point = focal_to_tan_pix.applyForward(point) 

98 xx = pixel_point.getX() 

99 yy = pixel_point.getY() 

100 if xpix_min is None or xx<xpix_min: 

101 xpix_min = xx 

102 if ypix_min is None or yy<ypix_min: 

103 ypix_min = yy 

104 if xpix_max is None or xx>xpix_max: 

105 xpix_max = xx 

106 if ypix_max is None or yy>ypix_max: 

107 ypix_max = yy 

108 

109 pix_bounds_wrapper = camera_wrapper.getTanPixelBounds(name) 

110 self.assertEqual(pix_bounds_wrapper[0], xpix_min) 

111 self.assertEqual(pix_bounds_wrapper[1], xpix_max) 

112 self.assertEqual(pix_bounds_wrapper[2], ypix_min) 

113 self.assertEqual(pix_bounds_wrapper[3], ypix_max) 

114 

115 x_pup = rng.random_sample(10)*0.005-0.01 

116 y_pup = rng.random_sample(10)*0.005-0.01 

117 x_pix, y_pix = pixelCoordsFromPupilCoords(x_pup, y_pup, chipName=name, 

118 camera=camera) 

119 

120 (x_pix_wrapper, 

121 y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords(x_pup, y_pup, 

122 name, obs) 

123 

124 nan_x = np.where(np.isnan(x_pix)) 

125 self.assertEqual(len(nan_x[0]), 0) 

126 np.testing.assert_array_equal(x_pix, x_pix_wrapper) 

127 np.testing.assert_array_equal(y_pix, y_pix_wrapper) 

128 

129 x_pix = rng.random_sample(10)*100.0-200.0 

130 y_pix = rng.random_sample(10)*100.0-200.0 

131 x_pup, y_pup = pupilCoordsFromPixelCoords(x_pix, y_pix, chipName=name, 

132 camera=camera) 

133 

134 (x_pup_wrapper, 

135 y_pup_wrapper) = camera_wrapper.pupilCoordsFromPixelCoords(x_pix, y_pix, name, 

136 obs) 

137 

138 nan_x = np.where(np.isnan(x_pup)) 

139 self.assertEqual(len(nan_x[0]), 0) 

140 np.testing.assert_array_equal(x_pup, x_pup_wrapper) 

141 np.testing.assert_array_equal(y_pup, y_pup_wrapper) 

142 

143 ra, dec = raDecFromPixelCoords(x_pix, y_pix, name, camera=camera, 

144 obs_metadata=obs) 

145 

146 (ra_wrapper, 

147 dec_wrapper) = camera_wrapper.raDecFromPixelCoords(x_pix, y_pix, name, obs) 

148 

149 nan_ra = np.where(np.isnan(ra)) 

150 self.assertEqual(len(nan_ra[0]), 0) 

151 np.testing.assert_array_equal(ra, ra_wrapper) 

152 np.testing.assert_array_equal(dec, dec_wrapper) 

153 

154 ra, dec = _raDecFromPixelCoords(x_pix, y_pix, name, camera=camera, 

155 obs_metadata=obs) 

156 

157 (ra_wrapper, 

158 dec_wrapper) = camera_wrapper._raDecFromPixelCoords(x_pix, y_pix, name, obs) 

159 

160 nan_ra = np.where(np.isnan(ra)) 

161 self.assertEqual(len(nan_ra[0]), 0) 

162 np.testing.assert_array_equal(ra, ra_wrapper) 

163 np.testing.assert_array_equal(dec, dec_wrapper) 

164 

165 ra = obs.pointingRA + (rng.random_sample(10)*50.0-100.0)/60.0 

166 dec = obs.pointingDec + (rng.random_sample(10)*50.0-100.0)/60.0 

167 

168 x_pix, y_pix = pixelCoordsFromRaDec(ra, dec, chipName=name, camera=camera, 

169 obs_metadata=obs) 

170 

171 (x_pix_wrapper, 

172 y_pix_wrapper) = camera_wrapper.pixelCoordsFromRaDec(ra, dec, chipName=name, 

173 obs_metadata=obs) 

174 

175 nan_x = np.where(np.isnan(x_pix)) 

176 self.assertEqual(len(nan_x[0]), 0) 

177 np.testing.assert_array_equal(x_pix, x_pix_wrapper) 

178 np.testing.assert_array_equal(y_pix, y_pix_wrapper) 

179 

180 ra = np.radians(ra) 

181 dec = np.radians(dec) 

182 

183 x_pix, y_pix = _pixelCoordsFromRaDec(ra, dec, chipName=name, camera=camera, 

184 obs_metadata=obs) 

185 

186 (x_pix_wrapper, 

187 y_pix_wrapper) = camera_wrapper._pixelCoordsFromRaDec(ra, dec, chipName=name, 

188 obs_metadata=obs) 

189 

190 nan_x = np.where(np.isnan(x_pix)) 

191 self.assertEqual(len(nan_x[0]), 0) 

192 np.testing.assert_array_equal(x_pix, x_pix_wrapper) 

193 np.testing.assert_array_equal(y_pix, y_pix_wrapper) 

194 

195 del camera 

196 

197 def test_LSST_camera_wrapper(self): 

198 """ 

199 Test that LSSTCameraWrapper wraps its methods as expected. 

200 

201 Recall that the LSSTCameraWrapper applies the 90 degree rotation 

202 to go from DM pixel coordinates to Camera team pixel coordinates. 

203 Namely, 

204 

205 Camera +y = DM +x 

206 Camera +x = DM -y 

207 """ 

208 camera = lsst_camera() 

209 camera_wrapper = LSSTCameraWrapper() 

210 

211 obs_mjd = ObservationMetaData(mjd=60000.0) 

212 ra, dec = raDecFromAltAz(35.0, 112.0, obs_mjd) 

213 obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, 

214 mjd=obs_mjd.mjd, 

215 rotSkyPos=22.4, 

216 bandpassName='u') 

217 

218 rng = np.random.RandomState(8124) 

219 

220 for detector in camera: 

221 name = detector.getName() 

222 bbox = camera[name].getBBox() 

223 bbox_wrapper = camera_wrapper.getBBox(name) 

224 self.assertEqual(bbox.getMinX(), bbox_wrapper.getMinY()) 

225 self.assertEqual(bbox.getMaxX(), bbox_wrapper.getMaxY()) 

226 self.assertEqual(bbox.getMinY(), bbox_wrapper.getMinX()) 

227 self.assertEqual(bbox.getMaxY(), bbox_wrapper.getMaxX()) 

228 self.assertGreater(bbox_wrapper.getMaxY()-bbox_wrapper.getMinY(), 

229 bbox_wrapper.getMaxX()-bbox_wrapper.getMinX()) 

230 

231 center_point = camera[name].getCenter(FOCAL_PLANE) 

232 pixel_system = camera[name].makeCameraSys(PIXELS) 

233 center_pix = camera.transform(center_point, FOCAL_PLANE, pixel_system) 

234 center_pix_wrapper = camera_wrapper.getCenterPixel(name) 

235 self.assertEqual(center_pix.getX(), center_pix_wrapper.getY()) 

236 self.assertEqual(center_pix.getY(), center_pix_wrapper.getX()) 

237 

238 # Note that DM and the Camera team agree on the orientation 

239 # of the pupil coordinate/field angle axes 

240 pupil_system = camera[name].makeCameraSys(FIELD_ANGLE) 

241 center_pupil = camera.transform(center_point, FOCAL_PLANE, pupil_system) 

242 center_pupil_wrapper = camera_wrapper.getCenterPupil(name) 

243 self.assertEqual(center_pupil.getX(), center_pupil_wrapper.getX()) 

244 self.assertEqual(center_pupil.getY(), center_pupil_wrapper.getY()) 

245 

246 corner_pupil_wrapper = camera_wrapper.getCornerPupilList(name) 

247 corner_point_list = camera[name].getCorners(FOCAL_PLANE) 

248 for point in corner_point_list: 

249 point_pupil = camera.transform(point, FOCAL_PLANE, pupil_system) 

250 dd_min = 1.0e10 

251 for wrapper_point in corner_pupil_wrapper: 

252 dd = np.sqrt((point_pupil.getX()-wrapper_point.getX())**2 + 

253 (point_pupil.getY()-wrapper_point.getY())**2) 

254 

255 if dd < dd_min: 

256 dd_min = dd 

257 self.assertLess(dd_min, 1.0e-20) 

258 

259 xpix_min = None 

260 xpix_max = None 

261 ypix_min = None 

262 ypix_max = None 

263 focal_to_tan_pix = camera[name].getTransform(FOCAL_PLANE, TAN_PIXELS) 

264 for point in corner_point_list: 

265 pixel_point = focal_to_tan_pix.applyForward(point) 

266 xx = pixel_point.getX() 

267 yy = pixel_point.getY() 

268 if xpix_min is None or xx<xpix_min: 

269 xpix_min = xx 

270 if ypix_min is None or yy<ypix_min: 

271 ypix_min = yy 

272 if xpix_max is None or xx>xpix_max: 

273 xpix_max = xx 

274 if ypix_max is None or yy>ypix_max: 

275 ypix_max = yy 

276 

277 pix_bounds_wrapper = camera_wrapper.getTanPixelBounds(name) 

278 self.assertEqual(pix_bounds_wrapper[0], ypix_min) 

279 self.assertEqual(pix_bounds_wrapper[1], ypix_max) 

280 self.assertEqual(pix_bounds_wrapper[2], xpix_min) 

281 self.assertEqual(pix_bounds_wrapper[3], xpix_max) 

282 

283 # generate some random pupil coordinates; 

284 # verify that the relationship between the DM and Camera team 

285 # pixel coordinates corresponding to those pupil coordinates 

286 # is as expected 

287 x_pup = rng.random_sample(10)*0.005-0.01 

288 y_pup = rng.random_sample(10)*0.005-0.01 

289 x_pix, y_pix = pixelCoordsFromPupilCoordsLSST(x_pup, y_pup, chipName=name, 

290 band=obs.bandpass) 

291 

292 (x_pix_wrapper, 

293 y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords(x_pup, y_pup, 

294 name, obs) 

295 

296 nan_x = np.where(np.isnan(x_pix)) 

297 self.assertEqual(len(nan_x[0]), 0) 

298 np.testing.assert_allclose(x_pix-center_pix.getX(), 

299 y_pix_wrapper-center_pix_wrapper.getY(), 

300 atol=1.0e-10, rtol=0.0) 

301 np.testing.assert_allclose(y_pix-center_pix.getY(), 

302 center_pix_wrapper.getX()-x_pix_wrapper, 

303 atol=1.0e-10, rtol=0.0) 

304 

305 # use camera_wrapper.pupilCoordsFromPixelCoords to go back to pupil 

306 # coordinates from x_pix_wrapper, y_pix_wrapper; make sure you get 

307 # the original pupil coordinates back out 

308 (x_pup_wrapper, 

309 y_pup_wrapper) = camera_wrapper.pupilCoordsFromPixelCoords(x_pix_wrapper, 

310 y_pix_wrapper, 

311 name, obs) 

312 msg = 'worst error %e' % np.abs(x_pup-x_pup_wrapper).max() 

313 np.testing.assert_allclose(x_pup, x_pup_wrapper, atol=1.0e-10, rtol=0.0, err_msg=msg) 

314 msg = 'worst error %e' % np.abs(y_pup-y_pup_wrapper).max() 

315 np.testing.assert_allclose(y_pup, y_pup_wrapper, atol=1.0e-10, rtol=0.0, err_msg=msg) 

316 

317 # generate some random sky coordinates; verify that the methods that 

318 # convert between (RA, Dec) and pixel coordinates behave as expected. 

319 # NOTE: x_pix, y_pix will be in DM pixel coordinate convention 

320 x_pix = bbox.getMinX() + rng.random_sample(10)*(bbox.getMaxX()-bbox.getMinX()) 

321 y_pix = bbox.getMinY() + rng.random_sample(10)*(bbox.getMaxY()-bbox.getMinY()) 

322 

323 ra, dec = raDecFromPixelCoordsLSST(x_pix, y_pix, name, obs_metadata=obs, 

324 band=obs.bandpass) 

325 

326 (ra_wrapper, 

327 dec_wrapper) = camera_wrapper.raDecFromPixelCoords(2.0*center_pix.getY()-y_pix, 

328 x_pix, name, obs) 

329 

330 nan_ra = np.where(np.isnan(ra)) 

331 self.assertEqual(len(nan_ra[0]), 0) 

332 np.testing.assert_allclose(ra, ra_wrapper, atol=1.0e-10, rtol=0.0) 

333 np.testing.assert_allclose(dec, dec_wrapper, atol=1.0e-10, rtol=0.0) 

334 

335 # make sure that the method that returns RA, Dec in radians agrees with 

336 # the method that returns RA, Dec in degrees 

337 (ra_rad, 

338 dec_rad) = camera_wrapper._raDecFromPixelCoords(2.0*center_pix.getY()-y_pix, 

339 x_pix, name, obs) 

340 

341 np.testing.assert_allclose(np.radians(ra_wrapper), ra_rad, atol=1.0e-10, rtol=0.0) 

342 np.testing.assert_allclose(np.radians(dec_wrapper), dec_rad, atol=1.0e-10, rtol=0.0) 

343 

344 # Go back to pixel coordinates with pixelCoordsFromRaDec; verify that 

345 # the result relates to the original DM pixel coordinates as expected 

346 # (x_pix_inv, y_pix_inv will be in Camera pixel coordinates) 

347 (x_pix_inv, 

348 y_pix_inv) = camera_wrapper.pixelCoordsFromRaDec(ra_wrapper, dec_wrapper, 

349 chipName=name, 

350 obs_metadata=obs) 

351 

352 np.testing.assert_allclose(y_pix_inv, x_pix, atol=1.0e-4, rtol=0.0) 

353 np.testing.assert_allclose(x_pix_inv, 2.0*center_pix.getY()-y_pix, atol=1.0e-4, rtol=0.0) 

354 

355 ra = np.radians(ra_wrapper) 

356 dec = np.radians(dec_wrapper) 

357 

358 # check that the the method that accepts RA, Dec in radians agrees with the 

359 # method that accepts RA, Dec in degrees 

360 (x_pix_wrapper, 

361 y_pix_wrapper) = camera_wrapper._pixelCoordsFromRaDec(ra, dec, chipName=name, 

362 obs_metadata=obs) 

363 

364 np.testing.assert_allclose(x_pix_inv, x_pix_wrapper, atol=1.0e-10, rtol=0.0) 

365 np.testing.assert_allclose(y_pix_inv, y_pix_wrapper, atol=1.0e-10, rtol=0.0) 

366 

367 del camera 

368 del camera_wrapper 

369 del lsst_camera._lsst_camera 

370 

371 def test_dmPixFromCameraPix(self): 

372 """ 

373 Test that the method to return DM pixel coordinates from 

374 Camera Team pixel coordinates works. 

375 """ 

376 camera = lsst_camera() 

377 camera_wrapper = LSSTCameraWrapper() 

378 obs = ObservationMetaData(bandpassName='u') 

379 

380 npts = 100 

381 rng = np.random.RandomState(1824) 

382 dm_x_pix_list = rng.random_sample(npts)*4000.0 

383 dm_y_pix_list = rng.random_sample(npts)*4000.0 

384 name_list = [] 

385 for det in camera: 

386 name_list.append(det.getName()) 

387 chip_name_list = rng.choice(name_list, size=npts) 

388 

389 (xPup_list, 

390 yPup_list) = pupilCoordsFromPixelCoordsLSST(dm_x_pix_list, 

391 dm_y_pix_list, 

392 chipName=chip_name_list, 

393 band=obs.bandpass) 

394 

395 (cam_x_pix_list, 

396 cam_y_pix_list) = camera_wrapper.pixelCoordsFromPupilCoords(xPup_list, 

397 yPup_list, 

398 chip_name_list, 

399 obs) 

400 

401 (dm_x_test, 

402 dm_y_test) = camera_wrapper.dmPixFromCameraPix(cam_x_pix_list, 

403 cam_y_pix_list, 

404 chip_name_list) 

405 

406 np.testing.assert_array_almost_equal(dm_x_test, dm_x_pix_list, 

407 decimal=4) 

408 np.testing.assert_array_almost_equal(dm_y_test, dm_y_pix_list, 

409 decimal=4) 

410 

411 # test transformations made one at a time 

412 for ii in range(len(cam_x_pix_list)): 

413 dm_x, dm_y = camera_wrapper.dmPixFromCameraPix(cam_x_pix_list[ii], 

414 cam_y_pix_list[ii], 

415 chip_name_list[ii]) 

416 

417 self.assertAlmostEqual(dm_x_pix_list[ii], dm_x, 4) 

418 self.assertAlmostEqual(dm_y_pix_list[ii], dm_y, 4) 

419 

420 # test case where an array of points is on a single chip 

421 chip_name = chip_name_list[10] 

422 

423 (xPup_list, 

424 yPup_list) = pupilCoordsFromPixelCoordsLSST(dm_x_pix_list, 

425 dm_y_pix_list, 

426 chipName=chip_name, 

427 band=obs.bandpass) 

428 

429 (cam_x_pix_list, 

430 cam_y_pix_list) = camera_wrapper.pixelCoordsFromPupilCoords(xPup_list, 

431 yPup_list, 

432 chip_name, 

433 obs) 

434 

435 (dm_x_test, 

436 dm_y_test) = camera_wrapper.dmPixFromCameraPix(cam_x_pix_list, 

437 cam_y_pix_list, 

438 chip_name) 

439 

440 np.testing.assert_array_almost_equal(dm_x_test, dm_x_pix_list, 

441 decimal=4) 

442 np.testing.assert_array_almost_equal(dm_y_test, dm_y_pix_list, 

443 decimal=4) 

444 

445 del camera 

446 del camera_wrapper 

447 del lsst_camera._lsst_camera 

448 

449 def test_camPixFromDMpix(self): 

450 """ 

451 test that camPixFromDMpix inverts dmPixFromCamPix 

452 """ 

453 camera_wrapper = LSSTCameraWrapper() 

454 rng = np.random.RandomState() 

455 npts = 200 

456 cam_x_in = rng.random_sample(npts)*4000.0 

457 cam_y_in = rng.random_sample(npts)*4000.0 

458 dm_x, dm_y = camera_wrapper.dmPixFromCameraPix(cam_x_in, cam_y_in, 'R:1,1 S:2,2') 

459 cam_x, cam_y = camera_wrapper.cameraPixFromDMPix(dm_x, dm_y, 'R:1,1 S:2,2') 

460 np.testing.assert_array_almost_equal(cam_x_in, cam_x, decimal=10) 

461 np.testing.assert_array_almost_equal(cam_y_in, cam_y, decimal=10) 

462 del camera_wrapper 

463 del lsst_camera._lsst_camera 

464 

465 

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

467 pass 

468 

469 

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

471 lsst.utils.tests.init() 

472 unittest.main()