Beispiel #1
0
    def testHalfLightRadiusOfImage(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 = os.path.join(getPackageDir('sims_GalSimInterface'),
                                  'tests', 'scratchSpace')
        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')

        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)

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

        hlrTestList = [1.0, 2.0, 4.0]

        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 = hlrCat(db, obs_metadata=obs)
            cat.camera = camera

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

            totalFlux, hlrFlux = self.get_flux_in_half_light_radius(
                imageName, hlr, detector, 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)
Beispiel #2
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)
Beispiel #3
0
class fwhmCat(GalSimStars):
    camera = ReturnCamera(os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData'))
    bandpassNames = ['u']

    default_columns = GalSimStars.default_columns

    default_columns += [('sedFilename', 'sed_flat.txt', (str, 12)),
                        ('properMotionRa', 0.0, np.float),
                        ('properMotionDec', 0.0, np.float),
                        ('radialVelocity', 0.0, np.float),
                        ('parallax', 0.0, np.float)]
Beispiel #4
0
class hlrCat(GalSimGalaxies):
    camera = ReturnCamera(
        os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                     'cameraData'))
    bandpassNames = ['u']
    default_columns = [('sedFilename', 'sed_flat.txt', (str, 12)),
                       ('magNorm', 21.0, float), ('galacticAv', 0.1, float),
                       ('galacticRv', 3.1, float),
                       ('galSimType', 'sersic', (str, 11)),
                       ('internalAv', 0.1, float), ('internalRv', 3.1, float),
                       ('redshift', 0.0, float),
                       ('majorAxis', radiansFromArcsec(1.0), float),
                       ('minorAxis', radiansFromArcsec(1.0), float),
                       ('sindex', 4.0, float)]
    def setUpClass(cls):

        if _USE_LSST_CAMERA:
            cls.camera = LsstSimMapper().camera
            cls.detector = cls.camera['R:1,1 S:2,2']
        else:
            baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData')
            cls.camera = ReturnCamera(baseDir)
            cls.detector = cls.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
Beispiel #6
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)
Beispiel #7
0
    def setUp(self):
        baseDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                               'cameraData')

        self.camera = ReturnCamera(baseDir)

        ra = 145.0
        dec = -73.0
        self.epoch = 2000.0
        mjd = 49250.0
        rotSkyPos = 45.0
        self.obs = ObservationMetaData(pointingRA=ra,
                                       pointingDec=dec,
                                       boundType='circle',
                                       boundLength=1.0,
                                       mjd=mjd,
                                       rotSkyPos=rotSkyPos)
Beispiel #8
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)
Beispiel #9
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 = os.path.join(getPackageDir('sims_GalSimInterface'),
                                  'tests', 'scratchSpace')
        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 = 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)
Beispiel #10
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)
Beispiel #11
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)
 def setUpClass(cls):
     cls.camera = ReturnCamera(
         os.path.join(getPackageDir('sims_GalSimInterface'), 'tests',
                      'cameraData'))