def testWriteFits(self): """Test writing FITS files""" tmpFile = "foo.fits" if self.fileForMetadata: imgU = afwImage.DecoratedImageF(self.fileForMetadata) else: print >> sys.stderr, "Warning: afwdata is not set up; not running the FITS metadata I/O tests" imgU = afwImage.DecoratedImageF() self.dimage1.writeFits(tmpFile, imgU.getMetadata()) # # Read it back # rimage = afwImage.DecoratedImageF(tmpFile) os.remove(tmpFile) self.assertEqual(self.dimage1.getImage().get(0, 0), rimage.getImage().get(0, 0)) # # Check that we wrote (and read) the metadata successfully if self.fileForMetadata: meta = self.trueMetadata for k in meta.keys(): self.assertEqual(rimage.getMetadata().getAsDouble(k), meta[k])
def testReadFits(self): """Test reading FITS files""" dataDir = eups.productDir("afwdata") if dataDir: dataDir = os.path.join(dataDir, "data") else: print >> sys.stderr, "Warning: afwdata is not set up; not running the FITS I/O tests" return hdus = {} fileName = os.path.join(dataDir, "small_MI.fits") hdus["img"] = 2 # an S16 fits HDU hdus["msk"] = 3 # an U8 fits HDU hdus["var"] = 4 # an F32 fits HDU imgU = afwImage.DecoratedImageU(fileName, hdus["img"]) # read as unsigned short imgF = afwImage.DecoratedImageF(fileName, hdus["img"]) # read as float self.assertEqual(imgU.getHeight(), 256) self.assertEqual(imgF.getImage().getWidth(), 256) self.assertEqual(imgU.getImage().get(0, 0), imgF.getImage().get(0, 0)) # # Check the metadata # meta = self.trueMetadata for k in meta.keys(): self.assertEqual(imgU.getMetadata().getAsDouble(k), meta[k]) self.assertEqual(imgF.getMetadata().getAsDouble(k), meta[k]) # # Read an F32 image # varU = afwImage.DecoratedImageF(fileName, hdus["var"]) # read as unsigned short varF = afwImage.DecoratedImageF(fileName, hdus["var"]) # read as float self.assertEqual(varU.getHeight(), 256) self.assertEqual(varF.getImage().getWidth(), 256) self.assertEqual(varU.getImage().get(0, 0), varF.getImage().get(0, 0)) # # Read a char image # maskImg = afwImage.DecoratedImageU(fileName, hdus["msk"]).getImage() # read a char file self.assertEqual(maskImg.getHeight(), 256) self.assertEqual(maskImg.getWidth(), 256) self.assertEqual(maskImg.get(0, 0), 1) # # Read a U16 image # tmpFile = "foo.fits" imgU.writeFits(tmpFile) try: imgU16 = afwImage.DecoratedImageF(tmpFile) # read as unsigned short except: os.remove(tmpFile) raise os.remove(tmpFile)
def testCopyConstructors(self): dimage = afwImage.DecoratedImageF(self.dimage1, True) # deep copy self.dimage1.getImage().set(0, 0, 1 + 2*self.val1) self.assertEqual(dimage.getImage().get(0, 0), self.val1) dimage = afwImage.DecoratedImageF(self.dimage1) # shallow copy self.dimage1.getImage().set(0, 0, 1 + 2*self.val1) self.assertNotEqual(dimage.getImage().get(0, 0), self.val1)
def testCopyConstructors(self): dimage = afwImage.DecoratedImageF(self.dimage1, True) # deep copy self.dimage1.image[0, 0] = 1 + 2 * self.val1 self.assertEqual(dimage.image[0, 0], self.val1) dimage = afwImage.DecoratedImageF(self.dimage1) # shallow copy self.dimage1.image[0, 0] = 1 + 2 * self.val1 self.assertNotEqual(dimage.image[0, 0], self.val1)
def testReadFits(self): """Test reading FITS files""" try: dataDir = lsst.utils.getPackageDir("afwdata") except Exception: print >> sys.stderr, "Warning: afwdata is not set up; not running the FITS I/O tests" return dataDir = os.path.join(dataDir, "data") hdus = {} fileName = os.path.join(dataDir, "small_MI.fits") hdus["img"] = 2 # an S16 fits HDU hdus["msk"] = 3 # an U8 fits HDU hdus["var"] = 4 # an F32 fits HDU imgU = afwImage.DecoratedImageU(fileName, hdus["img"]) # read as unsigned short imgF = afwImage.DecoratedImageF(fileName, hdus["img"]) # read as float self.assertEqual(imgU.getHeight(), 256) self.assertEqual(imgF.getImage().getWidth(), 256) self.assertEqual(imgU.getImage().get(0, 0), imgF.getImage().get(0, 0)) # # Check the metadata # meta = self.trueMetadata for k in meta.keys(): self.assertEqual(imgU.getMetadata().getAsDouble(k), meta[k]) self.assertEqual(imgF.getMetadata().getAsDouble(k), meta[k]) # # Read an F32 image # varU = afwImage.DecoratedImageF(fileName, hdus["var"]) # read as unsigned short varF = afwImage.DecoratedImageF(fileName, hdus["var"]) # read as float self.assertEqual(varU.getHeight(), 256) self.assertEqual(varF.getImage().getWidth(), 256) self.assertEqual(varU.getImage().get(0, 0), varF.getImage().get(0, 0)) # # Read a char image # maskImg = afwImage.DecoratedImageU(fileName, hdus["msk"]).getImage() # read a char file self.assertEqual(maskImg.getHeight(), 256) self.assertEqual(maskImg.getWidth(), 256) self.assertEqual(maskImg.get(0, 0), 1) # # Read a U16 image # with utilsTests.getTempFilePath(".fits") as tmpFile: imgU.writeFits(tmpFile) afwImage.DecoratedImageF(tmpFile) # read as unsigned short
def testReadFits(self): """Test reading FITS files""" hdus = {} hdus["img"] = 1 # an S16 fits HDU hdus["msk"] = 2 # an U8 fits HDU hdus["var"] = 3 # an F32 fits HDU # read as unsigned short imgU = afwImage.DecoratedImageU(self.fileForMetadata, hdus["img"], allowUnsafe=True) # read as float imgF = afwImage.DecoratedImageF(self.fileForMetadata, hdus["img"]) self.assertEqual(imgU.getHeight(), 256) self.assertEqual(imgF.image.getWidth(), 256) self.assertEqual(imgU.image[0, 0, afwImage.LOCAL], imgF.image[0, 0, afwImage.LOCAL]) # # Check the metadata # meta = self.trueMetadata for k in meta.keys(): self.assertEqual(imgU.getMetadata().getAsDouble(k), meta[k]) self.assertEqual(imgF.getMetadata().getAsDouble(k), meta[k]) # # Read an F32 image # # read as unsigned short varU = afwImage.DecoratedImageF(self.fileForMetadata, hdus["var"]) # read as float varF = afwImage.DecoratedImageF(self.fileForMetadata, hdus["var"]) self.assertEqual(varU.getHeight(), 256) self.assertEqual(varF.image.getWidth(), 256) self.assertEqual(varU.image[0, 0, afwImage.LOCAL], varF.image[0, 0, afwImage.LOCAL]) # # Read a char image # maskImg = afwImage.DecoratedImageU( self.fileForMetadata, hdus["msk"]).image # read a char file self.assertEqual(maskImg.getHeight(), 256) self.assertEqual(maskImg.getWidth(), 256) self.assertEqual(maskImg[0, 0, afwImage.LOCAL], 1) # # Read a U16 image # with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: imgU.writeFits(tmpFile) afwImage.DecoratedImageF(tmpFile) # read as unsigned short
def testReadMetadata(self): im = afwImage.DecoratedImageF(self.fileForMetadata) meta = afwImage.readMetadata(self.fileForMetadata) self.assertIn("NAXIS1", meta.names()) self.assertEqual(im.getWidth(), meta.get("NAXIS1")) self.assertEqual(im.getHeight(), meta.get("NAXIS2"))
def testWriteBool(self): """Test that we can read and write bools""" import lsst.afw.image as afwImage import lsst.daf.base as dafBase imPath = "data" if os.path.exists("tests"): imPath = os.path.join("tests", imPath) imPath = os.path.join(imPath, "tmp.fits") im = afwImage.ImageF(afwGeom.ExtentI(10, 20)) md = dafBase.PropertySet() keys = { "BAD": False, "GOOD": True, } for k, v in keys.items(): md.add(k, v) im.writeFits(imPath, md) jim = afwImage.DecoratedImageF(imPath) os.remove(imPath) for k, v in keys.items(): self.assertEqual(jim.getMetadata().get(k), v)
def testBackgroundTestImages(self): """Tests Laher's afwdata/Statistics/*.fits images (doubles)""" imginfolist = [] # cooked to known value imginfolist.append(["v1_i1_g_m400_s20_f.fits", 399.9912966583894]) for imginfo in imginfolist: imgfile, centerValue = imginfo imgPath = os.path.join(AfwdataDir, "Statistics", imgfile) # get the image and header dimg = afwImage.DecoratedImageF(imgPath) img = dimg.getImage() fitsHdr = dimg.getMetadata() # the FITS header # get the True values of the mean and stdev reqMean = fitsHdr.getAsDouble("MEANREQ") reqStdev = fitsHdr.getAsDouble("SIGREQ") naxis1 = img.getWidth() naxis2 = img.getHeight() # create a background control object bctrl = afwMath.BackgroundControl(5, 5) # run the background constructor and call the getImage() function. backobj = afwMath.makeBackground(img, bctrl) pixPerSubimage = img.getWidth()*img.getHeight() / \ (bctrl.getNxSample()*bctrl.getNySample()) stdevInterp = reqStdev / math.sqrt(pixPerSubimage) # test getImage() by checking the center pixel bimg = backobj.getImageF(afwMath.Interpolate.AKIMA_SPLINE) testImgval = bimg[naxis1 // 2, naxis2 // 2, afwImage.LOCAL] self.assertLess(abs(testImgval - reqMean), 2 * stdevInterp)
def getSwarpedImage(self, kernelName, useSubregion=False, useDeepCopy=False): """ Inputs: - kernelName: name of kernel in the form used by afwImage.makeKernel - useSubregion: if True then the original source exposure (from which the usual test exposure was extracted) is read and the correct subregion extracted - useDeepCopy: if True then the copy of the subimage is a deep copy, else it is a shallow copy; ignored if useSubregion is False Returns: - originalExposure - swarpedImage - swarpedWcs """ if useSubregion: originalFullExposure = afwImage.ExposureF(originalExposurePath) # "medsub" is a subregion of med starting at 0-indexed pixel (40, 150) of size 145 x 200 bbox = afwGeom.Box2I(afwGeom.Point2I(40, 150), afwGeom.Extent2I(145, 200)) originalExposure = afwImage.ExposureF(originalFullExposure, bbox, useDeepCopy) swarpedImageName = "medsubswarp1%s.fits" % (kernelName, ) else: originalExposure = afwImage.ExposureF(originalExposurePath) swarpedImageName = "medswarp1%s.fits" % (kernelName, ) swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage() swarpedMetadata = swarpedDecoratedImage.getMetadata() swarpedWcs = afwImage.makeWcs(swarpedMetadata) return (originalExposure, swarpedImage, swarpedWcs)
def testCreateDecoratedImageFromImage(self): image = afwImage.ImageF(lsst.geom.Extent2I(self.width, self.height)) image[:] = self.dimage1.image dimage = afwImage.DecoratedImageF(image) self.assertEqual(dimage.getWidth(), self.width) self.assertEqual(dimage.getHeight(), self.height) self.assertEqual(dimage.image[0, 0], self.val1)
def testTransformBasedWarp(self): """Test warping using Transform<Point2Endpoint, Point2Endpoint> """ for interpLength in (0, 1, 2, 4): kernelName = "lanczos3" rtol = 4e-5 atol = 1e-2 warpingControl = afwMath.WarpingControl( warpingKernelName=kernelName, interpLength=interpLength, ) originalExposure = afwImage.ExposureF(originalExposurePath) originalMetadata = afwImage.DecoratedImageF( originalExposurePath).getMetadata() originalSkyWcs = afwGeom.SkyWcs(originalMetadata) swarpedImageName = "medswarp1%s.fits" % (kernelName, ) swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage() swarpedMetadata = swarpedDecoratedImage.getMetadata() warpedSkyWcs = afwGeom.SkyWcs(swarpedMetadata) # warped image is destination, original image is source # and WCS computes pixels to sky in the forward direction, so... destToSrc = warpedSkyWcs.then(originalSkyWcs.getInverse()) afwWarpedMaskedImage = afwImage.MaskedImageF( swarpedImage.getDimensions()) originalMaskedImage = originalExposure.getMaskedImage() numGoodPix = afwMath.warpImage(afwWarpedMaskedImage, originalMaskedImage, destToSrc, warpingControl) self.assertGreater(numGoodPix, 50) afwWarpedImage = afwWarpedMaskedImage.getImage() afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol)
def testTransformBasedWarp(self): """Test warping using TransformPoint2ToPoint2 """ for interpLength in (0, 1, 2, 4): kernelName = "lanczos3" rtol = 4e-5 atol = 1e-2 warpingControl = afwMath.WarpingControl( warpingKernelName=kernelName, interpLength=interpLength, ) originalExposure = afwImage.ExposureF(originalExposurePath) originalMetadata = afwImage.DecoratedImageF( originalExposurePath).getMetadata() originalSkyWcs = afwGeom.makeSkyWcs(originalMetadata) swarpedImageName = f"medswarp1{kernelName}.fits" swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage() swarpedMetadata = swarpedDecoratedImage.getMetadata() warpedSkyWcs = afwGeom.makeSkyWcs(swarpedMetadata) # original image is source, warped image is destination srcToDest = afwGeom.makeWcsPairTransform(originalSkyWcs, warpedSkyWcs) afwWarpedMaskedImage = afwImage.MaskedImageF( swarpedImage.getDimensions()) originalMaskedImage = originalExposure.getMaskedImage() numGoodPix = afwMath.warpImage(afwWarpedMaskedImage, originalMaskedImage, srcToDest, warpingControl) self.assertGreater(numGoodPix, 50) afwWarpedImage = afwWarpedMaskedImage.getImage() afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol)
def bypass_raw(self, datasetType, pythonType, location, dataId): ccd = dataId['ccd'] x, y = ccd % 6, ccd // 6 xSize, ySize = 2048, 4096 filename = location.getLocations()[0] bbox = afwGeom.Box2I(afwGeom.Point2I(x * xSize, y * ySize), afwGeom.Extent2I(xSize, ySize)) if False: # XXX seems to be some sort of bug here for ccd=11 # cfitsio returns: READ_ERROR 108 /* error reading from FITS file */ hdu = 0 image = afwImage.DecoratedImageF(filename, hdu, bbox) else: # XXX this can't be at all efficient, but it works raw = afwImage.ImageF(filename) sub = raw.Factory(raw, bbox, True) del raw sub.setXY0(afwGeom.Point2I(0, 0)) image = afwImage.DecoratedImageF(sub) del sub exp = exposureFromImage(image) del image md = exp.getMetadata() md.add("EXPTIME", 1.0) md.add("FILTER", "OPEN") # Install a basic (wrong, apart from pixel scale) WCS so the pixel scale can be used md.add("RA", "00:00:00.00") md.add("DEC", "00:00:00.0") coord = afwCoord.IcrsCoord(afwGeom.Point2D(0, 0), afwGeom.radians) point = afwGeom.Point2D(0, 0) pixScale = (0.9 * afwGeom.arcseconds).asDegrees() wcs = afwImage.makeWcs(coord, point, pixScale, 0, 0, pixScale) wcsMd = wcs.getFitsMetadata() for key in wcsMd.names(): md.add(key, wcsMd.get(key)) exp.setWcs(wcs) return self._standardizeExposure(self.exposures['raw'], exp, dataId, filter=True, trimmed=False)
def testCreateDecoratedImageFromImage(self): image = afwImage.ImageF(afwGeom.Extent2I(self.width, self.height)) image[:] = self.dimage1.getImage() dimage = afwImage.DecoratedImageF(image) self.assertEqual(dimage.getWidth(), self.width) self.assertEqual(dimage.getHeight(), self.height) self.assertEqual(dimage.getImage().get(0, 0), self.val1)
def testDecoratedImage(self): image = self.maskedImage.getImage() decoImage = afwImage.DecoratedImageF(image) metadata = PropertyList() metadata.set("FOO", "BAR") decoImage.setMetadata(metadata) exposure = exposureFromImage(decoImage) self.assertImagesEqual(exposure.getMaskedImage().getImage(), image) md = exposure.getMetadata() self.assertEqual(md.get("FOO"), "BAR")
def testReadMetadata(self): if self.fileForMetadata: im = afwImage.DecoratedImageF(self.fileForMetadata) else: print >> sys.stderr, "Warning: afwdata is not set up; not running the FITS metadata I/O tests" return meta = afwImage.readMetadata(self.fileForMetadata) self.assertTrue("NAXIS1" in meta.names()) self.assertEqual(im.getWidth(), meta.get("NAXIS1")) self.assertEqual(im.getHeight(), meta.get("NAXIS2"))
def _read_with_bg_subtraction(filename): config = MuonConfig() imagearray = [] for i in range(2,18): original_image = afwImage.DecoratedImageF(filename, i) maskedImg = afwImage.MaskedImageF(original_image.getImage()) exposure = afwImage.ExposureF(maskedImg) bg, bgSubExp = estimateBackground(exposure, config.background, subtract=True) bgSubImg = bgSubExp.getMaskedImage().getImage() imagearray.append(bgSubImg) return imagearray
def setUp(self): np.random.seed(1) self.val1, self.val2 = 10, 100 self.width, self.height = 200, 100 self.dimage1 = afwImage.DecoratedImageF( afwGeom.Extent2I(self.width, self.height) ) self.dimage1.getImage().set(self.val1) if afwdataDir is not None: self.fileForMetadata = os.path.join(afwdataDir, "data", "small_MI.fits") self.trueMetadata = {"RELHUMID": 10.69}
def testDecoratedImageBadWcs(self): """Test that exposureFromImage() attaches a None wcs to the exposure when makeWcs() raises an invalidParameter error """ image = self.maskedImage.getImage() decoImage = afwImage.DecoratedImageF(image) metadata = PropertyList() metadata.set("CTYPE1", "RA---TPV") metadata.set("CTYPE2", "DEC--TPV") decoImage.setMetadata(metadata) exposure = exposureFromImage(decoImage) self.assertIs(exposure.getWcs(), None)
def testDecoratedImageBadWcs(self): """Test that exposureFromImage() attaches a None wcs to the exposure when the WCS cannot be constructed """ image = self.maskedImage.getImage() decoImage = afwImage.DecoratedImageF(image) metadata = PropertyList() metadata.set("CTYPE1", "RA---TPV") metadata.set("CTYPE2", "DEC--TPV") decoImage.setMetadata(metadata) exposure = exposureFromImage(decoImage) self.assertIs(exposure.getWcs(), None)
def testNonIcrs(self): """Test that warping to a non-ICRS-like coordinate system produces different results It would be better to also test that the results are as expected, but I have not been able to get swarp to perform this operation, so have not found an independent means of generating the expected results. """ kernelName = "lanczos3" rtol = 4e-5 atol = 1e-2 warpingControl = afwMath.WarpingControl(kernelName, ) originalExposure = afwImage.ExposureF(originalExposurePath) originalImage = originalExposure.getMaskedImage().getImage() originalWcs = originalExposure.getWcs() swarpedImageName = "medswarp1%s.fits" % (kernelName, ) swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage() for changeEquinox in (False, True): swarpedMetadata = swarpedDecoratedImage.getMetadata() if changeEquinox: swarpedMetadata.set("RADECSYS", "FK5") swarpedMetadata.set("EQUINOX", swarpedMetadata.get("EQUINOX") + 1) warpedWcs = afwImage.makeWcs(swarpedMetadata) afwWarpedImage = afwImage.ImageF(swarpedImage.getDimensions()) originalImage = originalExposure.getMaskedImage().getImage() originalWcs = originalExposure.getWcs() numGoodPix = afwMath.warpImage(afwWarpedImage, warpedWcs, originalImage, originalWcs, warpingControl) self.assertGreater(numGoodPix, 50) afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) if changeEquinox: with self.assertRaises(AssertionError): self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol) else: self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol)
def GetImage_SingleAmp(filename, subtract_background = False, amp_number = 0): '''Return a single amplifier image, optionally background subtracted. Amplifiers are indexed 0-->15''' if subtract_background: config = MuonConfig() original_image = afwImage.DecoratedImageF(filename, amp_number + 2) maskedImg = afwImage.MaskedImageF(original_image.getImage()) exposure = afwImage.ExposureF(maskedImg) bg, bgSubExp = estimateBackground(exposure, config.background, subtract=True) bgSubImg = bgSubExp.getMaskedImage().getImage() return bgSubImg else: return afwImage.ImageF(filename, amp_number + 2) return image
def testWriteFits(self): """Test writing FITS files""" with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: if self.fileForMetadata: imgU = afwImage.DecoratedImageF(self.fileForMetadata) else: imgU = afwImage.DecoratedImageF() self.dimage1.writeFits(tmpFile, imgU.getMetadata()) # # Read it back # rimage = afwImage.DecoratedImageF(tmpFile) self.assertEqual(self.dimage1.getImage().get(0, 0), rimage.getImage().get(0, 0)) # # Check that we wrote (and read) the metadata successfully if self.fileForMetadata: meta = self.trueMetadata for k in meta.keys(): self.assertEqual(rimage.getMetadata().getAsDouble(k), meta[k])
def setUp(self): self.val1, self.val2 = 10, 100 self.width, self.height = 200, 100 self.dimage1 = afwImage.DecoratedImageF( afwGeom.Extent2I(self.width, self.height) ) self.dimage1.getImage().set(self.val1) dataDir = eups.productDir("afwdata") if dataDir: self.fileForMetadata = os.path.join(dataDir, "data", "small_MI.fits") self.trueMetadata = {"RELHUMID" : 10.69} else: self.fileForMetadata = None
def setUp(self): self.val1, self.val2 = 10, 100 self.width, self.height = 200, 100 self.dimage1 = afwImage.DecoratedImageF( afwGeom.Extent2I(self.width, self.height) ) self.dimage1.getImage().set(self.val1) try: dataDir = lsst.utils.getPackageDir("afwdata") except Exception: self.fileForMetadata = None else: self.fileForMetadata = os.path.join(dataDir, "data", "small_MI.fits") self.trueMetadata = {"RELHUMID" : 10.69}
def combine(self, cache, struct, outputId): """!Combine multiple exposures of a particular CCD and write the output Only the slave nodes execute this method. @param cache Process pool cache @param struct Parameters for the combination, which has the following components: * ccdName Name tuple for CCD * ccdIdList List of data identifiers for combination * scales Scales to apply (expScales are scalings for each exposure, ccdScale is final scale for combined image) @param outputId Data identifier for combined image (exposure part only) """ # Check if we need to look up any keys that aren't in the output dataId fullOutputId = { k: struct.ccdName[i] for i, k in enumerate(self.config.ccdKeys) } fullOutputId.update(outputId) self.addMissingKeys(fullOutputId, cache.butler) fullOutputId.update( outputId) # must be after the call to queryMetadata outputId = fullOutputId del fullOutputId dataRefList = [ getDataRef(cache.butler, dataId) if dataId is not None else None for dataId in struct.ccdIdList ] self.log.info("Combining %s on %s" % (outputId, NODE)) calib = self.combination.run(dataRefList, expScales=struct.scales.expScales, finalScale=struct.scales.ccdScale) if not hasattr(calib, "getMetadata"): if hasattr(calib, "getVariance"): calib = afwImage.makeExposure(calib) else: calib = afwImage.DecoratedImageF( calib.getImage()) # n.b. hardwires "F" for the output type self.updateMetadata(calib, self.exposureTime) self.recordCalibInputs(cache.butler, calib, struct.ccdIdList, outputId) self.interpolateNans(calib) self.write(cache.butler, calib, outputId)
def testWriteBool(self): """Test that we can read and write bools""" with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: im = afwImage.ImageF(lsst.geom.ExtentI(10, 20)) md = dafBase.PropertySet() keys = {"BAD": False, "GOOD": True, } for k, v in keys.items(): md.add(k, v) im.writeFits(tmpFile, md) jim = afwImage.DecoratedImageF(tmpFile) for k, v in keys.items(): self.assertEqual(jim.getMetadata().getScalar(k), v)
def testSampleImageStats(self): """ Compare our results to known values in test data """ imgfiles = [] imgfiles.append("v1_i1_g_m400_s20_f.fits") imgfiles.append("v1_i1_g_m400_s20_u16.fits") imgfiles.append("v1_i2_g_m400_s20_f.fits") imgfiles.append("v1_i2_g_m400_s20_u16.fits") imgfiles.append("v2_i1_p_m9_f.fits") imgfiles.append("v2_i1_p_m9_u16.fits") imgfiles.append("v2_i2_p_m9_f.fits") imgfiles.append("v2_i2_p_m9_u16.fits") afwdataDir = os.getenv("AFWDATA_DIR") for imgfile in imgfiles: imgPath = os.path.join(afwdataDir, "Statistics", imgfile) # get the image and header dimg = afwImage.DecoratedImageF(imgPath) fitsHdr = dimg.getMetadata() # get the true values of the mean and stdev trueMean = fitsHdr.getAsDouble("MEANCOMP") trueStdev = fitsHdr.getAsDouble("SIGCOMP") # measure the mean and stdev with the Statistics class img = dimg.getImage() statobj = afwMath.makeStatistics(img, afwMath.MEAN | afwMath.STDEV) mean = statobj.getValue(afwMath.MEAN) stdev = statobj.getValue(afwMath.STDEV) # print trueMean, mean, trueStdev, stdev self.assertAlmostEqual(mean, trueMean, 8) self.assertAlmostEqual(stdev, trueStdev, 8)
def processRun(wcsList, outputDir, kind, bc, res): """Creates and persists coverage maps for an entire run. """ crpix = afwGeom.Point2D(0.5 * (res + 1), 0.5 * (res + 1)) crval = afwGeom.Point2D(bc.getCenter()[0], bc.getCenter()[1]) scale = bc.getRadius() * 2.0 / res runWcs = afwImage.createWcs(crval, crpix, scale, 0.0, 0.0, scale) covMaps = [] for i in xrange(6): runImg = afwImage.DecoratedImageF(afwGeom.Extent2I(res, res)) if hasattr(runImg, 'setWcs'): runImg.setWcs(runWcs) elif hasattr(runImg, 'setMetadata'): runImg.setMetadata(runWcs.getFitsMetadata()) covMaps.append(runImg) if kind == 'imsim': width, height = 4072, 4000 else: width, height = 2048, 4610 for wcs, filter in wcsList: assert filter >= 0 and filter < 6 apUtils.updateCoverageMap(covMaps[filter].getImage(), runWcs, wcs, width, height, 0) persistCovMaps(covMaps, outputDir)