Exemplo n.º 1
0
    def readWriteImage(self, ImageClass, image, filename, options, *args):
        """Read the image after it has been written

        This implementation does the persistence using methods on the
        ImageClass.

        Parameters
        ----------
        ImageClass : `type`, an `lsst.afw.image.Image` class
            Class of image to create.
        image : `lsst.afw.image.Image`
            Image to compress.
        filename : `str`
            Filename to which to write.
        options : `lsst.afw.fits.ImageWriteOptions`
            Options for writing.
        """
        image.writeFits(filename, options, *args)
        return ImageClass(filename)
Exemplo n.º 2
0
    def readWriteMaskedImage(self, image, filename, imageOptions, maskOptions,
                             varianceOptions):
        """Read the MaskedImage after it has been written

        This implementation does the persistence using methods on the
        MaskedImage class.

        Parameters
        ----------
        image : `lsst.afw.image.Image`
            Image to compress.
        filename : `str`
            Filename to which to write.
        imageOptions, maskOptions, varianceOptions : `lsst.afw.fits.ImageWriteOptions`
            Options for writing the image, mask and variance planes.
        """
        image.writeFits(filename, imageOptions, maskOptions, varianceOptions)
        if hasattr(image, "getMaskedImage"):
            image = image.getMaskedImage()
        return lsst.afw.image.MaskedImageF(filename)
Exemplo n.º 3
0
    def makeImage(self, ImageClass, scaling, addNoise=True):
        """Make an image for testing

        We create an image, persist and unpersist it, returning
        some data to the caller.

        Parameters
        ----------
        ImageClass : `type`, an `lsst.afw.image.Image` class
            Class of image to create.
        scaling : `lsst.afw.fits.ImageScalingOptions`
            Scaling to apply during persistence.
        addNoise : `bool`
            Add noise to image?

        Returns
        -------
        image : `lsst.afw.image.Image` (ImageClass)
            Created image.
        unpersisted : `lsst.afw.image.Image` (ImageClass)
            Unpersisted image.
        bscale, bzero : `float`
            FITS scale factor and zero used.
        minValue, maxValue : `float`
            Minimum and maximum value given the nominated scaling.
        """
        image = ImageClass(self.bbox)
        mask = lsst.afw.image.Mask(self.bbox)
        mask.addMaskPlane(self.badMask)
        bad = mask.getPlaneBitMask(self.badMask)
        image.set(self.base)
        image[self.highPixel, LOCAL] = self.highValue
        image[self.lowPixel, LOCAL] = self.lowValue
        image[self.maskedPixel, LOCAL] = self.maskedValue
        mask[self.maskedPixel, LOCAL] = bad

        rng = np.random.RandomState(12345)
        dtype = image.getArray().dtype
        if addNoise:
            image.getArray()[:] += rng.normal(
                0.0, self.stdev,
                image.getArray().shape).astype(dtype)

        with lsst.utils.tests.getTempFilePath(".fits") as filename:
            with lsst.afw.fits.Fits(filename, "w") as fits:
                options = lsst.afw.fits.ImageWriteOptions(scaling)
                header = lsst.daf.base.PropertyList()
                image.writeFits(fits, options, header, mask)
            unpersisted = ImageClass(filename)
            self.assertEqual(image.getBBox(), unpersisted.getBBox())

            header = lsst.afw.fits.readMetadata(filename)
            bscale = header.getScalar("BSCALE")
            bzero = header.getScalar("BZERO")

            if scaling.algorithm != ImageScalingOptions.NONE:
                self.assertEqual(header.getScalar("BITPIX"), scaling.bitpix)

            if scaling.bitpix == 8:  # unsigned, says FITS
                maxValue = bscale * (2**scaling.bitpix - 1) + bzero
                minValue = bzero
            else:
                maxValue = bscale * (2**(scaling.bitpix - 1) - 1) + bzero
                if scaling.bitpix == 32:
                    # cfitsio pads 10 values, and so do we
                    minValue = -bscale * (2**(scaling.bitpix - 1) - 10) + bzero
                else:
                    minValue = -bscale * (2**(scaling.bitpix - 1)) + bzero

            # Convert scalars to the appropriate type
            maxValue = np.array(maxValue, dtype=image.getArray().dtype)
            minValue = np.array(minValue, dtype=image.getArray().dtype)

            checkAstropy(unpersisted, filename)

        return image, unpersisted, bscale, bzero, minValue, maxValue