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 os 

3import numpy as np 

4import tempfile 

5import palpy 

6 

7import lsst.utils.tests 

8from lsst.utils import getPackageDir 

9 

10from lsst.sims.catalogs.db import fileDBObject 

11from lsst.sims.utils import _angularSeparation 

12from lsst.sims.utils import angularSeparation 

13from lsst.sims.utils import arcsecFromRadians 

14from lsst.sims.catalogs.definitions import InstanceCatalog 

15from lsst.sims.catUtils.utils import makePhoSimTestDB 

16from lsst.sims.catUtils.utils import testStarsDBObj, testGalaxyDiskDBObj 

17from lsst.sims.catUtils.mixins import PhoSimAstrometryBase 

18from lsst.sims.catUtils.mixins import PhoSimAstrometryStars 

19from lsst.sims.catUtils.mixins import PhoSimAstrometryGalaxies 

20 

21from lsst.sims.utils import _observedFromICRS 

22from lsst.sims.utils import _observedFromAppGeo 

23 

24from lsst.sims.utils import observedFromICRS 

25from lsst.sims.utils import observedFromAppGeo 

26 

27from lsst.sims.utils import Site, ObservationMetaData 

28from lsst.sims.utils import distanceToSun 

29from lsst.sims.utils import raDecFromAltAz 

30from lsst.sims.utils import pupilCoordsFromRaDec 

31from lsst.sims.coordUtils import focalPlaneCoordsFromPupilCoords 

32from lsst.sims.coordUtils import lsst_camera 

33 

34from lsst.sims.utils.CodeUtilities import sims_clean_up 

35from lsst.sims.utils.CodeUtilities import _validate_inputs 

36 

37from lsst.sims.catUtils.exampleCatalogDefinitions import PhoSimCatalogPoint 

38from lsst.sims.catUtils.exampleCatalogDefinitions import DefaultPhoSimHeaderMap 

39 

40from lsst.sims.utils import angularSeparation 

41from lsst.sims.utils import _angularSeparation,arcsecFromRadians 

42 

43from lsst.sims.coordUtils import clean_up_lsst_camera 

44 

45ROOT = os.path.abspath(os.path.dirname(__file__)) 

46 

47 

48def setup_module(module): 

49 lsst.utils.tests.init() 

50 

51 

52def _naivePupilCoordsFromObserved(ra_obs, dec_obs, ra0, dec0, rotSkyPos): 

53 """ 

54 Convert Observed RA, Dec into pupil coordinates 

55 

56 Parameters 

57 ---------- 

58 ra_obs is the observed RA in radians 

59 

60 dec_obs is the observed Dec in radians 

61 

62 obs_metadata is an ObservationMetaData characterizing the telescope location and pointing 

63 

64 epoch is the epoch of the mean RA and Dec in julian years (default=2000.0) 

65 

66 includeRefraction is a boolean controlling the application of refraction. 

67 

68 Returns 

69 -------- 

70 A numpy array whose first row is the x coordinate on the pupil in 

71 radians and whose second row is the y coordinate in radians 

72 """ 

73 

74 are_arrays = _validate_inputs([ra_obs, dec_obs], ['ra_obs', 'dec_obs'], 

75 "pupilCoordsFromObserved") 

76 

77 theta = -1.0*rotSkyPos 

78 

79 ra_pointing = ra0 

80 dec_pointing = dec0 

81 

82 # palpy.ds2tp performs the gnomonic projection on ra_in and dec_in 

83 # with a tangent point at (pointingRA, pointingDec) 

84 # 

85 if not are_arrays: 

86 try: 

87 x, y = palpy.ds2tp(ra_obs, dec_obs, ra_pointing, dec_pointing) 

88 except: 

89 x = np.NaN 

90 y = np.NaN 

91 else: 

92 try: 

93 x, y = palpy.ds2tpVector(ra_obs, dec_obs, ra_pointing, dec_pointing) 

94 except: 

95 # apparently, one of your ra/dec values was improper; we will have to do this 

96 # element-wise, putting NaN in the place of the bad values 

97 x = [] 

98 y = [] 

99 for rr, dd in zip(ra_obs, dec_obs): 

100 try: 

101 xx, yy = palpy.ds2tp(rr, dd, ra_pointing, dec_pointing) 

102 except: 

103 xx = np.NaN 

104 yy = np.NaN 

105 x.append(xx) 

106 y.append(yy) 

107 x = np.array(x) 

108 y = np.array(y) 

109 

110 # rotate the result by rotskypos (rotskypos being "the angle of the sky relative to 

111 # camera coordinates" according to phoSim documentation) to account for 

112 # the rotation of the focal plane about the telescope pointing 

113 

114 x_out = x*np.cos(theta) - y*np.sin(theta) 

115 y_out = x*np.sin(theta) + y*np.cos(theta) 

116 

117 return np.array([x_out, y_out]) 

118 

119 

120class StarTestCatalog(PhoSimAstrometryStars, InstanceCatalog): 

121 column_outputs = ['raICRS', 'decICRS', 'raPhoSim', 'decPhoSim', 

122 'raJ2000', 'decJ2000', 

123 'properMotionRa','properMotionDec', 'parallax', 

124 'radialVelocity'] 

125 

126 transformations = {'raICRS': np.degrees, 'decICRS': np.degrees, 

127 'raPhoSim': np.degrees, 'decPhoSim': np.degrees} 

128 

129 default_formats = {'f': '%.12g'} 

130 

131 delimiter = ' ' 

132 

133 

134class GalaxyTestCatalog(PhoSimAstrometryGalaxies, InstanceCatalog): 

135 column_outputs = ['raICRS', 'decICRS', 'raPhoSim', 'decPhoSim'] 

136 transformations = {'raICRS': np.degrees, 'decICRS': np.degrees, 

137 'raPhoSim': np.degrees, 'decPhoSim': np.degrees} 

138 

139 override_formats = {'raICRS': '%.12g', 'decICRS': '%.12g', 

140 'raPhoSim': '%.12g', 'decPhoSim': '%.12g'} 

141 

142 delimiter = ' ' 

143 

144 

145class PhoSimAstrometryTestCase(unittest.TestCase): 

146 

147 @classmethod 

148 def setUpClass(cls): 

149 cls.db_name = tempfile.mktemp(dir=ROOT, prefix='PhoSimAstDB', suffix='.db') 

150 cls.obs = makePhoSimTestDB(filename=cls.db_name, 

151 size=1000) 

152 

153 @classmethod 

154 def tearDownClass(cls): 

155 sims_clean_up() 

156 if os.path.exists(cls.db_name): 

157 os.unlink(cls.db_name) 

158 

159 clean_up_lsst_camera() 

160 

161 def test_stellar_astrometry_radians(self): 

162 """ 

163 Test that we can go from raPhoSim, decPhoSim to ICRS coordinates 

164 in the case of stars (in radians) 

165 """ 

166 db = testStarsDBObj(driver='sqlite', database=self.db_name) 

167 cat = StarTestCatalog(db, obs_metadata=self.obs) 

168 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

169 cat.write_catalog(cat_name) 

170 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

171 ('raPhoSim', float), ('decPhoSim', float), 

172 ('raJ2000', float), ('decJ2000', float), 

173 ('pmRA', float), ('pmDec', float), 

174 ('parallax', float), ('vRad', float)]) 

175 data = np.genfromtxt(cat_name, dtype=dtype) 

176 self.assertGreater(len(data), 100) 

177 ra_pho_rad = np.radians(data['raPhoSim']) 

178 dec_pho_rad = np.radians(data['decPhoSim']) 

179 

180 # verify that, when transforming back to ICRS, we are within 

181 # 10^-3 arcsec 

182 ra_icrs, dec_icrs = PhoSimAstrometryBase._icrsFromPhoSim(ra_pho_rad, 

183 dec_pho_rad, 

184 self.obs) 

185 dist = _angularSeparation(np.radians(data['raICRS']), 

186 np.radians(data['decICRS']), 

187 ra_icrs, dec_icrs) 

188 

189 dist = arcsecFromRadians(dist) 

190 self.assertLess(dist.max(), 0.001) 

191 

192 # verify that the distance between raPhoSim, decPhoSim and 

193 # raICRS, decICRS is greater than the distance between 

194 # the original raICRS, decICRS and the newly-calculated 

195 # raICRS, decICRS 

196 dist_bad = _angularSeparation(ra_pho_rad, dec_pho_rad, 

197 np.radians(data['raICRS']), 

198 np.radians(data['decICRS'])) 

199 

200 dist_bad = arcsecFromRadians(dist_bad) 

201 self.assertGreater(dist_bad.min(), dist.max()) 

202 

203 del db 

204 

205 def test_stellar_astrometry_degrees(self): 

206 """ 

207 Test that we can go from raPhoSim, decPhoSim to ICRS coordinates 

208 in the case of stars (in degrees) 

209 """ 

210 db = testStarsDBObj(driver='sqlite', database=self.db_name) 

211 cat = StarTestCatalog(db, obs_metadata=self.obs) 

212 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

213 cat.write_catalog(cat_name) 

214 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

215 ('raPhoSim', float), ('decPhoSim', float), 

216 ('raJ2000', float), ('decJ2000', float), 

217 ('pmRA', float), ('pmDec', float), 

218 ('parallax', float), ('vRad', float)]) 

219 data = np.genfromtxt(cat_name, dtype=dtype) 

220 self.assertGreater(len(data), 100) 

221 

222 # verify that, when transforming back to ICRS, we are within 

223 # 10^-3 arcsec 

224 ra_icrs, dec_icrs = PhoSimAstrometryBase.icrsFromPhoSim(data['raPhoSim'], 

225 data['decPhoSim'], 

226 self.obs) 

227 dist = angularSeparation(data['raICRS'], data['decICRS'], 

228 ra_icrs, dec_icrs) 

229 

230 dist = 3600.0*dist 

231 self.assertLess(dist.max(), 0.001) 

232 

233 # verify that the distance between raPhoSim, decPhoSim and 

234 # raICRS, decICRS is greater than the distance between 

235 # the original raICRS, decICRS and the newly-calculated 

236 # raICRS, decICRS 

237 dist_bad = angularSeparation(data['raPhoSim'], data['decPhoSim'], 

238 data['raICRS'], data['decICRS']) 

239 

240 dist_bad = 3600.0*dist_bad 

241 self.assertGreater(dist_bad.min(), dist.max()) 

242 

243 del db 

244 

245 def test_stellar_observed_radians(self): 

246 """ 

247 Test ability to go all the way to observed RA, Dec 

248 from PhoSim (this is necessary for the ImSim software 

249 that DESC is working on) 

250 """ 

251 db = testStarsDBObj(driver='sqlite', database=self.db_name) 

252 cat = StarTestCatalog(db, obs_metadata=self.obs) 

253 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

254 cat.write_catalog(cat_name) 

255 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

256 ('raPhoSim', float), ('decPhoSim', float), 

257 ('raJ2000', float), ('decJ2000', float), 

258 ('pmRA', float), ('pmDec', float), 

259 ('parallax', float), ('vRad', float)]) 

260 data = np.genfromtxt(cat_name, dtype=dtype) 

261 self.assertGreater(len(data), 100) 

262 

263 (ra_obs, 

264 dec_obs) = _observedFromICRS(data['raJ2000'], 

265 data['decJ2000'], 

266 obs_metadata=self.obs, 

267 pm_ra=data['pmRA'], 

268 pm_dec=data['pmDec'], 

269 parallax=data['parallax'], 

270 v_rad=data['vRad'], 

271 includeRefraction=True, 

272 epoch=2000.0) 

273 

274 (ra_appGeo, 

275 dec_appGeo) = PhoSimAstrometryBase._appGeoFromPhoSim(np.radians(data['raPhoSim']), 

276 np.radians(data['decPhoSim']), 

277 self.obs) 

278 

279 (ra_obs_2, 

280 dec_obs_2) = _observedFromAppGeo(ra_appGeo, dec_appGeo, 

281 obs_metadata=self.obs, 

282 includeRefraction=True) 

283 

284 dd = arcsecFromRadians(_angularSeparation(ra_obs, dec_obs, ra_obs_2, dec_obs_2)) 

285 self.assertLess(dd.max(), 1.0e-5) 

286 

287 def test_stellar_observed_degrees(self): 

288 """ 

289 Test ability to go all the way to observed RA, Dec 

290 from PhoSim (this is necessary for the ImSim software 

291 that DESC is working on) 

292 """ 

293 db = testStarsDBObj(driver='sqlite', database=self.db_name) 

294 cat = StarTestCatalog(db, obs_metadata=self.obs) 

295 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

296 cat.write_catalog(cat_name) 

297 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

298 ('raPhoSim', float), ('decPhoSim', float), 

299 ('raJ2000', float), ('decJ2000', float), 

300 ('pmRA', float), ('pmDec', float), 

301 ('parallax', float), ('vRad', float)]) 

302 data = np.genfromtxt(cat_name, dtype=dtype) 

303 self.assertGreater(len(data), 100) 

304 

305 (ra_obs, 

306 dec_obs) = observedFromICRS(np.degrees(data['raJ2000']), 

307 np.degrees(data['decJ2000']), 

308 obs_metadata=self.obs, 

309 pm_ra=arcsecFromRadians(data['pmRA']), 

310 pm_dec=arcsecFromRadians(data['pmDec']), 

311 parallax=arcsecFromRadians(data['parallax']), 

312 v_rad=data['vRad'], 

313 includeRefraction=True, 

314 epoch=2000.0) 

315 

316 (ra_appGeo, 

317 dec_appGeo) = PhoSimAstrometryBase.appGeoFromPhoSim(data['raPhoSim'], 

318 data['decPhoSim'], 

319 self.obs) 

320 

321 (ra_obs_2, 

322 dec_obs_2) = observedFromAppGeo(ra_appGeo, dec_appGeo, 

323 obs_metadata=self.obs, 

324 includeRefraction=True) 

325 

326 dd = 3600.0*angularSeparation(ra_obs, dec_obs, ra_obs_2, dec_obs_2) 

327 self.assertLess(dd.max(), 1.0e-5) 

328 

329 def test_galaxy_astrometry_radians(self): 

330 """ 

331 Test that we can go from raPhoSim, decPhoSim to ICRS coordinates 

332 in the case of galaxies (in radians) 

333 """ 

334 db = testGalaxyDiskDBObj(driver='sqlite', database=self.db_name) 

335 cat = GalaxyTestCatalog(db, obs_metadata=self.obs) 

336 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

337 cat.write_catalog(cat_name) 

338 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

339 ('raPhoSim', float), ('decPhoSim', float)]) 

340 data = np.genfromtxt(cat_name, dtype=dtype) 

341 self.assertGreater(len(data), 100) 

342 ra_pho_rad = np.radians(data['raPhoSim']) 

343 dec_pho_rad = np.radians(data['decPhoSim']) 

344 

345 # verify that, when transforming back to ICRS, we are within 

346 # 10^-3 arcsec 

347 ra_icrs, dec_icrs = PhoSimAstrometryBase._icrsFromPhoSim(ra_pho_rad, 

348 dec_pho_rad, 

349 self.obs) 

350 dist = _angularSeparation(np.radians(data['raICRS']), 

351 np.radians(data['decICRS']), 

352 ra_icrs, dec_icrs) 

353 

354 dist = arcsecFromRadians(dist) 

355 self.assertLess(dist.max(), 0.001) 

356 

357 # verify that the distance between raPhoSim, decPhoSim and 

358 # raICRS, decICRS is greater than the distance between 

359 # the original raICRS, decICRS and the newly-calculated 

360 # raICRS, decICRS 

361 dist_bad = _angularSeparation(ra_pho_rad, dec_pho_rad, 

362 np.radians(data['raICRS']), 

363 np.radians(data['decICRS'])) 

364 

365 dist_bad = arcsecFromRadians(dist_bad) 

366 self.assertGreater(dist_bad.min(), dist.max()) 

367 

368 del db 

369 

370 def test_galaxy_astrometry_degrees(self): 

371 """ 

372 Test that we can go from raPhoSim, decPhoSim to ICRS coordinates 

373 in the case of galaxies (in degrees) 

374 """ 

375 db = testGalaxyDiskDBObj(driver='sqlite', database=self.db_name) 

376 cat = GalaxyTestCatalog(db, obs_metadata=self.obs) 

377 with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

378 cat.write_catalog(cat_name) 

379 dtype = np.dtype([('raICRS', float), ('decICRS', float), 

380 ('raPhoSim', float), ('decPhoSim', float)]) 

381 data = np.genfromtxt(cat_name, dtype=dtype) 

382 self.assertGreater(len(data), 100) 

383 

384 # verify that, when transforming back to ICRS, we are within 

385 # 10^-3 arcsec 

386 ra_icrs, dec_icrs = PhoSimAstrometryBase.icrsFromPhoSim(data['raPhoSim'], 

387 data['decPhoSim'], 

388 self.obs) 

389 dist = angularSeparation(data['raICRS'], data['decICRS'], 

390 ra_icrs, dec_icrs) 

391 

392 dist = 3600.0*dist 

393 self.assertLess(dist.max(), 0.001) 

394 

395 # verify that the distance between raPhoSim, decPhoSim and 

396 # raICRS, decICRS is greater than the distance between 

397 # the original raICRS, decICRS and the newly-calculated 

398 # raICRS, decICRS 

399 dist_bad = angularSeparation(data['raPhoSim'], data['decPhoSim'], 

400 data['raICRS'], data['decICRS']) 

401 

402 dist_bad = 3600.0*dist_bad 

403 self.assertGreater(dist_bad.min(), dist.max()) 

404 

405 del db 

406 

407 def test_naive_focal_plane_position(self): 

408 """ 

409 Test deprecession of PhoSim coordinates by comparing 

410 the focal plane position predicted by CatSim from ICRS 

411 with the focal plane position predicted by CatSim from deprecessed 

412 coordinates. 

413 """ 

414 

415 phosim_mixin = PhoSimAstrometryBase() 

416 

417 mjd = 59587.2 

418 

419 # create site with no atmosphere so that we can avoid 

420 # refraction 

421 site = Site(name="LSST", pressure=0.0, humidity=0.0) 

422 

423 obs = ObservationMetaData(mjd=mjd, site=site) 

424 ra, dec = raDecFromAltAz(31.0, 112.0, obs) 

425 

426 d_sun = distanceToSun(ra, dec, obs.mjd) 

427 self.assertGreater(d_sun, 45.0) 

428 

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

430 rotSkyPos=27.3, mjd=mjd, 

431 site=site) 

432 ra_icrs = np.arange(obs.pointingRA-2.0, obs.pointingRA+2.0, 0.05) 

433 dec_icrs = np.arange(obs.pointingDec-2.0, obs.pointingDec+2.0, 0.05) 

434 

435 coord_grid = np.meshgrid(ra_icrs, dec_icrs) 

436 ra_icrs = coord_grid[0].flatten() 

437 dec_icrs = coord_grid[1].flatten() 

438 

439 (xpup_icrs, 

440 ypup_icrs) = pupilCoordsFromRaDec(ra_icrs, dec_icrs, 

441 obs_metadata=obs, 

442 epoch=2000.0, 

443 includeRefraction=False) 

444 

445 (x_focal_icrs, 

446 y_focal_icrs) = focalPlaneCoordsFromPupilCoords(xpup_icrs, 

447 ypup_icrs, 

448 camera=lsst_camera()) 

449 

450 ra_obs, dec_obs = observedFromICRS(ra_icrs, dec_icrs, obs_metadata=obs, 

451 epoch=2000.0, 

452 includeRefraction=False) 

453 

454 ra_obs_rad = np.radians(ra_obs) 

455 dec_obs_rad = np.radians(dec_obs) 

456 

457 (ra_deprecessed_rad, 

458 dec_deprecessed_rad) = phosim_mixin._dePrecess(ra_obs_rad, 

459 dec_obs_rad, obs) 

460 

461 (xpup_deprecessed, 

462 ypup_deprecessed) = _naivePupilCoordsFromObserved(ra_deprecessed_rad, 

463 dec_deprecessed_rad, 

464 obs._pointingRA, 

465 obs._pointingDec, 

466 obs._rotSkyPos) 

467 

468 (x_focal_deprecessed, 

469 y_focal_deprecessed) = focalPlaneCoordsFromPupilCoords(xpup_deprecessed, 

470 ypup_deprecessed, 

471 camera=lsst_camera()) 

472 

473 dd = np.sqrt((x_focal_icrs-x_focal_deprecessed)**2 

474 +(y_focal_icrs-y_focal_deprecessed)**2) 

475 

476 self.assertLess(dd.max(), 5.0e-8) 

477 

478 def test_against_catalog(self): 

479 """ 

480 Compare deprecession results to a catalog that was validated 

481 with PhoSim. 

482 """ 

483 obs = ObservationMetaData(pointingRA=53.00913847303155535, 

484 pointingDec=-27.43894880881512321, 

485 rotSkyPos=256.75075318193080420, 

486 mjd=59580.13955500000156462, 

487 site=Site(name="LSST", pressure=0.0, 

488 humidity=0.0)) 

489 

490 dtype = np.dtype([('id', int), ('ra', float), ('dec', float), 

491 ('ra_deprecessed', float), ('dec_deprecessed', float), 

492 ('x_dm', float), ('y_dm', float), 

493 ('x_focal', float), ('y_focal', float), 

494 ('x_cam', float), ('y_cam', float)]) 

495 

496 data = np.genfromtxt(os.path.join(getPackageDir('sims_catUtils'), 

497 'tests', 'testData', 

498 'pixel_prediction_catalog.txt'), 

499 dtype=dtype) 

500 

501 ra_obs, dec_obs = observedFromICRS(data['ra'], data['dec'], 

502 obs_metadata=obs, 

503 includeRefraction=False, 

504 epoch=2000.0) 

505 

506 phosim_mixin = PhoSimAstrometryBase() 

507 ra_dep, dec_dep = phosim_mixin._dePrecess(np.radians(ra_obs), 

508 np.radians(dec_obs), 

509 obs) 

510 

511 dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], 

512 np.degrees(ra_dep), np.degrees(dec_dep)) 

513 self.assertLess(dd.max(), 1.0e-5) 

514 

515 def test_InstanceCatalog_against_catalog(self): 

516 """ 

517 Test that we can reproduce the validated data using the 

518 InstanceCatalog framework 

519 """ 

520 

521 obs = ObservationMetaData(pointingRA=53.00913847303155535, 

522 pointingDec=-27.43894880881512321, 

523 rotSkyPos=256.75075318193080420, 

524 mjd=59580.13955500000156462, 

525 bandpassName='r', 

526 site=Site(name="LSST", pressure=0.0, 

527 humidity=0.0)) 

528 

529 data_dir = os.path.join(getPackageDir('sims_catUtils'),'tests', 

530 'testData') 

531 

532 dtype = np.dtype([('id', int), ('ra', float), ('dec', float), 

533 ('ra_deprecessed', float), ('dec_deprecessed', float), 

534 ('x_dm', float), ('y_dm', float), 

535 ('x_focal', float), ('y_focal', float), 

536 ('x_cam', float), ('y_cam', float)]) 

537 

538 data = np.genfromtxt(os.path.join(data_dir, 

539 'pixel_prediction_catalog.txt'), 

540 dtype=dtype) 

541 

542 data_txt_file = tempfile.mktemp(dir=data_dir, 

543 prefix='ic_validation_cat', 

544 suffix='.txt') 

545 

546 cat_dtype = np.dtype([('id', int), 

547 ('raJ2000', float), ('decJ2000', float)]) 

548 

549 with open(data_txt_file, 'w') as out_file: 

550 out_file.write('# a header\n') 

551 for ii, rr, dd in zip(data['id'], 

552 np.radians(data['ra']), 

553 np.radians(data['dec'])): 

554 

555 out_file.write('%d %.17f %.17f\n' % (ii, rr, dd)) 

556 

557 db = fileDBObject(data_txt_file, idColKey='id', dtype=cat_dtype, 

558 delimiter=' ') 

559 

560 class DeprecessionTestCatalog(PhoSimCatalogPoint): 

561 def get_uniqueId(self): 

562 return self.column_by_name('id') 

563 

564 def get_properMotionRa(self): 

565 return np.zeros(len(self.column_by_name('raJ2000'))) 

566 

567 def get_properMotionDec(self): 

568 return np.zeros(len(self.column_by_name('raJ2000'))) 

569 

570 def get_radialVelocity(self): 

571 return np.zeros(len(self.column_by_name('raJ2000'))) 

572 

573 def get_parallax(self): 

574 return np.zeros(len(self.column_by_name('raJ2000'))) 

575 

576 def get_galacticAv(self): 

577 return np.zeros(len(self.column_by_name('raJ2000'))) 

578 

579 def get_galacticRv(self): 

580 return 3.1*np.ones(len(self.column_by_name('raJ2000'))) 

581 

582 def get_sedFilepath(self): 

583 return np.array(['sed_flat.txt.gz']*len(self.column_by_name('raJ2000'))) 

584 

585 def get_phoSimMagNorm(self): 

586 return np.ones(len(self.column_by_name('raJ2000'))) 

587 

588 cat = DeprecessionTestCatalog(db, obs_metadata=obs) 

589 cat.phoSimHeaderMap = DefaultPhoSimHeaderMap 

590 

591 id_list = [] 

592 ra_dep_list = [] 

593 dec_dep_list= [] 

594 

595 phosim_cat_name = tempfile.mktemp(dir=data_dir, 

596 prefix='phosim_dep', 

597 suffix='.txt') 

598 

599 cat.write_catalog(phosim_cat_name) 

600 with open(phosim_cat_name, 'r') as input_file: 

601 for line in input_file: 

602 params = line.strip().split() 

603 if len(params) < 3: 

604 continue 

605 id_list.append(int(params[1])) 

606 ra_dep_list.append(float(params[2])) 

607 dec_dep_list.append(float(params[3])) 

608 

609 id_list = np.array(id_list) 

610 np.testing.assert_array_equal(id_list, data['id']) 

611 ra_dep_list = np.array(ra_dep_list) 

612 dec_dep_list = np.array(dec_dep_list) 

613 

614 dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], 

615 ra_dep_list, dec_dep_list) 

616 

617 self.assertLess(dd.max(), 1.0e-5) 

618 

619 if os.path.exists(data_txt_file): 

620 os.unlink(data_txt_file) 

621 

622 if os.path.exists(phosim_cat_name): 

623 os.unlink(phosim_cat_name) 

624 

625 def test_InstanceCatalog_against_catalog_chunks(self): 

626 """ 

627 Test that we can reproduce the validated data using the 

628 InstanceCatalog framework when the catalog must be written 

629 in multiple chunks 

630 """ 

631 

632 obs = ObservationMetaData(pointingRA=53.00913847303155535, 

633 pointingDec=-27.43894880881512321, 

634 rotSkyPos=256.75075318193080420, 

635 mjd=59580.13955500000156462, 

636 bandpassName='r', 

637 site=Site(name="LSST", pressure=0.0, 

638 humidity=0.0)) 

639 

640 data_dir = os.path.join(getPackageDir('sims_catUtils'),'tests', 

641 'testData') 

642 

643 dtype = np.dtype([('id', int), ('ra', float), ('dec', float), 

644 ('ra_deprecessed', float), ('dec_deprecessed', float), 

645 ('x_dm', float), ('y_dm', float), 

646 ('x_focal', float), ('y_focal', float), 

647 ('x_cam', float), ('y_cam', float)]) 

648 

649 data = np.genfromtxt(os.path.join(data_dir, 

650 'pixel_prediction_catalog.txt'), 

651 dtype=dtype) 

652 

653 data_txt_file = tempfile.mktemp(dir=data_dir, 

654 prefix='ic_validation_cat', 

655 suffix='.txt') 

656 

657 cat_dtype = np.dtype([('id', int), 

658 ('raJ2000', float), ('decJ2000', float)]) 

659 

660 with open(data_txt_file, 'w') as out_file: 

661 out_file.write('# a header\n') 

662 for ii, rr, dd in zip(data['id'], 

663 np.radians(data['ra']), 

664 np.radians(data['dec'])): 

665 

666 out_file.write('%d %.17f %.17f\n' % (ii, rr, dd)) 

667 

668 db = fileDBObject(data_txt_file, idColKey='id', dtype=cat_dtype, 

669 delimiter=' ') 

670 

671 class DeprecessionTestCatalog_chunks(PhoSimCatalogPoint): 

672 def get_uniqueId(self): 

673 return self.column_by_name('id') 

674 

675 def get_properMotionRa(self): 

676 return np.zeros(len(self.column_by_name('raJ2000'))) 

677 

678 def get_properMotionDec(self): 

679 return np.zeros(len(self.column_by_name('raJ2000'))) 

680 

681 def get_radialVelocity(self): 

682 return np.zeros(len(self.column_by_name('raJ2000'))) 

683 

684 def get_parallax(self): 

685 return np.zeros(len(self.column_by_name('raJ2000'))) 

686 

687 def get_galacticAv(self): 

688 return np.zeros(len(self.column_by_name('raJ2000'))) 

689 

690 def get_galacticRv(self): 

691 return 3.1*np.ones(len(self.column_by_name('raJ2000'))) 

692 

693 def get_sedFilepath(self): 

694 return np.array(['sed_flat.txt.gz']*len(self.column_by_name('raJ2000'))) 

695 

696 def get_phoSimMagNorm(self): 

697 return np.ones(len(self.column_by_name('raJ2000'))) 

698 

699 cat = DeprecessionTestCatalog_chunks(db, obs_metadata=obs) 

700 cat.phoSimHeaderMap = DefaultPhoSimHeaderMap 

701 

702 id_list = [] 

703 ra_dep_list = [] 

704 dec_dep_list= [] 

705 

706 phosim_cat_name = tempfile.mktemp(dir=data_dir, 

707 prefix='phosim_dep', 

708 suffix='.txt') 

709 

710 cat.write_catalog(phosim_cat_name, chunk_size=10) 

711 with open(phosim_cat_name, 'r') as input_file: 

712 for line in input_file: 

713 params = line.strip().split() 

714 if len(params) < 3: 

715 continue 

716 id_list.append(int(params[1])) 

717 ra_dep_list.append(float(params[2])) 

718 dec_dep_list.append(float(params[3])) 

719 

720 id_list = np.array(id_list) 

721 np.testing.assert_array_equal(id_list, data['id']) 

722 ra_dep_list = np.array(ra_dep_list) 

723 dec_dep_list = np.array(dec_dep_list) 

724 

725 dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], 

726 ra_dep_list, dec_dep_list) 

727 

728 self.assertLess(dd.max(), 1.0e-5) 

729 

730 if os.path.exists(data_txt_file): 

731 os.unlink(data_txt_file) 

732 

733 if os.path.exists(phosim_cat_name): 

734 os.unlink(phosim_cat_name) 

735 

736 

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

738 pass 

739 

740 

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

742 lsst.utils.tests.init() 

743 unittest.main()