Пример #1
0
    def pupilCoordsFromPixelCoords(self,
                                   xPix,
                                   yPix,
                                   chipName,
                                   includeDistortion=True):
        """
        Convert pixel coordinates into pupil coordinates

        Parameters
        ----------
        xPix is the x pixel coordinate of the point.
        Can be either a float or a numpy array.
        Defined in the Camera team system (not the DM system).

        yPix is the y pixel coordinate of the point.
        Can be either a float or a numpy array.
        Defined in the Camera team system (not the DM system).

        chipName is the name of the chip(s) on which the pixel coordinates
        are defined.  This can be a list (in which case there should be one chip name
        for each (xPix, yPix) coordinate pair), or a single value (in which case, all
        of the (xPix, yPix) points will be reckoned on that chip).

        includeDistortion is a boolean.  If True (default), then this method will
        expect the true pixel coordinates with optical distortion included.  If False, this
        method will expect 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 pupil coordinate
        and the second row is the y pupil coordinate (both in radians)
        """
        dm_xPix = yPix
        if isinstance(chipName, list) or isinstance(chipName, np.ndarray):
            dm_yPix = np.zeros(len(xPix))
            for ix, (det_name, xx) in enumerate(zip(chipName, xPix)):
                came_center_pix = self.getCenterPixel(det_name)
                dm_yPix[ix] = 2.0 * cam_center_pix.getX() - xPix[ix]
        else:
            cam_center_pix = self.getCenterPixel(chipName)
            dm_yPix = 2.0 * cam_center_pix.getX() - xPix
        return coordUtils.pupilCoordsFromPixelCoords(
            dm_xPix,
            dm_yPix,
            chipName,
            camera=coordUtils.lsst_camera(),
            includeDistortion=includeDistortion)
    def testContainsPupilCoordinates(self):
        """
        Test whether or not the method containsRaDec correctly identifies
        RA and Dec that fall inside and outside the detector
        """

        photParams = PhotometricParameters()
        gsdet = GalSimDetector(self.camera[0], self.camera, \
                               self.obs, self.epoch,
                               photParams=photParams)

        xxList = [gsdet.xMinPix, gsdet.xMaxPix]
        yyList = [gsdet.yMinPix, gsdet.yMaxPix]
        dxList = [-1.0, 1.0]
        dyList = [-1.0, 1.0]

        xPixList = []
        yPixList = []
        correctAnswer = []

        for xx, yy, dx, dy in zip(xxList, yyList, dxList, dyList):
            xPixList.append(xx)
            yPixList.append(yy)
            correctAnswer.append(True)

            xPixList.append(xx+dx)
            yPixList.append(yy)
            correctAnswer.append(False)

            xPixList.append(xx)
            yPixList.append(yy+dy)
            correctAnswer.append(False)


        nameList = [gsdet.name]*len(xPixList)
        xPixList = numpy.array(xPixList)
        yPixList = numpy.array(yPixList)

        xPupilList, yPupilList = \
               pupilCoordsFromPixelCoords(xPixList, yPixList,
                                          nameList,
                                          camera=self.camera)


        testAnswer = gsdet.containsPupilCoordinates(xPupilList, yPupilList)

        for c, t in zip(correctAnswer, testAnswer):
            self.assertTrue(c is t)
    def testContainsPupilCoordinates(self):
        """
        Test whether or not the method containsRaDec correctly identifies
        RA and Dec that fall inside and outside the detector
        """

        photParams = PhotometricParameters()
        gsdet = GalSimDetector(self.camera[0].getName(),
                               GalSimCameraWrapper(self.camera),
                               self.obs,
                               self.epoch,
                               photParams=photParams)

        xxList = [gsdet.xMinPix, gsdet.xMaxPix]
        yyList = [gsdet.yMinPix, gsdet.yMaxPix]
        dxList = [-1.0, 1.0]
        dyList = [-1.0, 1.0]

        xPixList = []
        yPixList = []
        correctAnswer = []

        for xx, yy, dx, dy in zip(xxList, yyList, dxList, dyList):
            xPixList.append(xx)
            yPixList.append(yy)
            correctAnswer.append(True)

            xPixList.append(xx + dx)
            yPixList.append(yy)
            correctAnswer.append(False)

            xPixList.append(xx)
            yPixList.append(yy + dy)
            correctAnswer.append(False)

        nameList = [gsdet.name] * len(xPixList)
        xPixList = np.array(xPixList)
        yPixList = np.array(yPixList)

        xPupilList, yPupilList = \
        pupilCoordsFromPixelCoords(xPixList, yPixList,
                                   nameList, camera=self.camera)

        testAnswer = gsdet.containsPupilCoordinates(xPupilList, yPupilList)

        for c, t in zip(correctAnswer, testAnswer):
            self.assertIs(c, t)
    def pupilCoordsFromPixelCoords(self,
                                   xPix,
                                   yPix,
                                   chipName,
                                   obs_metadata,
                                   includeDistortion=True):
        """
        Convert pixel coordinates into pupil coordinates

        Parameters
        ----------
        xPix is the x pixel coordinate of the point.
        Can be either a float or a numpy array.

        yPix is the y pixel coordinate of the point.
        Can be either a float or a numpy array.

        chipName is the name of the chip(s) on which the pixel coordinates
        are defined.  This can be a list (in which case there should be one chip name
        for each (xPix, yPix) coordinate pair), or a single value (in which case, all
        of the (xPix, yPix) points will be reckoned on that chip).

        obs_metadata is an ObservationMetaData characterizing the telescope
        pointing.

        includeDistortion is a boolean.  If True (default), then this method will
        expect the true pixel coordinates with optical distortion included.  If False, this
        method will expect 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 pupil coordinate
        and the second row is the y pupil coordinate (both in radians)
        """

        return coordUtils.pupilCoordsFromPixelCoords(
            xPix,
            yPix,
            chipName,
            camera=self._camera,
            includeDistortion=includeDistortion)
Пример #5
0
def pupilCoordsFromPixelCoordsLSST(xPix,
                                   yPix,
                                   chipName=None,
                                   band="r",
                                   includeDistortion=True):
    """
    Convert pixel coordinates into radians on the pupil

    Parameters
    ----------
    xPix -- the x pixel coordinate

    yPix -- the y pixel coordinate

    chipName -- the name(s) of the chips on which xPix, yPix are reckoned

    band -- the filter we are simulating (default=r)

    includeDistortion -- a boolean which turns on or off optical
    distortions (default=True)

    Returns
    -------
    a 2-D numpy array in which the first row is the x
    pupil coordinate and the second row is the y pupil
    coordinate (both in radians)
    """

    if not includeDistortion:
        return pupilCoordsFromPixelCoords(xPix,
                                          yPix,
                                          chipName=chipName,
                                          camera=lsst_camera(),
                                          includeDistortion=includeDistortion)

    are_arrays, \
    chipNameList = _validate_inputs_and_chipname([xPix, yPix], ['xPix', 'yPix'],
                                                 "pupilCoordsFromPixelCoords",
                                                 chipName,
                                                 chipname_can_be_none=False)

    pixel_to_focal_dict = {}
    camera = lsst_camera()
    name_to_int = {}
    name_to_int[None] = 0
    name_to_int['None'] = 0
    ii = 1

    if are_arrays:
        chip_name_int = np.zeros(len(xPix), dtype=int)

    have_transform = set()
    for i_obj, name in enumerate(chipNameList):
        if name not in have_transform and name is not None and name != 'None':
            pixel_to_focal_dict[name] = camera[name].getTransform(
                PIXELS, FOCAL_PLANE)
            name_to_int[name] = ii
            have_transform.add(name)
            ii += 1

        if are_arrays:
            chip_name_int[i_obj] = name_to_int[name]

    if are_arrays:
        x_f = np.zeros(len(xPix), dtype=float)
        y_f = np.zeros(len(yPix), dtype=float)

        for name in name_to_int:
            local_int = name_to_int[name]
            local_valid = np.where(chip_name_int == local_int)
            if len(local_valid[0]) == 0:
                continue

            if name is None or name == 'None':
                x_f[local_valid] = np.NaN
                y_f[local_valid] = np.NaN
                continue

            pixel_pt_arr = [
                geom.Point2D(xPix[ii], yPix[ii]) for ii in local_valid[0]
            ]

            focal_pt_arr = pixel_to_focal_dict[name].applyForward(pixel_pt_arr)
            focal_coord_arr = np.array([[pt.getX(), pt.getY()]
                                        for pt in focal_pt_arr]).transpose()
            x_f[local_valid] = focal_coord_arr[0]
            y_f[local_valid] = focal_coord_arr[1]

    else:
        if chipNameList[0] is None or chipNameList[0] == 'None':
            x_f = np.NaN
            y_f = np.NaN
        else:
            pixel_pt = geom.Point2D(xPix, yPix)
            focal_pt = pixel_to_focal_dict[chipNameList[0]].applyForward(
                pixel_pt)
            x_f = focal_pt.getX()
            y_f = focal_pt.getY()

    return pupilCoordsFromFocalPlaneCoordsLSST(x_f, y_f, band=band)
    def test_generic_camera_wrapper(self):
        """
        Test that GalSimCameraWrapper wraps its methods as expected.
        This is mostly to catch changes in afw API.
        """
        camera = camTestUtils.CameraWrapper().camera
        camera_wrapper = GalSimCameraWrapper(camera)

        obs_mjd = ObservationMetaData(mjd=60000.0)
        ra, dec = raDecFromAltAz(35.0, 112.0, obs_mjd)
        obs = ObservationMetaData(pointingRA=ra,
                                  pointingDec=dec,
                                  mjd=obs_mjd.mjd,
                                  rotSkyPos=22.4)

        rng = np.random.RandomState(8124)

        for detector in camera:
            name = detector.getName()
            bbox = camera[name].getBBox()
            bbox_wrapper = camera_wrapper.getBBox(name)
            self.assertEqual(bbox.getMinX(), bbox_wrapper.getMinX())
            self.assertEqual(bbox.getMaxX(), bbox_wrapper.getMaxX())
            self.assertEqual(bbox.getMinY(), bbox_wrapper.getMinY())
            self.assertEqual(bbox.getMaxY(), bbox_wrapper.getMaxY())

            center_point = camera[name].getCenter(FOCAL_PLANE)
            pixel_system = camera[name].makeCameraSys(PIXELS)
            center_pix = camera.transform(center_point, FOCAL_PLANE,
                                          pixel_system)
            center_pix_wrapper = camera_wrapper.getCenterPixel(name)
            self.assertEqual(center_pix.getX(), center_pix_wrapper.getX())
            self.assertEqual(center_pix.getY(), center_pix_wrapper.getY())

            pupil_system = camera[name].makeCameraSys(FIELD_ANGLE)
            center_pupil = camera.transform(center_point, FOCAL_PLANE,
                                            pupil_system)
            center_pupil_wrapper = camera_wrapper.getCenterPupil(name)
            self.assertEqual(center_pupil.getX(), center_pupil_wrapper.getX())
            self.assertEqual(center_pupil.getY(), center_pupil_wrapper.getY())

            corner_pupil_wrapper = camera_wrapper.getCornerPupilList(name)
            corner_point_list = camera[name].getCorners(FOCAL_PLANE)
            for point in corner_point_list:
                point_pupil = camera.transform(point, FOCAL_PLANE,
                                               pupil_system)
                dd_min = 1.0e10
                for wrapper_point in corner_pupil_wrapper:
                    dd = np.sqrt(
                        (point_pupil.getX() - wrapper_point.getX())**2 +
                        (point_pupil.getY() - wrapper_point.getY())**2)

                    if dd < dd_min:
                        dd_min = dd
                self.assertLess(dd_min, 1.0e-20)

            xpix_min = None
            xpix_max = None
            ypix_min = None
            ypix_max = None
            focal_to_tan_pix = camera[name].getTransform(
                FOCAL_PLANE, TAN_PIXELS)
            for point in corner_point_list:
                pixel_point = focal_to_tan_pix.applyForward(point)
                xx = pixel_point.getX()
                yy = pixel_point.getY()
                if xpix_min is None or xx < xpix_min:
                    xpix_min = xx
                if ypix_min is None or yy < ypix_min:
                    ypix_min = yy
                if xpix_max is None or xx > xpix_max:
                    xpix_max = xx
                if ypix_max is None or yy > ypix_max:
                    ypix_max = yy

            pix_bounds_wrapper = camera_wrapper.getTanPixelBounds(name)
            self.assertEqual(pix_bounds_wrapper[0], xpix_min)
            self.assertEqual(pix_bounds_wrapper[1], xpix_max)
            self.assertEqual(pix_bounds_wrapper[2], ypix_min)
            self.assertEqual(pix_bounds_wrapper[3], ypix_max)

            x_pup = rng.random_sample(10) * 0.005 - 0.01
            y_pup = rng.random_sample(10) * 0.005 - 0.01
            x_pix, y_pix = pixelCoordsFromPupilCoords(x_pup,
                                                      y_pup,
                                                      chipName=name,
                                                      camera=camera)

            (x_pix_wrapper,
             y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords(
                 x_pup, y_pup, name, obs)

            nan_x = np.where(np.isnan(x_pix))
            self.assertEqual(len(nan_x[0]), 0)
            np.testing.assert_array_equal(x_pix, x_pix_wrapper)
            np.testing.assert_array_equal(y_pix, y_pix_wrapper)

            x_pix = rng.random_sample(10) * 100.0 - 200.0
            y_pix = rng.random_sample(10) * 100.0 - 200.0
            x_pup, y_pup = pupilCoordsFromPixelCoords(x_pix,
                                                      y_pix,
                                                      chipName=name,
                                                      camera=camera)

            (x_pup_wrapper,
             y_pup_wrapper) = camera_wrapper.pupilCoordsFromPixelCoords(
                 x_pix, y_pix, name, obs)

            nan_x = np.where(np.isnan(x_pup))
            self.assertEqual(len(nan_x[0]), 0)
            np.testing.assert_array_equal(x_pup, x_pup_wrapper)
            np.testing.assert_array_equal(y_pup, y_pup_wrapper)

            ra, dec = raDecFromPixelCoords(x_pix,
                                           y_pix,
                                           name,
                                           camera=camera,
                                           obs_metadata=obs)

            (ra_wrapper, dec_wrapper) = camera_wrapper.raDecFromPixelCoords(
                x_pix, y_pix, name, obs)

            nan_ra = np.where(np.isnan(ra))
            self.assertEqual(len(nan_ra[0]), 0)
            np.testing.assert_array_equal(ra, ra_wrapper)
            np.testing.assert_array_equal(dec, dec_wrapper)

            ra, dec = _raDecFromPixelCoords(x_pix,
                                            y_pix,
                                            name,
                                            camera=camera,
                                            obs_metadata=obs)

            (ra_wrapper, dec_wrapper) = camera_wrapper._raDecFromPixelCoords(
                x_pix, y_pix, name, obs)

            nan_ra = np.where(np.isnan(ra))
            self.assertEqual(len(nan_ra[0]), 0)
            np.testing.assert_array_equal(ra, ra_wrapper)
            np.testing.assert_array_equal(dec, dec_wrapper)

            ra = obs.pointingRA + (rng.random_sample(10) * 150.0 -
                                   100.0) / 160.0
            dec = obs.pointingDec + (rng.random_sample(10) * 150.0 -
                                     100.0) / 160.0

            x_pix, y_pix = pixelCoordsFromRaDec(ra,
                                                dec,
                                                chipName=name,
                                                camera=camera,
                                                obs_metadata=obs)

            (x_pix_wrapper,
             y_pix_wrapper) = camera_wrapper.pixelCoordsFromRaDec(
                 ra, dec, chipName=name, obs_metadata=obs)

            nan_x = np.where(np.isnan(x_pix))
            self.assertEqual(len(nan_x[0]), 0)
            np.testing.assert_array_equal(x_pix, x_pix_wrapper)
            np.testing.assert_array_equal(y_pix, y_pix_wrapper)

            ra = np.radians(ra)
            dec = np.radians(dec)

            x_pix, y_pix = _pixelCoordsFromRaDec(ra,
                                                 dec,
                                                 chipName=name,
                                                 camera=camera,
                                                 obs_metadata=obs)

            (x_pix_wrapper,
             y_pix_wrapper) = camera_wrapper._pixelCoordsFromRaDec(
                 ra, dec, chipName=name, obs_metadata=obs)

            nan_x = np.where(np.isnan(x_pix))
            self.assertEqual(len(nan_x[0]), 0)
            np.testing.assert_array_equal(x_pix, x_pix_wrapper)
            np.testing.assert_array_equal(y_pix, y_pix_wrapper)

        del camera
def _build_lsst_pupil_coord_map():
    """
    This method populates the global variable _lsst_pupil_coord_map
    _lsst_pupil_coord_map['name'] contains a list of the names of each chip in the lsst camera
    _lsst_pupil_coord_map['xx'] contains the x pupil coordinate of the center of each chip
    _lsst_pupil_coord_map['yy'] contains the y pupil coordinate of the center of each chip
    _lsst_pupil_coord_map['dp'] contains the radius (in pupil coordinates) of the circle containing each chip
    """
    global _lsst_pupil_coord_map
    if _lsst_pupil_coord_map is not None:
        raise RuntimeError("Calling _build_pupil_coord_map(), "
                           "but it is already built.")

    name_list = []
    x_pix_list = []
    y_pix_list = []
    n_chips = 0
    for chip in lsst_camera():
        chip_name = chip.getName()
        n_chips += 1
        corner_list = getCornerPixels(chip_name, lsst_camera())
        for corner in corner_list:
            x_pix_list.append(corner[0])
            y_pix_list.append(corner[1])
            name_list.append(chip_name)

    x_pix_list = np.array(x_pix_list)
    y_pix_list = np.array(y_pix_list)

    x_pup_list, y_pup_list = pupilCoordsFromPixelCoords(x_pix_list,
                                                        y_pix_list,
                                                        name_list,
                                                        camera=lsst_camera())
    center_x = np.zeros(n_chips, dtype=float)
    center_y = np.zeros(n_chips, dtype=float)
    extent = np.zeros(n_chips, dtype=float)
    final_name = []
    for ix_ct in range(n_chips):
        ix = ix_ct*4
        chip_name = name_list[ix]
        xx = 0.25*(x_pup_list[ix] + x_pup_list[ix+1] +
                   x_pup_list[ix+2] + x_pup_list[ix+3])

        yy = 0.25*(y_pup_list[ix] + y_pup_list[ix+1] +
                   y_pup_list[ix+2] + y_pup_list[ix+3])

        dx = 0.25*np.array([np.sqrt(np.power(xx-x_pup_list[ix+ii], 2) +
                                    np.power(yy-y_pup_list[ix+ii], 2)) for ii in range(4)]).sum()

        center_x[ix_ct] = xx
        center_y[ix_ct] = yy
        extent[ix_ct] = dx
        final_name.append(chip_name)

    final_name = np.array(final_name)

    _lsst_pupil_coord_map = {}
    _lsst_pupil_coord_map['name'] = final_name
    _lsst_pupil_coord_map['xx'] = center_x
    _lsst_pupil_coord_map['yy'] = center_y
    _lsst_pupil_coord_map['dp'] = extent