示例#1
0
 def setUp(self):
     self.lsstCamWrapper = testUtils.CameraWrapper(isLsstLike=True)
     self.scCamWrapper = testUtils.CameraWrapper(isLsstLike=False)
     self.cameraList = (self.lsstCamWrapper, self.scCamWrapper)
     self.assemblyList = {}
     self.assemblyList[self.lsstCamWrapper.camera.getName()] =\
         [afwImage.ImageU('tests/test_amp.fits.gz') for i in range(8)]
     self.assemblyList[self.scCamWrapper.camera.getName()] =\
         [afwImage.ImageU('tests/test.fits.gz')]
示例#2
0
 def setUp(self):
     self.lsstCamWrapper = testUtils.CameraWrapper(isLsstLike=True)
     self.scCamWrapper = testUtils.CameraWrapper(isLsstLike=False)
     self.cameraList = (self.lsstCamWrapper, self.scCamWrapper)
     self.assemblyList = {}
     self.assemblyList[self.lsstCamWrapper.camera.getName()] =\
         [afwImage.ImageU(os.path.join(testPath, 'test_amp.fits.gz'))
          for i in range(8)]
     self.assemblyList[self.scCamWrapper.camera.getName()] =\
         [afwImage.ImageU(os.path.join(testPath, 'test.fits.gz'), allowUnsafe=True)]
示例#3
0
    def getCamera(self):
        """Construct a test camera object.

        Returns
        -------
        camera : `lsst.afw.cameraGeom.camera`
            Test camera.
        """
        cameraWrapper = afwTestUtils.CameraWrapper(self.config.isLsstLike)
        camera = cameraWrapper.camera
        return camera
class testStellarCatalog(InstanceCatalog, AstrometryStars, CameraCoords):
    """
    Define a catalog of stars with all possible astrometric columns
    """

    camera = camTestUtils.CameraWrapper().camera

    column_outputs = [
        'glon', 'glat', 'x_pupil', 'y_pupil', 'xPix', 'yPix', 'xFocalPlane',
        'yFocalPlane', 'chipName', 'raObserved', 'decObserved'
    ]
示例#5
0
    def setUpClass(cls):
        cls.scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='allowedChipsTest-')
        cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1,
                                      mjd=57381.2, rotSkyPos=43.2,
                                      bandpassName='r')

        cls.camera = camTestUtils.CameraWrapper().camera

        cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt')
        if os.path.exists(cls.dbFileName):
            os.unlink(cls.dbFileName)

        cls.controlSed = Sed()
        cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'),
                                                    'flatSED', 'sed_flat.txt.gz'))
        cls.magNorm = 18.1
        imsim = Bandpass()
        imsim.imsimBandpass()
        ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim)
        cls.controlSed.multiplyFluxNorm(ff)
        a_x, b_x = cls.controlSed.setupCCMab()
        cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1)
        bpd = BandpassDict.loadTotalBandpassesFromFiles()
        pp = PhotometricParameters()
        cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp)
        cls.countSigma = np.sqrt(cls.controlADU/pp.gain)

        cls.x_pix = 50
        cls.y_pix = 50

        x_list = []
        y_list = []
        name_list = []
        for dd in cls.camera:
            x_list.append(cls.x_pix)
            y_list.append(cls.y_pix)
            name_list.append(dd.getName())

        x_list = np.array(x_list)
        y_list = np.array(y_list)

        ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list,
                                                 camera=cls.camera, obs_metadata=cls.obs,
                                                 epoch=2000.0)

        dra_list = 3600.0*(ra_list-cls.obs.pointingRA)
        ddec_list = 3600.0*(dec_list-cls.obs.pointingDec)

        create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list,
                            mag_norm=[cls.magNorm]*len(dra_list))

        cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
示例#6
0
    def getCamera(self):
        """Construct a test camera object.

        Returns
        -------
        camera : `lsst.afw.cameraGeom.camera`
            Test camera.
        """
        cameraWrapper = afwTestUtils.CameraWrapper(
            plateScale=self.config.plateScale,
            radialDistortion=self.config.radialDistortion,
            isLsstLike=self.config.isLsstLike,
        )
        camera = cameraWrapper.camera
        return camera
    def setUpClass(cls):
        cls.camera = camTestUtils.CameraWrapper().camera
        cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix='GalSimInterfaceTest-')
        cls.dbName = os.path.join(cls.scratch_dir, 'galSimTestDB.db')

        deltaRA = np.array([72.0/3600.0])
        deltaDec = np.array([0.0])
        defaults = LSSTdefaults()
        cls.bandpassNameList = ['u', 'g', 'r', 'i', 'z', 'y']
        cls.m5 = [16.0+ix for ix in range(len(cls.bandpassNameList))]
        cls.seeing = [defaults._FWHMeff[bb] for bb in cls.bandpassNameList]
        cls.obs_metadata = makePhoSimTestDB(filename=cls.dbName, size=1,
                                            deltaRA=deltaRA,
                                            deltaDec=deltaDec,
                                            bandpass=cls.bandpassNameList,
                                            m5=cls.m5,
                                            seeing=cls.seeing,
                                            seedVal=65)

        cls.driver = 'sqlite'
示例#8
0
class testCatalog(InstanceCatalog, AstrometryStars, CameraCoords):
    """
    A (somewhat meaningless) instance catalog class that will allow us
    to run the astrometry routines for testing purposes
    """
    catalog_type = __file__ + 'test_stars'
    column_outputs = ['id', 'raICRS', 'decICRS',
                      'parallax', 'radial_velocity',
                      'x_pupil', 'y_pupil',
                      'chipName', 'xPix', 'yPix', 'xFocalPlane', 'yFocalPlane']
    # Needed to do camera coordinate transforms.
    camera = camTestUtils.CameraWrapper().camera
    default_formats = {'f': '%.12f'}

    delimiter = ';'  # so that np.loadtxt can parse the chipNames which may contain commas
                     # (see testClassMethods)

    default_columns = [('properMotionRa', 0., float),
                       ('properMotionDec', 0., float),
                       ('parallax', 1.2, float),
                       ('radial_velocity', 0., float)]
    def setUpClass(cls):
        cls.camera = camTestUtils.CameraWrapper().camera
        cls.dataDir = tempfile.mkdtemp(dir=ROOT, prefix='GalSimPhoSimTest-')
        cls.n_objects = 5
        rng = np.random.RandomState(45)
        pointingRA = 45.2
        pointingDec = -31.6

        cls.obs = ObservationMetaData(pointingRA=pointingRA,
                                      pointingDec=pointingDec,
                                      rotSkyPos=1.2,
                                      bandpassName='r',
                                      mjd=57341.5,
                                      boundLength=0.1,
                                      boundType='circle')

        cls.dtype = np.dtype([
            ('id', int), ('raJ2000', np.float), ('decJ2000', np.float),
            ('ra_deg', np.float), ('dec_deg', np.float),
            ('sedFilename', (str, 300)), ('magNorm', np.float),
            ('redshift', np.float), ('majorAxis', np.float),
            ('minorAxis', np.float), ('positionAngle', np.float),
            ('halfLightRadius', np.float), ('sindex', np.float),
            ('internalAv', np.float), ('internalRv', np.float),
            ('galacticAv', np.float), ('galacticRv', np.float),
            ('properMotionRa', np.float), ('properMotionDec', np.float),
            ('radialVelocity', np.float), ('parallax', np.float)
        ])

        # generate some galaxy bulge data
        redshift = rng.random_sample(cls.n_objects) * 1.5
        rr = rng.random_sample(cls.n_objects) * 0.05
        theta = rng.random_sample(cls.n_objects) * 2.0 * np.pi
        ra = np.radians(pointingRA + rr * np.cos(theta))
        dec = np.radians(pointingDec + rr * np.sin(theta))
        magNorm = rng.random_sample(cls.n_objects) * 7.0 + 18.0
        sindex = rng.random_sample(cls.n_objects) * 4.0 + 1.0
        hlr = radiansFromArcsec(rng.random_sample(cls.n_objects) * 10.0 + 1.0)
        positionAngle = rng.random_sample(cls.n_objects) * np.pi
        internalAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        internalRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        majorAxis = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 2.0 + 0.5)
        minorAxis = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 2.0 + 0.5)
        galacticAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        galacticRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        properMotionRa = np.zeros(cls.n_objects)
        properMotionDec = np.zeros(cls.n_objects)
        radialVelocity = np.zeros(cls.n_objects)
        parallax = np.zeros(cls.n_objects)
        cls.bulge_name = os.path.join(cls.dataDir,
                                      'galSimPhoSim_test_bulge.dat')
        with open(cls.bulge_name, 'w') as output_file:
            output_file.write('# header\n')
            for ix in range(cls.n_objects):
                output_file.write(
                    '%d %f %f %f %f Const.79E06.002Z.spec %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n'
                    % (ix, ra[ix], dec[ix], np.degrees(ra[ix]),
                       np.degrees(dec[ix]), magNorm[ix], redshift[ix],
                       max(majorAxis[ix], minorAxis[ix]),
                       min(majorAxis[ix], minorAxis[ix]), positionAngle[ix],
                       hlr[ix], sindex[ix], internalAv[ix], internalRv[ix],
                       galacticAv[ix], galacticRv[ix], properMotionRa[ix],
                       properMotionDec[ix], radialVelocity[ix], parallax[ix]))

        # generate some galaxy disk data
        redshift = rng.random_sample(cls.n_objects) * 1.5
        rr = rng.random_sample(cls.n_objects) * 0.05
        theta = rng.random_sample(cls.n_objects) * 2.0 * np.pi
        ra = np.radians(pointingRA + rr * np.cos(theta))
        dec = np.radians(pointingDec + rr * np.sin(theta))
        magNorm = rng.random_sample(cls.n_objects) * 7.0 + 18.0
        sindex = rng.random_sample(cls.n_objects) * 4.0 + 1.0
        hlr = radiansFromArcsec(rng.random_sample(cls.n_objects) * 10.0 + 1.0)
        positionAngle = rng.random_sample(cls.n_objects) * np.pi
        internalAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        internalRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        majorAxis = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 2.0 + 0.5)
        minorAxis = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 2.0 + 0.5)
        galacticAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        galacticRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        properMotionRa = np.zeros(cls.n_objects)
        properMotionDec = np.zeros(cls.n_objects)
        radialVelocity = np.zeros(cls.n_objects)
        parallax = np.zeros(cls.n_objects)
        cls.disk_name = os.path.join(cls.dataDir, 'galSimPhoSim_test_disk.dat')
        with open(cls.disk_name, 'w') as output_file:
            output_file.write('# header\n')
            for ix in range(cls.n_objects):
                output_file.write(
                    '%d %f %f %f %f Inst.79E06.02Z.spec %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n'
                    % (ix, ra[ix], dec[ix], np.degrees(ra[ix]),
                       np.degrees(dec[ix]), magNorm[ix], redshift[ix],
                       max(majorAxis[ix], minorAxis[ix]),
                       min(majorAxis[ix], minorAxis[ix]), positionAngle[ix],
                       hlr[ix], sindex[ix], internalAv[ix], internalRv[ix],
                       galacticAv[ix], galacticRv[ix], properMotionRa[ix],
                       properMotionDec[ix], radialVelocity[ix], parallax[ix]))

        # generate some agn data
        redshift = rng.random_sample(cls.n_objects) * 1.5
        rr = rng.random_sample(cls.n_objects) * 0.05
        theta = rng.random_sample(cls.n_objects) * 2.0 * np.pi
        ra = np.radians(pointingRA + rr * np.cos(theta))
        dec = np.radians(pointingDec + rr * np.sin(theta))
        magNorm = rng.random_sample(cls.n_objects) * 7.0 + 18.0
        sindex = np.zeros(cls.n_objects)
        hlr = np.zeros(cls.n_objects)
        positionAngle = np.zeros(cls.n_objects)
        internalAv = np.zeros(cls.n_objects)
        internalRv = np.zeros(cls.n_objects)
        majorAxis = np.zeros(cls.n_objects)
        minorAxis = np.zeros(cls.n_objects)
        galacticAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        galacticRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        properMotionRa = np.zeros(cls.n_objects)
        properMotionDec = np.zeros(cls.n_objects)
        radialVelocity = np.zeros(cls.n_objects)
        parallax = np.zeros(cls.n_objects)
        cls.agn_name = os.path.join(cls.dataDir, 'galSimPhoSim_test_agn.dat')
        with open(cls.agn_name, 'w') as output_file:
            output_file.write('# header\n')
            for ix in range(cls.n_objects):
                output_file.write(
                    '%d %f %f %f %f agn.spec %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n'
                    % (ix, ra[ix], dec[ix], np.degrees(ra[ix]),
                       np.degrees(dec[ix]), magNorm[ix], redshift[ix],
                       max(majorAxis[ix], minorAxis[ix]),
                       min(majorAxis[ix], minorAxis[ix]), positionAngle[ix],
                       hlr[ix], sindex[ix], internalAv[ix], internalRv[ix],
                       galacticAv[ix], galacticRv[ix], properMotionRa[ix],
                       properMotionDec[ix], radialVelocity[ix], parallax[ix]))

        # generate some star data
        redshift = rng.random_sample(cls.n_objects) * 1.5
        rr = rng.random_sample(cls.n_objects) * 0.05
        theta = rng.random_sample(cls.n_objects) * 2.0 * np.pi
        ra = np.radians(pointingRA + rr * np.cos(theta))
        dec = np.radians(pointingDec + rr * np.sin(theta))
        magNorm = rng.random_sample(cls.n_objects) * 7.0 + 18.0
        sindex = np.zeros(cls.n_objects)
        hlr = np.zeros(cls.n_objects)
        positionAngle = np.zeros(cls.n_objects)
        internalAv = np.zeros(cls.n_objects)
        internalRv = np.zeros(cls.n_objects)
        majorAxis = np.zeros(cls.n_objects)
        minorAxis = np.zeros(cls.n_objects)
        galacticAv = rng.random_sample(cls.n_objects) * 0.5 + 0.1
        galacticRv = rng.random_sample(cls.n_objects) * 0.5 + 2.7
        properMotionRa = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 0.0002)
        properMotionDec = radiansFromArcsec(
            rng.random_sample(cls.n_objects) * 0.0002)
        radialVelocity = rng.random_sample(cls.n_objects) * 200.0
        parallax = radiansFromArcsec(rng.random_sample(cls.n_objects) * 0.0002)
        cls.star_name = os.path.join(cls.dataDir, 'galSimPhoSim_test_star.dat')
        with open(cls.star_name, 'w') as output_file:
            output_file.write('# header\n')
            for ix in range(cls.n_objects):
                output_file.write(
                    '%d %f %f %f %f km30_5000.fits_g10_5040 %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n'
                    % (ix, ra[ix], dec[ix], np.degrees(ra[ix]),
                       np.degrees(dec[ix]), magNorm[ix], redshift[ix],
                       max(majorAxis[ix], minorAxis[ix]),
                       min(majorAxis[ix], minorAxis[ix]), positionAngle[ix],
                       hlr[ix], sindex[ix], internalAv[ix], internalRv[ix],
                       galacticAv[ix], galacticRv[ix], properMotionRa[ix],
                       properMotionDec[ix], radialVelocity[ix], parallax[ix]))
    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
示例#11
0
#!/usr/bin/env python2
import lsst.afw.cameraGeom.testUtils as testUtils
import lsst.afw.cameraGeom as cameraGeom
import lsst.afw.geom as afwGeom

# Construct a mock LSST-like camera.
# Normally you would obtain a camera from a data butler using butler.get("camera")
# but when this example was written the software stack did not including a sample data repository.
camera = testUtils.CameraWrapper(isLsstLike=True).camera

# Get a detector from the camera by name (though you may specify an ID, if you prefer).
det = camera["R:1,0 S:1,1"]

# Convert a 2-d point from PIXELS to both FOCAL_PLANE and PUPIL coordinates.
detPoint = det.makeCameraPoint(afwGeom.Point2D(
    25, 43.2), cameraGeom.PIXELS)  # position on detector in pixels
fpPoint = det.transform(
    detPoint, cameraGeom.FOCAL_PLANE)  # position in focal plane in mm
pupilPoint = camera.transform(
    detPoint, cameraGeom.PUPIL)  # position in pupil, in radians

# Find all detectors that overlap a specific point (in this case find the detector we already have)
detList = camera.findDetectors(fpPoint)
assert len(detList) == 1
assert detList[0].getName() == det.getName()

# Convert a point from PUPIL to PIXELS coordinates.
# For a detector-based coordinate system, such as PIXELS, may specify a particular detector
# or let the Camera find a detector:
# * To specify a particular detector, specify the target coordinate system as a CameraSys
#   with the specified detectorName filled in (e.g. use detector.makeCameraSys).
class GalSimBase(InstanceCatalog, CameraCoords):
    """
    The catalog classes in this file use the InstanceCatalog infrastructure to construct
    FITS images for each detector-filter combination on a simulated camera.  This is done by
    instantiating the class GalSimInterpreter.  GalSimInterpreter is the class which
    actually generates the FITS images.  As the GalSim InstanceCatalogs are iterated over,
    each object in the catalog is passed to the GalSimInterpeter, which adds the object
    to the appropriate FITS images.  The user can then write the images to disk by calling
    the write_images method in the GalSim InstanceCatalog.

    Objects are passed to the GalSimInterpreter by the get_fitsFiles getter function, which
    adds a column to the InstanceCatalog indicating which detectors' FITS files contain each
    object.

    Note: because each GalSim InstanceCatalog has its own GalSimInterpreter, it means
    that each GalSimInterpreter will only draw FITS images containing one type of object
    (whatever type of object is contained in the GalSim InstanceCatalog).  If the user
    wishes to generate FITS images containing multiple types of object, the method
    copyGalSimInterpreter allows the user to pass the GalSimInterpreter from one
    GalSim InstanceCatalog to another (so, the user could create a GalSim Instance
    Catalog of stars, generate that InstanceCatalog, then create a GalSim InstanceCatalog
    of galaxies, pass the GalSimInterpreter from the star catalog to this new catalog,
    and thus create FITS images that contain both stars and galaxies; see galSimCompoundGenerator.py
    in the examples/ directory of sims_catUtils for an example).

    This class (GalSimBase) is the base class for all GalSim InstanceCatalogs.  Daughter
    classes of this class need to behave like ordinary InstanceCatalog daughter classes
    with the following exceptions:

    1) If they re-define column_outputs, they must be certain to include the column
    'fitsFiles.'  The getter for this column (defined in this class) calls all of the
    GalSim image generation infrastructure

    2) Daughter classes of this class must define a member variable galsim_type that is either
    'sersic' or 'pointSource'.  This variable tells the GalSimInterpreter how to draw the
    object (to allow a different kind of image profile, define a new method in the GalSimInterpreter
    class similar to drawPoinSource and drawSersic)

    3) The variables bandpass_names (a list of the form ['u', 'g', 'r', 'i', 'z', 'y']),
    bandpass_directory, and bandpass_root should be defined to tell the GalSim InstanceCatalog
    where to find the files defining the bandpasses to be used for these FITS files.
    The GalSim InstanceCatalog will look for bandpass files in files with the names

    for bpn in bandpass_names:
        name = self.bandpass_directory+'/'+self.bandpass_root+'_'+bpn+'.dat'

    one should also define the following member variables:

        componentList is a list of files ins banpass_directory containing the response
        curves for the different components of the camera, e.g.
        ['detector.dat', 'm1.dat', 'm2.dat', 'm3.dat', 'lens1.dat', 'lens2.dat', 'lens3.dat']

        atomTransmissionName is the name of the file in bandpass_directory that contains the
        atmostpheric transmissivity, e.g. 'atmos_std.dat'

    4) Telescope parameters such as exposure time, area, and gain are stored in the
    GalSim InstanceCatalog member variable photParams, which is an instantiation of
    the class PhotometricParameters defined in sims_photUtils.

    Daughter classes of GalSimBase will generate both FITS images for all of the detectors/filters
    in their corresponding cameras and InstanceCatalogs listing all of the objects
    contained in those images.  The catalog is written using the normal write_catalog()
    method provided for all InstanceClasses.  The FITS files are drawn using the write_images()
    method that is unique to GalSim InstanceCatalogs.  The FITS file will be named something like:

    DetectorName_FilterName.fits

    (a typical LSST fits file might be R_0_0_S_1_0_y.fits)

    Note: If you call write_images() before iterating over the catalog (either by calling
    write_catalog() or using the iterator returned by InstanceCatalog.iter_catalog()),
    you will get empty or incomplete FITS files.  Objects are only added to the GalSimInterpreter
    in the course of iterating over the InstanceCatalog.
    """

    seed = 42

    # This is sort of a hack; it prevents findChipName in coordUtils from dying
    # if an object lands on multiple science chips.
    allow_multiple_chips = True

    # There is no point in writing things to the InstanceCatalog that do not have SEDs and/or
    # do not land on any detectors
    cannot_be_null = ['sedFilepath']

    column_outputs = [
        'galSimType', 'uniqueId', 'raICRS', 'decICRS', 'chipName', 'x_pupil',
        'y_pupil', 'sedFilepath', 'majorAxis', 'minorAxis', 'sindex',
        'halfLightRadius', 'positionAngle', 'fitsFiles'
    ]

    transformations = {
        'raICRS': np.degrees,
        'decICRS': np.degrees,
        'x_pupil': arcsecFromRadians,
        'y_pupil': arcsecFromRadians,
        'halfLightRadius': arcsecFromRadians
    }

    default_formats = {'S': '%s', 'f': '%.9g', 'i': '%i'}

    # This is used as the delimiter because the names of the detectors printed in the fitsFiles
    # column contain both ':' and ','
    delimiter = '; '

    sedDir = lsst.utils.getPackageDir('sims_sed_library')

    bandpassNames = None
    bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),
                               'baseline')
    bandpassRoot = 'filter_'
    componentList = [
        'detector.dat', 'm1.dat', 'm2.dat', 'm3.dat', 'lens1.dat', 'lens2.dat',
        'lens3.dat'
    ]
    atmoTransmissionName = 'atmos_std.dat'

    # allowed_chips is a list of the names of the detectors we actually want to draw.
    # If 'None', then all chips are drawn.
    allowed_chips = None

    # This member variable will define a PSF to convolve with the sources.
    # See the classes PSFbase and DoubleGaussianPSF in
    # galSimUtilities.py for more information
    PSF = None

    # This member variable can store a GalSim noise model instantiation
    # which will be applied to the FITS images when they are created
    noise_and_background = None

    # Stores the gain and readnoise
    photParams = PhotometricParameters()

    # This is just a place holder for the camera object associated with the InstanceCatalog.
    # If you want to assign a different camera, you can do so immediately after instantiating this class
    camera = camTestUtils.CameraWrapper().camera

    uniqueSeds = {
    }  # a cache for un-normalized SED files, so that we do not waste time on I/O

    hasBeenInitialized = False

    galSimInterpreter = None  # the GalSimInterpreter instantiation for this catalog

    totalDrawings = 0
    totalObjects = 0

    def _initializeGalSimCatalog(self):
        """
        Initializes an empty list of objects that have already been drawn to FITS images.
        We do not want to accidentally draw an object twice.

        Also initializes the GalSimInterpreter by calling self._initializeGalSimInterpreter()

        Objects are stored based on their uniqueId values.
        """
        self.objectHasBeenDrawn = []
        self._initializeGalSimInterpreter()
        self.hasBeenInitialized = True

    @cached
    def get_sedFilepath(self):
        """
        Maps the name of the SED as stored in the database to the file stored in
        sims_sed_library
        """
        # copied from the phoSim catalogs
        return np.array([
            self.specFileMap[k] if k in self.specFileMap else None
            for k in self.column_by_name('sedFilename')
        ])

    def _calcSingleGalSimSed(self, sedName, zz, iAv, iRv, gAv, gRv, norm):
        """
        correct the SED for redshift, dust, etc.  Return an Sed object as defined in
        sims_photUtils/../../Sed.py
        """
        if _is_null(sedName):
            return None
        sed = self._getSedCopy(sedName)
        imsimband = Bandpass()
        imsimband.imsimBandpass()
        # normalize the SED
        # Consulting the file sed.py in GalSim/galsim/ it appears that GalSim expects
        # its SEDs to ultimately be in units of ergs/nm so that, when called, they can
        # be converted to photons/nm (see the function __call__() and the assignment of
        # self._rest_photons in the __init__() of galsim's sed.py file).  Thus, we need
        # to read in our SEDs, normalize them, and then multiply by the exposure time
        # and the effective area to get from ergs/s/cm^2/nm to ergs/nm.
        #
        # The gain parameter should convert between photons and ADU (so: it is the
        # traditional definition of "gain" -- electrons per ADU -- multiplied by the
        # quantum efficiency of the detector).  Because we fold the quantum efficiency
        # of the detector into our total_[u,g,r,i,z,y].dat bandpass files
        # (see the readme in the THROUGHPUTS_DIR/baseline/), we only need to multiply
        # by the electrons per ADU gain.
        #
        # We will take these parameters from an instantiation of the PhotometricParameters
        # class (which can be reassigned by defining a daughter class of this class)
        #
        fNorm = sed.calcFluxNorm(norm, imsimband)
        sed.multiplyFluxNorm(fNorm)

        # apply dust extinction (internal)
        if iAv != 0.0 and iRv != 0.0:
            a_int, b_int = sed.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=iAv, R_v=iRv)

        # 22 June 2015
        # apply redshift; there is no need to apply the distance modulus from
        # sims/photUtils/CosmologyWrapper; magNorm takes that into account
        # however, magNorm does not take into account cosmological dimming
        if zz != 0.0:
            sed.redshiftSED(zz, dimming=True)

        # apply dust extinction (galactic)
        if gAv != 0.0 and gRv != 0.0:
            a_int, b_int = sed.setupCCMab()
            sed.addCCMDust(a_int, b_int, A_v=gAv, R_v=gRv)
        return sed

    def _getSedCopy(self, sedName):
        """
        Return a copy of the requested SED, either from the cached
        version or creating a new one and caching a copy for later
        reuse.
        """
        if sedName in self.uniqueSeds:
            # we have already read in this file; no need to do it again
            sed = Sed(wavelen=self.uniqueSeds[sedName].wavelen,
                      flambda=self.uniqueSeds[sedName].flambda,
                      fnu=self.uniqueSeds[sedName].fnu,
                      name=self.uniqueSeds[sedName].name)
        else:
            # load the SED of the object
            sed = Sed()
            sedFile = os.path.join(self.sedDir, sedName)
            sed.readSED_flambda(sedFile)

            flambdaCopy = copy.deepcopy(sed.flambda)

            # If the SED is zero inside of the bandpass, GalSim raises an error.
            # This sets a minimum flux value of 1.0e-30 so that the SED is never technically
            # zero inside of the bandpass.
            sed.flambda = np.array(
                [ff if ff > 1.0e-30 else 1.0e-30 for ff in flambdaCopy])
            sed.fnu = None

            # copy the unnormalized file to uniqueSeds so we don't have to read it in again
            sedCopy = Sed(wavelen=sed.wavelen,
                          flambda=sed.flambda,
                          fnu=sed.fnu,
                          name=sed.name)
            self.uniqueSeds[sedName] = sedCopy
        return sed

    def _calculateGalSimSeds(self):
        """
        Apply any physical corrections to the objects' SEDS (redshift them, apply dust, etc.).

        Return a generator that serves up the Sed objects in order.
        """
        actualSEDnames = self.column_by_name('sedFilepath')
        redshift = self.column_by_name('redshift')
        internalAv = self.column_by_name('internalAv')
        internalRv = self.column_by_name('internalRv')
        galacticAv = self.column_by_name('galacticAv')
        galacticRv = self.column_by_name('galacticRv')
        magNorm = self.column_by_name('magNorm')

        return (self._calcSingleGalSimSed(*args)
                for args in zip(actualSEDnames, redshift, internalAv,
                                internalRv, galacticAv, galacticRv, magNorm))

    @cached
    def get_fitsFiles(self):
        """
        This getter returns a column listing the names of the detectors whose corresponding
        FITS files contain the object in question.  The detector names will be separated by a '//'

        This getter also passes objects to the GalSimInterpreter to actually draw the FITS
        images.

        WARNING: do not include 'fitsFiles' in the cannot_be_null list of non-null columns.
        If you do that, this method will be called several times by the catalog, as it
        attempts to determine which rows are actually in the catalog.  That will cause
        your images to have too much flux in them.
        """
        if self.bandpassNames is None:
            if isinstance(self.obs_metadata.bandpass, list):
                self.bandpassNames = [self.obs_metadata.bandpass]
            else:
                self.bandpassNames = self.obs_metadata.bandpass

        objectNames = self.column_by_name('uniqueId')
        raICRS = self.column_by_name('raICRS')
        decICRS = self.column_by_name('decICRS')
        xPupil = self.column_by_name('x_pupil')
        yPupil = self.column_by_name('y_pupil')
        halfLight = self.column_by_name('halfLightRadius')
        minorAxis = self.column_by_name('minorAxis')
        majorAxis = self.column_by_name('majorAxis')
        positionAngle = self.column_by_name('positionAngle')
        sindex = self.column_by_name('sindex')

        sedList = self._calculateGalSimSeds()

        if self.hasBeenInitialized is False and len(objectNames) > 0:
            # This needs to be here in case, instead of writing the whole catalog with write_catalog(),
            # the user wishes to iterate through the catalog with InstanceCatalog.iter_catalog(),
            # which will not call write_header()
            self._initializeGalSimCatalog()
            if not hasattr(self, 'bandpassDict'):
                raise RuntimeError(
                    'ran initializeGalSimCatalog but do not have bandpassDict')

        output = []
        for (name, ra, dec, xp, yp, hlr, minor, major, pa, ss, sn) in \
            zip(objectNames, raICRS, decICRS, xPupil, yPupil, halfLight,
                 minorAxis, majorAxis, positionAngle, sedList, sindex):

            if name in self.objectHasBeenDrawn:
                raise RuntimeError('Trying to draw %s more than once ' %
                                   str(name))
            elif ss is None:
                raise RuntimeError('Trying to draw an object with SED == None')
            else:

                self.objectHasBeenDrawn.append(name)

                flux_dict = {}
                for bb in self.bandpassNames:
                    adu = ss.calcADU(self.bandpassDict[bb], self.photParams)
                    flux_dict[bb] = adu * self.photParams.gain

                gsObj = GalSimCelestialObject(self.galsim_type, ss, ra, dec,
                                              xp, yp, hlr, minor, major, pa,
                                              sn, flux_dict)

                # actually draw the object
                detectorsString = self.galSimInterpreter.drawObject(gsObj)

                output.append(detectorsString)

        return np.array(output)

    def setPSF(self, PSF):
        """
        Set the PSF of this GalSimCatalog after instantiation.

        @param [in] PSF is an instantiation of a GalSimPSF class.
        """
        self.PSF = PSF
        if self.galSimInterpreter is not None:
            self.galSimInterpreter.setPSF(PSF=PSF)

    def copyGalSimInterpreter(self, otherCatalog):
        """
        Copy the camera, GalSimInterpreter, from another GalSim InstanceCatalog
        so that multiple types of object (stars, AGN, galaxy bulges, galaxy disks, etc.)
        can be drawn on the same FITS files.

        @param [in] otherCatalog is another GalSim InstanceCatalog that already has
        an initialized GalSimInterpreter

        See galSimCompoundGenerator.py in the examples/ directory of sims_catUtils for
        an example of how this is used.
        """
        self.camera = otherCatalog.camera
        self.photParams = otherCatalog.photParams
        self.bandpassDict = otherCatalog.bandpassDict
        self.galSimInterpreter = otherCatalog.galSimInterpreter
        self.PSF = otherCatalog.PSF
        self.noise_and_background = otherCatalog.noise_and_background

    def _initializeGalSimInterpreter(self):
        """
        This method creates the GalSimInterpreter (if it is None)

        This method reads in all of the data about the camera and pass it into
        the GalSimInterpreter.

        This method calls _getBandpasses to construct the paths to
        the files containing the bandpass data.
        """

        if self.galSimInterpreter is None:

            # This list will contain instantiations of the GalSimDetector class
            # (see galSimInterpreter.py), which stores detector information in a way
            # that the GalSimInterpreter will understand
            detectors = []

            for dd in self.camera:
                if self.allowed_chips is None or dd.getName(
                ) in self.allowed_chips:
                    cs = dd.makeCameraSys(PUPIL)
                    centerPupil = self.camera.transform(
                        dd.getCenter(FOCAL_PLANE), cs).getPoint()
                    centerPixel = dd.getCenter(PIXELS).getPoint()

                    translationPixel = afwGeom.Point2D(centerPixel.getX() + 1,
                                                       centerPixel.getY() + 1)
                    translationPupil = self.camera.transform(
                        dd.makeCameraPoint(translationPixel, PIXELS),
                        cs).getPoint()

                    plateScale = np.sqrt(
                        np.power(translationPupil.getX() -
                                 centerPupil.getX(), 2) +
                        np.power(translationPupil.getY() -
                                 centerPupil.getY(), 2)) / np.sqrt(2.0)

                    plateScale = 3600.0 * np.degrees(plateScale)

                    # make a detector-custom photParams that copies all of the quantities
                    # in the catalog photParams, except the platescale, which is
                    # calculated above
                    params = PhotometricParameters(
                        exptime=self.photParams.exptime,
                        nexp=self.photParams.nexp,
                        effarea=self.photParams.effarea,
                        gain=self.photParams.gain,
                        readnoise=self.photParams.readnoise,
                        darkcurrent=self.photParams.darkcurrent,
                        othernoise=self.photParams.othernoise,
                        platescale=plateScale)

                    detector = GalSimDetector(dd,
                                              self.camera,
                                              obs_metadata=self.obs_metadata,
                                              epoch=self.db_obj.epoch,
                                              photParams=params)

                    detectors.append(detector)

            if not hasattr(self, 'bandpassDict'):
                if self.noise_and_background is not None:
                    if self.obs_metadata.m5 is None:
                        raise RuntimeError(
                            'WARNING  in GalSimCatalog; you did not specify m5 in your '
                            'obs_metadata. m5 is required in order to '
                            'add noise to your images')

                    for name in self.bandpassNames:
                        if name not in self.obs_metadata.m5:
                            raise RuntimeError(
                                'WARNING in GalSimCatalog; your obs_metadata does not have '
                                + 'm5 values for all of your bandpasses \n' +
                                'bandpass has: %s \n' %
                                self.bandpassNames.__repr__() + 'm5 has: %s ' %
                                list(self.obs_metadata.m5.keys()).__repr__())

                    if self.obs_metadata.seeing is None:
                        raise RuntimeError(
                            'WARNING  in GalSimCatalog; you did not specify seeing in your '
                            'obs_metadata.  seeing is required in order to add '
                            'noise to your images')

                    for name in self.bandpassNames:
                        if name not in self.obs_metadata.seeing:
                            raise RuntimeError(
                                'WARNING in GalSimCatalog; your obs_metadata does not have '
                                +
                                'seeing values for all of your bandpasses \n' +
                                'bandpass has: %s \n' %
                                self.bandpassNames.__repr__() +
                                'seeing has: %s ' %
                                list(self.obs_metadata.seeing.keys()).__repr__(
                                ))

                (self.bandpassDict,
                 hardwareDict) = BandpassDict.loadBandpassesFromFiles(
                     bandpassNames=self.bandpassNames,
                     filedir=self.bandpassDir,
                     bandpassRoot=self.bandpassRoot,
                     componentList=self.componentList,
                     atmoTransmission=os.path.join(self.bandpassDir,
                                                   self.atmoTransmissionName))

            self.galSimInterpreter = GalSimInterpreter(
                obs_metadata=self.obs_metadata,
                epoch=self.db_obj.epoch,
                detectors=detectors,
                bandpassDict=self.bandpassDict,
                noiseWrapper=self.noise_and_background,
                seed=self.seed)

            self.galSimInterpreter.setPSF(PSF=self.PSF)

    def write_images(self, nameRoot=None):
        """
        Writes the FITS images associated with this InstanceCatalog.

        Cannot be called before write_catalog is called.

        @param [in] nameRoot is an optional string prepended to the names
        of the FITS images.  The FITS images will be named

        @param [out] namesWritten is a list of the names of the FITS files generated

        nameRoot_DetectorName_FilterName.fits

        (e.g. myImages_R_0_0_S_1_1_y.fits for an LSST-like camera with
        nameRoot = 'myImages')
        """
        namesWritten = self.galSimInterpreter.writeImages(nameRoot=nameRoot)

        return namesWritten
示例#13
0
    def test_checkpointing(self):
        "Test checkpointing of .detectorImages data."
        camera = camTestUtils.CameraWrapper().camera
        camera_wrapper = GalSimCameraWrapper(camera)
        phot_params = PhotometricParameters()
        obs_md = ObservationMetaData(pointingRA=23.0,
                                     pointingDec=12.0,
                                     rotSkyPos=13.2,
                                     mjd=59580.0,
                                     bandpassName='r')

        detectors = [
            make_galsim_detector(camera_wrapper, dd.getName(), phot_params,
                                 obs_md) for dd in camera_wrapper.camera
        ]

        # Create a GalSimInterpreter object and set the checkpoint
        # attributes.
        gs_interpreter = GalSimInterpreter(detectors=detectors)
        gs_interpreter.checkpoint_file = self.cp_file

        nobj = 10
        gs_interpreter.nobj_checkpoint = nobj

        # Set the image data by hand.
        key = "R00_S00_r.fits"
        detname = "R:0,0 S:0,0"
        detector = make_galsim_detector(camera_wrapper, detname, phot_params,
                                        obs_md)
        image = gs_interpreter.blankImage(detector=detector)
        image += 17
        gs_interpreter.detectorImages[key] = image

        # Add some drawn objects and check that the checkpoint file is
        # written at the right cadence.
        for uniqueId in range(1, nobj + 1):
            gs_interpreter.drawn_objects.add(uniqueId)
            gs_interpreter.write_checkpoint()
            if uniqueId < nobj:
                self.assertFalse(os.path.isfile(self.cp_file))
            else:
                self.assertTrue(os.path.isfile(self.cp_file))

        # Verify that the checkpointed data has the expected content.
        with open(self.cp_file, 'rb') as input_:
            cp_data = pickle.load(input_)
        self.assertTrue(np.array_equal(cp_data['images'][key], image.array))

        # Check the restore_checkpoint function.
        new_interpreter = GalSimInterpreter(detectors=detectors)
        new_interpreter.checkpoint_file = self.cp_file
        new_interpreter.restore_checkpoint(camera_wrapper, phot_params, obs_md)

        self.assertEqual(new_interpreter.drawn_objects,
                         gs_interpreter.drawn_objects)

        self.assertEqual(set(new_interpreter.detectorImages.keys()),
                         set(gs_interpreter.detectorImages.keys()))

        for det_name in new_interpreter.detectorImages.keys():
            new_img = new_interpreter.detectorImages[det_name]
            gs_img = gs_interpreter.detectorImages[det_name]
            np.testing.assert_array_equal(new_img.array, gs_img.array)
            self.assertEqual(new_img.bounds, gs_img.bounds)
            self.assertEqual(new_img.wcs.crpix1, gs_img.wcs.crpix1)
            self.assertEqual(new_img.wcs.crpix2, gs_img.wcs.crpix2)
            self.assertEqual(new_img.wcs.crval1, gs_img.wcs.crval1)
            self.assertEqual(new_img.wcs.crval2, gs_img.wcs.crval2)
            self.assertEqual(new_img.wcs.detectorName, gs_img.wcs.detectorName)
            for name in new_img.wcs.fitsHeader.names():
                self.assertEqual(new_img.wcs.fitsHeader.get(name),
                                 gs_img.wcs.fitsHeader.get(name))