예제 #1
0
    def testNoisyCatalog(self):
        """
        Compare noisy and noiseless images drawn from the same catalog.
        Make sure that the pixel-by-pixel difference between the two is
        as expected from Poisson statistics.
        """
        noisyCatName = os.path.join(self.scratch_dir, 'testNoisyCatalog.sav')
        cleanCatName = os.path.join(self.scratch_dir, 'testCleanCatalog.sav')

        gals = testGalaxyBulgeDBObj(driver=self.driver, database=self.dbName)

        noisyCat = noisyCatalog(gals, obs_metadata=self.obs_metadata)
        cleanCat = backgroundCatalog(gals, obs_metadata=self.obs_metadata)

        noisyCat.camera_wrapper = GalSimCameraWrapper(self.camera)
        cleanCat.camera_wrapper = GalSimCameraWrapper(self.camera)

        noisyCat.write_catalog(noisyCatName)
        cleanCat.write_catalog(cleanCatName)

        self.compareCatalogs(cleanCat, noisyCat,
                             PhotometricParameters().gain,
                             PhotometricParameters().readnoise)

        if os.path.exists(noisyCatName):
            os.unlink(noisyCatName)
        if os.path.exists(cleanCatName):
            os.unlink(cleanCatName)
예제 #2
0
    def testFakeSeds(self):
        """
        Test GalSim catalog with alternate Seds
        """
        catName = os.path.join(self.scratch_dir, 'testFakeSedCat.sav')
        m5 = [22.0, 23.0, 25.0]
        seeing = [0.6, 0.5, 0.7]
        bandpassNames = ['x', 'y', 'z']
        obs_metadata = ObservationMetaData(
            pointingRA=self.obs_metadata.pointingRA,
            pointingDec=self.obs_metadata.pointingDec,
            rotSkyPos=self.obs_metadata.rotSkyPos,
            mjd=self.obs_metadata.mjd,
            bandpassName=bandpassNames,
            m5=m5,
            seeing=seeing)

        stars = testStarsDBObj(driver=self.driver, database=self.dbName)
        cat = testFakeSedCatalog(stars, obs_metadata=obs_metadata)
        cat.camera_wrapper = GalSimCameraWrapper(self.camera)
        cat.write_catalog(catName)
        bandpassDir = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                                   'testThroughputs')
        sedDir = os.path.join(getPackageDir('sims_catUtils'), 'tests',
                              'testSeds')
        self.catalogTester(catName=catName,
                           catalog=cat,
                           nameRoot='fakeSed',
                           bandpassDir=bandpassDir,
                           bandpassRoot='fakeTotal_',
                           sedDir=sedDir)

        if os.path.exists(catName):
            os.unlink(catName)
    def testCompoundFitsFiles_one_empty(self):
        """
        Test that GalSimInterpreter puts the right number of counts on images
        containing different types of objects in the case where one of the
        input catalogs is empty (really, this is testing that we can
        successfully copy the GalSimInterpreter and all of the supporting
        properties from an empty GalSimCatalog to another GalSimCatalog)
        """
        driver = 'sqlite'
        dbName1 = os.path.join(self.scratch_dir, 'galSimTestCompound1DB_one_empty.db')
        if os.path.exists(dbName1):
            os.unlink(dbName1)

        deltaRA = np.array([72.0/3600.0, 55.0/3600.0, 75.0/3600.0])
        deltaDec = np.array([0.0, 15.0/3600.0, -15.0/3600.0])
        obs_metadata1 = makePhoSimTestDB(filename=dbName1, size=1,
                                         deltaRA=deltaRA, deltaDec=deltaDec,
                                         bandpass=self.bandpassNameList,
                                         m5=self.m5, seeing=self.seeing)

        dbName2 = os.path.join(self.scratch_dir, 'galSimTestCompound2DB_one_empty.db')
        if os.path.exists(dbName2):
            os.unlink(dbName2)

        deltaRA = np.array([55.0/3600.0, 60.0/3600.0, 62.0/3600.0])
        deltaDec = np.array([-3.0/3600.0, 10.0/3600.0, 10.0/3600.0])
        obs_metadata2 = makePhoSimTestDB(filename=dbName2, size=1,
                                         deltaRA=deltaRA, deltaDec=deltaDec,
                                         bandpass=self.bandpassNameList,
                                         m5=self.m5, seeing=self.seeing)

        gals = testGalaxyBulgeDBObj(driver=driver, database=dbName1)

        # shift the obs_metadata so that the catalog will not contain
        # any objects
        ra0 = obs_metadata1.pointingRA
        dec0 = obs_metadata1.pointingDec
        obs_metadata1.pointingRA = ra0 + 20.0

        cat1 = testGalaxyCatalog(gals, obs_metadata=obs_metadata1)
        cat1.camera_wrapper = GalSimCameraWrapper(self.camera)
        catName = os.path.join(self.scratch_dir, 'compoundCatalog_one_empty.sav')
        cat1.write_catalog(catName)
        with open(catName, "r") as input_file:
            input_lines = input_file.readlines()
            self.assertEqual(len(input_lines), 1)  # just the header
        self.assertFalse(hasattr(cat1, 'bandpassDict'))

        stars = testStarsDBObj(driver=driver, database=dbName2)
        cat2 = testStarCatalog(stars, obs_metadata=obs_metadata2)
        cat2.copyGalSimInterpreter(cat1)
        cat2.write_catalog(catName, write_header=False, write_mode='a')
        self.catalogTester(catName=catName, catalog=cat2, nameRoot='compound_one_empty')

        if os.path.exists(dbName1):
            os.unlink(dbName1)
        if os.path.exists(dbName2):
            os.unlink(dbName2)
        if os.path.exists(catName):
            os.unlink(catName)
    def testHalfLightRadiusOfImageRandomWalk(self):
        """
        Test that GalSim is generating images of objects with the expected half light radius
        by generating images with one object on them and comparing the total flux in the image
        with the flux contained within the expected half light radius.  Raise an exception
        if the deviation is greater than 3-sigma.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT,
                                      prefix='testHalfLightRadiusOfImage-')
        catName = os.path.join(scratchDir, 'hlr_test_Catalog.dat')
        imageRoot = os.path.join(scratchDir, 'hlr_test_Image')
        dbFileName = os.path.join(scratchDir, 'hlr_test_InputCatalog.dat')

        detector = self.camera[0]
        detName = detector.getName()
        imageName = '%s_%s_u.fits' % (imageRoot, detName)

        obs = ObservationMetaData(pointingRA=75.0,
                                  pointingDec=-12.0,
                                  boundType='circle',
                                  boundLength=4.0,
                                  rotSkyPos=33.0,
                                  mjd=49250.0)

        hlrTestList = [1., 2., 4.]

        for hlr in hlrTestList:
            create_text_catalog(obs,
                                dbFileName,
                                np.array([3.0]),
                                np.array([1.0]),
                                hlr=[hlr])

            db = hlrFileDBObj(dbFileName, runtable='test')

            cat = hlrCatRandomWalk(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(self.camera)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            totalFlux, hlrFlux = self.get_flux_in_half_light_radius(
                imageName, hlr, detector, self.camera, obs)
            self.assertGreater(totalFlux,
                               1000.0)  # make sure the image is not blank

            # divide by gain because Poisson stats apply to photons
            sigmaFlux = np.sqrt(0.5 * totalFlux / cat.photParams.gain)
            self.assertLess(np.abs(hlrFlux - 0.5 * totalFlux), 4.0 * sigmaFlux)

            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
예제 #5
0
    def testFwhmOfImage(self):
        """
        Test that GalSim generates images with the expected Full Width at Half Maximum.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testFwhmOfImage-')
        catName = os.path.join(scratchDir, 'fwhm_test_Catalog.dat')
        imageRoot = os.path.join(scratchDir, 'fwhm_test_Image')
        dbFileName = os.path.join(scratchDir, 'fwhm_test_InputCatalog.dat')

        baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                               'cameraData')

        # instantiate a test camera with pixel_scale = 0.02 arcsec/pixel
        camera = ReturnCamera(baseDir)

        detector = camera[0]
        detName = detector.getName()
        imageName = '%s_%s_u.fits' % (imageRoot, detName)

        obs = ObservationMetaData(pointingRA=75.0,
                                  pointingDec=-12.0,
                                  boundType='circle',
                                  boundLength=4.0,
                                  rotSkyPos=33.0,
                                  mjd=49250.0)

        create_text_catalog(obs,
                            dbFileName,
                            np.array([3.0]),
                            np.array([1.0]),
                            mag_norm=[13.0])

        db = fwhmFileDBObj(dbFileName, runtable='test')

        for fwhm in (0.1, 0.14):

            cat = fwhmCat(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(camera)

            psf = SNRdocumentPSF(fwhm=fwhm, pixel_scale=0.02)
            cat.setPSF(psf)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            self.verify_fwhm(imageName, fwhm, 0.02)

            if os.path.exists(catName):
                os.unlink(catName)

            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(dbFileName):
            os.unlink(dbFileName)
        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
예제 #6
0
    def testMultipleImages(self):
        """
        Test that GalSimInterpreter puts the right number of counts on images of multiple objects
        """
        dbName = os.path.join(self.scratch_dir, 'galSimTestMultipleDB.db')
        driver = 'sqlite'

        if os.path.exists(dbName):
            os.unlink(dbName)

        deltaRA = np.array([72.0 / 3600.0, 55.0 / 3600.0, 75.0 / 3600.0])
        deltaDec = np.array([0.0, 15.0 / 3600.0, -15.0 / 3600.0])
        obs_metadata = makePhoSimTestDB(filename=dbName,
                                        size=1,
                                        deltaRA=deltaRA,
                                        deltaDec=deltaDec,
                                        bandpass=self.bandpassNameList,
                                        m5=self.m5,
                                        seeing=self.seeing)

        gals = testGalaxyBulgeDBObj(driver=driver, database=dbName)
        cat = testGalaxyCatalog(gals, obs_metadata=obs_metadata)
        cat.camera_wrapper = GalSimCameraWrapper(self.camera)
        catName = os.path.join(self.scratch_dir, 'multipleCatalog.sav')
        cat.write_catalog(catName)
        self.catalogTester(catName=catName, catalog=cat, nameRoot='multiple')
        if os.path.exists(catName):
            os.unlink(catName)

        stars = testStarsDBObj(driver=driver, database=dbName)
        cat = testStarCatalog(stars, obs_metadata=obs_metadata)
        cat.camera_wrapper = GalSimCameraWrapper(self.camera)
        catName = os.path.join(self.scratch_dir, 'multipleStarCatalog.sav')
        cat.write_catalog(catName)
        self.catalogTester(catName=catName,
                           catalog=cat,
                           nameRoot='multipleStars')
        if os.path.exists(catName):
            os.unlink(catName)

        if os.path.exists(dbName):
            os.unlink(dbName)
예제 #7
0
 def testAgns(self):
     """
     Test that GalSimInterpreter puts the right number of counts on images of AGN
     """
     catName = os.path.join(self.scratch_dir, 'testAgnCat.sav')
     agn = testGalaxyAgnDBObj(driver=self.driver, database=self.dbName)
     cat = testAgnCatalog(agn, obs_metadata=self.obs_metadata)
     cat.camera_wrapper = GalSimCameraWrapper(self.camera)
     cat.write_catalog(catName)
     self.catalogTester(catName=catName, catalog=cat, nameRoot='agn')
     if os.path.exists(catName):
         os.unlink(catName)
예제 #8
0
    def testCompoundFitsFiles(self):
        """
        Test that GalSimInterpreter puts the right number of counts on images
        containing different types of objects
        """
        driver = 'sqlite'
        dbName1 = os.path.join(self.scratch_dir, 'galSimTestCompound1DB.db')
        if os.path.exists(dbName1):
            os.unlink(dbName1)

        deltaRA = np.array([72.0 / 3600.0, 55.0 / 3600.0, 75.0 / 3600.0])
        deltaDec = np.array([0.0, 15.0 / 3600.0, -15.0 / 3600.0])
        obs_metadata1 = makePhoSimTestDB(filename=dbName1,
                                         size=1,
                                         deltaRA=deltaRA,
                                         deltaDec=deltaDec,
                                         bandpass=self.bandpassNameList,
                                         m5=self.m5,
                                         seeing=self.seeing)

        dbName2 = os.path.join(self.scratch_dir, 'galSimTestCompound2DB.db')
        if os.path.exists(dbName2):
            os.unlink(dbName2)

        deltaRA = np.array([55.0 / 3600.0, 60.0 / 3600.0, 62.0 / 3600.0])
        deltaDec = np.array([-3.0 / 3600.0, 10.0 / 3600.0, 10.0 / 3600.0])
        obs_metadata2 = makePhoSimTestDB(filename=dbName2,
                                         size=1,
                                         deltaRA=deltaRA,
                                         deltaDec=deltaDec,
                                         bandpass=self.bandpassNameList,
                                         m5=self.m5,
                                         seeing=self.seeing)

        gals = testGalaxyBulgeDBObj(driver=driver, database=dbName1)
        cat1 = testGalaxyCatalog(gals, obs_metadata=obs_metadata1)
        cat1.camera_wrapper = GalSimCameraWrapper(self.camera)
        catName = os.path.join(self.scratch_dir, 'compoundCatalog.sav')
        cat1.write_catalog(catName)

        stars = testStarsDBObj(driver=driver, database=dbName2)
        cat2 = testStarCatalog(stars, obs_metadata=obs_metadata2)
        cat2.copyGalSimInterpreter(cat1)
        cat2.write_catalog(catName, write_header=False, write_mode='a')
        self.catalogTester(catName=catName, catalog=cat2, nameRoot='compound')

        if os.path.exists(dbName1):
            os.unlink(dbName1)
        if os.path.exists(dbName2):
            os.unlink(dbName2)
        if os.path.exists(catName):
            os.unlink(catName)
예제 #9
0
 def testBackground(self):
     """
     Test that GalSimInterpreter puts the right number of counts on images of Galaxy bulges with
     a sky background
     """
     catName = os.path.join(self.scratch_dir, 'testBackgroundCat.sav')
     gals = testGalaxyBulgeDBObj(driver=self.driver, database=self.dbName)
     cat = backgroundCatalog(gals, obs_metadata=self.obs_metadata)
     cat.camera_wrapper = GalSimCameraWrapper(self.camera)
     cat.write_catalog(catName)
     self.catalogTester(catName=catName, catalog=cat, nameRoot='background')
     if os.path.exists(catName):
         os.unlink(catName)
예제 #10
0
    def testContainsRaDec(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)

        raList, decList = _raDecFromPixelCoords(xPixList,
                                                yPixList,
                                                nameList,
                                                camera=self.camera,
                                                obs_metadata=self.obs,
                                                epoch=self.epoch)

        testAnswer = gsdet.containsRaDec(raList, decList)

        for c, t in zip(correctAnswer, testAnswer):
            self.assertIs(c, t)
예제 #11
0
    def testKolmogorovGaussianPSF(self):
        scratchDir = tempfile.mkdtemp(prefix='testKolmogorovGaussianPSF',
                                      dir=ROOT)
        catName = os.path.join(scratchDir,
                               'kolmogorov_gaussian_test_Catalog.dat')
        imageRoot = os.path.join(scratchDir, 'kolmogorov_gaussian_test_Image')
        dbFileName = os.path.join(scratchDir,
                                  'kolmogorov_gaussian_test_InputCatalog.dat')

        baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                               'cameraData')

        # instantiate a test camera with pixel_scale = 0.02 arcsec/pixel
        camera = ReturnCamera(baseDir)

        detector = camera[0]
        detName = detector.getName()
        imageName = '%s_%s_u.fits' % (imageRoot, detName)

        obs = ObservationMetaData(pointingRA=75.0,
                                  pointingDec=-12.0,
                                  boundType='circle',
                                  boundLength=4.0,
                                  rotSkyPos=33.0,
                                  mjd=49250.0)

        create_text_catalog(obs,
                            dbFileName,
                            np.array([3.0]),
                            np.array([1.0]),
                            mag_norm=[13.0])

        db = fwhmFileDBObj(dbFileName, runtable='test')

        cat = fwhmCat(db, obs_metadata=obs)
        cat.camera_wrapper = GalSimCameraWrapper(camera)

        psf = Kolmogorov_and_Gaussian_PSF(rawSeeing=0.7,
                                          airmass=1.05,
                                          band='g')
        cat.setPSF(psf)

        cat.write_catalog(catName)
        cat.write_images(nameRoot=imageRoot)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
예제 #12
0
    def setUpClass(cls):

        if _USE_LSST_CAMERA:
            cls.camera_wrapper = LSSTCameraWrapper()
            cls.detector = cls.camera_wrapper.camera['R:1,1 S:2,2']
        else:
            baseDir = os.path.join(getPackageDir('sims_GalSimInterface'),
                                   'tests', 'cameraData')
            cls.camera_wrapper = GalSimCameraWrapper(ReturnCamera(baseDir))
            cls.detector = cls.camera_wrapper.camera[0]

        cls.obs = ObservationMetaData(pointingRA=25.0,
                                      pointingDec=-10.0,
                                      boundType='circle',
                                      boundLength=1.0,
                                      mjd=49250.0,
                                      rotSkyPos=0.0)
        cls.epoch = 2000.0
예제 #13
0
 def testFitsHeaderKeywords(self):
     """
     Test that the FITS header keywords with the observing information
     are set correctly.
     """
     photParams = PhotometricParameters()
     gsdet = GalSimDetector(self.camera[0].getName(),
                            GalSimCameraWrapper(self.camera),
                            self.obs,
                            self.epoch,
                            photParams=photParams)
     self.assertEqual(gsdet.wcs.fitsHeader.getScalar('MJD-OBS'),
                      self.obs.mjd.TAI)
     self.assertEqual(gsdet.wcs.fitsHeader.getScalar('EXPTIME'),
                      photParams.nexp * photParams.exptime)
     self.assertEqual(gsdet.wcs.fitsHeader.getScalar('RATEL'),
                      self.obs.pointingRA)
     self.assertEqual(gsdet.wcs.fitsHeader.getScalar('DECTEL'),
                      self.obs.pointingDec)
     self.assertEqual(gsdet.wcs.fitsHeader.getScalar('ROTANGLE'),
                      self.obs.rotSkyPos)
예제 #14
0
    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)

            for hlr in hlrTestList:
            create_text_catalog(obs, dbFileName, np.array([3.0]), np.array([1.0]),
                                hlr=[hlr])

            db = hlrFileDBObj(dbFileName, runtable='test')

            cat = hlrCatSersic(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(self.camera)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            totalFlux, hlrFlux = self.get_flux_in_half_light_radius(imageName, hlr, detector, self.camera, obs)
            self.assertGreater(totalFlux, 1000.0)  # make sure the image is not blank

            # divide by gain because Poisson stats apply to photons
            sigmaFlux = np.sqrt(0.5*totalFlux/cat.photParams.gain)
            self.assertLess(np.abs(hlrFlux-0.5*totalFlux), 4.0*sigmaFlux)

            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
            
    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_wrapper = otherCatalog.camera_wrapper
        self.photParams = otherCatalog.photParams
        self.PSF = otherCatalog.PSF
        self.noise_and_background = otherCatalog.noise_and_background
        if otherCatalog.hasBeenInitialized:
            self.bandpassDict = otherCatalog.bandpassDict
            self.galSimInterpreter = otherCatalog.galSimInterpreter

    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 not isinstance(self.camera_wrapper, GalSimCameraWrapper):
            raise RuntimeError("GalSimCatalog.camera_wrapper must be an instantiation of "
                               "GalSimCameraWrapper or one of its daughter classes\n"
                               "It is actually of type %s" % str(type(self.camera_wrapper)))

        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_wrapper.camera:
                if dd.getType() == DetectorType.WAVEFRONT or dd.getType() == DetectorType.GUIDER:
                    # This package does not yet handle the 90-degree rotation
                    # in WCS that occurs for wavefront or guide sensors
                    continue

                if self.allowed_chips is None or dd.getName() in self.allowed_chips:
                    detectors.append(make_galsim_detector(self.camera_wrapper, dd.getName(),
                                                          self.photParams, self.obs_metadata,
                                                          epoch=self.db_obj.epoch))

            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
예제 #15
0
    def testPositionAngle(self):
        """
        Test that GalSim is generating images with the correct position angle
        by creating a FITS image with one extended source in it.  Measure
        the angle between the semi-major axis of the source and the north
        axis of the image.  Throw an exception if that angle differs
        from the expected position angle by more than 2 degrees.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testPositionAngle-')
        catName = os.path.join(scratchDir, 'pa_test_Catalog.dat')
        imageRoot = os.path.join(scratchDir, 'pa_test_Image')
        dbFileName = os.path.join(scratchDir, 'pa_test_InputCatalog.dat')

        baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                               'cameraData')
        camera = ReturnCamera(baseDir)
        detector = camera[0]
        detName = detector.getName()

        rng = np.random.RandomState(42)
        paList = rng.random_sample(7) * 360.0
        rotSkyPosList = rng.random_sample(77) * 360.0

        for pa, rotSkyPos in zip(paList, rotSkyPosList):

            imageName = '%s_%s_u.fits' % (imageRoot, detName)

            obs = ObservationMetaData(pointingRA=75.0,
                                      pointingDec=-12.0,
                                      boundType='circle',
                                      boundLength=4.0,
                                      rotSkyPos=rotSkyPos,
                                      mjd=49250.0)

            create_text_catalog(obs,
                                dbFileName,
                                rng.random_sample(1) * 20.0 - 10.0,
                                rng.random_sample(1) * 20.0 - 10.0,
                                pa=[pa],
                                mag_norm=[17.0])

            db = paFileDBObj(dbFileName, runtable='test')

            cat = paCat(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(camera)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            paTest = self.get_position_angle(imageName, camera, detector, obs,
                                             2000.0)

            # need to compare against all angles displaced by either 180 or 360 degrees
            # from expected answer
            deviation = np.abs(
                np.array([
                    pa - paTest, pa - 180.0 - paTest, pa + 180.0 - paTest,
                    pa - 360.0 - paTest, pa + 360.0 - paTest
                ])).min()

            self.assertLess(deviation, 3.0)

            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
    def testGalSimPhoSimCat(self):
        """
        Run a GalSimPhoSim catalog on some data. Then, generate an ordinary PhoSim catalog using
        the same data.  Verify that the two resulting PhoSim catalogs are identical.
        """

        galsim_cat_name = os.path.join(self.dataDir,
                                       'galSimPhoSim_galsim_cat.txt')
        phosim_cat_name = os.path.join(self.dataDir,
                                       'galSimPhoSim_phosim_cat.txt')
        galsim_image_root = os.path.join(self.dataDir, 'galSimPhoSim_images')
        db = fileDBObject(self.bulge_name,
                          dtype=self.dtype,
                          runtable='test_bulges',
                          idColKey='id')
        db.raColName = 'ra_deg'
        db.decColName = 'dec_deg'
        db.objectTypeId = 55

        gs_cat = GalSimPhoSimGalaxies(db, obs_metadata=self.obs)
        gs_cat.camera_wrapper = GalSimCameraWrapper(self.camera)
        gs_cat.bandpassNames = self.obs.bandpass
        gs_cat.PSF = SNRdocumentPSF()
        gs_cat.phoSimHeaderMap = {}
        gs_cat.write_catalog(galsim_cat_name)

        gs_cat_0 = gs_cat

        ps_cat = PhoSimCatalogSersic2D(db, obs_metadata=self.obs)
        ps_cat.phoSimHeaderMap = {}
        ps_cat.write_catalog(phosim_cat_name)

        db = fileDBObject(self.disk_name,
                          dtype=self.dtype,
                          runtable='test_disks',
                          idColKey='id')
        db.raColName = 'ra_deg'
        db.decColName = 'dec_deg'
        db.objectTypeId = 155

        gs_cat = GalSimPhoSimGalaxies(db, obs_metadata=self.obs)
        gs_cat.bandpassNames = self.obs.bandpass
        gs_cat.copyGalSimInterpreter(gs_cat_0)
        gs_cat.write_catalog(galsim_cat_name,
                             write_header=False,
                             write_mode='a')

        gs_cat_0 = gs_cat

        ps_cat = PhoSimCatalogSersic2D(db, obs_metadata=self.obs)
        ps_cat.write_catalog(phosim_cat_name,
                             write_header=False,
                             write_mode='a')

        db = fileDBObject(self.agn_name,
                          dtype=self.dtype,
                          runtable='test_agn',
                          idColKey='id')
        db.raColName = 'ra_deg'
        db.decColName = 'dec_deg'
        db.objectTypeId = 255

        gs_cat = GalSimPhoSimAgn(db, obs_metadata=self.obs)
        gs_cat.bandpassNames = self.obs.bandpass
        gs_cat.copyGalSimInterpreter(gs_cat_0)
        gs_cat.write_catalog(galsim_cat_name,
                             write_header=False,
                             write_mode='a')

        gs_cat_0 = gs_cat

        ps_cat = PhoSimCatalogZPoint(db, obs_metadata=self.obs)
        ps_cat.write_catalog(phosim_cat_name,
                             write_header=False,
                             write_mode='a')

        db = fileDBObject(self.star_name,
                          dtype=self.dtype,
                          runtable='test_agn',
                          idColKey='id')
        db.raColName = 'ra_deg'
        db.decColName = 'dec_deg'
        db.objectTypeId = 255

        gs_cat = GalSimPhoSimStars(db, obs_metadata=self.obs)
        gs_cat.bandpassNames = self.obs.bandpass
        gs_cat.copyGalSimInterpreter(gs_cat_0)
        gs_cat.write_catalog(galsim_cat_name,
                             write_header=False,
                             write_mode='a')

        ps_cat = PhoSimCatalogPoint(db, obs_metadata=self.obs)
        ps_cat.write_catalog(phosim_cat_name,
                             write_header=False,
                             write_mode='a')

        written_files = gs_cat.write_images(nameRoot=galsim_image_root)
        self.assertGreater(len(written_files), 0)
        for name in written_files:
            os.unlink(name)

        with open(galsim_cat_name, 'r') as galsim_input:
            with open(phosim_cat_name, 'r') as phosim_input:
                galsim_lines = galsim_input.readlines()
                phosim_lines = phosim_input.readlines()
                self.assertEqual(len(galsim_lines), len(phosim_lines))
                self.assertEqual(len(galsim_lines), 4 * self.n_objects + 7)
                for line in galsim_lines:
                    self.assertIn(line, phosim_lines)
                for line in phosim_lines:
                    self.assertIn(line, galsim_lines)

        if os.path.exists(galsim_cat_name):
            os.unlink(galsim_cat_name)

        if os.path.exists(phosim_cat_name):
            os.unlink(phosim_cat_name)
예제 #17
0
    def testObjectPlacement(self):
        """
        Test that GalSim places objects on the correct pixel by drawing
        images, reading them in, and then comparing the flux contained in
        circles of 2 fwhm radii about the object's expected positions with
        the actual expected flux of the objects.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testObjectPlacement-')
        catName = os.path.join(scratchDir, 'placementCatalog.dat')
        imageRoot = os.path.join(scratchDir, 'placementImage')
        dbFileName = os.path.join(scratchDir, 'placementInputCatalog.dat')

        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
        camera = ReturnCamera(cameraDir)
        detector = camera[0]
        imageName = '%s_%s_u.fits' % (imageRoot, detector.getName())

        controlSed = Sed()
        controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'),
                                                'flatSED', 'sed_flat.txt.gz'))

        uBandpass = Bandpass()
        uBandpass.readThroughput(os.path.join(getPackageDir('throughputs'),
                                              'baseline', 'total_u.dat'))

        controlBandpass = Bandpass()
        controlBandpass.imsimBandpass()

        ff = controlSed.calcFluxNorm(self.magNorm, uBandpass)
        controlSed.multiplyFluxNorm(ff)
        a_int, b_int = controlSed.setupCCMab()
        controlSed.addCCMDust(a_int, b_int, A_v=0.1, R_v=3.1)

        nSamples = 3
        rng = np.random.RandomState(42)
        pointingRaList = rng.random_sample(nSamples)*360.0
        pointingDecList = rng.random_sample(nSamples)*180.0 - 90.0
        rotSkyPosList = rng.random_sample(nSamples)*360.0
        fwhmList = rng.random_sample(nSamples)*1.0 + 0.3

        actualCounts = None

        for pointingRA, pointingDec, rotSkyPos, fwhm in \
        zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList):

            obs = ObservationMetaData(pointingRA=pointingRA,
                                      pointingDec=pointingDec,
                                      boundType='circle',
                                      boundLength=4.0,
                                      mjd=49250.0,
                                      rotSkyPos=rotSkyPos)

            xDisplacementList = rng.random_sample(nSamples)*60.0-30.0
            yDisplacementList = rng.random_sample(nSamples)*60.0-30.0
            create_text_catalog(obs, dbFileName, xDisplacementList, yDisplacementList,
                                mag_norm=[self.magNorm]*len(xDisplacementList))
            db = placementFileDBObj(dbFileName, runtable='test')
            cat = placementCatalog(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(camera)
            if actualCounts is None:
                actualCounts = controlSed.calcADU(uBandpass, cat.photParams)

            psf = SNRdocumentPSF(fwhm=fwhm)
            cat.setPSF(psf)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            objRaList = []
            objDecList = []
            with open(catName, 'r') as inFile:
                for line in inFile:
                    if line[0] != '#':
                        words = line.split(';')
                        objRaList.append(np.radians(np.float(words[2])))
                        objDecList.append(np.radians(np.float(words[3])))

            objRaList = np.array(objRaList)
            objDecList = np.array(objDecList)

            self.assertGreater(len(objRaList), 0)  # make sure we aren't testing
                                                   # an empty catalog/image

            self.check_placement(imageName, objRaList, objDecList,
                                 [fwhm]*len(objRaList),
                                 np.array([actualCounts]*len(objRaList)),
                                 cat.photParams.gain, detector, camera, obs, epoch=2000.0)

            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
    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
예제 #19
0
    def testOutputWcsOfImage(self):
        """
        Test that, when GalSim generates an image, in encodes the WCS in a
        way afw can read.  This is done by creating an image,then reading
        it back in, getting its WCS, and comparing the pixel-to-sky conversion
        both for the read WCS and the original afw.cameraGeom.detector.
        Raise an exception if the median difference between the two is
        greater than 0.01 arcseconds.
        """
        scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testOutputWcsOfImage-')
        catName = os.path.join(scratchDir, 'outputWcs_test_Catalog.dat')
        imageRoot = os.path.join(scratchDir, 'outputWcs_test_Image')
        dbFileName = os.path.join(scratchDir,
                                  'outputWcs_test_InputCatalog.dat')

        baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                               'cameraData')
        camera = ReturnCamera(baseDir)

        detector = camera[0]
        detName = detector.getName()
        imageName = '%s_%s_u.fits' % (imageRoot, detName)

        nSamples = 3
        rng = np.random.RandomState(42)
        pointingRaList = rng.random_sample(nSamples) * 360.0
        pointingDecList = rng.random_sample(nSamples) * 180.0 - 90.0
        rotSkyPosList = rng.random_sample(nSamples) * 360.0

        for raPointing, decPointing, rotSkyPos in \
            zip(pointingRaList, pointingDecList, rotSkyPosList):

            obs = ObservationMetaData(pointingRA=raPointing,
                                      pointingDec=decPointing,
                                      boundType='circle',
                                      boundLength=4.0,
                                      rotSkyPos=rotSkyPos,
                                      mjd=49250.0)

            fwhm = 0.7
            create_text_catalog(obs, dbFileName, np.array([3.0]),
                                np.array([1.0]))

            db = outputWcsFileDBObj(dbFileName, runtable='test')

            cat = outputWcsCat(db, obs_metadata=obs)
            cat.camera_wrapper = GalSimCameraWrapper(camera)

            psf = SNRdocumentPSF(fwhm=fwhm)
            cat.setPSF(psf)

            cat.write_catalog(catName)
            cat.write_images(nameRoot=imageRoot)

            # 20 March 2017
            # the 'try' block is how it worked in SWIG;
            # the 'except' block is how it works in pybind11
            try:
                exposure = afwImage.ExposureD_readFits(imageName)
            except AttributeError:
                exposure = afwImage.ExposureD.readFits(imageName)

            wcs = exposure.getWcs()

            xxTestList = []
            yyTestList = []

            raImage = []
            decImage = []

            for xx in np.arange(0.0, 4001.0, 100.0):
                for yy in np.arange(0.0, 4001.0, 100.0):
                    xxTestList.append(xx)
                    yyTestList.append(yy)

                    pt = afwGeom.Point2D(xx, yy)
                    skyPt = wcs.pixelToSky(pt).getPosition()
                    raImage.append(skyPt.getX())
                    decImage.append(skyPt.getY())

            xxTestList = np.array(xxTestList)
            yyTestList = np.array(yyTestList)

            raImage = np.radians(np.array(raImage))
            decImage = np.radians(np.array(decImage))

            raControl, \
            decControl = _raDecFromPixelCoords(xxTestList, yyTestList,
                                               [detector.getName()]*len(xxTestList),
                                               camera=camera, obs_metadata=obs,
                                               epoch=2000.0)

            errorList = arcsecFromRadians(
                haversine(raControl, decControl, raImage, decImage))

            medianError = np.median(errorList)
            msg = 'medianError was %e' % medianError
            self.assertLess(medianError, 0.01, msg=msg)

            if os.path.exists(catName):
                os.unlink(catName)
            if os.path.exists(dbFileName):
                os.unlink(dbFileName)
            if os.path.exists(imageName):
                os.unlink(imageName)

        if os.path.exists(scratchDir):
            shutil.rmtree(scratchDir)
예제 #20
0
    def testPlacement(self):
        """
        Test that GalSimInterpreter puts objects on the right detectors.

        Do so by creating a catalog of 3 closely-packed stars.  Draw test FITS
        images of them using the GalSim Catalog infrastructure.  Draw control FITS
        images of the detectors in the camera, paranoidly including every star
        in every control image (GalSim contains code such that it will not
        actually add flux to an image in cases where we try to include a
        star that does not actually fall on a detector).  Compare that

        a) the fluxes of the test and control images agree within some tolerance

        b) the fluxes of control images that have no corresponding test image
        (i.e. detectors on which no star actually fell) are effectively zero
        """

        # generate the database
        np_rng = np.random.RandomState(32)
        gs_rng = galsim.UniformDeviate(112)
        catSize = 3
        dbName = 'galSimPlacementTestDB.db'
        driver = 'sqlite'
        if os.path.exists(dbName):
            os.unlink(dbName)

        deltaRA = (-40.0 + np_rng.random_sample(catSize) * (120.0)) / 3600.0
        deltaDec = (-20.0 + np_rng.random_sample(catSize) * (80.0)) / 3600.0
        obs_metadata = makePhoSimTestDB(filename=dbName,
                                        deltaRA=deltaRA,
                                        deltaDec=deltaDec,
                                        bandpass=self.bandpassNameList,
                                        m5=self.m5,
                                        seeing=self.seeing)

        stars = testStarsDBObj(driver=driver, database=dbName)

        # create the catalog
        cat = testStarCatalog(stars, obs_metadata=obs_metadata)
        cat.camera_wrapper = GalSimCameraWrapper(self.camera)
        results = cat.iter_catalog()
        firstLine = True

        # iterate over the catalog, giving every star a chance to
        # illumine every detector
        controlImages = {}
        for i, line in enumerate(results):
            xPupil = line[5]
            yPupil = line[6]

            if firstLine:
                sedList = list(cat._calculateGalSimSeds())
                for detector in cat.galSimInterpreter.detectors:
                    for bandpass in cat.galSimInterpreter.bandpassDict:
                        controlImages['placementControl_' +
                                      cat.galSimInterpreter._getFileName(detector=detector,
                                                                         bandpassName=bandpass)] = \
                            cat.galSimInterpreter.blankImage(detector=detector)

                firstLine = False

            for bp in cat.galSimInterpreter.bandpassDict:
                bandpass = cat.galSimInterpreter.bandpassDict[bp]
                adu = sedList[i].calcADU(bandpass, cat.photParams)
                for detector in cat.galSimInterpreter.detectors:
                    centeredObj = cat.galSimInterpreter.PSF.applyPSF(
                        xPupil=xPupil, yPupil=yPupil)

                    xPix, yPix = pixelCoordsFromPupilCoords(
                        radiansFromArcsec(xPupil),
                        radiansFromArcsec(yPupil),
                        chipName=detector.name,
                        camera=detector._cameraWrapper.camera)

                    dx = xPix - detector.xCenterPix
                    dy = yPix - detector.yCenterPix
                    obj = centeredObj.withFlux(adu * detector.photParams.gain)
                    localImage = cat.galSimInterpreter.blankImage(
                        detector=detector)
                    localImage = obj.drawImage(wcs=detector.wcs,
                                               method='phot',
                                               gain=detector.photParams.gain,
                                               image=localImage,
                                               offset=galsim.PositionD(dx, dy),
                                               rng=gs_rng)

                    controlImages[
                        'placementControl_' +
                        cat.galSimInterpreter._getFileName(
                            detector=detector, bandpassName=bp)] += localImage

        self.assertGreater(len(controlImages), 0)

        for name in controlImages:
            controlImages[name].write(file_name=name)

        # write the test images using the catalog infrastructure
        testNames = cat.write_images(nameRoot='placementTest')

        # make sure that every test image has a corresponding control image
        for testName in testNames:
            controlName = testName.replace('Test', 'Control')
            msg = '%s has no counterpart ' % testName
            self.assertIn(controlName, controlImages, msg=msg)

        # make sure that the test and control images agree to some tolerance
        ignored = 0
        zeroFlux = 0
        valid = 0
        for controlName in controlImages:
            controlImage = afwImage.ImageF(controlName)
            controlFlux = controlImage.getArray().sum()

            testName = controlName.replace('Control', 'Test')
            if testName in testNames:
                testImage = afwImage.ImageF(testName)
                testFlux = testImage.getArray().sum()
                if controlFlux > 1000.0:
                    countSigma = np.sqrt(controlFlux / cat.photParams.gain)
                    msg = '%s: controlFlux = %e, testFlux = %e, sigma %e' \
                          % (controlName, controlFlux, testFlux, countSigma)

                    # the randomness of photon shooting means that faint images won't agree
                    self.assertLess(np.abs(controlFlux - testFlux),
                                    4.0 * countSigma,
                                    msg=msg)
                    valid += 1
                else:
                    ignored += 1
            else:
                # make sure that controlImages that have no corresponding test image really do
                # have zero flux (because no star fell on them)
                zeroFlux += 1
                msg = '%s has flux %e but was not written by catalog' % (
                    controlName, controlFlux)
                self.assertLess(controlFlux, 1.0, msg=msg)

        self.assertGreater(valid, 0)
        self.assertLess(ignored, len(testNames) / 2)
        self.assertGreater(zeroFlux, 0)

        for testName in testNames:
            if os.path.exists(testName):
                os.unlink(testName)

        for controlName in controlImages:
            if os.path.exists(controlName):
                os.unlink(controlName)

        if os.path.exists(dbName):
            os.unlink(dbName)
예제 #21
0
    def testFitsHeader(self):
        """
        Create a test image with the LSST camera and with the
        cartoon camera.  Verify that the image created with the LSST
        camera has the DM-required cards in its FITS header while the
        image created with the cartoon camera does not
        """

        cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'),
                                 'tests', 'cameraData')
        cartoonCamera = ReturnCamera(cameraDir)

        outputDir = tempfile.mkdtemp(dir=ROOT, prefix='testFitsHeader-')

        lsst_cat_name = os.path.join(outputDir, 'fits_test_lsst_cat.txt')
        lsst_cat_root = os.path.join(outputDir, 'fits_test_lsst_image')

        cartoon_cat_name = os.path.join(outputDir, 'fits_test_cartoon_cat.txt')
        cartoon_cat_root = os.path.join(outputDir, 'fits_test_cartoon_image')

        obs = ObservationMetaData(pointingRA=32.0,
                                  pointingDec=22.0,
                                  boundLength=0.1,
                                  boundType='circle',
                                  mjd=58000.0,
                                  rotSkyPos=14.0,
                                  bandpassName='u')

        obs.OpsimMetaData = {'obshistID': 112}

        dbFileName = os.path.join(outputDir, 'fits_test_db.dat')
        create_text_catalog(obs, dbFileName, np.array([30.0]),
                            np.array([30.0]), [22.0])
        db = fitsHeaderFileDBObj(dbFileName, runtable='test')

        # first test the lsst camera
        lsstCat = fitsHeaderCatalog(db, obs_metadata=obs)
        lsstCat.camera_wrapper = LSSTCameraWrapper()
        lsstCat.PSF = SNRdocumentPSF()
        lsstCat.write_catalog(lsst_cat_name)
        lsstCat.write_images(nameRoot=lsst_cat_root)

        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if lsst_cat_root in true_name:
                ct += 1
                with fits.open(true_name) as fitsTest:
                    header = fitsTest[0].header
                    self.assertIn('CHIPID', header)
                    self.assertIn('OBSID', header)
                    self.assertIn('OUTFILE', header)
                    self.assertEqual(header['OBSID'], 112)
                    self.assertEqual(header['CHIPID'], 'R22_S11')
                    self.assertEqual(header['OUTFILE'],
                                     'lsst_e_112_f0_R22_S11_E000')
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(lsst_cat_name):
            os.unlink(lsst_cat_name)

        # now test with the cartoon camera
        cartoonCat = fitsHeaderCatalog(db, obs_metadata=obs)
        cartoonCat.camera_wrapper = GalSimCameraWrapper(cartoonCamera)
        cartoonCat.PSF = SNRdocumentPSF()
        cartoonCat.write_catalog(cartoon_cat_name)
        cartoonCat.write_images(nameRoot=cartoon_cat_root)
        list_of_files = os.listdir(outputDir)
        ct = 0
        for file_name in list_of_files:
            true_name = os.path.join(outputDir, file_name)
            if cartoon_cat_root in true_name:
                ct += 1
                with fits.open(true_name) as fitsTest:
                    header = fitsTest[0].header
                    self.assertNotIn('CHIPID', header)
                    self.assertNotIn('OBSID', header)
                    self.assertNotIn('OUTFILE', header)
                os.unlink(true_name)

        self.assertGreater(ct, 0)
        if os.path.exists(cartoon_cat_name):
            os.unlink(cartoon_cat_name)

        if os.path.exists(dbFileName):
            os.unlink(dbFileName)

        if os.path.exists(outputDir):
            shutil.rmtree(outputDir)
예제 #22
0
    def testCamera(self):
        """
        Test that GalSimCatalogs respect the allowed_chips variable by
        generating a catalog with one object on each chip.

        Generate images from a control catalog that allows all chips.
        Verify that each image contains the expected flux.

        Generate images from a test catalog that only allows two chips.
        Verify that only the two expected images exist and that each
        contains only the expected flux.
        """

        controlCatalog = allowedChipsCatalog(self.db, obs_metadata=self.obs)
        testCatalog = allowedChipsCatalog(self.db, obs_metadata=self.obs)
        psf = SNRdocumentPSF()
        controlCatalog.setPSF(psf)
        testCatalog.setPSF(psf)
        controlCatalog.camera_wrapper = GalSimCameraWrapper(self.camera)
        testCatalog.camera_wrapper = GalSimCameraWrapper(self.camera)

        test_root = os.path.join(self.scratchDir, 'allowed_chip_test_image')
        control_root = os.path.join(self.scratchDir,
                                    'allowed_chip_control_image')

        name_list = []
        for dd in self.camera:
            if dd.getType() == WAVEFRONT or dd.getType() == GUIDER:
                continue
            name = dd.getName()
            name_list.append(name)
            stripped_name = name.replace(':', '')
            stripped_name = stripped_name.replace(',', '')
            stripped_name = stripped_name.replace(' ', '_')

            test_image_name = os.path.join(
                self.scratchDir, test_root + '_' + stripped_name + '_u.fits')
            control_image_name = os.path.join(
                self.scratchDir,
                control_root + '_' + stripped_name + '_u.fits')

            # remove any images that were generated the last time this test
            # was run
            if os.path.exists(test_image_name):
                os.unlink(test_image_name)
            if os.path.exists(control_image_name):
                os.unlink(control_image_name)

        # only allow two chips in the test catalog
        allowed_chips = [name_list[3], name_list[4]]

        testCatalog.allowed_chips = allowed_chips

        test_cat_name = os.path.join(self.scratchDir,
                                     'allowed_chips_test_cat.txt')
        control_cat_name = os.path.join(self.scratchDir,
                                        'allowed_chips_control_cat.txt')

        testCatalog.write_catalog(test_cat_name)
        controlCatalog.write_catalog(control_cat_name)

        testCatalog.write_images(nameRoot=test_root)
        controlCatalog.write_images(nameRoot=control_root)

        test_image_ct = 0

        for name in name_list:
            # Loop through each chip on the camera.
            # Verify that the control catalog generated an image for each chip.
            # Verify that the test catalog only generated images for the two
            # specified chips.
            # Verify that each image contains the expected amount of flux.

            stripped_name = name.replace(':', '')
            stripped_name = stripped_name.replace(',', '')
            stripped_name = stripped_name.replace(' ', '_')

            test_image_name = os.path.join(
                self.scratchDir, test_root + '_' + stripped_name + '_u.fits')
            control_image_name = os.path.join(
                self.scratchDir,
                control_root + '_' + stripped_name + '_u.fits')

            msg = '%s does not exist; it should' % control_image_name
            self.assertTrue(os.path.exists(control_image_name), msg=msg)
            im = afwImage.ImageF(control_image_name).getArray()
            msg = "\nimage contains %e counts\nshould contain %e\n\n" % (
                im.sum(), self.controlADU)
            self.assertLess(np.abs(im.sum() - self.controlADU),
                            3.0 * self.countSigma,
                            msg=msg)
            os.unlink(control_image_name)

            if name in allowed_chips:
                msg = '%s does not exist; it should' % test_image_name
                self.assertTrue(os.path.exists(test_image_name), msg=msg)
                im = afwImage.ImageF(test_image_name).getArray()
                self.assertLess(np.abs(im.sum() - self.controlADU),
                                3.0 * self.countSigma)
                os.unlink(test_image_name)
                test_image_ct += 1
            else:
                msg = '%s exists; it should not' % test_image_name
                self.assertFalse(os.path.exists(test_image_name), msg=msg)

        self.assertEqual(test_image_ct, len(allowed_chips))

        if os.path.exists(test_cat_name):
            os.unlink(test_cat_name)
        if os.path.exists(control_cat_name):
            os.unlink(control_cat_name)
예제 #23
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))