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)
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)
def setUpClass(cls): cls.scratchDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2) cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED','sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
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)
def setUpClass(cls): cls.scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='allowedChipsTest-') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2, bandpassName='r') cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED', 'sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
def testFwhmOfImage(self): """ Test that GalSim generates images with the expected Full Width at Half Maximum. """ scratchDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') 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') 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, numpy.array([3.0]), \ numpy.array([1.0]), mag_norm=[13.0]) db = fwhmFileDBObj(dbFileName, runtable='test') for fwhm in (0.5, 1.3): cat = fwhmCat(db, obs_metadata=obs) cat.camera = camera psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.write_catalog(catName) cat.write_images(nameRoot=imageRoot) self.verify_fwhm(imageName, fwhm, detector, camera, obs) if os.path.exists(catName): os.unlink(catName) if os.path.exists(imageName): os.unlink(imageName) if os.path.exists(dbFileName): os.unlink(dbFileName)
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)
def testFwhmOfImage(self): """ Test that GalSim generates images with the expected Full Width at Half Maximum. """ scratchDir = os.path.join(getPackageDir("sims_GalSimInterface"), "tests", "scratchSpace") 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") camera = ReturnCamera(baseDir) detector = camera[0] detName = detector.getName() imageName = "%s_%s_u.fits" % (imageRoot, detName) obs = ObservationMetaData( unrefractedRA=75.0, unrefractedDec=-12.0, boundType="circle", boundLength=4.0, rotSkyPos=33.0, mjd=49250.0 ) fwhmTestList = [0.5, 0.9, 1.3] for fwhm in fwhmTestList: create_text_catalog(obs, dbFileName, numpy.array([3.0]), numpy.array([1.0]), mag_norm=[14.0]) db = fwhmFileDBObj(dbFileName, runtable="test") cat = fwhmCat(db, obs_metadata=obs) cat.camera = camera psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.write_catalog(catName) cat.write_images(nameRoot=imageRoot) self.verify_fwhm(imageName, fwhm, detector, camera, obs) if os.path.exists(catName): os.unlink(catName) if os.path.exists(dbFileName): os.unlink(dbFileName) if os.path.exists(imageName): os.unlink(imageName)
def testObjectPlacement(self): """ Test that GalSim places objects on the correct pixel by drawing images containing single objects and no background, reading those images back in, and comparing the flux-averaged centroids of the images with the expected pixel positions of the input objects. """ scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='testLSSTObjectPlacement-') if os.path.exists(scratchDir): shutil.rmtree(scratchDir) os.mkdir(scratchDir) detector = lsst_camera()['R:0,3 S:2,2'] det_name = 'R03_S22' magNorm = 19.0 pixel_transformer = DMtoCameraPixelTransformer() for band in 'ugrizy': obs = self.obs_dict[band] catName = os.path.join(scratchDir, 'placementCatalog.dat') imageRoot = os.path.join(scratchDir, 'placementImage') dbFileName = os.path.join(scratchDir, 'placementInputCatalog.dat') imageName = '%s_%s_%s.fits' % (imageRoot, det_name, obs.bandpass) ra_c, dec_c = raDecFromPixelCoordsLSST(2000.0, 2000.0, detector.getName(), band=obs.bandpass, obs_metadata=obs) nSamples = 30 rng = np.random.RandomState(42) fwhm = 0.12 for iteration in range(nSamples): if os.path.exists(dbFileName): os.unlink(dbFileName) ra_obj = ra_c + rng.random_sample()*0.2 - 0.1 dec_obj = dec_c + rng.random_sample()*0.2 - 0.1 dmx_wrong, dmy_wrong = pixelCoordsFromRaDec(ra_obj, dec_obj, chipName=detector.getName(), obs_metadata=obs, camera=lsst_camera()) dmx_pix, dmy_pix = pixelCoordsFromRaDecLSST(ra_obj, dec_obj, chipName=detector.getName(), obs_metadata=obs, band=obs.bandpass) x_pix, y_pix = pixel_transformer.cameraPixFromDMPix(dmx_pix, dmy_pix, detector.getName()) x_pix_wrong, y_pix_wrong = pixel_transformer.cameraPixFromDMPix(dmx_wrong, dmy_wrong, detector.getName()) d_ra = 360.0*(ra_obj - obs.pointingRA) # in arcseconds d_dec = 360.0*(dec_obj - obs.pointingDec) create_text_catalog(obs, dbFileName, np.array([d_ra]), np.array([d_dec]), mag_norm=[magNorm]) db = LSSTPlacementFileDBObj(dbFileName, runtable='test') cat = LSSTPlacementCatalog(db, obs_metadata=obs) cat.camera_wrapper = LSSTCameraWrapper() psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.write_catalog(catName) cat.write_images(nameRoot=imageRoot) im = afwImage.ImageF(imageName).getArray() tot_flux = im.sum() self.assertGreater(tot_flux, 10.0) y_centroid = sum([ii*im[ii,:].sum() for ii in range(im.shape[0])])/tot_flux x_centroid = sum([ii*im[:,ii].sum() for ii in range(im.shape[1])])/tot_flux dd = np.sqrt((x_pix-x_centroid)**2 + (y_pix-y_centroid)**2) self.assertLess(dd, 0.5*fwhm) dd_wrong = np.sqrt((x_pix_wrong-x_centroid)**2 + (y_pix_wrong-y_centroid)**2) self.assertLess(dd, dd_wrong) 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 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 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 """ lsstCamera = LsstSimMapper().camera cameraDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'cameraData') cartoonCamera = ReturnCamera(cameraDir) outputDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') 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 = lsstCamera 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 fitsTest = fits.open(true_name) 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 = 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 fitsTest = fits.open(true_name) 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)
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)
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 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 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) 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)
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 = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') 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() numpy.random.seed(42) paList = numpy.random.random_sample(2)*360.0 rotSkyPosList = numpy.random.random_sample(2)*360.0 for pa in paList: for rotSkyPos in 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, numpy.random.random_sample(1)*20.0-10.0, numpy.random.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 = 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 = numpy.abs(numpy.array([ pa-paTest, pa-180.0-paTest, pa+180.0-paTest, pa-360.0-paTest, pa+360.0-paTest ])).min() self.assertLess(deviation, 2.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)
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 = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') 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 = 5 numpy.random.seed(42) pointingRaList = numpy.random.random_sample(nSamples)*360.0 pointingDecList = numpy.random.random_sample(nSamples)*180.0 - 90.0 rotSkyPosList = numpy.random.random_sample(nSamples)*360.0 fwhmList = numpy.random.random_sample(nSamples)*1.0 + 0.3 actualCounts = None for pointingRA, pointingDec, rotSkyPos, fwhm in \ zip(pointingRaList, pointingDecList, rotSkyPosList, fwhmList): obs = ObservationMetaData(unrefractedRA=pointingRA, unrefractedDec=pointingDec, boundType='circle', boundLength=4.0, mjd=49250.0, rotSkyPos=rotSkyPos) xDisplacementList = numpy.random.random_sample(nSamples)*60.0-30.0 yDisplacementList = numpy.random.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) if actualCounts is None: actualCounts = controlSed.calcADU(uBandpass, cat.photParams) psf = SNRdocumentPSF(fwhm=fwhm) cat.setPSF(psf) cat.camera = camera 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(numpy.radians(numpy.float(words[2]))) objDecList.append(numpy.radians(numpy.float(words[3]))) objRaList = numpy.array(objRaList) objDecList = numpy.array(objDecList) self.check_placement(imageName, objRaList, objDecList, [fwhm]*len(objRaList), numpy.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)