def test_one_by_one(self):
        """
        test that running RA, Dec pairs in one at a time gives the same
        results as running them in in batches
        """

        ra = 145.0
        dec = -25.0
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=59580.0,
                                  rotSkyPos=113.0)
        rng = np.random.RandomState(100)
        theta = rng.random_sample(100) * 2.0 * np.pi
        rr = rng.random_sample(len(theta)) * 2.0
        ra_list = ra + rr * np.cos(theta)
        dec_list = dec + rr * np.sin(theta)
        name_control = chipNameFromRaDec(ra_list,
                                         dec_list,
                                         obs_metadata=obs,
                                         camera=self.camera)
        is_none = 0
        for ra, dec, name in zip(ra_list, dec_list, name_control):
            test_name = chipNameFromRaDec(ra,
                                          dec,
                                          obs_metadata=obs,
                                          camera=self.camera)
            self.assertEqual(test_name, name)
            if test_name is None:
                is_none += 1

        self.assertGreater(is_none, 0)
        self.assertLess(is_none, (3 * len(ra_list)) // 4)
    def test_outside_radius(self):
        """
        Test that methods can gracefully handle points
        outside of the focal plane
        """
        rng = np.random.RandomState(7123)
        ra = 145.0
        dec = -25.0
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=59580.0,
                                  rotSkyPos=113.0)

        rr = rng.random_sample(100) * 5.0
        self.assertGreater(rr.max(), 4.5)
        theta = rng.random_sample(100) * 2.0 * np.pi
        ra_vec = ra + rr * np.cos(theta)
        dec_vec = dec + rr * np.sin(theta)
        chip_name_list = chipNameFromRaDec(ra_vec,
                                           dec_vec,
                                           obs_metadata=obs,
                                           camera=self.camera)

        rr = angularSeparation(ra, dec, ra_vec, dec_vec)

        ct_none = 0
        for rr, name in zip(rr, chip_name_list):
            if rr > 2.0:
                self.assertIsNone(name)
                ct_none += 1
        self.assertGreater(ct_none, 0)
Exemple #3
0
    def ssoInCameraFov(self, ephems, obsData):
        """Determine which observations are within the actual camera footprint for a series of observations.
        Note that ephems and obsData must be the same length.

        Parameters
        ----------
        ephems : np.recarray
            Ephemerides for the objects.
        obsData : np.recarray
            Observation pointings.

        Returns
        -------
        np.ndarray
            Returns the indexes of the numpy array of the object observations which are inside the fov.
        """
        if not hasattr(self, 'camera'):
            self._setupCamera()
        epoch = 2000.0
        # See if the object is within 'rFov' of the center of the boresight.
        idxObsRough = self._ssoInCircleFov(ephems, obsData, rFov=2.1)
        # Then test for the camera footprint exactly.
        idxObs = []
        for idx in idxObsRough:
            mjd_date = obsData[idx][self.obsTimeCol]
            if self.obsTimeScale == 'TAI':
                mjd = ModifiedJulianDate(TAI=mjd_date)
            elif self.obsTimeScale == 'UTC':
                mjd = ModifiedJulianDate(UTC=mjd_date)
            else:
                warnings.warn(
                    'Expected timescale of TAI or UTC, but did not match. Using TAI.'
                )
                mjd = ModifiedJulianDate(TAI=mjd_date)
            if not self.obsDegrees:
                obs_metadata = ObservationMetaData(
                    pointingRA=np.degrees(obsData[idx][self.obsRA]),
                    pointingDec=np.degrees(obsData[idx][self.obsDec]),
                    rotSkyPos=np.degrees(obsData[idx][self.obsRotSkyPos]),
                    mjd=mjd)
            else:
                obs_metadata = ObservationMetaData(
                    pointingRA=obsData[idx][self.obsRA],
                    pointingDec=obsData[idx][self.obsDec],
                    rotSkyPos=obsData[idx][self.obsRotSkyPos],
                    mjd=mjd)
            # Catch the warnings from astropy about the time being in the future.
            with warnings.catch_warnings(record=False):
                warnings.simplefilter('ignore')
                chipName = chipNameFromRaDec(ra=ephems['ra'][idx],
                                             dec=ephems['dec'][idx],
                                             epoch=epoch,
                                             obs_metadata=obs_metadata,
                                             camera=self.camera)
            if chipName is not None:
                tt = self.ccd_type_dict[self.camera[chipName].getType()]
                if tt == 'science':
                    idxObs.append(idx)
        idxObs = np.array(idxObs, int)
        return idxObs
    def test_chip_name_from_ra_dec_degrees(self):
        """
        test that chipNameFromRaDecLSST agrees with chipNameFromRaDec
        """

        n_obj = 1000
        raP = 112.1
        decP = -34.1
        obs = ObservationMetaData(pointingRA=raP, pointingDec=decP,
                                  rotSkyPos=45.0, mjd=43000.0)

        rng = np.random.RandomState(8731)
        rr = rng.random_sample(n_obj)*1.75
        theta = rng.random_sample(n_obj)*2.0*np.pi
        ra_list = raP + rr*np.cos(theta)
        dec_list = decP + rr*np.sin(theta)
        control_name_list = chipNameFromRaDec(ra_list, dec_list,
                                              obs_metadata=obs,
                                              camera=self.camera)

        test_name_list = chipNameFromRaDecLSST(ra_list, dec_list,
                                               obs_metadata=obs)

        try:
            np.testing.assert_array_equal(control_name_list.astype(str),
                                          test_name_list.astype(str))
        except AssertionError:
            n_problematic = 0
            for ii, (c_n, t_n) in enumerate(zip(control_name_list.astype(str), test_name_list.astype(str))):
                if c_n != t_n:
                    x_pix, y_pix = pixelCoordsFromRaDecLSST(ra_list[ii], dec_list[ii], obs_metadata=obs)
                    if c_n != 'None':
                        n_problematic += 1
            if n_problematic > 0:
                raise

        self.assertLessEqual(len(np.where(np.char.rfind(test_name_list.astype(str), 'None') >= 0)[0]),
                             n_obj/10)

        # test that exceptions are raised when incomplete ObservationMetaData are used
        obs = ObservationMetaData(pointingRA=raP, pointingDec=decP, mjd=59580.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("rotSkyPos", context.exception.args[0])

        obs = ObservationMetaData(pointingRA=raP, pointingDec=decP, rotSkyPos=35.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("mjd", context.exception.args[0])

        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list)
        self.assertIn("ObservationMetaData", context.exception.args[0])

        # check that exceptions are raised when ra_list, dec_list are of the wrong shape
        obs = ObservationMetaData(pointingRA=raP, pointingDec=decP, rotSkyPos=24.0, mjd=43000.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list[:5], obs_metadata=obs)
        self.assertIn("chipNameFromRaDecLSST", context.exception.args[0])
    def test_chipName(self):
        """
        Verify that chipNameFromRaDecLSST has not changed.
        """
        chip_name_arr = chipNameFromRaDec(self.pix_data['ra'],
                                          self.pix_data['dec'],
                                          obs_metadata=self.obs,
                                          camera=self.camera)

        np.testing.assert_array_equal(chip_name_arr, self.pix_data['name'])
    def test_chipName(self):
        """
        Verify that chipNameFromRaDecLSST has not changed.
        """
        chip_name_arr = chipNameFromRaDec(self.pix_data['ra'],
                                          self.pix_data['dec'],
                                          obs_metadata=self.obs,
                                          camera=self.camera)

        np.testing.assert_array_equal(chip_name_arr, self.pix_data['name'])
    def test_chip_center(self):
        """
        Test that, if we ask for the chip at the bore site,
        we get back 'R:2,2 S:1,1'
        """

        ra = 145.0
        dec = -25.0
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=59580.0,
                                  rotSkyPos=113.0)

        name = chipNameFromRaDec(ra, dec, obs_metadata=obs, camera=self.camera)
        self.assertEqual(name, 'R22_S11')
Exemple #8
0
    def inCameraFov(self, ephems, obsData, epoch=2000.0, timeCol='observationStartMJD'):
        """Determine which observations are within the actual camera footprint for a series of observations.

        Parameters
        ----------
        ephems : np.recarray
            Ephemerides for the objects, with RA and Dec as 'ra' and 'dec' columns (in degrees).
        obsData : np.recarray
            Observation pointings, with RA and Dec as 'ra' and 'dec' columns (in degrees).
            The telescope rotation angle should be in 'rotSkyPos' (in degrees), and the time of each
            pointing should be in the 'expMJD' column.
        epoch: float, opt
            The epoch of the ephemerides and pointing data. Default 2000.0.

        Returns
        -------
        np.ndarray
            Returns the indexes of the numpy array of the object observations which are inside the fov.
        """
        # See if the object is within 'rFov' of the center of the boresight.
        sep = angularSeparation(ephems['ra'], ephems['dec'], obsData['ra'], obsData['dec'])
        idxObsRough = np.where(sep <= 2.1)[0]
        # Or go on and use the camera footprint.
        idxObs = []
        for idx in idxObsRough:
            mjd_date = obsData[idx][timeCol]
            mjd = ModifiedJulianDate(TAI=mjd_date)
            obs_metadata = ObservationMetaData(pointingRA=obsData[idx]['ra'],
                                               pointingDec=obsData[idx]['dec'],
                                               rotSkyPos=obsData[idx]['rotSkyPos'],
                                               mjd=mjd)
            # Catch the warnings from astropy about the time being in the future.
            with warnings.catch_warnings(record=False):
                warnings.simplefilter('ignore')
                chipName = chipNameFromRaDec(ra=ephems['ra'][idx],dec=ephems['dec'][idx],
                                                  epoch=epoch, obs_metadata=obs_metadata, camera=self.camera)
            if chipName != None:
                tt = self.ccd_type_dict[self.camera[chipName].getType()]
                if tt == 'science':
                    idxObs.append(idx)
        idxObs = np.array(idxObs, int)
        return idxObs
Exemple #9
0
def select_by_chip_name(objs,
                        chip_name,
                        obs_md,
                        camera,
                        logger=default_logger):
    """
    Select only objects that are on the specified chip.
    Parameters
    ----------
    objs : pandas.DataFrame
        DataFrame of phosim objects.
    obs_md : lsst.sims.utils.ObservationMetaData
        Obsevation metadata extracted from the phosim commands.
    camera : lsst.afw.cameraGeom.camera.Camera
        The camera instance from lsst.obs.lsstSim.LsstSimMapper().
    logger : logging.Logger, optional
        The logger to use.

    Returns
    -------
    pandas.DataFrame
        The DataFrame containing down-selected objects.
    """
    t0 = time.time()
    my_objs = objs.copy(deep=True)
    my_objs['chip_name'] = coordUtils.chipNameFromRaDec(my_objs['ra'].values,
                                                        my_objs['dec'].values,
                                                        camera=camera,
                                                        obs_metadata=obs_md)
    my_objs = my_objs.query('chip_name=="%s"' % chip_name)
    del my_objs['chip_name']
    logger.debug('select_by_chip_name:\n  elapsed time: %f s',
                 time.time() - t0)
    logger.debug('  # objects remaining: %i', len(my_objs))
    logger.debug(mem_use_message())
    return my_objs
    def test_chip_name_from_ra_dec_degrees(self):
        """
        test that chipNameFromRaDecLSST agrees with chipNameFromRaDec
        """

        n_obj = 1000
        raP = 112.1
        decP = -34.1
        obs = ObservationMetaData(pointingRA=raP,
                                  pointingDec=decP,
                                  rotSkyPos=45.0,
                                  mjd=43000.0)

        rng = np.random.RandomState(8731)
        rr = rng.random_sample(n_obj) * 1.75
        theta = rng.random_sample(n_obj) * 2.0 * np.pi
        ra_list = raP + rr * np.cos(theta)
        dec_list = decP + rr * np.sin(theta)
        control_name_list = chipNameFromRaDec(ra_list,
                                              dec_list,
                                              obs_metadata=obs,
                                              camera=self.camera)

        test_name_list = chipNameFromRaDecLSST(ra_list,
                                               dec_list,
                                               obs_metadata=obs)

        np.testing.assert_array_equal(control_name_list.astype(str),
                                      test_name_list.astype(str))
        self.assertLessEqual(
            len(
                np.where(
                    np.char.rfind(test_name_list.astype(str), 'None') >= 0)
                [0]), n_obj / 10)

        # test that exceptions are raised when incomplete ObservationMetaData are used
        obs = ObservationMetaData(pointingRA=raP,
                                  pointingDec=decP,
                                  mjd=59580.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("rotSkyPos", context.exception.args[0])

        obs = ObservationMetaData(pointingRA=raP,
                                  pointingDec=decP,
                                  rotSkyPos=35.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("mjd", context.exception.args[0])

        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list)
        self.assertIn("ObservationMetaData", context.exception.args[0])

        # check that exceptions are raised when ra_list, dec_list are of the wrong shape
        obs = ObservationMetaData(pointingRA=raP,
                                  pointingDec=decP,
                                  rotSkyPos=24.0,
                                  mjd=43000.0)
        with self.assertRaises(RuntimeError) as context:
            chipNameFromRaDecLSST(ra_list, dec_list[:5], obs_metadata=obs)
        self.assertIn("chipNameFromRaDecLSST", context.exception.args[0])
    def test_alert_data_generation(self):

        dmag_cutoff = 0.005
        mag_name_to_int = {'u': 0, 'g': 1, 'r': 2, 'i': 3, 'z' : 4, 'y': 5}

        _max_var_param_str = self.max_str_len

        class StarAlertTestDBObj(StellarAlertDBObjMixin, CatalogDBObject):
            objid = 'star_alert'
            tableid = 'stars'
            idColKey = 'simobjid'
            raColName = 'ra'
            decColName = 'dec'
            objectTypeId = 0
            columns = [('raJ2000', 'ra*0.01745329252'),
                       ('decJ2000', 'dec*0.01745329252'),
                       ('parallax', 'px*0.01745329252/3600.0'),
                       ('properMotionRa', 'pmra*0.01745329252/3600.0'),
                       ('properMotionDec', 'pmdec*0.01745329252/3600.0'),
                       ('radialVelocity', 'vrad'),
                       ('variabilityParameters', 'varParamStr', str, _max_var_param_str)]

        class TestAlertsVarCatMixin(object):

            @register_method('alert_test')
            def applyAlertTest(self, valid_dexes, params, expmjd, variability_cache=None):
                if len(params) == 0:
                    return np.array([[], [], [], [], [], []])

                if isinstance(expmjd, numbers.Number):
                    dmags_out = np.zeros((6, self.num_variable_obj(params)))
                else:
                    dmags_out = np.zeros((6, self.num_variable_obj(params), len(expmjd)))

                for i_star in range(self.num_variable_obj(params)):
                    if params['amp'][i_star] is not None:
                        dmags = params['amp'][i_star]*np.cos(params['per'][i_star]*expmjd)
                        for i_filter in range(6):
                            dmags_out[i_filter][i_star] = dmags

                return dmags_out

        class TestAlertsVarCat(TestAlertsVarCatMixin, AlertStellarVariabilityCatalog):
            pass

        class TestAlertsTruthCat(TestAlertsVarCatMixin, CameraCoords, AstrometryStars,
                                 Variability, InstanceCatalog):
            column_outputs = ['uniqueId', 'chipName', 'dmagAlert', 'magAlert']

            camera = obs_lsst_phosim.PhosimMapper().camera

            @compound('delta_umag', 'delta_gmag', 'delta_rmag',
                      'delta_imag', 'delta_zmag', 'delta_ymag')
            def get_TruthVariability(self):
                return self.applyVariability(self.column_by_name('varParamStr'))

            @cached
            def get_dmagAlert(self):
                return self.column_by_name('delta_%smag' % self.obs_metadata.bandpass)

            @cached
            def get_magAlert(self):
                return self.column_by_name('%smag' % self.obs_metadata.bandpass) + \
                       self.column_by_name('dmagAlert')

        star_db = StarAlertTestDBObj(database=self.star_db_name, driver='sqlite')

        # assemble the true light curves for each object; we need to figure out
        # if their np.max(dMag) ever goes over dmag_cutoff; then we will know if
        # we are supposed to simulate them
        true_lc_dict = {}
        true_lc_obshistid_dict = {}
        is_visible_dict = {}
        obs_dict = {}
        max_obshistid = -1
        n_total_observations = 0
        for obs in self.obs_list:
            obs_dict[obs.OpsimMetaData['obsHistID']] = obs
            obshistid = obs.OpsimMetaData['obsHistID']
            if obshistid > max_obshistid:
                max_obshistid = obshistid
            cat = TestAlertsTruthCat(star_db, obs_metadata=obs)

            for line in cat.iter_catalog():
                if line[1] is None:
                    continue

                n_total_observations += 1
                if line[0] not in true_lc_dict:
                    true_lc_dict[line[0]] = {}
                    true_lc_obshistid_dict[line[0]] = []

                true_lc_dict[line[0]][obshistid] = line[2]
                true_lc_obshistid_dict[line[0]].append(obshistid)

                if line[0] not in is_visible_dict:
                    is_visible_dict[line[0]] = False

                if line[3] <= self.obs_mag_cutoff[mag_name_to_int[obs.bandpass]]:
                    is_visible_dict[line[0]] = True

        obshistid_bits = int(np.ceil(np.log(max_obshistid)/np.log(2)))

        skipped_due_to_mag = 0

        objects_to_simulate = []
        obshistid_unqid_set = set()
        for obj_id in true_lc_dict:

            dmag_max = -1.0
            for obshistid in true_lc_dict[obj_id]:
                if np.abs(true_lc_dict[obj_id][obshistid]) > dmag_max:
                    dmag_max = np.abs(true_lc_dict[obj_id][obshistid])

            if dmag_max >= dmag_cutoff:
                if not is_visible_dict[obj_id]:
                    skipped_due_to_mag += 1
                    continue

                objects_to_simulate.append(obj_id)
                for obshistid in true_lc_obshistid_dict[obj_id]:
                    obshistid_unqid_set.add((obj_id << obshistid_bits) + obshistid)

        self.assertGreater(len(objects_to_simulate), 10)
        self.assertGreater(skipped_due_to_mag, 0)

        log_file_name = tempfile.mktemp(dir=self.output_dir, suffix='log.txt')
        alert_gen = AlertDataGenerator(testing=True)

        alert_gen.subdivide_obs(self.obs_list, htmid_level=6)

        for htmid in alert_gen.htmid_list:
            alert_gen.alert_data_from_htmid(htmid, star_db,
                                            photometry_class=TestAlertsVarCat,
                                            output_prefix='alert_test',
                                            output_dir=self.output_dir,
                                            dmag_cutoff=dmag_cutoff,
                                            log_file_name=log_file_name)

        dummy_sed = Sed()

        bp_dict = BandpassDict.loadTotalBandpassesFromFiles()

        phot_params = PhotometricParameters()

        # First, verify that the contents of the sqlite files are all correct

        n_tot_simulated = 0

        alert_query = 'SELECT alert.uniqueId, alert.obshistId, meta.TAI, '
        alert_query += 'meta.band, quiescent.flux, alert.dflux, '
        alert_query += 'quiescent.snr, alert.snr, '
        alert_query += 'alert.ra, alert.dec, alert.chipNum, '
        alert_query += 'alert.xPix, alert.yPix, ast.pmRA, ast.pmDec, '
        alert_query += 'ast.parallax '
        alert_query += 'FROM alert_data AS alert '
        alert_query += 'INNER JOIN metadata AS meta ON meta.obshistId=alert.obshistId '
        alert_query += 'INNER JOIN quiescent_flux AS quiescent '
        alert_query += 'ON quiescent.uniqueId=alert.uniqueId '
        alert_query += 'AND quiescent.band=meta.band '
        alert_query += 'INNER JOIN baseline_astrometry AS ast '
        alert_query += 'ON ast.uniqueId=alert.uniqueId'

        alert_dtype = np.dtype([('uniqueId', int), ('obshistId', int),
                                ('TAI', float), ('band', int),
                                ('q_flux', float), ('dflux', float),
                                ('q_snr', float), ('tot_snr', float),
                                ('ra', float), ('dec', float),
                                ('chipNum', int), ('xPix', float), ('yPix', float),
                                ('pmRA', float), ('pmDec', float), ('parallax', float)])

        sqlite_file_list = os.listdir(self.output_dir)

        n_tot_simulated = 0
        obshistid_unqid_simulated_set = set()
        for file_name in sqlite_file_list:
            if not file_name.endswith('db'):
                continue
            full_name = os.path.join(self.output_dir, file_name)
            self.assertTrue(os.path.exists(full_name))
            alert_db = DBObject(full_name, driver='sqlite')
            alert_data = alert_db.execute_arbitrary(alert_query, dtype=alert_dtype)
            if len(alert_data) == 0:
                continue

            mjd_list = ModifiedJulianDate.get_list(TAI=alert_data['TAI'])
            for i_obj in range(len(alert_data)):
                n_tot_simulated += 1
                obshistid_unqid_simulated_set.add((alert_data['uniqueId'][i_obj] << obshistid_bits) +
                                                  alert_data['obshistId'][i_obj])

                unq = alert_data['uniqueId'][i_obj]
                obj_dex = (unq//1024)-1
                self.assertAlmostEqual(self.pmra_truth[obj_dex], 0.001*alert_data['pmRA'][i_obj], 4)
                self.assertAlmostEqual(self.pmdec_truth[obj_dex], 0.001*alert_data['pmDec'][i_obj], 4)
                self.assertAlmostEqual(self.px_truth[obj_dex], 0.001*alert_data['parallax'][i_obj], 4)

                ra_truth, dec_truth = applyProperMotion(self.ra_truth[obj_dex], self.dec_truth[obj_dex],
                                                        self.pmra_truth[obj_dex], self.pmdec_truth[obj_dex],
                                                        self.px_truth[obj_dex], self.vrad_truth[obj_dex],
                                                        mjd=mjd_list[i_obj])
                distance = angularSeparation(ra_truth, dec_truth,
                                             alert_data['ra'][i_obj], alert_data['dec'][i_obj])

                distance_arcsec = 3600.0*distance
                msg = '\ntruth: %e %e\nalert: %e %e\n' % (ra_truth, dec_truth,
                                                          alert_data['ra'][i_obj],
                                                          alert_data['dec'][i_obj])

                self.assertLess(distance_arcsec, 0.0005, msg=msg)

                obs = obs_dict[alert_data['obshistId'][i_obj]]


                chipname = chipNameFromRaDec(self.ra_truth[obj_dex], self.dec_truth[obj_dex],
                                             pm_ra=self.pmra_truth[obj_dex],
                                             pm_dec=self.pmdec_truth[obj_dex],
                                             parallax=self.px_truth[obj_dex],
                                             v_rad=self.vrad_truth[obj_dex],
                                             obs_metadata=obs,
                                             camera=self.camera)

                chipnum = int(chipname.replace('R', '').replace('S', '').
                              replace(' ', '').replace(';', '').replace(',', '').
                              replace(':', ''))

                self.assertEqual(chipnum, alert_data['chipNum'][i_obj])

                xpix, ypix = pixelCoordsFromRaDec(self.ra_truth[obj_dex], self.dec_truth[obj_dex],
                                                  pm_ra=self.pmra_truth[obj_dex],
                                                  pm_dec=self.pmdec_truth[obj_dex],
                                                  parallax=self.px_truth[obj_dex],
                                                  v_rad=self.vrad_truth[obj_dex],
                                                  obs_metadata=obs,
                                                  camera=self.camera)

                self.assertAlmostEqual(alert_data['xPix'][i_obj], xpix, 4)
                self.assertAlmostEqual(alert_data['yPix'][i_obj], ypix, 4)

                dmag_sim = -2.5*np.log10(1.0+alert_data['dflux'][i_obj]/alert_data['q_flux'][i_obj])
                self.assertAlmostEqual(true_lc_dict[alert_data['uniqueId'][i_obj]][alert_data['obshistId'][i_obj]],
                                       dmag_sim, 3)

                mag_name = ('u', 'g', 'r', 'i', 'z', 'y')[alert_data['band'][i_obj]]
                m5 = obs.m5[mag_name]

                q_mag = dummy_sed.magFromFlux(alert_data['q_flux'][i_obj])
                self.assertAlmostEqual(self.mag0_truth_dict[alert_data['band'][i_obj]][obj_dex],
                                       q_mag, 4)

                snr, gamma = calcSNR_m5(self.mag0_truth_dict[alert_data['band'][i_obj]][obj_dex],
                                        bp_dict[mag_name],
                                        self.obs_mag_cutoff[alert_data['band'][i_obj]],
                                        phot_params)

                self.assertAlmostEqual(snr/alert_data['q_snr'][i_obj], 1.0, 4)

                tot_mag = self.mag0_truth_dict[alert_data['band'][i_obj]][obj_dex] + \
                          true_lc_dict[alert_data['uniqueId'][i_obj]][alert_data['obshistId'][i_obj]]

                snr, gamma = calcSNR_m5(tot_mag, bp_dict[mag_name],
                                        m5, phot_params)
                self.assertAlmostEqual(snr/alert_data['tot_snr'][i_obj], 1.0, 4)

        for val in obshistid_unqid_set:
            self.assertIn(val, obshistid_unqid_simulated_set)
        self.assertEqual(len(obshistid_unqid_set), len(obshistid_unqid_simulated_set))

        astrometry_query = 'SELECT uniqueId, ra, dec, TAI '
        astrometry_query += 'FROM baseline_astrometry'
        astrometry_dtype = np.dtype([('uniqueId', int),
                                     ('ra', float),
                                     ('dec', float),
                                     ('TAI', float)])

        tai_list = []
        for obs in self.obs_list:
            tai_list.append(obs.mjd.TAI)
        tai_list = np.array(tai_list)

        n_tot_ast_simulated = 0
        for file_name in sqlite_file_list:
            if not file_name.endswith('db'):
                continue
            full_name = os.path.join(self.output_dir, file_name)
            self.assertTrue(os.path.exists(full_name))
            alert_db = DBObject(full_name, driver='sqlite')
            astrometry_data = alert_db.execute_arbitrary(astrometry_query, dtype=astrometry_dtype)

            if len(astrometry_data) == 0:
                continue

            mjd_list = ModifiedJulianDate.get_list(TAI=astrometry_data['TAI'])
            for i_obj in range(len(astrometry_data)):
                n_tot_ast_simulated += 1
                obj_dex = (astrometry_data['uniqueId'][i_obj]//1024) - 1
                ra_truth, dec_truth = applyProperMotion(self.ra_truth[obj_dex], self.dec_truth[obj_dex],
                                                        self.pmra_truth[obj_dex], self.pmdec_truth[obj_dex],
                                                        self.px_truth[obj_dex], self.vrad_truth[obj_dex],
                                                        mjd=mjd_list[i_obj])

                distance = angularSeparation(ra_truth, dec_truth,
                                             astrometry_data['ra'][i_obj],
                                             astrometry_data['dec'][i_obj])

                self.assertLess(3600.0*distance, 0.0005)

        del alert_gen
        gc.collect()
        self.assertGreater(n_tot_simulated, 10)
        self.assertGreater(len(obshistid_unqid_simulated_set), 10)
        self.assertLess(len(obshistid_unqid_simulated_set), n_total_observations)
        self.assertGreater(n_tot_ast_simulated, 0)
Exemple #12
0
obs_metadata_list = OpSimDB.getObservationMetaData((88.0, -40.0),
                                                   5.0,
                                                   fovRadius=radiusDegrees,
                                                   makeCircBounds=True)

obs_metadata = obs_metadata_list[0]

#generate some random RA and Dec to find chips for
nsamples = 10
numpy.random.seed(32)
rr = numpy.radians(2.0) * numpy.random.sample(nsamples)
theta = 2.0 * numpy.pi * numpy.random.sample(nsamples)
ra = numpy.degrees(
    numpy.radians(obs_metadata.pointingRA) + rr * numpy.cos(theta))
dec = numpy.degrees(
    numpy.radians(obs_metadata.pointingDec) + rr * numpy.sin(theta))

#need to correct coordinates for precession, nutation, and aberration
ra, dec = observedFromICRS(ra, dec, obs_metadata=obs_metadata, epoch=epoch)

xx, yy = pupilCoordsFromRaDec(ra, dec, obs_metadata=obs_metadata, epoch=epoch)

chipNames = chipNameFromRaDec(ra,
                              dec,
                              epoch=epoch,
                              camera=camera,
                              obs_metadata=obs_metadata)

for (rr, dd, x, y, nn) in zip(ra, dec, xx, yy, chipNames):
    print(rr, dd, arcsecFromRadians(x), arcsecFromRadians(y), nn)
Exemple #13
0
def main(file, psf, outdir):
    """
    Drive GalSim to simulate the LSST.
    """
    # Setup a parser to take command line arguments

    config = desc.imsim.read_config(None)

    logger = desc.imsim.get_logger("INFO")

    # Get the number of rows to read from the instance file.  Use
    # default if not specified.

    numRows = None
    sensor = None
    # The PhoSim instance file contains both pointing commands and
    # objects.  The parser will split them and return a both phosim
    # command dictionary and a dataframe of objects.
    commands, phosim_objects = \
        desc.imsim.parsePhoSimInstanceFile(file, numRows)

    phosim_objects = \
        desc.imsim.validate_phosim_object_list(phosim_objects).accepted

    # Build the ObservationMetaData with values taken from the
    # PhoSim commands at the top of the instance file.
    obs_md = desc.imsim.phosim_obs_metadata(commands)
    #print (commands)
    #obs_md.OpsimMetaData['altitude' ] = 20
    camera = LsstSimMapper().camera

    # Sub-divide the source dataframe into stars and galaxies.
    if sensor is not None:
        # Trim the input catalog to a single chip.
        phosim_objects['chipName'] = \
            chipNameFromRaDec(phosim_objects['raICRS'].values,
                              phosim_objects['decICRS'].values,
                              parallax=phosim_objects['parallax'].values,
                              camera=camera, obs_metadata=obs_md,
                              epoch=2000.0)

        starDataBase = \
            phosim_objects.query("galSimType=='pointSource' and chipName=='%s'"
                                 % sensor)
        galaxyDataBase = \
            phosim_objects.query("galSimType=='sersic' and chipName=='%s'"
                                 % sensor)
    else:
        starDataBase = \
            phosim_objects.query("galSimType=='pointSource'")
        galaxyDataBase = \
            phosim_objects.query("galSimType=='sersic'")

    # Simulate the objects in the Pandas Dataframes.

    # First simulate stars
    phoSimStarCatalog = desc.imsim.ImSimStars(starDataBase, obs_md)
    phoSimStarCatalog.photParams = desc.imsim.photometricParameters(commands)

    # Add noise and sky background
    # The simple code using the default lsst-GalSim interface would be:
    #
    #    PhoSimStarCatalog.noise_and_background = ExampleCCDNoise(addNoise=True,
    #                                                             addBackground=True)
    #
    # But, we need a more realistic sky model and we need to pass more than
    # this basic info to use Peter Y's ESO sky model.
    # We must pass obs_metadata, chip information etc...
    phoSimStarCatalog.noise_and_background = ESOSkyModel(obs_md,
                                                         addNoise=True,
                                                         addBackground=True)

    # Add a PSF.
    if psf.lower() == "doublegaussian":
        # This one is taken from equation 30 of
        # www.astro.washington.edu/users/ivezic/Astr511/LSST_SNRdoc.pdf .
        #
        # Set seeing from self.obs_metadata.
        phoSimStarCatalog.PSF = \
            SNRdocumentPSF(obs_md.OpsimMetaData['FWHMgeom'])
    elif psf.lower() == "kolmogorov":
        # This PSF was presented by David Kirkby at the 23 March 2017
        # Survey Simulations Working Group telecon
        #
        # https://confluence.slac.stanford.edu/pages/viewpage.action?spaceKey=LSSTDESC&title=SSim+2017-03-23

        # equation 3 of Krisciunas and Schaefer 1991
        airmass = 1.0 / np.sqrt(
            1.0 - 0.96 *
            (np.sin(0.5 * np.pi - obs_md.OpsimMetaData['altitude']))**2)

        phoSimStarCatalog.PSF = \
            Kolmogorov_and_Gaussian_PSF(airmass=airmass,
                                        rawSeeing=obs_md.OpsimMetaData['rawSeeing'],
                                        band=obs_md.bandpass)
    else:
        raise RuntimeError("Do not know what to do with psf model: "
                           "%s" % psf)

    phoSimStarCatalog.camera = camera
    phoSimStarCatalog.get_fitsFiles()

    # Now galaxies
    phoSimGalaxyCatalog = desc.imsim.ImSimGalaxies(galaxyDataBase, obs_md)
    phoSimGalaxyCatalog.copyGalSimInterpreter(phoSimStarCatalog)
    phoSimGalaxyCatalog.PSF = phoSimStarCatalog.PSF
    phoSimGalaxyCatalog.noise_and_background = phoSimStarCatalog.noise_and_background
    phoSimGalaxyCatalog.get_fitsFiles()

    # Write out the fits files
    outdir = outdir
    if not os.path.isdir(outdir):
        os.makedirs(outdir)
    prefix = config['persistence']['eimage_prefix']
    phoSimGalaxyCatalog.write_images(nameRoot=os.path.join(outdir, prefix) +
                                     str(commands['obshistid']))
from lsst.obs.lsstSim import LsstSimMapper

mapper = LsstSimMapper()
camera = mapper.camera

epoch = 2000.0

#generate an ObservationMetaData object based on an actual OpSim pointing
obshistid = 88625744
radiusDegrees = 3.0
OpSimDB = OpSim3_61DBObject()
obs_metadata = OpSimDB.getObservationMetaData(obshistid, radiusDegrees, makeCircBounds=True)

#generate some random RA and Dec to find chips for
nsamples = 10
numpy.random.seed(32)
rr = numpy.radians(2.0)*numpy.random.sample(nsamples)
theta = 2.0*numpy.pi*numpy.random.sample(nsamples)
ra = numpy.degrees(numpy.radians(obs_metadata.pointingRA) + rr*numpy.cos(theta))
dec = numpy.degrees(numpy.radians(obs_metadata.pointingDec) + rr*numpy.sin(theta))

#need to correct coordinates for precession, nutation, and aberration
ra, dec = observedFromICRS(ra, dec, obs_metadata=obs_metadata, epoch=epoch)

xx, yy = pupilCoordsFromRaDec(ra, dec, obs_metadata=obs_metadata, epoch=epoch)

chipNames = chipNameFromRaDec(ra, dec, epoch=epoch, camera=camera, obs_metadata=obs_metadata)

for (rr,dd,x,y,nn) in zip(ra,dec,xx,yy,chipNames):
    print rr,dd,arcsecFromRadians(x),arcsecFromRadians(y),nn
    header_msg += '# regenerated.\n#\n'

    camera = lsst_camera()
    ra = 25.0
    dec = -62.0
    obs = ObservationMetaData(pointingRA=ra, pointingDec=dec,
                              rotSkyPos=57.2, mjd=59586.2)

    ra_range = np.arange(ra-4.0, ra+4.0, 0.1)
    dec_range = np.arange(dec-4.0, dec+4.0, 0.1)

    ra_grid, dec_grid = np.meshgrid(ra_range, dec_range)
    ra_grid = ra_grid.flatten()
    dec_grid = dec_grid.flatten()

    chip_name_grid = chipNameFromRaDec(ra_grid, dec_grid,
                                       obs_metadata=obs, camera=camera)

    valid = np.where(np.char.find(chip_name_grid.astype(str), 'None')<0)

    ra_grid = ra_grid[valid]
    dec_grid = dec_grid[valid]
    chip_name_grid = chip_name_grid[valid]

    focal_x, focal_y = focalPlaneCoordsFromRaDec(ra_grid, dec_grid,
                                                 obs_metadata=obs,
                                                 camera=camera)

    pix_x, pix_y = pixelCoordsFromRaDec(ra_grid, dec_grid,
                                        chipName=chip_name_grid,
                                        obs_metadata=obs,
                                        camera=camera)
Exemple #16
0
    ra = 25.0
    dec = -62.0
    obs = ObservationMetaData(pointingRA=ra,
                              pointingDec=dec,
                              rotSkyPos=57.2,
                              mjd=59586.2)

    ra_range = np.arange(ra - 4.0, ra + 4.0, 0.1)
    dec_range = np.arange(dec - 4.0, dec + 4.0, 0.1)

    ra_grid, dec_grid = np.meshgrid(ra_range, dec_range)
    ra_grid = ra_grid.flatten()
    dec_grid = dec_grid.flatten()

    chip_name_grid = chipNameFromRaDec(ra_grid,
                                       dec_grid,
                                       obs_metadata=obs,
                                       camera=camera)

    valid = np.where(np.char.find(chip_name_grid.astype(str), 'None') < 0)

    ra_grid = ra_grid[valid]
    dec_grid = dec_grid[valid]
    chip_name_grid = chip_name_grid[valid]

    focal_x, focal_y = focalPlaneCoordsFromRaDec(ra_grid,
                                                 dec_grid,
                                                 obs_metadata=obs,
                                                 camera=camera)

    pix_x, pix_y = pixelCoordsFromRaDec(ra_grid,
                                        dec_grid,