예제 #1
0
    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)
예제 #2
0
 def setUp(self):
     self.tempDB = os.path.join(self.scratch_dir, 'PhoSimTestDatabase.db')
     self.obs_metadata = makePhoSimTestDB(size=10, filename=self.tempDB)
     self.bulgeDB = testGalaxyBulgeDBObj(driver='sqlite',
                                         database=self.tempDB)
     self.diskDB = testGalaxyDiskDBObj(driver='sqlite',
                                       database=self.tempDB)
     self.agnDB = testGalaxyAgnDBObj(driver='sqlite', database=self.tempDB)
     self.starDB = testStarsDBObj(driver='sqlite', database=self.tempDB)
     filter_translation = {'u': 0, 'g': 1, 'r': 2, 'i': 3, 'z': 4, 'y': 5}
     alt, az, pa = altAzPaFromRaDec(self.obs_metadata.pointingRA,
                                    self.obs_metadata.pointingDec,
                                    self.obs_metadata,
                                    includeRefraction=False)
     self.control_header = [
         'moondec %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['moondec']),
         'rottelpos %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['rottelpos']),
         'declination %.17f\n' % self.obs_metadata.pointingDec,
         'moonalt %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['moonalt']),
         'rotskypos %.17f\n' % self.obs_metadata.rotSkyPos,
         'moonra %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['moonra']),
         'sunalt %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['sunalt']),
         'mjd %.17f\n' % (self.obs_metadata.mjd.TAI + 16.5 / 86400.0),
         'azimuth %.17f\n' % az,
         'rightascension %.17f\n' % self.obs_metadata.pointingRA,
         'dist2moon %.7f\n' %
         np.degrees(self.obs_metadata.OpsimMetaData['dist2moon']),
         'filter %d\n' % filter_translation[self.obs_metadata.bandpass],
         'altitude %.17f\n' % alt
     ]
예제 #3
0
    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)
예제 #4
0
    def test_degrees(self):
        nside = 64
        hpids = np.arange(hp.nside2npix(nside))
        ra, dec = utils.hpid2RaDec(nside, hpids)
        mjd = 59852.
        obs = utils.ObservationMetaData(mjd=mjd)

        alt1, az1, pa1 = utils.altAzPaFromRaDec(ra, dec, obs)

        alt2, az2 = utils.approx_RaDec2AltAz(ra, dec, obs.site.latitude,
                                             obs.site.longitude, mjd)

        # Check that the fast is similar to the more precice transform
        tol = 2  # Degrees
        tol_mean = 1.
        separations = utils.angularSeparation(az1, alt1, az2, alt2)
        self.assertLess(np.max(separations), tol)
        self.assertLess(np.mean(separations), tol_mean)

        # Check that the fast can nearly round-trip
        ra_back, dec_back = utils.approx_altAz2RaDec(alt2, az2, obs.site.latitude,
                                                     obs.site.longitude, mjd)
        separations = utils.angularSeparation(ra, dec, ra_back, dec_back)
        self.assertLess(np.max(separations), tol)
        self.assertLess(np.mean(separations), tol_mean)
예제 #5
0
    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)
예제 #6
0
    def test_degrees(self):
        nside = 64
        hpids = np.arange(hp.nside2npix(nside))
        ra, dec = utils.hpid2RaDec(nside, hpids)
        mjd = 59852.
        obs = utils.ObservationMetaData(mjd=mjd)

        alt1, az1, pa1 = utils.altAzPaFromRaDec(ra, dec, obs)

        alt2, az2 = utils.approx_RaDec2AltAz(ra, dec, obs.site.latitude,
                                             obs.site.longitude, mjd)

        # Check that the fast is similar to the more precice transform
        tol = 2  # Degrees
        tol_mean = 1.
        separations = utils.angularSeparation(az1, alt1, az2, alt2)
        self.assertLess(np.max(separations), tol)
        self.assertLess(np.mean(separations), tol_mean)

        # Check that the fast can nearly round-trip
        ra_back, dec_back = utils.approx_altAz2RaDec(alt2, az2,
                                                     obs.site.latitude,
                                                     obs.site.longitude, mjd)
        separations = utils.angularSeparation(ra, dec, ra_back, dec_back)
        self.assertLess(np.max(separations), tol)
        self.assertLess(np.mean(separations), tol_mean)
예제 #7
0
    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)))
예제 #8
0
    def testAltAzPaFromRaDec(self):
        mjd = 57432.7
        obs = ObservationMetaData(mjd=mjd,
                                  site=Site(longitude=self.lon,
                                            latitude=self.lat,
                                            name='LSST'))

        altRad, azRad, paRad = utils._altAzPaFromRaDec(self.raList,
                                                       self.decList, obs)

        altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(np.degrees(self.raList),
                                                      np.degrees(self.decList),
                                                      obs)

        np.testing.assert_array_almost_equal(altRad, np.radians(altDeg), 10)
        np.testing.assert_array_almost_equal(azRad, np.radians(azDeg), 10)
        np.testing.assert_array_almost_equal(paRad, np.radians(paDeg), 10)

        altRad, azRad, paRad = utils._altAzPaFromRaDec(self.raList,
                                                       self.decList, obs)

        altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(np.degrees(self.raList),
                                                      np.degrees(self.decList),
                                                      obs)

        np.testing.assert_array_almost_equal(altRad, np.radians(altDeg), 10)
        np.testing.assert_array_almost_equal(azRad, np.radians(azDeg), 10)
        np.testing.assert_array_almost_equal(paRad, np.radians(paDeg), 10)

        for ra, dec, in zip(self.raList, self.decList):
            altRad, azRad, paRad = utils._altAzPaFromRaDec(ra, dec, obs)
            altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(
                np.degrees(ra), np.degrees(dec), obs)

            self.assertAlmostEqual(altRad, np.radians(altDeg), 10)
            self.assertAlmostEqual(azRad, np.radians(azDeg), 10)
            self.assertAlmostEqual(paRad, np.radians(paDeg), 10)
    def testAltAzPaFromRaDec(self):
        mjd = 57432.7
        obs = ObservationMetaData(mjd=mjd, site=Site(longitude=self.lon, latitude=self.lat, name='LSST'))

        altRad, azRad, paRad = utils._altAzPaFromRaDec(self.raList, self.decList, obs)

        altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(np.degrees(self.raList),
                                                      np.degrees(self.decList),
                                                      obs)


        np.testing.assert_array_almost_equal(altRad, np.radians(altDeg), 10)
        np.testing.assert_array_almost_equal(azRad, np.radians(azDeg), 10)
        np.testing.assert_array_almost_equal(paRad, np.radians(paDeg), 10)


        altRad, azRad, paRad = utils._altAzPaFromRaDec(self.raList, self.decList, obs)

        altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(np.degrees(self.raList),
                                                      np.degrees(self.decList),
                                                      obs)


        np.testing.assert_array_almost_equal(altRad, np.radians(altDeg), 10)
        np.testing.assert_array_almost_equal(azRad, np.radians(azDeg), 10)
        np.testing.assert_array_almost_equal(paRad, np.radians(paDeg), 10)


        for ra, dec, in zip(self.raList, self.decList):
            altRad, azRad, paRad = utils._altAzPaFromRaDec(ra, dec, obs)
            altDeg, azDeg, paDeg = utils.altAzPaFromRaDec(np.degrees(ra),
                                                          np.degrees(dec),
                                                          obs)

            self.assertAlmostEqual(altRad, np.radians(altDeg), 10)
            self.assertAlmostEqual(azRad, np.radians(azDeg), 10)
            self.assertAlmostEqual(paRad, np.radians(paDeg), 10)
예제 #10
0
    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)
예제 #11
0
    def run(self, simData):
        # Add new columns to simData.
        simData = self._addStackers(simData)
        simData['nObservatories'] = 0

        for obs in self.telescopes:
            obsCount = simData['nObservatories']*0
            for step in self.timeSteps:
                alt,az,pa = altAzPaFromRaDec(simData[self.raCol], simData[self.decCol],
                                             np.radians(obs['lon']), np.radians(obs['lat']),
                                             simData[self.expMJDCol]+step/24.)
                airmass = 1./(np.cos(np.pi/2.-alt))
                good = np.where((airmass <= self.airmassLimit) & (airmass >= 1.) )
                obsCount[good] = 1

            simData['nObservatories'] += obsCount


        return simData
예제 #12
0
    def run(self, simData):
        # Add new columns to simData.
        simData = self._addStackers(simData)
        simData['nObservatories'] = 0

        for obs in self.telescopes:
            obsCount = simData['nObservatories'] * 0
            for step in self.timeSteps:
                alt, az, pa = altAzPaFromRaDec(
                    simData[self.raCol], simData[self.decCol],
                    np.radians(obs['lon']), np.radians(obs['lat']),
                    simData[self.expMJDCol] + step / 24.)
                airmass = 1. / (np.cos(np.pi / 2. - alt))
                good = np.where((airmass <= self.airmassLimit)
                                & (airmass >= 1.))
                obsCount[good] = 1

            simData['nObservatories'] += obsCount

        return simData
def get_val_from_obs(tag, obs):
    """
    tag is the name of a data column

    obs is an ObservationMetaData

    returns the value of 'tag' for this obs
    """
    if tag == 'fieldRA':
        return obs.pointingRA
    elif tag == 'fieldDec':
        return obs.pointingDec
    elif tag == 'rotSkyPos':
        return obs.rotSkyPos
    elif tag == 'expMJD':
        return obs.mjd.TAI
    elif tag == 'airmass':
        alt, az, pa = altAzPaFromRaDec(obs.pointingRA, obs.pointingDec, obs)
        return 1.0 / (np.cos(np.pi / 2.0 - np.radians(alt)))
    elif tag == 'm5':
        return obs.m5[obs.bandpass]
    elif tag == 'skyBrightness':
        return obs.skyBrightness
    elif tag == 'seeing':
        return obs.seeing[obs.bandpass]
    elif tag == 'telescopeFilter':
        return obs.bandpass

    transforms = {
        'dist2Moon': np.degrees,
        'moonAlt': np.degrees,
        'sunAlt': np.degrees,
        'moonRA': np.degrees,
        'moonDec': np.degrees
    }

    if tag in transforms:
        return transforms[tag](obs.OpsimMetaData[tag])

    return obs.OpsimMetaData[tag]
def get_val_from_obs(tag, obs):
    """
    tag is the name of a data column

    obs is an ObservationMetaData

    returns the value of 'tag' for this obs
    """
    if tag == 'fieldRA':
        return obs.pointingRA
    elif tag == 'fieldDec':
        return obs.pointingDec
    elif tag == 'rotSkyPos':
        return obs.rotSkyPos
    elif tag == 'expMJD':
        return obs.mjd.TAI
    elif tag == 'airmass':
        alt, az, pa = altAzPaFromRaDec(obs.pointingRA, obs.pointingDec, obs)
        return 1.0/(np.cos(np.pi/2.0-np.radians(alt)))
    elif tag == 'm5':
        return obs.m5[obs.bandpass]
    elif tag == 'skyBrightness':
        return obs.skyBrightness
    elif tag == 'seeing':
        return obs.seeing[obs.bandpass]
    elif tag == 'telescopeFilter':
        return obs.bandpass

    transforms = {'dist2Moon': np.degrees,
                  'moonAlt': np.degrees,
                  'sunAlt': np.degrees,
                  'moonRA': np.degrees,
                  'moonDec': np.degrees}

    if tag in transforms:
        return transforms[tag](obs.OpsimMetaData[tag])

    return obs.OpsimMetaData[tag]
예제 #15
0
 def setUp(self):
     self.tempDB = os.path.join(self.scratch_dir, 'PhoSimTestDatabase.db')
     self.obs_metadata = makePhoSimTestDB(size=10, filename=self.tempDB)
     self.bulgeDB = testGalaxyBulgeDBObj(driver='sqlite', database=self.tempDB)
     self.diskDB = testGalaxyDiskDBObj(driver='sqlite', database=self.tempDB)
     self.agnDB = testGalaxyAgnDBObj(driver='sqlite', database=self.tempDB)
     self.starDB = testStarsDBObj(driver='sqlite', database=self.tempDB)
     filter_translation = {'u': 0, 'g': 1, 'r': 2, 'i': 3, 'z': 4, 'y': 5}
     alt, az, pa = altAzPaFromRaDec(self.obs_metadata.pointingRA,
                                    self.obs_metadata.pointingDec,
                                    self.obs_metadata, includeRefraction=False)
     self.control_header = ['moondec %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moondec']),
                            'rottelpos %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['rottelpos']),
                            'declination %.17f\n' % self.obs_metadata.pointingDec,
                            'moonalt %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moonalt']),
                            'rotskypos %.17f\n' % self.obs_metadata.rotSkyPos,
                            'moonra %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moonra']),
                            'sunalt %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['sunalt']),
                            'mjd %.17f\n' % (self.obs_metadata.mjd.TAI+16.5/86400.0),
                            'azimuth %.17f\n' % az,
                            'rightascension %.17f\n' % self.obs_metadata.pointingRA,
                            'dist2moon %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['dist2moon']),
                            'filter %d\n' % filter_translation[self.obs_metadata.bandpass],
                            'altitude %.17f\n' % alt]
    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
예제 #17
0
        umjd = np.unique(data['mjd'])

        left = np.searchsorted(data['mjd'], umjd)
        right = np.searchsorted(data['mjd'], umjd, side='right')

        altaz = np.zeros(data.size, dtype=zip(['alt', 'az'], [float] * 2))
        moonAlt = np.zeros(data.size, dtype=float)

        print 'computing alts and azs'

        for j, (le, ri, mjd) in enumerate(zip(left, right, umjd)):
            Observatory.date = mjd2djd(mjd)
            sun.compute(Observatory)
            alt, az, pa = altAzPaFromRaDec(data['ra'][le:ri],
                                           data['dec'][le:ri], telescope.lon,
                                           telescope.lat, mjd)
            az = wrapRA(az - sun.az)
            altaz['alt'][le:ri] += alt
            altaz['az'][le:ri] += az
            moon.compute(Observatory)
            moonAlt[le:ri] += moon.alt

        print 'making maps'
        good = np.where(moonAlt < 0)
        magMap[:, i] = healbin(altaz['az'][good],
                               altaz['alt'][good],
                               data['sky'][good],
                               nside=nside,
                               reduceFunc=np.median)
        rmsMap[:, i] = healbin(altaz['az'][good],
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)
    file_handle.write('altitude %.7f\n' % alt_pointing)
def write_phoSim_header(obs, file_handle, phosim_header_map):
    """
    Write the data contained in an ObservationMetaData as a header in a
    PhoSim InstanceCatalog.

    obs is the ObservationMetaData

    file_handle points to the catalog being written.
    """

    if phosim_header_map is None:
        raw_opsim_contents = ""

        if obs.OpsimMetaData is not None:
            sorted_opsim_metadata = list(obs.OpsimMetaData.keys())
            sorted_opsim_metadata.sort()
            for tag in sorted_opsim_metadata:
                raw_opsim_contents += "%s\n" % tag

        raise RuntimeError("You have tried to write a PhoSim InstanceCatalog without specifying "
                           "a phoSimHeaderMap in your call to write_catalog().\n"
                           "\n"
                           "A phoSimHeaderMap is a dict that maps between columns stored in the "
                           "OpSim database from which an ObservationMetaData was created and "
                           "header parameters expected by PhoSim.  The dict is keyed on the names of PhoSim "
                           "parameters.  The values of the dict are either straight values, in which "
                           "case those values are written to the PhoSim InstanceCatalog header, or tuples "
                           "containing the name of the OpSim column which should be use to calculate "
                           "the PhoSim parameter and any transformation needed to go from OpSim "
                           "units to PhoSim units (the transform is 'None' if no transform is needed).\n"
                           "\n"
                           "To add a phoSimHeaderMap, simple assign it to the 'phoSimHeaderMap' "
                           "member variable of your catalog with (for instance):\n"
                           "\n"
                           "myCat.phoSimHeaderMap = my_phosim_header_map\n"
                           "\n"
                           "Before calling write_catalog()\n"
                           "\n"
                           "The header parameters expected by PhoSim can be found at\n"
                           "\n"
                           "https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog\n"
                           "\n"
                           "The contents of the OpSim database's Summary table can be found at\n"
                           "\n"
                           "https://www.lsst.org/scientists/simulations/opsim/summary-table-column-descriptions-v335\n"
                           "\n"
                           "If you do not wish to use any of these columns, you can just pass in an empty "
                           "dict.  If you want to use a pre-made mapping, use the DefaultPhoSimHeaderMap "
                           "imported from lsst.sims.catUtils.exampleCatalogDefinitions\n"
                           "\n"
                           "Note: do not specify ra, dec, alt, az, mjd, filter, or rotSkyPos in your "
                           "phoSimHeaderMap.  These are handled directly by the ObservationMetaData "
                           "to ensure self-consistency.\n"
                           "\n"
                           "For reference, the OpSim columns you can choose to map (i.e. those contained "
                           "in your ObservationMetaData) are:\n\n" +
                           raw_opsim_contents +
                           "\n(Even if your ObservationMetaData contains no extra OpSim Columns "
                           "you may wish to consider adding default PhoSim parameters through "
                           "the phoSimHeaderMap)")

    try:

        # PhoSim wants the MJD at the middle of the visit (i.e. between the two exposures
        # in our two-snap model).  OpSim gives the MJD at the start of the visit.
        # below we calculate the change in MJD necessary to transform the OpSim value
        # into the PhoSim value
        vistime = evaluate_phosim_header('vistime', phosim_header_map, obs)
        if vistime is not None:
            delta_t = 0.5*vistime
        else:
            delta_t = 16.5  # half of the default 33 seconds

        file_handle.write('rightascension %.7f\n' % obs.pointingRA)
        file_handle.write('declination %.7f\n' % obs.pointingDec)
        file_handle.write('mjd %.7f\n' % (obs.mjd.TAI + delta_t/86400.0))
        alt, az, pa = altAzPaFromRaDec(obs.pointingRA, obs.pointingDec, obs, includeRefraction=False)
        file_handle.write('altitude %.7f\n' % alt)
        file_handle.write('azimuth %.7f\n' % az)
        file_handle.write('filter %d\n' %
                          {'u': 0, 'g': 1, 'r': 2, 'i': 3, 'z': 4, 'y': 5}[obs.bandpass])

        file_handle.write('rotskypos %.7f\n' % obs.rotSkyPos)
    except:
        print("\n\n")
        print("The ObservationMetaData you tried to write a PhoSim header from")
        print("lacks one of the required parameters")
        print("(pointingRA, pointingDec, mjd, bandpass, rotSkyPos)\n")
        raise

    # sort the header map keys so that PhoSim headers generated with the same
    # map always have parameters in the same order.
    sorted_header_keys = list(phosim_header_map.keys())
    sorted_header_keys.sort()

    for name in sorted_header_keys:
        val = evaluate_phosim_header(name, phosim_header_map, obs)
        if val is not None:
            if isinstance(val, float) or isinstance(val, np.float):
                file_handle.write('%s %.7f\n' % (name, val))
            elif isinstance(val, int) or isinstance(val, np.int):
                file_handle.write('%s %d\n' % (name, val))
            elif isinstance(val, int):
                file_handle.write('%s %ld\n' % (name, val))
            else:
                file_handle.write('%s %s\n' % (name, str(val)))
예제 #20
0
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)
        data.sort(order='mjd')

        umjd = np.unique(data['mjd'])

        left = np.searchsorted(data['mjd'], umjd)
        right = np.searchsorted(data['mjd'], umjd, side='right')

        altaz = np.zeros(data.size, dtype=zip(['alt','az'], [float]*2))
        moonAlt = np.zeros(data.size, dtype=float)

        print 'computing alts and azs'

        for j, (le, ri, mjd) in enumerate(zip(left,right,umjd)):
            Observatory.date = mjd2djd(mjd)
            sun.compute(Observatory)
            alt, az, pa = altAzPaFromRaDec(data['ra'][le:ri], data['dec'][le:ri],
                                           telescope.lon, telescope.lat, mjd)
            az = wrapRA(az - sun.az)
            altaz['alt'][le:ri] += alt
            altaz['az'][le:ri] += az
            moon.compute(Observatory)
            moonAlt[le:ri] += moon.alt

        print 'making maps'
        good = np.where(moonAlt < 0)
        magMap[:,i] = healbin(altaz['az'][good],altaz['alt'][good], data['sky'][good],
                              nside=nside, reduceFunc=np.median)
        rmsMap[:,i] = healbin(altaz['az'][good],altaz['alt'][good],data['sky'][good],
                              nside=nside, reduceFunc=robustRMS)

    print 'saving maps'
    np.savez('TwilightMaps/twiMaps_%s.npz' % filterName, magMap=magMap, rmsMap=rmsMap, sunAlts=sunAlts)
예제 #22
0
cmin = 18.
cmax = 21.5

zp = 12.11

#zps = []
counter = 0
maxI = np.size(dateIDs)
for i,dateID in enumerate(dateIDs):

    skydata, mjd = sb.allSkyDB(dateID , filt=band)#2744
    airmass = 1./np.cos(np.radians(90.-skydata['alt']))
    skydata = skydata[np.where((airmass < 2.1) & (airmass >= 1.))]

    alt,az,pa =  altAzPaFromRaDec(np.radians(skydata['ra']), np.radians(skydata['dec']),
                                  telescope.lon, telescope.lat, mjd)

    skyhp = healplots.healbin(az, alt, skydata['sky'], nside=nside)

    sm.setRaDecMjd(np.radians(skydata['ra']), np.radians(skydata['dec']), mjd, degrees=False)
    if sm.sunAlt < np.radians(-12.):
        sm.computeSpec()
        mags = sm.computeMags(canonDict[band])

        good = np.where(mags > 0)
        if np.size(good[0]) > 10:
            modelhp = healplots.healbin(az[good], alt[good], mags[good], nside=nside)
            zp = np.median(mags[good] - skydata['sky'][good])
            #zps.append(zp)
            fig = plt.figure(num=1)
            hp.mollview(skyhp+zp, rot=(0,90), sub=(2,1,1), fig=1, title='Cannon '+band+' mjd=%0.2f'%sm.mjd,
예제 #23
0
cmin = 18.
cmax = 21.5

zp = 12.11

#zps = []
counter = 0
maxI = np.size(dateIDs)
for i, dateID in enumerate(dateIDs):

    skydata, mjd = sb.allSkyDB(dateID, filt=band)  #2744
    airmass = 1. / np.cos(np.radians(90. - skydata['alt']))
    skydata = skydata[np.where((airmass < 2.1) & (airmass >= 1.))]

    alt, az, pa = altAzPaFromRaDec(np.radians(skydata['ra']),
                                   np.radians(skydata['dec']), telescope.lon,
                                   telescope.lat, mjd)

    skyhp = healplots.healbin(az, alt, skydata['sky'], nside=nside)

    sm.setRaDecMjd(np.radians(skydata['ra']),
                   np.radians(skydata['dec']),
                   mjd,
                   degrees=False)
    if sm.sunAlt < np.radians(-12.):
        sm.computeSpec()
        mags = sm.computeMags(canonDict[band])

        good = np.where(mags > 0)
        if np.size(good[0]) > 10:
            modelhp = healplots.healbin(az[good],
예제 #24
0
        for i_m in range(len(mags) - 1):
            print('%.4e ' % (mags[i_m] - mags[i_m + 1]))
        print('\n')

sed_names = rng.choice(sed_candidates, size=len(ra), replace=True)

opsim_meta_data = obs_root.OpsimMetaData

ref_made = False
ref_handle = None

mjd_grid = np.arange(59580.0, 59580.0 + 3652.5, 100.0)
mjd_good = []
for mjd in mjd_grid:
    obs_dummy = ObservationMetaData(mjd=mjd)
    alt, az, pa = altAzPaFromRaDec(obs_root.pointingRA, obs_root.pointingDec,
                                   obs_dummy)
    if alt > 40.0:
        mjd_good.append(mjd)
    if len(mjd_good) > 20:
        break

rot_sky_pos = rng.random_sample(len(mjd_good)) * 360.0
for i_mjd, mjd in enumerate(mjd_good):
    for i_filter in range(6):
        for all_same in (True, False):
            obs = ObservationMetaData(pointingRA=obs_root.pointingRA,
                                      pointingDec=obs_root.pointingDec,
                                      rotSkyPos=rot_sky_pos[i_mjd],
                                      mjd=mjd_good[i_mjd],
                                      site=obs_root.site,
                                      bandpassName='ugrizy'[i_filter])
예제 #25
0
    def write_fits_file(self,
                        outfile,
                        overwrite=True,
                        run_number=None,
                        lsst_num='LCA-11021_RTM-000',
                        compress=True,
                        image_type='SKYEXP',
                        added_keywords=None):
        """
        Write the processed eimage data as a multi-extension FITS file.

        Parameters
        ----------
        outfile: str
            Name of the output FITS file.
        overwrite: bool [True]
            Flag whether to overwrite an existing output file.
        run_number: int [None]
            Run number.  If None, then the visit number is used.
        compress: bool [True]
            Use RICE_1 compression for each image HDU.
        image_type: str ['SKYEXP']
            Image type to write to the `OBSTYPE` and `IMGTYPE` keywords.
        added_keywords: dict [None]
            Python dict of key/value pairs to add to the primary HDU.
            These will be set just before writing the FITS file, so
            they will override any existing keywords.
        """
        output = fits.HDUList(fits.PrimaryHDU())
        output[0].header = self.eimage[0].header
        # Since `.header` acts like an unordered dict when set like
        # this, we need to re-insert the WCSAXES keyword to precede
        # any other WCS keywords as stipulated in the FITS Standard,
        # version 4.0, section 8.2.
        wcsaxes = output[0].header['WCSAXES']
        del output[0].header['WCSAXES']
        output[0].header.insert(5, ('WCSAXES', wcsaxes, ''))
        if run_number is None:
            run_number = self.visit
        output[0].header['RUNNUM'] = str(run_number)
        output[0].header['DARKTIME'] = output[0].header['EXPTIME']
        output[0].header['TIMESYS'] = 'TAI'
        output[0].header['LSST_NUM'] = lsst_num
        output[0].header['TESTTYPE'] = 'IMSIM'
        output[0].header['IMGTYPE'] = image_type
        output[0].header['OBSTYPE'] = output[0].header['IMGTYPE']
        output[0].header['MONOWL'] = -1
        raft, ccd = output[0].header['CHIPID'].split('_')
        output[0].header['RAFTNAME'] = raft
        output[0].header['SENSNAME'] = ccd
        output[0].header['OUTFILE'] = os.path.basename(outfile)
        # Add boresight pointing angles and rotskypos (angle of sky
        # relative to Camera coordinates) from which obs_lsst can
        # infer the CCD-wide WCS.
        output[0].header['RATEL'] = self.ratel
        output[0].header['DECTEL'] = self.dectel
        output[0].header['ROTANGLE'] = self.rotangle

        # Add various keywords needed for jointcal, including  hour angle
        # and airmass values at the start and end of the observation.
        mjd_obs = output[0].header['MJD-OBS']
        mjd_end = astropy.time.Time(output[0].header['DATE-END'],
                                    format='isot',
                                    scale='tai').mjd
        observatory = LsstObservatory()
        output[0].header['HASTART'] \
            = getHourAngle(observatory, mjd_obs, self.ratel)
        output[0].header['HAEND'] \
            = getHourAngle(observatory, mjd_end, self.ratel)

        # Set the airmass from the start of the observation using the
        # opsim db value and compute the airmass from the altitude at
        # the end of the observation.
        output[0].header['AMSTART'] = output[0].header['AIRMASS']
        obs_md = ObservationMetaData(mjd=mjd_end,
                                     pointingRA=self.ratel,
                                     pointingDec=self.dectel,
                                     rotSkyPos=self.rotangle)
        alt_end, _, _ = altAzPaFromRaDec(self.ratel, self.dectel, obs_md)
        output[0].header['AMEND'] = airmass(alt_end)

        # Write the added_keywords.
        if added_keywords is not None:
            for key, value in added_keywords.items():
                output[0].header[key] = value

        # Write the seed used by the read noise and dark current.
        output[0].header['RN_SEED'] = self.seed
        # Use seg_ids to write the image extensions in the order
        # specified by LCA-10140.
        seg_ids = '10 11 12 13 14 15 16 17 07 06 05 04 03 02 01 00'.split()
        for seg_id in seg_ids:
            amp_name = '_C'.join((self.sensor_id, seg_id))
            output.append(self.get_amplifier_hdu(amp_name, compress=compress))
            output[-1].header['EXTNAME'] = 'Segment%s' % seg_id

        # Set the imSim version and LSST Stack product versions and
        # tags in the primary HDU.
        output[0].header.update(get_version_keywords())
        self.fits_atomic_write(output, outfile, overwrite=overwrite)
예제 #26
0
if __name__ == "__main__":

    rng = np.random.RandomState(61)
    ra_list = rng.random_sample(50) * 360.0
    dec_list = rng.random_sample(50) * 180.0 - 90.0
    rot_list = rng.random_sample(50) * 360.0
    mjd_list = rng.random_sample(50) * 10000.0 + 59580.0

    for ra, dec, rot, mjd in zip(ra_list, dec_list, rot_list, mjd_list):

        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  rotSkyPos=rot,
                                  mjd=mjd)

        alt, az, pa = altAzPaFromRaDec(ra, dec, obs)

        ra_corner, dec_corner = fovCorners(obs, side_length=20.0)
        print ra_corner
        print dec_corner

        max_orthogonal = -1.0

        for ix1, ix2 in zip((0, 0, 1, 2), (1, 3, 2, 3)):
            dd = distance_in_arcminutes(ra_corner[ix1], dec_corner[ix1],
                                        ra_corner[ix2], dec_corner[ix2])

            if np.abs(dd - 20.0) > max_orthogonal:
                max_orthogonal = np.abs(dd - 20.0)

        dd = distance_in_arcminutes(ra_corner[2], dec_corner[2], ra_corner[0],