def testRaDecFromAltAz(self): azList = self.raList altList = self.decList mjd = 47895.6 obs = ObservationMetaData(mjd=mjd, site=Site(longitude=self.lon, latitude=self.lat, name='LSST')) raRad, decRad = utils._raDecFromAltAz(altList, azList, obs) raDeg, decDeg = utils.raDecFromAltAz(np.degrees(altList), np.degrees(azList), obs) np.testing.assert_array_almost_equal(raRad, np.radians(raDeg), 10) np.testing.assert_array_almost_equal(decRad, np.radians(decDeg), 10) raRad, decRad = utils._raDecFromAltAz(altList, azList, obs) raDeg, decDeg = utils.raDecFromAltAz(np.degrees(altList), np.degrees(azList), obs) np.testing.assert_array_almost_equal(raRad, np.radians(raDeg), 10) np.testing.assert_array_almost_equal(decRad, np.radians(decDeg), 10) for alt, az in zip(altList, azList): raRad, decRad = utils._raDecFromAltAz(alt, az, obs) raDeg, decDeg = utils.raDecFromAltAz(np.degrees(alt), np.degrees(az), obs) self.assertAlmostEqual(raRad, np.radians(raDeg), 10) self.assertAlmostEqual(decRad, np.radians(decDeg), 10)
def testExceptions(self): """ Test to make sure that methods complain when incorrect data types are passed. """ obs = utils.ObservationMetaData(pointingRA=55.0, pointingDec=-72.0, mjd=53467.8) raFloat = 1.1 raList = np.array([0.2, 0.3]) decFloat = 1.1 decList = np.array([0.2, 0.3]) self.assertRaises(RuntimeError, utils._altAzPaFromRaDec, raList, decFloat, obs) self.assertRaises(RuntimeError, utils._altAzPaFromRaDec, raFloat, decList, obs) utils._altAzPaFromRaDec(raFloat, decFloat, obs) utils._altAzPaFromRaDec(raList, decList, obs) self.assertRaises(RuntimeError, utils._raDecFromAltAz, raList, decFloat, obs) self.assertRaises(RuntimeError, utils._raDecFromAltAz, raFloat, decList, obs) utils._raDecFromAltAz(raFloat, decFloat, obs) utils._raDecFromAltAz(raList, decList, obs) self.assertRaises(RuntimeError, utils.altAzPaFromRaDec, raList, decFloat, obs) self.assertRaises(RuntimeError, utils.altAzPaFromRaDec, raFloat, decList, obs) utils.altAzPaFromRaDec(raFloat, decFloat, obs) utils.altAzPaFromRaDec(raList, decList, obs) self.assertRaises(RuntimeError, utils.raDecFromAltAz, raList, decFloat, obs) self.assertRaises(RuntimeError, utils.raDecFromAltAz, raFloat, decList, obs) utils.raDecFromAltAz(raFloat, decFloat, obs) utils.raDecFromAltAz(raList, decList, obs)
def test_raDecFromAltAz_noref(self): """ test that raDecFromAltAz correctly inverts altAzPaFromRaDec, even when refraction is turned off """ rng = np.random.RandomState(55) n_samples = 10 n_batches = 10 for i_batch in range(n_batches): d_sun = 0.0 while d_sun < 45.0: # because ICRS->Observed transformation breaks down close to the sun alt_in = rng.random_sample(n_samples)*50.0 + 20.0 az_in = rng.random_sample(n_samples)*360.0 obs = utils.ObservationMetaData(mjd=43000.0) ra_in, dec_in = utils.raDecFromAltAz(alt_in, az_in, obs=obs, includeRefraction=False) d_sun = utils.distanceToSun(ra_in, dec_in, obs.mjd).min() alt_out, az_out, pa_out = utils.altAzPaFromRaDec(ra_in, dec_in, obs=obs, includeRefraction=False) dd = utils.haversine(np.radians(alt_out), np.radians(az_out), np.radians(alt_in), np.radians(az_in)) self.assertLess(utils.arcsecFromRadians(dd).max(), 0.01)
def test_raDecAltAz_noRefraction_degVsRadians(self): """ Check that raDecFromAltAz and altAzPaFromRaDec are consistent in a degrees-versus-radians sense when refraction is turned off """ rng = np.random.RandomState(34) n_samples = 10 ra_in = rng.random_sample(n_samples)*360.0 dec_in = rng.random_sample(n_samples)*180.0 - 90.0 mjd = 43000.0 obs = utils.ObservationMetaData(mjd=mjd) alt, az, pa = utils.altAzPaFromRaDec(ra_in, dec_in, obs, includeRefraction=False) alt_rad, az_rad, pa_rad = utils._altAzPaFromRaDec(np.radians(ra_in), np.radians(dec_in), obs, includeRefraction=False) distance = utils.haversine(az_rad, alt_rad, np.radians(az), np.radians(alt)) self.assertLess(utils.arcsecFromRadians(distance).min(), 0.001) np.testing.assert_array_almost_equal(pa, np.degrees(pa_rad), decimal=12) ra, dec = utils.raDecFromAltAz(alt, az, obs, includeRefraction=False) ra_rad, dec_rad = utils._raDecFromAltAz(alt_rad, az_rad, obs, includeRefraction=False) distance = utils.haversine(ra_rad, dec_rad, np.radians(ra), np.radians(dec)) self.assertLess(utils.arcsecFromRadians(distance).min(), 0.001)
def testAltAzRADecRoundTrip(self): """ Test that altAzPaFromRaDec and raDecFromAltAz really invert each other """ mjd = 58350.0 alt_in = [] az_in = [] for alt in np.arange(0.0, 90.0, 10.0): for az in np.arange(0.0, 360.0, 10.0): alt_in.append(alt) az_in.append(az) alt_in = np.array(alt_in) az_in = np.array(az_in) for lon in (0.0, 90.0, 135.0): for lat in (60.0, 30.0, -60.0, -30.0): obs = utils.ObservationMetaData(mjd=mjd, site=utils.Site(longitude=lon, latitude=lat, name='LSST')) ra_in, dec_in = utils.raDecFromAltAz(alt_in, az_in, obs) self.assertIsInstance(ra_in, np.ndarray) self.assertIsInstance(dec_in, np.ndarray) self.assertFalse(np.isnan(ra_in).any(), msg='there were NaNs in ra_in') self.assertFalse(np.isnan(dec_in).any(), msg='there were NaNs in dec_in') # test that passing them in one at a time gives the same answer for ix in range(len(alt_in)): ra_f, dec_f = utils.raDecFromAltAz(alt_in[ix], az_in[ix], obs) self.assertIsInstance(ra_f, np.float) self.assertIsInstance(dec_f, np.float) self.assertAlmostEqual(ra_f, ra_in[ix], 12) self.assertAlmostEqual(dec_f, dec_in[ix], 12) alt_out, az_out, pa_out = utils.altAzPaFromRaDec(ra_in, dec_in, obs) self.assertFalse(np.isnan(pa_out).any(), msg='there were NaNs in pa_out') for alt_c, az_c, alt_t, az_t in \ zip(np.radians(alt_in), np.radians(az_in), np.radians(alt_out), np.radians(az_out)): distance = utils.arcsecFromRadians(utils.haversine(az_c, alt_c, az_t, alt_t)) self.assertLess(distance, 0.2)
def test_altAzPaFromRaDec_no_refraction(self): """ Test that altAzPaFromRaDec gives a sane answer when you turn off refraction. """ rng = np.random.RandomState(44) n_samples = 10 n_batches = 10 for i_batch in range(n_batches): # first, generate some sane RA, Dec values by generating sane # Alt, Az values with refraction and converting them into # RA, Dec alt_sane = rng.random_sample(n_samples)*45.0 + 45.0 az_sane = rng.random_sample(n_samples)*360.0 mjd_input = rng.random_sample(n_samples)*10000.0 + 40000.0 mjd_list = utils.ModifiedJulianDate.get_list(TAI=mjd_input) ra_sane = [] dec_sane = [] obs_sane = [] for alt, az, mjd in zip(alt_sane, az_sane, mjd_list): obs = utils.ObservationMetaData(mjd=mjd) ra, dec = utils.raDecFromAltAz(alt, az, obs) ra_sane.append(ra) dec_sane.append(dec) obs_sane.append(obs) # Now, loop over our refracted RA, Dec, Alt, Az values. # Convert from RA, Dec to unrefracted Alt, Az. Then, apply refraction # with our applyRefraction method. Check that the resulting refracted # zenith distance is: # 1) within 0.1 arcsec of the zenith distance of the already refracted # alt value calculated above # # 2) closer to the zenith distance calculated above than to the # unrefracted zenith distance for ra, dec, obs, alt_ref, az_ref in \ zip(ra_sane, dec_sane, obs_sane, alt_sane, az_sane): alt, az, pa = utils.altAzPaFromRaDec(ra, dec, obs, includeRefraction = False) tanz, tanz3 = utils.refractionCoefficients(site=obs.site) refracted_zd = utils.applyRefraction(np.radians(90.0-alt), tanz, tanz3) # Check that the two independently refracted zenith distances agree # to within 0.1 arcsec self.assertLess(np.abs(utils.arcsecFromRadians(refracted_zd) - utils.arcsecFromRadians(np.radians(90.0-alt_ref))), 0.1) # Check that the two refracted zenith distances are closer to each other # than to the unrefracted zenith distance self.assertLess(np.abs(np.degrees(refracted_zd)-(90.0-alt_ref)), np.abs((90.0-alt_ref) - (90.0-alt))) self.assertLess(np.abs(np.degrees(refracted_zd)-(90.0-alt_ref)), np.abs(np.degrees(refracted_zd) - (90.0-alt)))
def test_appGeoFromObserved(self): """ Test that _appGeoFromObserved really does invert _observedFromAppGeo """ mjd = 58350.0 site = Site(longitude=numpy.degrees(0.235), latitude=numpy.degrees(-1.2), name='LSST') raCenter, decCenter = raDecFromAltAz(90.0, 0.0, ObservationMetaData(mjd=mjd, site=site)) obs = ObservationMetaData(pointingRA=raCenter, pointingDec=decCenter, mjd=ModifiedJulianDate(TAI=58350.0), site=site) numpy.random.seed(125543) nSamples = 200 # Note: the PALPY routines in question start to become inaccurate at # a zenith distance of about 75 degrees, so we restrict our test points # to be within 50 degrees of the telescope pointing, which is at zenith # in a flat sky approximation rr = numpy.random.random_sample(nSamples)*numpy.radians(50.0) theta = numpy.random.random_sample(nSamples)*2.0*numpy.pi ra_in = numpy.radians(raCenter) + rr*numpy.cos(theta) dec_in = numpy.radians(decCenter) + rr*numpy.sin(theta) xx_in = numpy.cos(dec_in)*numpy.cos(ra_in) yy_in = numpy.cos(dec_in)*numpy.sin(ra_in) zz_in = numpy.sin(dec_in) for includeRefraction in [True, False]: for wavelength in (0.5, 0.3, 0.7): ra_obs, dec_obs = _observedFromAppGeo(ra_in, dec_in, obs_metadata=obs, wavelength=wavelength, includeRefraction=includeRefraction) ra_out, dec_out = _appGeoFromObserved(ra_obs, dec_obs, obs_metadata=obs, wavelength=wavelength, includeRefraction=includeRefraction) xx_out = numpy.cos(dec_out)*numpy.cos(ra_out) yy_out = numpy.cos(dec_out)*numpy.sin(ra_out) zz_out = numpy.sin(dec_out) distance = numpy.sqrt(numpy.power(xx_in-xx_out,2) + numpy.power(yy_in-yy_out,2) + numpy.power(zz_in-zz_out,2)) self.assertLess(distance.max(), 1.0e-12)
def examplePhoSimNoOpSim(): """ This method outputs phoSim input files based on arbitrary input coordinates (rather than an OpSim pointing). catalog_test_stars_rd.dat is a file created from a specified RA, Dec pointing catalog_test_stars_aa.dat is a file created from a specified Alt, Az pointing """ raDeg= 15. decDeg = -30. mjd = 51999.75 obs_metadata_rd = ObservationMetaData(boundType='circle', boundLength=0.1, mjd=mjd, pointingRA=raDeg, pointingDec=decDeg, rotSkyPos=22.0, bandpassName='g') az = 220.0 alt = 79.0 mjd = 55958.0 obs_dummy = ObservationMetaData(mjd=mjd) ra, dec = raDecFromAltAz(alt, az, obs_dummy) obs_metadata_aa = ObservationMetaData(boundType='circle', boundLength=0.1, mjd=mjd, rotSkyPos=22.0, pointingRA=ra, pointingDec=dec, bandpassName='g') dbobj = CatalogDBObject.from_objid('msstars') t = dbobj.getCatalog('phoSim_catalog_POINT', obs_metadata= obs_metadata_rd) t.phoSimHeaderMap = {} t.write_catalog('catalog_test_stars_rd.dat') t = dbobj.getCatalog('phoSim_catalog_POINT', obs_metadata= obs_metadata_aa) t.phoSimHeaderMap = {} t.write_catalog('catalog_test_stars_aa.dat')
def testAltAzRADecRoundTrip(self): """ Test that altAzPaFromRaDec and raDecFromAltAz really invert each other """ np.random.seed(42) mjd = 58350.0 alt_in = [] az_in = [] for alt in np.arange(0.0, 90.0, 10.0): for az in np.arange(0.0, 360.0, 10.0): alt_in.append(alt) az_in.append(az) alt_in = np.array(alt_in) az_in = np.array(az_in) for lon in (0.0, 90.0, 135.0): for lat in (60.0, 30.0, -60.0, -30.0): obs = utils.ObservationMetaData(mjd=mjd, site=utils.Site(longitude=lon, latitude=lat, name='LSST')) ra_in, dec_in = utils.raDecFromAltAz(alt_in, az_in, obs) self.assertFalse(np.isnan(ra_in).any()) self.assertFalse(np.isnan(dec_in).any()) alt_out, az_out, pa_out = utils.altAzPaFromRaDec(ra_in, dec_in, obs) self.assertFalse(np.isnan(pa_out).any()) for alt_c, az_c, alt_t, az_t in \ zip(np.radians(alt_in), np.radians(az_in), np.radians(alt_out), np.radians(az_out)): distance = utils.arcsecFromRadians(utils.haversine(az_c, alt_c, az_t, alt_t)) self.assertLess(distance, 0.2) # not sure why 0.2 arcsec is the limiting precision of this test
def makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1, displacedRA=None, displacedDec=None, bandpass=None, m5=None, seeing=None, **kwargs): """ Make a test database to storing cartoon information for the test phoSim input catalog to use. The method will return an ObservationMetaData object guaranteed to encompass the objects in this database. @param [in] filename is a string indicating the name of the DB file to be created @param [in] size is the number of objects int he database @param [in] seedVal is the seed passed to the random number generator @param [in] radius is the radius (in degrees) of the field of view to be returned @param [in] bandpass is the bandpas(es) of the observation to be passed to ObservationMetaData (optional) @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData (optional) @param [in] seeing is the seeing value(s) in arcseconds to be passed to ObservationMetaData (optional) @param [in] displacedRA/Dec are numpy arrays that indicate where (in relation to the center of the field of view) objects should be placed. These coordinates are in degrees. Specifying either of these paramters will overwrite size. If you only specify one of these parameters, the other will be set randomly. These parameters are optional. """ if os.path.exists(filename): os.unlink(filename) #just an example of some valid SED file names galaxy_seds = [ 'Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec' ] agn_sed = 'agn.spec' star_seds = [ 'km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700' ] numpy.random.seed(seedVal) if displacedRA is not None and displacedDec is not None: if len(displacedRA) != len(displacedDec): raise RuntimeError( "WARNING in makePhoSimTestDB displacedRA and displacedDec have different lengths" ) if displacedRA is not None: size = len(displacedRA) elif displacedDec is not None: size = len(displacedDec) #create the ObservationMetaData object mjd = 52000.0 alt = numpy.pi / 2.0 az = 0.0 band = 'r' testSite = Site() centerRA, centerDec = raDecFromAltAz(alt, az, testSite.longitude, testSite.latitude, mjd) rotTel = getRotTelPos(centerRA, centerDec, testSite.longitude, testSite.latitude, mjd, 0.0) obsDict = calcObsDefaults(centerRA, centerDec, alt, az, rotTel, mjd, band, testSite.longitude, testSite.latitude) obsDict['Opsim_expmjd'] = mjd phoSimMetaData = OrderedDict([ (k, (obsDict[k], numpy.dtype(type(obsDict[k])))) for k in obsDict if k != 'Opsim_filter' or bandpass is None ]) obs_metadata = ObservationMetaData(boundType='circle', boundLength=2.0 * radius, phoSimMetaData=phoSimMetaData, site=testSite, bandpassName=bandpass, m5=m5, seeing=seeing) #Now begin building the database. #First create the tables. conn = sqlite3.connect(filename) c = conn.cursor() try: c.execute('''CREATE TABLE galaxy_bulge (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real, sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int, ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''' ) conn.commit() except: raise RuntimeError("Error creating galaxy_bulge table.") try: c.execute('''CREATE TABLE galaxy (galtileid int, galid int, ra real, dec real, bra real, bdec real, dra real, ddec real, agnra real, agndec real, magnorm_bulge, magnorm_disk, magnorm_agn, sedname_bulge text, sedname_disk text, sedname_agn text, varParamStr text, a_b real, b_b real, pa_bulge real, bulge_n int, a_d real, b_d real, pa_disk real, disk_n int, ext_model_b text, av_b real, rv_b real, ext_model_d text, av_d real, rv_d real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''' ) conn.commit() except: raise RuntimeError("Error creating galaxy table.") try: c.execute('''CREATE TABLE galaxy_agn (galtileid int, galid int, agnra real, agndec real, ra real, dec real, magnorm_agn real, sedname_agn text, varParamStr text, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''' ) except: raise RuntimeError("Error creating galaxy_agn table.") try: c.execute('''CREATE TABLE starsALL_forceseek (simobjid int, ra real, decl real, magNorm real, mudecl real, mura real, galacticAv real, vrad real, varParamStr text, sedFilename text, parallax real)''' ) except: raise RuntimeError("Error creating starsALL_forceseek table.") #Now generate the data to be stored in the tables. rr = numpy.random.sample(size) * numpy.radians(radius) theta = numpy.random.sample(size) * 2.0 * numpy.pi if displacedRA is None: ra = numpy.degrees(centerRA + rr * numpy.cos(theta)) else: ra = numpy.degrees(centerRA) + displacedRA if displacedDec is None: dec = numpy.degrees(centerDec + rr * numpy.sin(theta)) else: dec = numpy.degrees(centerDec) + displacedDec bra = numpy.radians(ra + numpy.random.sample(size) * 0.01 * radius) bdec = numpy.radians(dec + numpy.random.sample(size) * 0.01 * radius) dra = numpy.radians(ra + numpy.random.sample(size) * 0.01 * radius) ddec = numpy.radians(dec + numpy.random.sample(size) * 0.01 * radius) agnra = numpy.radians(ra + numpy.random.sample(size) * 0.01 * radius) agndec = numpy.radians(dec + numpy.random.sample(size) * 0.01 * radius) magnorm_bulge = numpy.random.sample(size) * 4.0 + 17.0 magnorm_disk = numpy.random.sample(size) * 5.0 + 17.0 magnorm_agn = numpy.random.sample(size) * 5.0 + 17.0 b_b = numpy.random.sample(size) * 0.2 a_b = b_b + numpy.random.sample(size) * 0.05 b_d = numpy.random.sample(size) * 0.5 a_d = b_d + numpy.random.sample(size) * 0.1 BulgeHalfLightRadius = numpy.random.sample(size) * 0.2 DiskHalfLightRadius = numpy.random.sample(size) * 0.5 pa_bulge = numpy.random.sample(size) * 360.0 pa_disk = numpy.random.sample(size) * 360.0 av_b = numpy.random.sample(size) * 0.4 av_d = numpy.random.sample(size) * 0.4 rv_b = numpy.random.sample(size) * 0.1 + 3.0 rv_d = numpy.random.sample(size) * 0.1 + 3.0 u_ab = numpy.random.sample(size) * 4.0 + 17.0 g_ab = numpy.random.sample(size) * 4.0 + 17.0 r_ab = numpy.random.sample(size) * 4.0 + 17.0 i_ab = numpy.random.sample(size) * 4.0 + 17.0 z_ab = numpy.random.sample(size) * 4.0 + 17.0 y_ab = numpy.random.sample(size) * 4.0 + 17.0 redshift = numpy.random.sample(size) * 2.0 t0_mjd = mjd - numpy.random.sample(size) * 1000.0 agn_tau = numpy.random.sample(size) * 1000.0 + 1000.0 agnSeed = numpy.random.random_integers(low=2, high=4000, size=size) agn_sfu = numpy.random.sample(size) agn_sfg = numpy.random.sample(size) agn_sfr = numpy.random.sample(size) agn_sfi = numpy.random.sample(size) agn_sfz = numpy.random.sample(size) agn_sfy = numpy.random.sample(size) rrStar = numpy.random.sample(size) * numpy.radians(radius) thetaStar = numpy.random.sample(size) * 2.0 * numpy.pi if displacedRA is None: raStar = centerRA + rrStar * numpy.cos(thetaStar) else: raStar = centerRA + numpy.radians(displacedRA) if displacedDec is None: decStar = centerDec + rrStar * numpy.sin(thetaStar) else: decStar = centerDec + numpy.radians(displacedDec) raStar = numpy.degrees(raStar) decStar = numpy.degrees(decStar) magnormStar = numpy.random.sample(size) * 4.0 + 17.0 mudecl = numpy.random.sample(size) * 0.0001 mura = numpy.random.sample(size) * 0.0001 galacticAv = numpy.random.sample(size) * 0.05 * 3.1 vrad = numpy.random.sample(size) * 1.0 parallax = 0.00045 + numpy.random.sample(size) * 0.00001 period = numpy.random.sample(size) * 20.0 amp = numpy.random.sample(size) * 5.0 #write the data to the tables. for i in range(size): cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)], a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i]) c.execute(cmd) varParam = { 'varMethodName': 'applyAgn', 'pars': { 'agn_tau': round(agn_tau[i], 4), 't0_mjd': round(t0_mjd[i], 4), 'agn_sfu': round(agn_sfu[i], 4), 'agn_sfg': round(agn_sfg[i], 4), 'agn_sfr': round(agn_sfr[i], 4), 'agn_sfi': round(agn_sfi[i], 4), 'agn_sfz': round(agn_sfz[i], 4), 'agn_sfy': round(agn_sfy[i], 4), 'seed': int(agnSeed[i]) } } paramStr = json.dumps(varParam) cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', '%s', '%s', %f, %f, %f, %i, %f, %f, %f, %i, '%s', %f, %f, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i], magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i], galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed, paramStr, a_b[i], b_b[i], pa_bulge[i], 4, a_d[i], b_d[i], pa_disk[i], 1, 'CCM', av_b[i], rv_b[i], 'CCM', av_d[i], rv_d[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) c.execute(cmd) cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s', %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, agnra[i], agndec[i], ra[i], dec[i], magnorm_agn[i], agn_sed, paramStr, u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i]) c.execute(cmd) varParam = { 'varMethodName': 'testVar', 'pars': { 'period': period[i], 'amplitude': amp[i] } } paramStr = json.dumps(varParam) cmd = '''INSERT INTO starsALL_forceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f)''' %\ (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i], galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i]) c.execute(cmd) conn.commit() conn.close() return obs_metadata
from __future__ import with_statement from lsst.sims.utils import raDecFromAltAz from lsst.sims.utils import altAzPaFromRaDec from lsst.sims.utils import ObservationMetaData obs_pointing = ObservationMetaData(mjd=59580.0) alt_pointing = 66.0 az_pointing = 11.0 (ra_pointing, dec_pointing) = raDecFromAltAz(alt_pointing, az_pointing, obs_pointing) (alt_check, az_check, pa_pointing) = altAzPaFromRaDec(ra_pointing, dec_pointing, obs_pointing) from lsst.sims.utils import angularSeparation dd = angularSeparation(alt_pointing, az_pointing, alt_check, az_check) assert dd < 1.0e-6 import numpy as np from lsst.sims.utils import getRotTelPos def write_header(file_handle, rot_sky, obshistid): file_handle.write('rightascension %.7f\n' % ra_pointing) file_handle.write('declination %.7f\n' % dec_pointing) file_handle.write('mjd %.5f\n' % obs_pointing.mjd.TAI)
def makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1, deltaRA=None, deltaDec=None, bandpass='******', m5=None, seeing=None, magnorm_min=17.0, delta_magnorm=4.0, **kwargs): """ Make a test database to storing cartoon information for the test phoSim input catalog to use. The method will return an ObservationMetaData object guaranteed to encompass the objects in this database. @param [in] filename is a string indicating the name of the DB file to be created @param [in] size is the number of objects int he database @param [in] seedVal is the seed passed to the random number generator @param [in] radius is the radius (in degrees) of the field of view to be returned @param [in] bandpass is the bandpas(es) of the observation to be passed to ObservationMetaData (optional) @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData (optional) @param [in] seeing is the seeing value(s) in arcseconds to be passed to ObservationMetaData (optional) @param [in] deltaRA/Dec are numpy arrays that indicate where (in relation to the center of the field of view) objects should be placed. These coordinates are in degrees. Specifying either of these paramters will overwrite size. If you only specify one of these parameters, the other will be set randomly. These parameters are optional. @param [in] magnorm_min is the min magnorm (magNorms for sources in the database will be distributed according to a random deviate drawn from magnorm_min + random*delta_magnorm) @param [in] delta_magnorm (see documentation for magnorm_min) """ if os.path.exists(filename): os.unlink(filename) # just an example of some valid SED file names galaxy_seds = [ 'Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec' ] agn_sed = 'agn.spec' star_seds = [ 'km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700' ] rng = np.random.RandomState(seedVal) if deltaRA is not None and deltaDec is not None: if len(deltaRA) != len(deltaDec): raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and " "deltaDec have different lengths") if deltaRA is not None: size = len(deltaRA) elif deltaDec is not None: size = len(deltaDec) # create the ObservationMetaData object mjd = 52000.0 alt = np.pi / 2.0 az = 0.0 testSite = Site(name='LSST') obsTemp = ObservationMetaData(mjd=mjd, site=testSite) centerRA, centerDec = _raDecFromAltAz(alt, az, obsTemp) rotTel = _getRotTelPos(centerRA, centerDec, obsTemp, 0.0) rotSkyPos = _getRotSkyPos(centerRA, centerDec, obsTemp, rotTel) obs_metadata = ObservationMetaData(pointingRA=np.degrees(centerRA), pointingDec=np.degrees(centerDec), rotSkyPos=np.degrees(rotSkyPos), bandpassName=bandpass, mjd=mjd, boundType='circle', boundLength=2.0 * radius, site=testSite, m5=m5, seeing=seeing) moon_alt = -90.0 sun_alt = -90.0 moon_ra, moon_dec = raDecFromAltAz(moon_alt, 0.0, obs_metadata) dist2moon = haversine(np.radians(moon_ra), np.radians(moon_dec), obs_metadata._pointingRA, obs_metadata._pointingDec) obs_metadata.OpsimMetaData = { 'moonra': moon_ra, 'moondec': moon_dec, 'moonalt': moon_alt, 'sunalt': sun_alt, 'dist2moon': dist2moon, 'rottelpos': np.degrees(rotTel) } # Now begin building the database. # First create the tables. conn = sqlite3.connect(filename) c = conn.cursor() try: c.execute('''CREATE TABLE galaxy_bulge (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real, sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int, ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''' ) conn.commit() except: raise RuntimeError("Error creating galaxy_bulge table.") try: c.execute('''CREATE TABLE galaxy (galtileid int, galid int, ra real, dec real, bra real, bdec real, dra real, ddec real, agnra real, agndec real, magnorm_bulge, magnorm_disk, magnorm_agn, sedname_bulge text, sedname_disk text, sedname_agn text, varParamStr text, a_b real, b_b real, pa_bulge real, bulge_n int, a_d real, b_d real, pa_disk real, disk_n int, ext_model_b text, av_b real, rv_b real, ext_model_d text, av_d real, rv_d real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''' ) conn.commit() except: raise RuntimeError("Error creating galaxy table.") try: c.execute('''CREATE TABLE galaxy_agn (galtileid int, galid int, agnra real, agndec real, ra real, dec real, magnorm_agn real, sedname_agn text, varParamStr text, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''' ) except: raise RuntimeError("Error creating galaxy_agn table.") try: c.execute('''CREATE TABLE StarAllForceseek (simobjid int, ra real, decl real, magNorm real, mudecl real, mura real, galacticAv real, vrad real, varParamStr text, sedFilename text, parallax real, ebv real)''') except: raise RuntimeError("Error creating StarAllForceseek table.") # Now generate the data to be stored in the tables. rr = rng.random_sample(size) * np.radians(radius) theta = rng.random_sample(size) * 2.0 * np.pi if deltaRA is None: ra = np.degrees(centerRA + rr * np.cos(theta)) else: ra = np.degrees(centerRA) + deltaRA if deltaDec is None: dec = np.degrees(centerDec + rr * np.sin(theta)) else: dec = np.degrees(centerDec) + deltaDec bra = np.radians(ra + rng.random_sample(size) * 0.01 * radius) bdec = np.radians(dec + rng.random_sample(size) * 0.01 * radius) dra = np.radians(ra + rng.random_sample(size) * 0.01 * radius) ddec = np.radians(dec + rng.random_sample(size) * 0.01 * radius) agnra = np.radians(ra + rng.random_sample(size) * 0.01 * radius) agndec = np.radians(dec + rng.random_sample(size) * 0.01 * radius) magnorm_bulge = rng.random_sample(size) * delta_magnorm + magnorm_min magnorm_disk = rng.random_sample(size) * delta_magnorm + magnorm_min magnorm_agn = rng.random_sample(size) * delta_magnorm + magnorm_min b_b = rng.random_sample(size) * 0.2 a_b = b_b + rng.random_sample(size) * 0.05 b_d = rng.random_sample(size) * 0.5 a_d = b_d + rng.random_sample(size) * 0.1 BulgeHalfLightRadius = rng.random_sample(size) * 0.2 DiskHalfLightRadius = rng.random_sample(size) * 0.5 pa_bulge = rng.random_sample(size) * 360.0 pa_disk = rng.random_sample(size) * 360.0 av_b = rng.random_sample(size) * 0.3 av_d = rng.random_sample(size) * 0.3 rv_b = rng.random_sample(size) * 0.1 + 2.0 rv_d = rng.random_sample(size) * 0.1 + 2.0 u_ab = rng.random_sample(size) * 4.0 + 17.0 g_ab = rng.random_sample(size) * 4.0 + 17.0 r_ab = rng.random_sample(size) * 4.0 + 17.0 i_ab = rng.random_sample(size) * 4.0 + 17.0 z_ab = rng.random_sample(size) * 4.0 + 17.0 y_ab = rng.random_sample(size) * 4.0 + 17.0 redshift = rng.random_sample(size) * 2.0 t0_mjd = mjd - rng.random_sample(size) * 1000.0 agn_tau = rng.random_sample(size) * 1000.0 + 1000.0 agnSeed = rng.randint(2, 4001, size=size) agn_sfu = rng.random_sample(size) agn_sfg = rng.random_sample(size) agn_sfr = rng.random_sample(size) agn_sfi = rng.random_sample(size) agn_sfz = rng.random_sample(size) agn_sfy = rng.random_sample(size) rrStar = rng.random_sample(size) * np.radians(radius) thetaStar = rng.random_sample(size) * 2.0 * np.pi if deltaRA is None: raStar = centerRA + rrStar * np.cos(thetaStar) else: raStar = centerRA + np.radians(deltaRA) if deltaDec is None: decStar = centerDec + rrStar * np.sin(thetaStar) else: decStar = centerDec + np.radians(deltaDec) raStar = np.degrees(raStar) decStar = np.degrees(decStar) magnormStar = rng.random_sample(size) * delta_magnorm + magnorm_min mudecl = rng.random_sample(size) * 0.0001 mura = rng.random_sample(size) * 0.0001 galacticAv = rng.random_sample(size) * 0.05 * 3.1 vrad = rng.random_sample(size) * 1.0 parallax = 0.00045 + rng.random_sample(size) * 0.00001 period = rng.random_sample(size) * 20.0 amp = rng.random_sample(size) * 5.0 # write the data to the tables. for i in range(size): cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)], a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i]) c.execute(cmd) varParam = { 'varMethodName': 'applyAgn', 'pars': { 'agn_tau': round(agn_tau[i], 4), 't0_mjd': round(t0_mjd[i], 4), 'agn_sfu': round(agn_sfu[i], 4), 'agn_sfg': round(agn_sfg[i], 4), 'agn_sfr': round(agn_sfr[i], 4), 'agn_sfi': round(agn_sfi[i], 4), 'agn_sfz': round(agn_sfz[i], 4), 'agn_sfy': round(agn_sfy[i], 4), 'seed': int(agnSeed[i]) } } paramStr = json.dumps(varParam) cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', '%s', '%s', %f, %f, %f, %i, %f, %f, %f, %i, '%s', %f, %f, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i], magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i], galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed, paramStr, a_b[i], b_b[i], pa_bulge[i], 4, a_d[i], b_d[i], pa_disk[i], 1, 'CCM', av_b[i], rv_b[i], 'CCM', av_d[i], rv_d[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) c.execute(cmd) cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s', %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, agnra[i], agndec[i], ra[i], dec[i], magnorm_agn[i], agn_sed, paramStr, u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i]) c.execute(cmd) varParam = { 'varMethodName': 'testVar', 'pars': { 'period': period[i], 'amplitude': amp[i] } } paramStr = json.dumps(varParam) cmd = '''INSERT INTO StarAllForceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f, %f)''' %\ (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i], galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i], galacticAv[i]/3.1) c.execute(cmd) conn.commit() conn.close() return obs_metadata
def test_with_proper_motion(self): """ Test that calculating pupil coordinates in the presence of proper motion, parallax, and radial velocity is equivalent to observedFromICRS -> icrsFromObserved -> pupilCoordsFromRaDec (mostly to make surethat pupilCoordsFromRaDec is correctly calling observedFromICRS with non-zero proper motion, etc.) """ rng = np.random.RandomState(38442) is_valid = False while not is_valid: mjd_tai = 59580.0 + 10000.0*rng.random_sample() obs = ObservationMetaData(mjd=mjd_tai) ra, dec = raDecFromAltAz(78.0, 112.0, obs) dd = distanceToSun(ra, dec, obs.mjd) if dd > 45.0: is_valid = True n_obj = 1000 rr = rng.random_sample(n_obj)*2.0 theta = rng.random_sample(n_obj)*2.0*np.pi ra_list = ra + rr*np.cos(theta) dec_list = dec + rr*np.sin(theta) obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, mjd=mjd_tai, rotSkyPos=19.0) pm_ra_list = rng.random_sample(n_obj)*100.0 - 50.0 pm_dec_list = rng.random_sample(n_obj)*100.0 - 50.0 px_list = rng.random_sample(n_obj) + 0.05 v_rad_list = rng.random_sample(n_obj)*600.0 - 300.0 for includeRefraction in (True, False): ra_obs, dec_obs = observedFromICRS(ra_list, dec_list, pm_ra=pm_ra_list, pm_dec=pm_dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) ra_icrs, dec_icrs = icrsFromObserved(ra_obs, dec_obs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_control, yp_control = pupilCoordsFromRaDec(ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_test, yp_test = pupilCoordsFromRaDec(ra_list, dec_list, pm_ra=pm_ra_list, pm_dec=pm_dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) distance = arcsecFromRadians(np.sqrt(np.power(xp_test-xp_control, 2) + np.power(yp_test-yp_control, 2))) self.assertLess(distance.max(), 0.006) # now test it in radians xp_rad, yp_rad = _pupilCoordsFromRaDec(np.radians(ra_list), np.radians(dec_list), pm_ra=radiansFromArcsec(pm_ra_list), pm_dec=radiansFromArcsec(pm_dec_list), parallax=radiansFromArcsec(px_list), v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) np.testing.assert_array_equal(xp_rad, xp_test) np.testing.assert_array_equal(yp_rad, yp_test) # now test it with proper motion = 0 ra_obs, dec_obs = observedFromICRS(ra_list, dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) ra_icrs, dec_icrs = icrsFromObserved(ra_obs, dec_obs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_control, yp_control = pupilCoordsFromRaDec(ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_test, yp_test = pupilCoordsFromRaDec(ra_list, dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) distance = arcsecFromRadians(np.sqrt(np.power(xp_test-xp_control, 2) + np.power(yp_test-yp_control, 2))) self.assertLess(distance.max(), 1.0e-6)
from __future__ import with_statement from lsst.sims.utils import raDecFromAltAz from lsst.sims.utils import altAzPaFromRaDec from lsst.sims.utils import ObservationMetaData obs_pointing = ObservationMetaData(mjd=59580.0) alt_pointing = 66.0 az_pointing = 11.0 (ra_pointing, dec_pointing) = raDecFromAltAz(alt_pointing, az_pointing, obs_pointing) (alt_check, az_check, pa_pointing) = altAzPaFromRaDec(ra_pointing, dec_pointing, obs_pointing) from lsst.sims.utils import angularSeparation dd = angularSeparation(alt_pointing, az_pointing, alt_check, az_check) assert dd < 1.0e-6 import numpy as np from lsst.sims.utils import getRotTelPos def write_header(file_handle, rot_sky, obshistid): file_handle.write('rightascension %.7f\n' % ra_pointing) file_handle.write('declination %.7f\n' % dec_pointing)
def test_naive_focal_plane_position(self): """ 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(), 1.0e-8)
def test_with_proper_motion(self): """ Test that calculating pupil coordinates in the presence of proper motion, parallax, and radial velocity is equivalent to observedFromICRS -> icrsFromObserved -> pupilCoordsFromRaDec (mostly to make surethat pupilCoordsFromRaDec is correctly calling observedFromICRS with non-zero proper motion, etc.) """ rng = np.random.RandomState(38442) is_valid = False while not is_valid: mjd_tai = 59580.0 + 10000.0 * rng.random_sample() obs = ObservationMetaData(mjd=mjd_tai) ra, dec = raDecFromAltAz(78.0, 112.0, obs) dd = distanceToSun(ra, dec, obs.mjd) if dd > 45.0: is_valid = True n_obj = 1000 rr = rng.random_sample(n_obj) * 2.0 theta = rng.random_sample(n_obj) * 2.0 * np.pi ra_list = ra + rr * np.cos(theta) dec_list = dec + rr * np.sin(theta) obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, mjd=mjd_tai, rotSkyPos=19.0) pm_ra_list = rng.random_sample(n_obj) * 100.0 - 50.0 pm_dec_list = rng.random_sample(n_obj) * 100.0 - 50.0 px_list = rng.random_sample(n_obj) + 0.05 v_rad_list = rng.random_sample(n_obj) * 600.0 - 300.0 for includeRefraction in (True, False): ra_obs, dec_obs = observedFromICRS( ra_list, dec_list, pm_ra=pm_ra_list, pm_dec=pm_dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) ra_icrs, dec_icrs = icrsFromObserved( ra_obs, dec_obs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_control, yp_control = pupilCoordsFromRaDec( ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_test, yp_test = pupilCoordsFromRaDec( ra_list, dec_list, pm_ra=pm_ra_list, pm_dec=pm_dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) distance = arcsecFromRadians( np.sqrt( np.power(xp_test - xp_control, 2) + np.power(yp_test - yp_control, 2))) self.assertLess(distance.max(), 0.006) # now test it in radians xp_rad, yp_rad = _pupilCoordsFromRaDec( np.radians(ra_list), np.radians(dec_list), pm_ra=radiansFromArcsec(pm_ra_list), pm_dec=radiansFromArcsec(pm_dec_list), parallax=radiansFromArcsec(px_list), v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) np.testing.assert_array_equal(xp_rad, xp_test) np.testing.assert_array_equal(yp_rad, yp_test) # now test it with proper motion = 0 ra_obs, dec_obs = observedFromICRS( ra_list, dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) ra_icrs, dec_icrs = icrsFromObserved( ra_obs, dec_obs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_control, yp_control = pupilCoordsFromRaDec( ra_icrs, dec_icrs, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) xp_test, yp_test = pupilCoordsFromRaDec( ra_list, dec_list, parallax=px_list, v_rad=v_rad_list, obs_metadata=obs, epoch=2000.0, includeRefraction=includeRefraction) distance = arcsecFromRadians( np.sqrt( np.power(xp_test - xp_control, 2) + np.power(yp_test - yp_control, 2))) self.assertLess(distance.max(), 1.0e-6)
def test_LSST_camera_wrapper(self): """ Test that LSSTCameraWrapper wraps its methods as expected. Recall that the LSSTCameraWrapper applies the 90 degree rotation to go from DM pixel coordinates to Camera team pixel coordinates. Namely, Camera +y = DM +x Camera +x = DM -y """ camera = lsst_camera() camera_wrapper = LSSTCameraWrapper() obs_mjd = ObservationMetaData(mjd=60000.0) ra, dec = raDecFromAltAz(135.0, 112.0, obs_mjd) obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, mjd=obs_mjd.mjd, rotSkyPos=22.4, bandpassName='u') rng = np.random.RandomState(8124) for detector in camera: name = detector.getName() bbox = camera[name].getBBox() bbox_wrapper = camera_wrapper.getBBox(name) self.assertEqual(bbox.getMinX(), bbox_wrapper.getMinY()) self.assertEqual(bbox.getMaxX(), bbox_wrapper.getMaxY()) self.assertEqual(bbox.getMinY(), bbox_wrapper.getMinX()) self.assertEqual(bbox.getMaxY(), bbox_wrapper.getMaxX()) self.assertGreater(bbox_wrapper.getMaxY() - bbox_wrapper.getMinY(), bbox_wrapper.getMaxX() - bbox_wrapper.getMinX()) center_point = camera[name].getCenter(FOCAL_PLANE) pixel_system = camera[name].makeCameraSys(PIXELS) center_pix = camera.transform(center_point, FOCAL_PLANE, pixel_system) center_pix_wrapper = camera_wrapper.getCenterPixel(name) self.assertEqual(center_pix.getX(), center_pix_wrapper.getY()) self.assertEqual(center_pix.getY(), center_pix_wrapper.getX()) # Note that DM and the Camera team agree on the orientation # of the pupil coordinate/field angle axes pupil_system = camera[name].makeCameraSys(FIELD_ANGLE) center_pupil = camera.transform(center_point, FOCAL_PLANE, pupil_system) center_pupil_wrapper = camera_wrapper.getCenterPupil(name) self.assertEqual(center_pupil.getX(), center_pupil_wrapper.getX()) self.assertEqual(center_pupil.getY(), center_pupil_wrapper.getY()) corner_pupil_wrapper = camera_wrapper.getCornerPupilList(name) corner_point_list = camera[name].getCorners(FOCAL_PLANE) for point in corner_point_list: point_pupil = camera.transform(point, FOCAL_PLANE, pupil_system) dd_min = 1.0e10 for wrapper_point in corner_pupil_wrapper: dd = np.sqrt( (point_pupil.getX() - wrapper_point.getX())**2 + (point_pupil.getY() - wrapper_point.getY())**2) if dd < dd_min: dd_min = dd self.assertLess(dd_min, 1.0e-20) xpix_min = None xpix_max = None ypix_min = None ypix_max = None focal_to_tan_pix = camera[name].getTransform( FOCAL_PLANE, TAN_PIXELS) for point in corner_point_list: pixel_point = focal_to_tan_pix.applyForward(point) xx = pixel_point.getX() yy = pixel_point.getY() if xpix_min is None or xx < xpix_min: xpix_min = xx if ypix_min is None or yy < ypix_min: ypix_min = yy if xpix_max is None or xx > xpix_max: xpix_max = xx if ypix_max is None or yy > ypix_max: ypix_max = yy pix_bounds_wrapper = camera_wrapper.getTanPixelBounds(name) self.assertEqual(pix_bounds_wrapper[0], ypix_min) self.assertEqual(pix_bounds_wrapper[1], ypix_max) self.assertEqual(pix_bounds_wrapper[2], xpix_min) self.assertEqual(pix_bounds_wrapper[3], xpix_max) # generate some random pupil coordinates; # verify that the relationship between the DM and Camera team # pixel coordinates corresponding to those pupil coordinates # is as expected x_pup = rng.random_sample(10) * 0.005 - 0.01 y_pup = rng.random_sample(10) * 0.005 - 0.01 x_pix, y_pix = pixelCoordsFromPupilCoordsLSST(x_pup, y_pup, chipName=name, band=obs.bandpass) (x_pix_wrapper, y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords( x_pup, y_pup, name, obs) nan_x = np.where(np.isnan(x_pix)) self.assertEqual(len(nan_x[0]), 0) np.testing.assert_allclose(x_pix - center_pix.getX(), y_pix_wrapper - center_pix_wrapper.getY(), atol=1.0e-10, rtol=0.0) np.testing.assert_allclose(y_pix - center_pix.getY(), center_pix_wrapper.getX() - x_pix_wrapper, atol=1.0e-10, rtol=0.0) # use camera_wrapper.pupilCoordsFromPixelCoords to go back to pupil # coordinates from x_pix_wrapper, y_pix_wrapper; make sure you get # the original pupil coordinates back out (x_pup_wrapper, y_pup_wrapper) = camera_wrapper.pupilCoordsFromPixelCoords( x_pix_wrapper, y_pix_wrapper, name, obs) msg = 'worst error %e' % np.abs(x_pup - x_pup_wrapper).max() np.testing.assert_allclose(x_pup, x_pup_wrapper, atol=1.0e-10, rtol=0.0, err_msg=msg) msg = 'worst error %e' % np.abs(y_pup - y_pup_wrapper).max() np.testing.assert_allclose(y_pup, y_pup_wrapper, atol=1.0e-10, rtol=0.0, err_msg=msg) # generate some random sky coordinates; verify that the methods that # convert between (RA, Dec) and pixel coordinates behave as expected. # NOTE: x_pix, y_pix will be in DM pixel coordinate convention x_pix = bbox.getMinX() + rng.random_sample(10) * (bbox.getMaxX() - bbox.getMinX()) y_pix = bbox.getMinY() + rng.random_sample(10) * (bbox.getMaxY() - bbox.getMinY()) ra, dec = raDecFromPixelCoordsLSST(x_pix, y_pix, name, obs_metadata=obs, band=obs.bandpass) (ra_wrapper, dec_wrapper) = camera_wrapper.raDecFromPixelCoords( 2.0 * center_pix.getY() - y_pix, x_pix, name, obs) nan_ra = np.where(np.isnan(ra)) self.assertEqual(len(nan_ra[0]), 0) np.testing.assert_allclose(ra, ra_wrapper, atol=1.0e-10, rtol=0.0) np.testing.assert_allclose(dec, dec_wrapper, atol=1.0e-10, rtol=0.0) # make sure that the method that returns RA, Dec in radians agrees with # the method that returns RA, Dec in degrees (ra_rad, dec_rad) = camera_wrapper._raDecFromPixelCoords( 2.0 * center_pix.getY() - y_pix, x_pix, name, obs) np.testing.assert_allclose(np.radians(ra_wrapper), ra_rad, atol=1.0e-10, rtol=0.0) np.testing.assert_allclose(np.radians(dec_wrapper), dec_rad, atol=1.0e-10, rtol=0.0) # Go back to pixel coordinates with pixelCoordsFromRaDec; verify that # the result relates to the original DM pixel coordinates as expected # (x_pix_inv, y_pix_inv will be in Camera pixel coordinates) (x_pix_inv, y_pix_inv) = camera_wrapper.pixelCoordsFromRaDec(ra_wrapper, dec_wrapper, chipName=name, obs_metadata=obs) np.testing.assert_allclose(y_pix_inv, x_pix, atol=1.0e-4, rtol=0.0) np.testing.assert_allclose(x_pix_inv, 2.0 * center_pix.getY() - y_pix, atol=1.0e-4, rtol=0.0) ra = np.radians(ra_wrapper) dec = np.radians(dec_wrapper) # check that the the method that accepts RA, Dec in radians agrees with the # method that accepts RA, Dec in degrees (x_pix_wrapper, y_pix_wrapper) = camera_wrapper._pixelCoordsFromRaDec( ra, dec, chipName=name, obs_metadata=obs) np.testing.assert_allclose(x_pix_inv, x_pix_wrapper, atol=1.0e-10, rtol=0.0) np.testing.assert_allclose(y_pix_inv, y_pix_wrapper, atol=1.0e-10, rtol=0.0) del camera del camera_wrapper del lsst_camera._lsst_camera
def test_generic_camera_wrapper(self): """ Test that GalSimCameraWrapper wraps its methods as expected. This is mostly to catch changes in afw API. """ camera = camTestUtils.CameraWrapper().camera camera_wrapper = GalSimCameraWrapper(camera) obs_mjd = ObservationMetaData(mjd=60000.0) ra, dec = raDecFromAltAz(35.0, 112.0, obs_mjd) obs = ObservationMetaData(pointingRA=ra, pointingDec=dec, mjd=obs_mjd.mjd, rotSkyPos=22.4) rng = np.random.RandomState(8124) for detector in camera: name = detector.getName() bbox = camera[name].getBBox() bbox_wrapper = camera_wrapper.getBBox(name) self.assertEqual(bbox.getMinX(), bbox_wrapper.getMinX()) self.assertEqual(bbox.getMaxX(), bbox_wrapper.getMaxX()) self.assertEqual(bbox.getMinY(), bbox_wrapper.getMinY()) self.assertEqual(bbox.getMaxY(), bbox_wrapper.getMaxY()) center_point = camera[name].getCenter(FOCAL_PLANE) pixel_system = camera[name].makeCameraSys(PIXELS) center_pix = camera.transform(center_point, FOCAL_PLANE, pixel_system) center_pix_wrapper = camera_wrapper.getCenterPixel(name) self.assertEqual(center_pix.getX(), center_pix_wrapper.getX()) self.assertEqual(center_pix.getY(), center_pix_wrapper.getY()) pupil_system = camera[name].makeCameraSys(FIELD_ANGLE) center_pupil = camera.transform(center_point, FOCAL_PLANE, pupil_system) center_pupil_wrapper = camera_wrapper.getCenterPupil(name) self.assertEqual(center_pupil.getX(), center_pupil_wrapper.getX()) self.assertEqual(center_pupil.getY(), center_pupil_wrapper.getY()) corner_pupil_wrapper = camera_wrapper.getCornerPupilList(name) corner_point_list = camera[name].getCorners(FOCAL_PLANE) for point in corner_point_list: point_pupil = camera.transform(point, FOCAL_PLANE, pupil_system) dd_min = 1.0e10 for wrapper_point in corner_pupil_wrapper: dd = np.sqrt( (point_pupil.getX() - wrapper_point.getX())**2 + (point_pupil.getY() - wrapper_point.getY())**2) if dd < dd_min: dd_min = dd self.assertLess(dd_min, 1.0e-20) xpix_min = None xpix_max = None ypix_min = None ypix_max = None focal_to_tan_pix = camera[name].getTransform( FOCAL_PLANE, TAN_PIXELS) for point in corner_point_list: pixel_point = focal_to_tan_pix.applyForward(point) xx = pixel_point.getX() yy = pixel_point.getY() if xpix_min is None or xx < xpix_min: xpix_min = xx if ypix_min is None or yy < ypix_min: ypix_min = yy if xpix_max is None or xx > xpix_max: xpix_max = xx if ypix_max is None or yy > ypix_max: ypix_max = yy pix_bounds_wrapper = camera_wrapper.getTanPixelBounds(name) self.assertEqual(pix_bounds_wrapper[0], xpix_min) self.assertEqual(pix_bounds_wrapper[1], xpix_max) self.assertEqual(pix_bounds_wrapper[2], ypix_min) self.assertEqual(pix_bounds_wrapper[3], ypix_max) x_pup = rng.random_sample(10) * 0.005 - 0.01 y_pup = rng.random_sample(10) * 0.005 - 0.01 x_pix, y_pix = pixelCoordsFromPupilCoords(x_pup, y_pup, chipName=name, camera=camera) (x_pix_wrapper, y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords( x_pup, y_pup, name, obs) nan_x = np.where(np.isnan(x_pix)) self.assertEqual(len(nan_x[0]), 0) np.testing.assert_array_equal(x_pix, x_pix_wrapper) np.testing.assert_array_equal(y_pix, y_pix_wrapper) x_pix = rng.random_sample(10) * 100.0 - 200.0 y_pix = rng.random_sample(10) * 100.0 - 200.0 x_pup, y_pup = pupilCoordsFromPixelCoords(x_pix, y_pix, chipName=name, camera=camera) (x_pup_wrapper, y_pup_wrapper) = camera_wrapper.pupilCoordsFromPixelCoords( x_pix, y_pix, name, obs) nan_x = np.where(np.isnan(x_pup)) self.assertEqual(len(nan_x[0]), 0) np.testing.assert_array_equal(x_pup, x_pup_wrapper) np.testing.assert_array_equal(y_pup, y_pup_wrapper) ra, dec = raDecFromPixelCoords(x_pix, y_pix, name, camera=camera, obs_metadata=obs) (ra_wrapper, dec_wrapper) = camera_wrapper.raDecFromPixelCoords( x_pix, y_pix, name, obs) nan_ra = np.where(np.isnan(ra)) self.assertEqual(len(nan_ra[0]), 0) np.testing.assert_array_equal(ra, ra_wrapper) np.testing.assert_array_equal(dec, dec_wrapper) ra, dec = _raDecFromPixelCoords(x_pix, y_pix, name, camera=camera, obs_metadata=obs) (ra_wrapper, dec_wrapper) = camera_wrapper._raDecFromPixelCoords( x_pix, y_pix, name, obs) nan_ra = np.where(np.isnan(ra)) self.assertEqual(len(nan_ra[0]), 0) np.testing.assert_array_equal(ra, ra_wrapper) np.testing.assert_array_equal(dec, dec_wrapper) ra = obs.pointingRA + (rng.random_sample(10) * 150.0 - 100.0) / 160.0 dec = obs.pointingDec + (rng.random_sample(10) * 150.0 - 100.0) / 160.0 x_pix, y_pix = pixelCoordsFromRaDec(ra, dec, chipName=name, camera=camera, obs_metadata=obs) (x_pix_wrapper, y_pix_wrapper) = camera_wrapper.pixelCoordsFromRaDec( ra, dec, chipName=name, obs_metadata=obs) nan_x = np.where(np.isnan(x_pix)) self.assertEqual(len(nan_x[0]), 0) np.testing.assert_array_equal(x_pix, x_pix_wrapper) np.testing.assert_array_equal(y_pix, y_pix_wrapper) ra = np.radians(ra) dec = np.radians(dec) x_pix, y_pix = _pixelCoordsFromRaDec(ra, dec, chipName=name, camera=camera, obs_metadata=obs) (x_pix_wrapper, y_pix_wrapper) = camera_wrapper._pixelCoordsFromRaDec( ra, dec, chipName=name, obs_metadata=obs) nan_x = np.where(np.isnan(x_pix)) self.assertEqual(len(nan_x[0]), 0) np.testing.assert_array_equal(x_pix, x_pix_wrapper) np.testing.assert_array_equal(y_pix, y_pix_wrapper) del camera
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 makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1, deltaRA=None, deltaDec=None, bandpass='******', m5=None, seeing=None, magnorm_min=17.0, delta_magnorm=4.0, **kwargs): """ Make a test database to storing cartoon information for the test phoSim input catalog to use. The method will return an ObservationMetaData object guaranteed to encompass the objects in this database. @param [in] filename is a string indicating the name of the DB file to be created @param [in] size is the number of objects int he database @param [in] seedVal is the seed passed to the random number generator @param [in] radius is the radius (in degrees) of the field of view to be returned @param [in] bandpass is the bandpas(es) of the observation to be passed to ObservationMetaData (optional) @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData (optional) @param [in] seeing is the seeing value(s) in arcseconds to be passed to ObservationMetaData (optional) @param [in] deltaRA/Dec are numpy arrays that indicate where (in relation to the center of the field of view) objects should be placed. These coordinates are in degrees. Specifying either of these paramters will overwrite size. If you only specify one of these parameters, the other will be set randomly. These parameters are optional. @param [in] magnorm_min is the min magnorm (magNorms for sources in the database will be distributed according to a random deviate drawn from magnorm_min + random*delta_magnorm) @param [in] delta_magnorm (see documentation for magnorm_min) """ if os.path.exists(filename): os.unlink(filename) # just an example of some valid SED file names galaxy_seds = ['Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec'] agn_sed = 'agn.spec' star_seds = ['km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700'] rng = np.random.RandomState(seedVal) if deltaRA is not None and deltaDec is not None: if len(deltaRA) != len(deltaDec): raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and " "deltaDec have different lengths") if deltaRA is not None: size = len(deltaRA) elif deltaDec is not None: size = len(deltaDec) # create the ObservationMetaData object mjd = 52000.0 alt = np.pi/2.0 az = 0.0 testSite = Site(name='LSST') obsTemp = ObservationMetaData(mjd=mjd, site=testSite) centerRA, centerDec = _raDecFromAltAz(alt, az, obsTemp) rotTel = _getRotTelPos(centerRA, centerDec, obsTemp, 0.0) rotSkyPos = _getRotSkyPos(centerRA, centerDec, obsTemp, rotTel) obs_metadata = ObservationMetaData(pointingRA=np.degrees(centerRA), pointingDec=np.degrees(centerDec), rotSkyPos=np.degrees(rotSkyPos), bandpassName=bandpass, mjd=mjd, boundType = 'circle', boundLength = 2.0*radius, site=testSite, m5=m5, seeing=seeing) moon_alt = -90.0 sun_alt = -90.0 moon_ra, moon_dec = raDecFromAltAz(moon_alt, 0.0, obs_metadata) dist2moon = haversine(np.radians(moon_ra), np.radians(moon_dec), obs_metadata._pointingRA, obs_metadata._pointingDec) obs_metadata.OpsimMetaData = {'moonra': moon_ra, 'moondec': moon_dec, 'moonalt': moon_alt, 'sunalt': sun_alt, 'dist2moon': dist2moon, 'rottelpos': np.degrees(rotTel)} # Now begin building the database. # First create the tables. conn = sqlite3.connect(filename) c = conn.cursor() try: c.execute('''CREATE TABLE galaxy_bulge (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real, sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int, ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''') conn.commit() except: raise RuntimeError("Error creating galaxy_bulge table.") try: c.execute('''CREATE TABLE galaxy (galtileid int, galid int, ra real, dec real, bra real, bdec real, dra real, ddec real, agnra real, agndec real, magnorm_bulge, magnorm_disk, magnorm_agn, sedname_bulge text, sedname_disk text, sedname_agn text, varParamStr text, a_b real, b_b real, pa_bulge real, bulge_n int, a_d real, b_d real, pa_disk real, disk_n int, ext_model_b text, av_b real, rv_b real, ext_model_d text, av_d real, rv_d real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''') conn.commit() except: raise RuntimeError("Error creating galaxy table.") try: c.execute('''CREATE TABLE galaxy_agn (galtileid int, galid int, agnra real, agndec real, ra real, dec real, magnorm_agn real, sedname_agn text, varParamStr text, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''') except: raise RuntimeError("Error creating galaxy_agn table.") try: c.execute('''CREATE TABLE StarAllForceseek (simobjid int, ra real, decl real, magNorm real, mudecl real, mura real, galacticAv real, vrad real, varParamStr text, sedFilename text, parallax real, ebv real)''') except: raise RuntimeError("Error creating StarAllForceseek table.") # Now generate the data to be stored in the tables. rr = rng.random_sample(size)*np.radians(radius) theta = rng.random_sample(size)*2.0*np.pi if deltaRA is None: ra = np.degrees(centerRA + rr*np.cos(theta)) else: ra = np.degrees(centerRA) + deltaRA if deltaDec is None: dec = np.degrees(centerDec + rr*np.sin(theta)) else: dec = np.degrees(centerDec) + deltaDec bra = np.radians(ra+rng.random_sample(size)*0.01*radius) bdec = np.radians(dec+rng.random_sample(size)*0.01*radius) dra = np.radians(ra + rng.random_sample(size)*0.01*radius) ddec = np.radians(dec + rng.random_sample(size)*0.01*radius) agnra = np.radians(ra + rng.random_sample(size)*0.01*radius) agndec = np.radians(dec + rng.random_sample(size)*0.01*radius) magnorm_bulge = rng.random_sample(size)*delta_magnorm + magnorm_min magnorm_disk = rng.random_sample(size)*delta_magnorm + magnorm_min magnorm_agn = rng.random_sample(size)*delta_magnorm + magnorm_min b_b = rng.random_sample(size)*0.2 a_b = b_b+rng.random_sample(size)*0.05 b_d = rng.random_sample(size)*0.5 a_d = b_d+rng.random_sample(size)*0.1 BulgeHalfLightRadius = rng.random_sample(size)*0.2 DiskHalfLightRadius = rng.random_sample(size)*0.5 pa_bulge = rng.random_sample(size)*360.0 pa_disk = rng.random_sample(size)*360.0 av_b = rng.random_sample(size)*0.3 av_d = rng.random_sample(size)*0.3 rv_b = rng.random_sample(size)*0.1 + 2.0 rv_d = rng.random_sample(size)*0.1 + 2.0 u_ab = rng.random_sample(size)*4.0 + 17.0 g_ab = rng.random_sample(size)*4.0 + 17.0 r_ab = rng.random_sample(size)*4.0 + 17.0 i_ab = rng.random_sample(size)*4.0 + 17.0 z_ab = rng.random_sample(size)*4.0 + 17.0 y_ab = rng.random_sample(size)*4.0 + 17.0 redshift = rng.random_sample(size)*2.0 t0_mjd = mjd - rng.random_sample(size)*1000.0 agn_tau = rng.random_sample(size)*1000.0 + 1000.0 agnSeed = rng.randint(2, 4001, size=size) agn_sfu = rng.random_sample(size) agn_sfg = rng.random_sample(size) agn_sfr = rng.random_sample(size) agn_sfi = rng.random_sample(size) agn_sfz = rng.random_sample(size) agn_sfy = rng.random_sample(size) rrStar = rng.random_sample(size)*np.radians(radius) thetaStar = rng.random_sample(size)*2.0*np.pi if deltaRA is None: raStar = centerRA + rrStar*np.cos(thetaStar) else: raStar = centerRA + np.radians(deltaRA) if deltaDec is None: decStar = centerDec + rrStar*np.sin(thetaStar) else: decStar = centerDec + np.radians(deltaDec) raStar = np.degrees(raStar) decStar = np.degrees(decStar) magnormStar = rng.random_sample(size)*delta_magnorm + magnorm_min mudecl = rng.random_sample(size)*0.0001 mura = rng.random_sample(size)*0.0001 galacticAv = rng.random_sample(size)*0.05*3.1 vrad = rng.random_sample(size)*1.0 parallax = 0.00045+rng.random_sample(size)*0.00001 period = rng.random_sample(size)*20.0 amp = rng.random_sample(size)*5.0 # write the data to the tables. for i in range(size): cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)], a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i]) c.execute(cmd) varParam = {'varMethodName': 'applyAgn', 'pars': {'agn_tau': round(agn_tau[i], 4), 't0_mjd': round(t0_mjd[i], 4), 'agn_sfu': round(agn_sfu[i], 4), 'agn_sfg': round(agn_sfg[i], 4), 'agn_sfr': round(agn_sfr[i], 4), 'agn_sfi': round(agn_sfi[i], 4), 'agn_sfz': round(agn_sfz[i], 4), 'agn_sfy': round(agn_sfy[i], 4), 'seed': int(agnSeed[i])}} paramStr = json.dumps(varParam) cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', '%s', '%s', %f, %f, %f, %i, %f, %f, %f, %i, '%s', %f, %f, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i], magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i], galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed, paramStr, a_b[i], b_b[i], pa_bulge[i], 4, a_d[i], b_d[i], pa_disk[i], 1, 'CCM', av_b[i], rv_b[i], 'CCM', av_d[i], rv_d[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) c.execute(cmd) cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s', %f, %f, %f, %f, %f, %f, %f)''' % \ (i, i, agnra[i], agndec[i], ra[i], dec[i], magnorm_agn[i], agn_sed, paramStr, u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i]) c.execute(cmd) varParam = {'varMethodName': 'testVar', 'pars': {'period': period[i], 'amplitude': amp[i]}} paramStr = json.dumps(varParam) cmd = '''INSERT INTO StarAllForceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f, %f)''' %\ (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i], galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i], galacticAv[i]/3.1) c.execute(cmd) conn.commit() conn.close() return obs_metadata
def test_naive_focal_plane_position(self): """ 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)
def testRaDec(self): """ Test that raDecFromNativeLonLat does invert nativeLonLatFromRaDec """ np.random.seed(42) nSamples = 100 rrList = np.random.random_sample(nSamples)*50.0 # because raDecFromNativeLonLat is only good # out to a zenith distance of ~ 70 degrees thetaList = np.random.random_sample(nSamples)*2.0*np.pi rrPointingList = np.random.random_sample(10)*50.0 thetaPointingList = np.random.random_sample(10)*2.0*np.pi mjdList = np.random.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(np.array([rr]), np.array([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 makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1, displacedRA=None, displacedDec=None, bandpass=None, m5=None, seeing=None, **kwargs): """ Make a test database to storing cartoon information for the test phoSim input catalog to use. The method will return an ObservationMetaData object guaranteed to encompass the objects in this database. @param [in] filename is a string indicating the name of the DB file to be created @param [in] size is the number of objects int he database @param [in] seedVal is the seed passed to the random number generator @param [in] radius is the radius (in degrees) of the field of view to be returned @param [in] bandpass is the bandpas(es) of the observation to be passed to ObservationMetaData (optional) @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData (optional) @param [in] seeing is the seeing value(s) in arcseconds to be passed to ObservationMetaData (optional) @param [in] displacedRA/Dec are numpy arrays that indicate where (in relation to the center of the field of view) objects should be placed. These coordinates are in degrees. Specifying either of these paramters will overwrite size. If you only specify one of these parameters, the other will be set randomly. These parameters are optional. """ if os.path.exists(filename): os.unlink(filename) #just an example of some valid SED file names galaxy_seds = ['Const.80E07.02Z.spec','Inst.80E07.002Z.spec','Burst.19E07.0005Z.spec'] agn_sed = 'agn.spec' star_seds = ['km20_5750.fits_g40_5790','m2.0Full.dat','bergeron_6500_85.dat_6700'] numpy.random.seed(seedVal) if displacedRA is not None and displacedDec is not None: if len(displacedRA) != len(displacedDec): raise RuntimeError("WARNING in makePhoSimTestDB displacedRA and displacedDec have different lengths") if displacedRA is not None: size = len(displacedRA) elif displacedDec is not None: size = len(displacedDec) #create the ObservationMetaData object mjd = 52000.0 alt = numpy.pi/2.0 az = 0.0 band = 'r' testSite = Site() centerRA, centerDec = raDecFromAltAz(alt,az,testSite.longitude,testSite.latitude,mjd) rotTel = getRotTelPos(centerRA, centerDec, testSite.longitude, testSite.latitude, mjd, 0.0) obsDict = calcObsDefaults(centerRA, centerDec, alt, az, rotTel, mjd, band, testSite.longitude, testSite.latitude) obsDict['Opsim_expmjd'] = mjd phoSimMetaData = OrderedDict([ (k, (obsDict[k],numpy.dtype(type(obsDict[k])))) for k in obsDict if k!='Opsim_filter' or bandpass is None ]) obs_metadata = ObservationMetaData(boundType = 'circle', boundLength = 2.0*radius, phoSimMetaData=phoSimMetaData, site=testSite, bandpassName=bandpass, m5=m5, seeing=seeing) #Now begin building the database. #First create the tables. conn = sqlite3.connect(filename) c = conn.cursor() try: c.execute('''CREATE TABLE galaxy_bulge (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real, sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int, ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''') conn.commit() except: raise RuntimeError("Error creating galaxy_bulge table.") try: c.execute('''CREATE TABLE galaxy (galtileid int, galid int, ra real, dec real, bra real, bdec real, dra real, ddec real, agnra real, agndec real, magnorm_bulge, magnorm_disk, magnorm_agn, sedname_bulge text, sedname_disk text, sedname_agn text, varParamStr text, a_b real, b_b real, pa_bulge real, bulge_n int, a_d real, b_d real, pa_disk real, disk_n int, ext_model_b text, av_b real, rv_b real, ext_model_d text, av_d real, rv_d real, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''') conn.commit() except: raise RuntimeError("Error creating galaxy table.") try: c.execute('''CREATE TABLE galaxy_agn (galtileid int, galid int, agnra real, agndec real, ra real, dec real, magnorm_agn real, sedname_agn text, varParamStr text, u_ab real, g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''') except: raise RuntimeError("Error creating galaxy_agn table.") try: c.execute('''CREATE TABLE starsALL_forceseek (simobjid int, ra real, decl real, magNorm real, mudecl real, mura real, galacticAv real, vrad real, varParamStr text, sedFilename text, parallax real)''') except: raise RuntimeError("Error creating starsALL_forceseek table.") #Now generate the data to be stored in the tables. rr = numpy.random.sample(size)*numpy.radians(radius) theta = numpy.random.sample(size)*2.0*numpy.pi if displacedRA is None: ra = numpy.degrees(centerRA + rr*numpy.cos(theta)) else: ra = numpy.degrees(centerRA) + displacedRA if displacedDec is None: dec = numpy.degrees(centerDec + rr*numpy.sin(theta)) else: dec = numpy.degrees(centerDec) + displacedDec bra = numpy.radians(ra+numpy.random.sample(size)*0.01*radius) bdec = numpy.radians(dec+numpy.random.sample(size)*0.01*radius) dra = numpy.radians(ra + numpy.random.sample(size)*0.01*radius) ddec = numpy.radians(dec + numpy.random.sample(size)*0.01*radius) agnra = numpy.radians(ra + numpy.random.sample(size)*0.01*radius) agndec = numpy.radians(dec + numpy.random.sample(size)*0.01*radius) magnorm_bulge = numpy.random.sample(size)*4.0 + 17.0 magnorm_disk = numpy.random.sample(size)*5.0 + 17.0 magnorm_agn = numpy.random.sample(size)*5.0 + 17.0 b_b = numpy.random.sample(size)*0.2 a_b = b_b+numpy.random.sample(size)*0.05 b_d = numpy.random.sample(size)*0.5 a_d = b_d+numpy.random.sample(size)*0.1 BulgeHalfLightRadius = numpy.random.sample(size)*0.2 DiskHalfLightRadius = numpy.random.sample(size)*0.5 pa_bulge = numpy.random.sample(size)*360.0 pa_disk = numpy.random.sample(size)*360.0 av_b = numpy.random.sample(size)*0.4 av_d = numpy.random.sample(size)*0.4 rv_b = numpy.random.sample(size)*0.1 + 3.0 rv_d = numpy.random.sample(size)*0.1 + 3.0 u_ab = numpy.random.sample(size)*4.0 + 17.0 g_ab = numpy.random.sample(size)*4.0 + 17.0 r_ab = numpy.random.sample(size)*4.0 + 17.0 i_ab = numpy.random.sample(size)*4.0 + 17.0 z_ab = numpy.random.sample(size)*4.0 + 17.0 y_ab = numpy.random.sample(size)*4.0 +17.0 redshift = numpy.random.sample(size)*2.0 t0_mjd = mjd - numpy.random.sample(size)*1000.0 agn_tau = numpy.random.sample(size)*1000.0 + 1000.0 agnSeed = numpy.random.random_integers(low=2, high=4000, size=size) agn_sfu = numpy.random.sample(size) agn_sfg = numpy.random.sample(size) agn_sfr = numpy.random.sample(size) agn_sfi = numpy.random.sample(size) agn_sfz = numpy.random.sample(size) agn_sfy = numpy.random.sample(size) rrStar = numpy.random.sample(size)*numpy.radians(radius) thetaStar = numpy.random.sample(size)*2.0*numpy.pi if displacedRA is None: raStar = centerRA + rrStar*numpy.cos(thetaStar) else: raStar = centerRA + numpy.radians(displacedRA) if displacedDec is None: decStar = centerDec + rrStar*numpy.sin(thetaStar) else: decStar = centerDec + numpy.radians(displacedDec) raStar = numpy.degrees(raStar) decStar = numpy.degrees(decStar) magnormStar = numpy.random.sample(size)*4.0 + 17.0 mudecl = numpy.random.sample(size)*0.0001 mura = numpy.random.sample(size)*0.0001 galacticAv = numpy.random.sample(size)*0.05*3.1 vrad = numpy.random.sample(size)*1.0 parallax = 0.00045+numpy.random.sample(size)*0.00001 period = numpy.random.sample(size)*20.0 amp = numpy.random.sample(size)*5.0 #write the data to the tables. for i in range(size): cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)], a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i]) c.execute(cmd) varParam = {'varMethodName':'applyAgn', 'pars':{'agn_tau':round(agn_tau[i],4), 't0_mjd':round(t0_mjd[i],4), 'agn_sfu':round(agn_sfu[i],4), 'agn_sfg':round(agn_sfg[i],4), 'agn_sfr':round(agn_sfr[i],4), 'agn_sfi':round(agn_sfi[i],4), 'agn_sfz':round(agn_sfz[i],4), 'agn_sfy':round(agn_sfy[i],4), 'seed':int(agnSeed[i])}} paramStr = json.dumps(varParam) cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', '%s', '%s', %f, %f, %f, %i, %f, %f, %f, %i, '%s', %f, %f, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i], magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i], galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed, paramStr, a_b[i], b_b[i], pa_bulge[i], 4, a_d[i], b_d[i], pa_disk[i], 1, 'CCM', av_b[i], rv_b[i], 'CCM', av_d[i], rv_d[i], u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) c.execute(cmd) cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s', %f, %f, %f, %f, %f, %f, %f)''' %\ (i, i, agnra[i], agndec[i], ra[i], dec[i], magnorm_agn[i], agn_sed, paramStr, u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i]) c.execute(cmd) varParam = {'varMethodName':'testVar', 'pars':{'period':period[i], 'amplitude':amp[i]}} paramStr = json.dumps(varParam) cmd = '''INSERT INTO starsALL_forceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f)''' %\ (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i], galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i]) c.execute(cmd) conn.commit() conn.close() return obs_metadata