Coverage for tests/testPhoSimAstrometry.py : 18%

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
lsst.utils.tests.init()
""" Convert Observed RA, Dec into pupil coordinates
Parameters ---------- ra_obs is the observed RA in radians
dec_obs is the observed Dec in radians
obs_metadata is an ObservationMetaData characterizing the telescope location and pointing
epoch is the epoch of the mean RA and Dec in julian years (default=2000.0)
includeRefraction is a boolean controlling the application of refraction.
Returns -------- A numpy array whose first row is the x coordinate on the pupil in radians and whose second row is the y coordinate in radians """
are_arrays = _validate_inputs([ra_obs, dec_obs], ['ra_obs', 'dec_obs'], "pupilCoordsFromObserved")
theta = -1.0*rotSkyPos
ra_pointing = ra0 dec_pointing = dec0
# palpy.ds2tp performs the gnomonic projection on ra_in and dec_in # with a tangent point at (pointingRA, pointingDec) # if not are_arrays: try: x, y = palpy.ds2tp(ra_obs, dec_obs, ra_pointing, dec_pointing) except: x = np.NaN y = np.NaN else: try: x, y = palpy.ds2tpVector(ra_obs, dec_obs, ra_pointing, dec_pointing) except: # apparently, one of your ra/dec values was improper; we will have to do this # element-wise, putting NaN in the place of the bad values x = [] y = [] for rr, dd in zip(ra_obs, dec_obs): try: xx, yy = palpy.ds2tp(rr, dd, ra_pointing, dec_pointing) except: xx = np.NaN yy = np.NaN x.append(xx) y.append(yy) x = np.array(x) y = np.array(y)
# rotate the result by rotskypos (rotskypos being "the angle of the sky relative to # camera coordinates" according to phoSim documentation) to account for # the rotation of the focal plane about the telescope pointing
x_out = x*np.cos(theta) - y*np.sin(theta) y_out = x*np.sin(theta) + y*np.cos(theta)
return np.array([x_out, y_out])
'raJ2000', 'decJ2000', 'properMotionRa','properMotionDec', 'parallax', 'radialVelocity']
'raPhoSim': np.degrees, 'decPhoSim': np.degrees}
'raPhoSim': np.degrees, 'decPhoSim': np.degrees}
'raPhoSim': '%.12g', 'decPhoSim': '%.12g'}
def setUpClass(cls): cls.db_name = tempfile.mktemp(dir=ROOT, prefix='PhoSimAstDB', suffix='.db') cls.obs = makePhoSimTestDB(filename=cls.db_name, size=1000)
def tearDownClass(cls): sims_clean_up() if os.path.exists(cls.db_name): os.unlink(cls.db_name)
clean_up_lsst_camera()
""" Test that we can go from raPhoSim, decPhoSim to ICRS coordinates in the case of stars (in radians) """ db = testStarsDBObj(driver='sqlite', database=self.db_name) cat = StarTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float), ('raJ2000', float), ('decJ2000', float), ('pmRA', float), ('pmDec', float), ('parallax', float), ('vRad', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100) ra_pho_rad = np.radians(data['raPhoSim']) dec_pho_rad = np.radians(data['decPhoSim'])
# verify that, when transforming back to ICRS, we are within # 10^-3 arcsec ra_icrs, dec_icrs = PhoSimAstrometryBase._icrsFromPhoSim(ra_pho_rad, dec_pho_rad, self.obs) dist = _angularSeparation(np.radians(data['raICRS']), np.radians(data['decICRS']), ra_icrs, dec_icrs)
dist = arcsecFromRadians(dist) self.assertLess(dist.max(), 0.001)
# verify that the distance between raPhoSim, decPhoSim and # raICRS, decICRS is greater than the distance between # the original raICRS, decICRS and the newly-calculated # raICRS, decICRS dist_bad = _angularSeparation(ra_pho_rad, dec_pho_rad, np.radians(data['raICRS']), np.radians(data['decICRS']))
dist_bad = arcsecFromRadians(dist_bad) self.assertGreater(dist_bad.min(), dist.max())
del db
""" Test that we can go from raPhoSim, decPhoSim to ICRS coordinates in the case of stars (in degrees) """ db = testStarsDBObj(driver='sqlite', database=self.db_name) cat = StarTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float), ('raJ2000', float), ('decJ2000', float), ('pmRA', float), ('pmDec', float), ('parallax', float), ('vRad', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100)
# verify that, when transforming back to ICRS, we are within # 10^-3 arcsec ra_icrs, dec_icrs = PhoSimAstrometryBase.icrsFromPhoSim(data['raPhoSim'], data['decPhoSim'], self.obs) dist = angularSeparation(data['raICRS'], data['decICRS'], ra_icrs, dec_icrs)
dist = 3600.0*dist self.assertLess(dist.max(), 0.001)
# verify that the distance between raPhoSim, decPhoSim and # raICRS, decICRS is greater than the distance between # the original raICRS, decICRS and the newly-calculated # raICRS, decICRS dist_bad = angularSeparation(data['raPhoSim'], data['decPhoSim'], data['raICRS'], data['decICRS'])
dist_bad = 3600.0*dist_bad self.assertGreater(dist_bad.min(), dist.max())
del db
""" Test ability to go all the way to observed RA, Dec from PhoSim (this is necessary for the ImSim software that DESC is working on) """ db = testStarsDBObj(driver='sqlite', database=self.db_name) cat = StarTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float), ('raJ2000', float), ('decJ2000', float), ('pmRA', float), ('pmDec', float), ('parallax', float), ('vRad', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100)
(ra_obs, dec_obs) = _observedFromICRS(data['raJ2000'], data['decJ2000'], obs_metadata=self.obs, pm_ra=data['pmRA'], pm_dec=data['pmDec'], parallax=data['parallax'], v_rad=data['vRad'], includeRefraction=True, epoch=2000.0)
(ra_appGeo, dec_appGeo) = PhoSimAstrometryBase._appGeoFromPhoSim(np.radians(data['raPhoSim']), np.radians(data['decPhoSim']), self.obs)
(ra_obs_2, dec_obs_2) = _observedFromAppGeo(ra_appGeo, dec_appGeo, obs_metadata=self.obs, includeRefraction=True)
dd = arcsecFromRadians(_angularSeparation(ra_obs, dec_obs, ra_obs_2, dec_obs_2)) self.assertLess(dd.max(), 1.0e-5)
""" Test ability to go all the way to observed RA, Dec from PhoSim (this is necessary for the ImSim software that DESC is working on) """ db = testStarsDBObj(driver='sqlite', database=self.db_name) cat = StarTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float), ('raJ2000', float), ('decJ2000', float), ('pmRA', float), ('pmDec', float), ('parallax', float), ('vRad', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100)
(ra_obs, dec_obs) = observedFromICRS(np.degrees(data['raJ2000']), np.degrees(data['decJ2000']), obs_metadata=self.obs, pm_ra=arcsecFromRadians(data['pmRA']), pm_dec=arcsecFromRadians(data['pmDec']), parallax=arcsecFromRadians(data['parallax']), v_rad=data['vRad'], includeRefraction=True, epoch=2000.0)
(ra_appGeo, dec_appGeo) = PhoSimAstrometryBase.appGeoFromPhoSim(data['raPhoSim'], data['decPhoSim'], self.obs)
(ra_obs_2, dec_obs_2) = observedFromAppGeo(ra_appGeo, dec_appGeo, obs_metadata=self.obs, includeRefraction=True)
dd = 3600.0*angularSeparation(ra_obs, dec_obs, ra_obs_2, dec_obs_2) self.assertLess(dd.max(), 1.0e-5)
""" Test that we can go from raPhoSim, decPhoSim to ICRS coordinates in the case of galaxies (in radians) """ db = testGalaxyDiskDBObj(driver='sqlite', database=self.db_name) cat = GalaxyTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100) ra_pho_rad = np.radians(data['raPhoSim']) dec_pho_rad = np.radians(data['decPhoSim'])
# verify that, when transforming back to ICRS, we are within # 10^-3 arcsec ra_icrs, dec_icrs = PhoSimAstrometryBase._icrsFromPhoSim(ra_pho_rad, dec_pho_rad, self.obs) dist = _angularSeparation(np.radians(data['raICRS']), np.radians(data['decICRS']), ra_icrs, dec_icrs)
dist = arcsecFromRadians(dist) self.assertLess(dist.max(), 0.001)
# verify that the distance between raPhoSim, decPhoSim and # raICRS, decICRS is greater than the distance between # the original raICRS, decICRS and the newly-calculated # raICRS, decICRS dist_bad = _angularSeparation(ra_pho_rad, dec_pho_rad, np.radians(data['raICRS']), np.radians(data['decICRS']))
dist_bad = arcsecFromRadians(dist_bad) self.assertGreater(dist_bad.min(), dist.max())
del db
""" Test that we can go from raPhoSim, decPhoSim to ICRS coordinates in the case of galaxies (in degrees) """ db = testGalaxyDiskDBObj(driver='sqlite', database=self.db_name) cat = GalaxyTestCatalog(db, obs_metadata=self.obs) with lsst.utils.tests.getTempFilePath('.txt') as cat_name: cat.write_catalog(cat_name) dtype = np.dtype([('raICRS', float), ('decICRS', float), ('raPhoSim', float), ('decPhoSim', float)]) data = np.genfromtxt(cat_name, dtype=dtype) self.assertGreater(len(data), 100)
# verify that, when transforming back to ICRS, we are within # 10^-3 arcsec ra_icrs, dec_icrs = PhoSimAstrometryBase.icrsFromPhoSim(data['raPhoSim'], data['decPhoSim'], self.obs) dist = angularSeparation(data['raICRS'], data['decICRS'], ra_icrs, dec_icrs)
dist = 3600.0*dist self.assertLess(dist.max(), 0.001)
# verify that the distance between raPhoSim, decPhoSim and # raICRS, decICRS is greater than the distance between # the original raICRS, decICRS and the newly-calculated # raICRS, decICRS dist_bad = angularSeparation(data['raPhoSim'], data['decPhoSim'], data['raICRS'], data['decICRS'])
dist_bad = 3600.0*dist_bad self.assertGreater(dist_bad.min(), dist.max())
del db
""" Test deprecession of PhoSim coordinates by comparing the focal plane position predicted by CatSim from ICRS with the focal plane position predicted by CatSim from deprecessed coordinates. """
phosim_mixin = PhoSimAstrometryBase()
mjd = 59587.2
# create site with no atmosphere so that we can avoid # refraction site = Site(name="LSST", pressure=0.0, humidity=0.0)
obs = ObservationMetaData(mjd=mjd, site=site) ra, dec = raDecFromAltAz(31.0, 112.0, obs)
d_sun = distanceToSun(ra, dec, obs.mjd) self.assertGreater(d_sun, 45.0)
obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, rotSkyPos=27.3, mjd=mjd, site=site) ra_icrs = np.arange(obs.pointingRA-2.0, obs.pointingRA+2.0, 0.05) dec_icrs = np.arange(obs.pointingDec-2.0, obs.pointingDec+2.0, 0.05)
coord_grid = np.meshgrid(ra_icrs, dec_icrs) ra_icrs = coord_grid[0].flatten() dec_icrs = coord_grid[1].flatten()
(xpup_icrs, ypup_icrs) = pupilCoordsFromRaDec(ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=False)
(x_focal_icrs, y_focal_icrs) = focalPlaneCoordsFromPupilCoords(xpup_icrs, ypup_icrs, camera=lsst_camera())
ra_obs, dec_obs = observedFromICRS(ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=False)
ra_obs_rad = np.radians(ra_obs) dec_obs_rad = np.radians(dec_obs)
(ra_deprecessed_rad, dec_deprecessed_rad) = phosim_mixin._dePrecess(ra_obs_rad, dec_obs_rad, obs)
(xpup_deprecessed, ypup_deprecessed) = _naivePupilCoordsFromObserved(ra_deprecessed_rad, dec_deprecessed_rad, obs._pointingRA, obs._pointingDec, obs._rotSkyPos)
(x_focal_deprecessed, y_focal_deprecessed) = focalPlaneCoordsFromPupilCoords(xpup_deprecessed, ypup_deprecessed, camera=lsst_camera())
dd = np.sqrt((x_focal_icrs-x_focal_deprecessed)**2 +(y_focal_icrs-y_focal_deprecessed)**2)
self.assertLess(dd.max(), 5.0e-8)
""" Compare deprecession results to a catalog that was validated with PhoSim. """ obs = ObservationMetaData(pointingRA=53.00913847303155535, pointingDec=-27.43894880881512321, rotSkyPos=256.75075318193080420, mjd=59580.13955500000156462, site=Site(name="LSST", pressure=0.0, humidity=0.0))
dtype = np.dtype([('id', int), ('ra', float), ('dec', float), ('ra_deprecessed', float), ('dec_deprecessed', float), ('x_dm', float), ('y_dm', float), ('x_focal', float), ('y_focal', float), ('x_cam', float), ('y_cam', float)])
data = np.genfromtxt(os.path.join(getPackageDir('sims_catUtils'), 'tests', 'testData', 'pixel_prediction_catalog.txt'), dtype=dtype)
ra_obs, dec_obs = observedFromICRS(data['ra'], data['dec'], obs_metadata=obs, includeRefraction=False, epoch=2000.0)
phosim_mixin = PhoSimAstrometryBase() ra_dep, dec_dep = phosim_mixin._dePrecess(np.radians(ra_obs), np.radians(dec_obs), obs)
dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], np.degrees(ra_dep), np.degrees(dec_dep)) self.assertLess(dd.max(), 1.0e-5)
""" Test that we can reproduce the validated data using the InstanceCatalog framework """
obs = ObservationMetaData(pointingRA=53.00913847303155535, pointingDec=-27.43894880881512321, rotSkyPos=256.75075318193080420, mjd=59580.13955500000156462, bandpassName='r', site=Site(name="LSST", pressure=0.0, humidity=0.0))
data_dir = os.path.join(getPackageDir('sims_catUtils'),'tests', 'testData')
dtype = np.dtype([('id', int), ('ra', float), ('dec', float), ('ra_deprecessed', float), ('dec_deprecessed', float), ('x_dm', float), ('y_dm', float), ('x_focal', float), ('y_focal', float), ('x_cam', float), ('y_cam', float)])
data = np.genfromtxt(os.path.join(data_dir, 'pixel_prediction_catalog.txt'), dtype=dtype)
data_txt_file = tempfile.mktemp(dir=data_dir, prefix='ic_validation_cat', suffix='.txt')
cat_dtype = np.dtype([('id', int), ('raJ2000', float), ('decJ2000', float)])
with open(data_txt_file, 'w') as out_file: out_file.write('# a header\n') for ii, rr, dd in zip(data['id'], np.radians(data['ra']), np.radians(data['dec'])):
out_file.write('%d %.17f %.17f\n' % (ii, rr, dd))
db = fileDBObject(data_txt_file, idColKey='id', dtype=cat_dtype, delimiter=' ')
class DeprecessionTestCatalog(PhoSimCatalogPoint): def get_uniqueId(self): return self.column_by_name('id')
def get_properMotionRa(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_properMotionDec(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_radialVelocity(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_parallax(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_galacticAv(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_galacticRv(self): return 3.1*np.ones(len(self.column_by_name('raJ2000')))
def get_sedFilepath(self): return np.array(['sed_flat.txt.gz']*len(self.column_by_name('raJ2000')))
def get_phoSimMagNorm(self): return np.ones(len(self.column_by_name('raJ2000')))
cat = DeprecessionTestCatalog(db, obs_metadata=obs) cat.phoSimHeaderMap = DefaultPhoSimHeaderMap
id_list = [] ra_dep_list = [] dec_dep_list= []
phosim_cat_name = tempfile.mktemp(dir=data_dir, prefix='phosim_dep', suffix='.txt')
cat.write_catalog(phosim_cat_name) with open(phosim_cat_name, 'r') as input_file: for line in input_file: params = line.strip().split() if len(params) < 3: continue id_list.append(int(params[1])) ra_dep_list.append(float(params[2])) dec_dep_list.append(float(params[3]))
id_list = np.array(id_list) np.testing.assert_array_equal(id_list, data['id']) ra_dep_list = np.array(ra_dep_list) dec_dep_list = np.array(dec_dep_list)
dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], ra_dep_list, dec_dep_list)
self.assertLess(dd.max(), 1.0e-5)
if os.path.exists(data_txt_file): os.unlink(data_txt_file)
if os.path.exists(phosim_cat_name): os.unlink(phosim_cat_name)
""" Test that we can reproduce the validated data using the InstanceCatalog framework when the catalog must be written in multiple chunks """
obs = ObservationMetaData(pointingRA=53.00913847303155535, pointingDec=-27.43894880881512321, rotSkyPos=256.75075318193080420, mjd=59580.13955500000156462, bandpassName='r', site=Site(name="LSST", pressure=0.0, humidity=0.0))
data_dir = os.path.join(getPackageDir('sims_catUtils'),'tests', 'testData')
dtype = np.dtype([('id', int), ('ra', float), ('dec', float), ('ra_deprecessed', float), ('dec_deprecessed', float), ('x_dm', float), ('y_dm', float), ('x_focal', float), ('y_focal', float), ('x_cam', float), ('y_cam', float)])
data = np.genfromtxt(os.path.join(data_dir, 'pixel_prediction_catalog.txt'), dtype=dtype)
data_txt_file = tempfile.mktemp(dir=data_dir, prefix='ic_validation_cat', suffix='.txt')
cat_dtype = np.dtype([('id', int), ('raJ2000', float), ('decJ2000', float)])
with open(data_txt_file, 'w') as out_file: out_file.write('# a header\n') for ii, rr, dd in zip(data['id'], np.radians(data['ra']), np.radians(data['dec'])):
out_file.write('%d %.17f %.17f\n' % (ii, rr, dd))
db = fileDBObject(data_txt_file, idColKey='id', dtype=cat_dtype, delimiter=' ')
class DeprecessionTestCatalog_chunks(PhoSimCatalogPoint): def get_uniqueId(self): return self.column_by_name('id')
def get_properMotionRa(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_properMotionDec(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_radialVelocity(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_parallax(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_galacticAv(self): return np.zeros(len(self.column_by_name('raJ2000')))
def get_galacticRv(self): return 3.1*np.ones(len(self.column_by_name('raJ2000')))
def get_sedFilepath(self): return np.array(['sed_flat.txt.gz']*len(self.column_by_name('raJ2000')))
def get_phoSimMagNorm(self): return np.ones(len(self.column_by_name('raJ2000')))
cat = DeprecessionTestCatalog_chunks(db, obs_metadata=obs) cat.phoSimHeaderMap = DefaultPhoSimHeaderMap
id_list = [] ra_dep_list = [] dec_dep_list= []
phosim_cat_name = tempfile.mktemp(dir=data_dir, prefix='phosim_dep', suffix='.txt')
cat.write_catalog(phosim_cat_name, chunk_size=10) with open(phosim_cat_name, 'r') as input_file: for line in input_file: params = line.strip().split() if len(params) < 3: continue id_list.append(int(params[1])) ra_dep_list.append(float(params[2])) dec_dep_list.append(float(params[3]))
id_list = np.array(id_list) np.testing.assert_array_equal(id_list, data['id']) ra_dep_list = np.array(ra_dep_list) dec_dep_list = np.array(dec_dep_list)
dd = 3600.0*angularSeparation(data['ra_deprecessed'], data['dec_deprecessed'], ra_dep_list, dec_dep_list)
self.assertLess(dd.max(), 1.0e-5)
if os.path.exists(data_txt_file): os.unlink(data_txt_file)
if os.path.exists(phosim_cat_name): os.unlink(phosim_cat_name)
lsst.utils.tests.init() unittest.main() |