def test_pixel_coords(self):
        """
        Test that pixelCoordsFromRaDecLSST with non-zero proper motion etc.
        agrees with pixelCoordsFromPupilCoords when pupilCoords are
        calculated with the same proper motion, etc.
        """
        (obs, ra_list, dec_list, pm_ra_list, pm_dec_list, parallax_list,
         v_rad_list) = self.set_data(26)

        for is_none in ('pm_ra', 'pm_dec', 'parallax', 'v_rad'):
            pm_ra = pm_ra_list
            pm_dec = pm_dec_list
            parallax = parallax_list
            v_rad = v_rad_list

            if is_none == 'pm_ra':
                pm_ra = None
            elif is_none == 'pm_dec':
                pm_dec = None
            elif is_none == 'parallax':
                parallax = None
            elif is_none == 'v_rad':
                v_rad = None

            xp, yp = pupilCoordsFromRaDec(ra_list,
                                          dec_list,
                                          pm_ra=pm_ra,
                                          pm_dec=pm_dec,
                                          parallax=parallax,
                                          v_rad=v_rad,
                                          obs_metadata=obs)

            xpx_control, ypx_control = pixelCoordsFromPupilCoords(
                xp, yp, camera=self.camera)

            xpx_test, ypx_test = pixelCoordsFromRaDecLSST(ra_list,
                                                          dec_list,
                                                          pm_ra=pm_ra,
                                                          pm_dec=pm_dec,
                                                          parallax=parallax,
                                                          v_rad=v_rad,
                                                          obs_metadata=obs)

            xpx_radians, ypx_radians = _pixelCoordsFromRaDecLSST(
                np.radians(ra_list),
                np.radians(dec_list),
                pm_ra=radiansFromArcsec(pm_ra),
                pm_dec=radiansFromArcsec(pm_dec),
                parallax=radiansFromArcsec(parallax),
                v_rad=v_rad,
                obs_metadata=obs)

            np.testing.assert_array_equal(xpx_control, xpx_test)
            np.testing.assert_array_equal(ypx_control, ypx_test)
            np.testing.assert_array_equal(xpx_control, xpx_radians)
            np.testing.assert_array_equal(ypx_control, ypx_radians)
            self.assertLess(len(np.where(np.isnan(xpx_control))[0]),
                            len(xpx_test) / 4)
            self.assertLess(len(np.where(np.isnan(ypx_control))[0]),
                            len(ypx_test) / 4)
    def test_pixel_coords(self):
        """
        Test that pixelCoordsFromRaDecLSST with non-zero proper motion etc.
        agrees with pixelCoordsFromPupilCoords when pupilCoords are
        calculated with the same proper motion, etc.
        """
        (obs, ra_list, dec_list,
         pm_ra_list, pm_dec_list,
         parallax_list, v_rad_list) = self.set_data(26)

        for is_none in ('pm_ra', 'pm_dec', 'parallax', 'v_rad'):
            pm_ra = pm_ra_list
            pm_dec = pm_dec_list
            parallax = parallax_list
            v_rad = v_rad_list

            if is_none == 'pm_ra':
                pm_ra = None
            elif is_none == 'pm_dec':
                pm_dec = None
            elif is_none == 'parallax':
                parallax = None
            elif is_none == 'v_rad':
                v_rad = None

            xp, yp = pupilCoordsFromRaDec(ra_list, dec_list,
                                          pm_ra=pm_ra, pm_dec=pm_dec,
                                          parallax=parallax, v_rad=v_rad,
                                          obs_metadata=obs)

            xpx_control, ypx_control = pixelCoordsFromPupilCoords(xp, yp, camera=self.camera,
                                                                  includeDistortion=False)

            xpx_test, ypx_test = pixelCoordsFromRaDecLSST(ra_list, dec_list,
                                                          pm_ra=pm_ra, pm_dec=pm_dec,
                                                          parallax=parallax, v_rad=v_rad,
                                                          obs_metadata=obs,
                                                          includeDistortion=False)

            xpx_radians, ypx_radians = _pixelCoordsFromRaDecLSST(np.radians(ra_list), np.radians(dec_list),
                                                                 pm_ra=radiansFromArcsec(pm_ra),
                                                                 pm_dec=radiansFromArcsec(pm_dec),
                                                                 parallax=radiansFromArcsec(parallax),
                                                                 v_rad=v_rad, obs_metadata=obs,
                                                                 includeDistortion=False)

            np.testing.assert_array_equal(xpx_control, xpx_test)
            np.testing.assert_array_equal(ypx_control, ypx_test)
            np.testing.assert_array_equal(xpx_control, xpx_radians)
            np.testing.assert_array_equal(ypx_control, ypx_radians)
            self.assertLess(len(np.where(np.isnan(xpx_control))[0]), len(xpx_test)/4)
            self.assertLess(len(np.where(np.isnan(ypx_control))[0]), len(ypx_test)/4)
示例#3
0
    def _pixelCoordsFromRaDec(self,
                              ra,
                              dec,
                              pm_ra=None,
                              pm_dec=None,
                              parallax=None,
                              v_rad=None,
                              obs_metadata=None,
                              chipName=None,
                              epoch=2000.0,
                              includeDistortion=True):
        """
        Get the pixel positions (or nan if not on a chip) for objects based
        on their RA, and Dec (in radians)

        Parameters
        ----------
        ra is in radians in the International Celestial Reference System.
        Can be either a float or a numpy array.

        dec is in radians in the International Celestial Reference System.
        Can be either a float or a numpy array.

        pm_ra is proper motion in RA multiplied by cos(Dec) (radians/yr)
        Can be a numpy array or a number or None (default=None).

        pm_dec is proper motion in dec (radians/yr)
        Can be a numpy array or a number or None (default=None).

        parallax is parallax in radians
        Can be a numpy array or a number or None (default=None).

        v_rad is radial velocity (km/s)
        Can be a numpy array or a number or None (default=None).

        obs_metadata is an ObservationMetaData characterizing the telescope
        pointing.

        epoch is the epoch in Julian years of the equinox against which
        RA is measured.  Default is 2000.

        chipName designates the names of the chips on which the pixel
        coordinates will be reckoned.  Can be either single value, an array, or None.
        If an array, there must be as many chipNames as there are (RA, Dec) pairs.
        If a single value, all of the pixel coordinates will be reckoned on the same
        chip.  If None, this method will calculate which chip each(RA, Dec) pair actually
        falls on, and return pixel coordinates for each (RA, Dec) pair on the appropriate
        chip.  Default is None.

        includeDistortion is a boolean.  If True (default), then this method will
        return the true pixel coordinates with optical distortion included.  If False, this
        method will return TAN_PIXEL coordinates, which are the pixel coordinates with
        estimated optical distortion removed.  See the documentation in afw.cameraGeom for more
        details.

        Returns
        -------
        a 2-D numpy array in which the first row is the x pixel coordinate
        and the second row is the y pixel coordinate.  These pixel coordinates
        are defined in the Camera team system, rather than the DM system.
        """

        dm_xPix, dm_yPix = coordUtils._pixelCoordsFromRaDecLSST(
            ra,
            dec,
            pm_ra=pm_ra,
            pm_dec=pm_dec,
            parallax=parallax,
            v_rad=v_rad,
            obs_metadata=obs_metadata,
            chipName=chipName,
            epoch=epoch,
            includeDistortion=includeDistortion)
        cam_yPix = dm_xPix

        if isinstance(chipName, list) or isinstance(chipName, np.ndarray):
            cam_xPix = np.zeros(len(dm_xPix))
            for ix, (det_name, yy) in enumerate(zip(chipName, dm_yPix)):
                cam_center_pix = self.getCenterPixel(det_name)
                cam_xPix[ix] = 2.0 * cam_center_pix.getX() - dm_yPix
        else:
            cam_center_pix = self.getCenterPixel(chipName)
            cam_xPix = 2.0 * cam_center_pix.getX() - dm_yPix

        return cam_xPix, cam_yPix
    def test_pixel_coords_from_ra_dec_radians(self):
        """
        Test that _pixelCoordsFromRaDec and _pixelCoordsFromRaDecLSST agree
        """
        raP = 74.2
        decP = 13.0
        obs = ObservationMetaData(pointingRA=raP,
                                  pointingDec=decP,
                                  rotSkyPos=13.0,
                                  mjd=43441.0)

        n_obj = 1000
        rng = np.random.RandomState(83241)
        rr = rng.random_sample(n_obj) * 1.75
        theta = rng.random_sample(n_obj) * 2.0 * np.pi
        ra_list = np.radians(raP + rr * np.cos(theta))
        dec_list = np.radians(decP + rr * np.sin(theta))
        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list,
                                             dec_list,
                                             obs_metadata=obs,
                                             camera=self.camera)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj / 10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj / 10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(ra_list,
                                                           dec_list,
                                                           obs_metadata=obs)
        np.testing.assert_array_equal(x_pix, x_pix_test)
        np.testing.assert_array_equal(y_pix, y_pix_test)

        # test when we force a chipName
        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list,
                                             dec_list,
                                             chipName=['R:2,2 S:1,1'],
                                             obs_metadata=obs,
                                             camera=self.camera)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj / 10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj / 10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(
            ra_list, dec_list, chipName=['R:2,2 S:1,1'], obs_metadata=obs)
        np.testing.assert_array_equal(x_pix, x_pix_test)
        np.testing.assert_array_equal(y_pix, y_pix_test)

        # test without distortion
        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list,
                                             dec_list,
                                             obs_metadata=obs,
                                             camera=self.camera,
                                             includeDistortion=False)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj / 10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj / 10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(
            ra_list, dec_list, obs_metadata=obs, includeDistortion=False)
        np.testing.assert_array_equal(x_pix, x_pix_test)
        np.testing.assert_array_equal(y_pix, y_pix_test)

        # 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:
            _pixelCoordsFromRaDecLSST(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:
            _pixelCoordsFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("mjd", context.exception.args[0])

        with self.assertRaises(RuntimeError) as context:
            _pixelCoordsFromRaDecLSST(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:
            _pixelCoordsFromRaDecLSST(ra_list, dec_list[:5], obs_metadata=obs)
        self.assertIn("pixelCoordsFromRaDecLSST", context.exception.args[0])
    def test_pixel_coords_from_ra_dec_radians(self):
        """
        Test that _pixelCoordsFromRaDec and _pixelCoordsFromRaDecLSST agree
        """
        raP = 74.2
        decP = 13.0
        obs = ObservationMetaData(pointingRA=raP, pointingDec=decP,
                                  rotSkyPos=13.0, mjd=43441.0)

        n_obj = 1000
        rng = np.random.RandomState(83241)
        rr = rng.random_sample(n_obj)*1.75
        theta = rng.random_sample(n_obj)*2.0*np.pi
        ra_list = np.radians(raP + rr*np.cos(theta))
        dec_list = np.radians(decP + rr*np.sin(theta))

        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list, dec_list, obs_metadata=obs, camera=self.camera,
                                             includeDistortion=False)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj/10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj/10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(ra_list, dec_list, obs_metadata=obs,
                                                           includeDistortion=False)

        try:
            np.testing.assert_array_equal(x_pix, x_pix_test)
            np.testing.assert_array_equal(y_pix, y_pix_test)
        except AssertionError:
            n_problematic = 0
            for xx, yy, xt, yt in zip(x_pix, y_pix, x_pix_test, y_pix_test):
                if xx!=xt or yy!=yt:
                    if (not np.isnan(xx) and not np.isnan(xt) and
                        not np.isnan(yy) and not np.isnan(yt)):
                        print(xx,yy,xt,yt)

                        n_problematic += 1
            if n_problematic>0:
                raise

        # test when we force a chipName
        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list, dec_list, chipName=['R:2,2 S:1,1'],
                                             obs_metadata=obs, camera=self.camera,
                                             includeDistortion=False)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj/10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj/10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(ra_list, dec_list, chipName=['R:2,2 S:1,1'],
                                                           obs_metadata=obs,
                                                           includeDistortion=False)
        np.testing.assert_array_equal(x_pix, x_pix_test)
        np.testing.assert_array_equal(y_pix, y_pix_test)

        # test without distortion
        x_pix, y_pix = _pixelCoordsFromRaDec(ra_list, dec_list, obs_metadata=obs, camera=self.camera,
                                             includeDistortion=False)
        self.assertLessEqual(len(np.where(np.isnan(x_pix))[0]), n_obj/10)
        self.assertLessEqual(len(np.where(np.isnan(y_pix))[0]), n_obj/10)

        x_pix_test, y_pix_test = _pixelCoordsFromRaDecLSST(ra_list, dec_list, obs_metadata=obs,
                                                           includeDistortion=False)
        try:
            np.testing.assert_array_equal(x_pix, x_pix_test)
            np.testing.assert_array_equal(y_pix, y_pix_test)
        except AssertionError:
            n_problematic = 0
            for xx, yy, xt, yt in zip(x_pix, y_pix, x_pix_test, y_pix_test):
                if xx!=xt or yy!=yt:
                    if (not np.isnan(xx) and not np.isnan(xt) and
                        not np.isnan(yy) and not np.isnan(yt)):
                        print(xx,yy,xt,yt)

                        n_problematic += 1
            if n_problematic>0:
                raise


        # 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:
            _pixelCoordsFromRaDecLSST(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:
            _pixelCoordsFromRaDecLSST(ra_list, dec_list, obs_metadata=obs)
        self.assertIn("mjd", context.exception.args[0])

        with self.assertRaises(RuntimeError) as context:
            _pixelCoordsFromRaDecLSST(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:
            _pixelCoordsFromRaDecLSST(ra_list, dec_list[:5], obs_metadata=obs)
        self.assertIn("same length", context.exception.args[0])