Ejemplo n.º 1
0
    def testHeaders(self):
        header = {
            "STR": "String",
            "INT": 12345,
            "FLOAT": 678.9,
            "NAN": numpy.nan,
            "PLUSINF": numpy.inf,
            "MINUSINF": -numpy.inf,
            "LONG": 987654321,
        }

        exp = afwImage.ExposureI(0, 0)
        metadata = exp.getMetadata()
        for k, v in header.items():
            metadata.add(k, v)

        with lsst.utils.tests.getTempFilePath(".fits") as filename:
            exp.writeFits(filename)
            exp = afwImage.ExposureI(filename)

        metadata = exp.getMetadata()
        for k, v in header.items():
            self.assertTrue(metadata.exists(k))
            if isinstance(v, float) and numpy.isnan(v):
                self.assertIsInstance(metadata.get(k), float)
                self.assertTrue(numpy.isnan(metadata.get(k)))
            else:
                self.assertEqual(metadata.get(k), v)
Ejemplo n.º 2
0
    def testHeaders(self):
        filename = "tests/header.fits"
        header = {
            "STR": "String",
            "INT": 12345,
            "FLOAT": 678.9,
            "NAN": numpy.nan,
            "PLUSINF": numpy.inf,
            "MINUSINF": -numpy.inf,
            "LONG": long(987654321),
        }

        exp = afwImage.ExposureI(0, 0)
        metadata = exp.getMetadata()
        for k, v in header.items():
            metadata.add(k, v)

        exp.writeFits(filename)

        exp = afwImage.ExposureI(filename)
        metadata = exp.getMetadata()
        for k, v in header.items():
            self.assertTrue(metadata.exists(k))
            if isinstance(v, float) and numpy.isnan(v):
                self.assertTrue(isinstance(metadata.get(k), float))
                self.assertTrue(numpy.isnan(metadata.get(k)))
            else:
                self.assertEqual(metadata.get(k), v)
Ejemplo n.º 3
0
    def writeFcr(self, dataRefList):
        self.log.info("Write Fcr ...")
        M_LN10 = math.log(10)
        dmag = list()
        for m in self.matchVec:
            if (m.good == True and m.mag != -9999 and m.jstar != -1 and
                m.mag0 != -9999 and m.mag_cat != -9999):
                mag = m.mag
                mag_cat = m.mag_cat
                exp_cor = -2.5*math.log10(self.fexp[m.iexp])
                chip_cor = -2.5*math.log10(self.fchip[m.ichip])
                gain_cor = self.ffpSet[m.iexp].eval(m.u, m.v)
                mag_cor = mag + exp_cor + chip_cor + gain_cor
                dmag.append(mag_cor - mag_cat)
        std, mean, n  = mosaicUtils.clippedStd(numpy.array(dmag), 2.1)
        for dataRef in dataRefList:
            iexp = dataRef.dataId["visit"]
            ichip = dataRef.dataId["ccd"]
            try:
                x0 = self.coeffSet[iexp].x0
                y0 = self.coeffSet[iexp].y0
            except:
                x0 = 0.0
                y0 = 0.0
            newP = measMosaic.convertFluxFitParams(measMosaic.FluxFitParams(self.ffpSet[iexp]),
                                                   self.ccdSet[ichip], x0, y0)
            metadata = measMosaic.metadataFromFluxFitParams(newP)
            exp = afwImage.ExposureI(0,0)
            exp.getMetadata().combine(metadata)
            scale = self.fexp[iexp]*self.fchip[ichip]
            constantPhotoCalib = afwImage.makePhotoCalibFromCalibZeroPoint(1.0/scale, 1.0/scale*std*M_LN10*0.4)
            exp.setPhotoCalib(constantPhotoCalib)
            try:
                dataRef.put(exp, "fcr")
            except Exception as e:
                print("failed to write fcr: %s" % (e))

            # Write the flux fit (including Jacobian) as a PhotoCalib for
            # future compatibility with jointcal.  This is redundant with
            # the above, and should eventually supercede it.
            detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
            nQuarter = detector.getOrientation().getNQuarter()
            bbox = detector.getBBox()
            try:
                # Reading the Wcs we just wrote obviously isn't efficient, but
                # it should be in the noise of the overall runtime and it
                # saves us from doing a bunch of refactoring in a fragile
                # package with no tests.
                wcs = dataRef.get("jointcal_wcs")
            except Exception as e:
                print("failed to read Wcs for PhotoCalib: %s" % (e))
                continue
            bf = measMosaic.FluxFitBoundedField(bbox, newP, wcs,
                                                zeroPoint=constantPhotoCalib.getInstFluxAtZeroMagnitude(),
                                                nQuarter=nQuarter)
            varyingPhotoCalib = afwImage.PhotoCalib(constantPhotoCalib.getCalibrationMean(),
                                                    constantPhotoCalib.getCalibrationErr(),
                                                    bf,
                                                    isConstant=False)
            dataRef.put(varyingPhotoCalib, "jointcal_photoCalib")
Ejemplo n.º 4
0
    def writeNewWcs(self, dataRefList):
        self.log.info("Write New WCS ...")
        exp = afwImage.ExposureI(0, 0)
        for dataRef in dataRefList:
            iexp = dataRef.dataId["visit"]
            ichip = dataRef.dataId["ccd"]
            c = measMosaic.convertCoeff(self.coeffSet[iexp],
                                        self.ccdSet[ichip])
            wcs = measMosaic.wcsFromCoeff(c)
            calexp_md = dataRef.get("calexp_md", immediate=True)
            hscRun = mosaicUtils.checkHscStack(calexp_md)
            if hscRun is None:
                detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
                nQuarter = detector.getOrientation().getNQuarter()
                if nQuarter % 4 != 0:
                    dimensions = afwGeom.Extent2I(calexp_md.get("NAXIS1"),
                                                  calexp_md.get("NAXIS2"))
                    if nQuarter % 2 != 0:
                        dimensions = afwGeom.Extent2I(calexp_md.get("NAXIS2"),
                                                      calexp_md.get("NAXIS1"))
                    wcs = measAstrom.rotateWcsPixelsBy90(
                        wcs, 4 - nQuarter, dimensions)

            exp.setWcs(wcs)
            try:
                dataRef.put(exp, "wcs")
            except Exception as e:
                print "failed to write wcs: %s" % (e)
Ejemplo n.º 5
0
    def _write_results(self, associations, astrom_model, photom_model,
                       visit_ccd_to_dataRef):
        """
        Write the fitted results (photometric and astrometric) to a new 'wcs' dataRef.

        Parameters
        ----------
        associations : lsst.jointcal.Associations
            The star/reference star associations to fit.
        astrom_model : lsst.jointcal.AstrometryModel
            The astrometric model that was fit.
        photom_model : lsst.jointcal.PhotometryModel
            The photometric model that was fit.
        visit_ccd_to_dataRef : dict of Key: lsst.daf.persistence.ButlerDataRef
            dict of ccdImage identifiers to dataRefs that were fit
        """

        ccdImageList = associations.getCcdImageList()
        for ccdImage in ccdImageList:
            # TODO: there must be a better way to identify this ccdImage than a visit,ccd pair?
            ccd = ccdImage.ccdId
            visit = ccdImage.visit
            dataRef = visit_ccd_to_dataRef[(visit, ccd)]
            exp = afwImage.ExposureI(0, 0)
            if self.config.doAstrometry:
                self.log.info("Updating WCS for visit: %d, ccd: %d", visit,
                              ccd)
                tanSip = astrom_model.produceSipWcs(ccdImage)
                tanWcs = lsst.jointcal.gtransfoToTanWcs(
                    tanSip, ccdImage.imageFrame, False)
                exp.setWcs(tanWcs)
            if self.config.doPhotometry:
                self.log.info("Updating Calib for visit: %d, ccd: %d", visit,
                              ccd)
                # start with the original calib saved to the ccdImage
                fluxMag0 = ccdImage.getPhotoCalib().getInstFluxMag0()
                fluxMag0Err = ccdImage.getPhotoCalib().getInstFluxMag0Err()
                exp.getCalib().setFluxMag0(
                    fluxMag0 / photom_model.photomFactor(ccdImage),
                    fluxMag0Err)
            try:
                dataRef.put(exp, 'wcs')
            except pexExceptions.Exception as e:
                self.log.fatal('Failed to write updated Wcs and Calib: %s',
                               str(e))
                raise e
 def writeFcr(self, butler, ccdIdList, ccdSet, filterName, fexp, fchip,
              ffpSet):
     for ccdId in ccdIdList:
         iexp, ichip = self.decodeCcdExposureId(ccdId)
         if ichip not in ccdSet:
             continue
         x0 = 0.0
         y0 = 0.0
         newP = measMosaic.convertFluxFitParams(
             measMosaic.FluxFitParams(ffpSet[iexp]), ccdSet[ichip], x0, y0)
         metadata = measMosaic.metadataFromFluxFitParams(newP)
         exp = afwImage.ExposureI(0, 0)
         exp.getMetadata().combine(metadata)
         scale = fexp[iexp] * fchip[ichip]
         exp.setPhotoCalib(
             afwImage.makePhotoCalibFromCalibZeroPoint(1.0 / scale))
         exp.setFilter(afwImage.Filter(filterName))
         try:
             butler.put(exp, 'fcr', {'visit': iexp, 'ccd': ichip})
         except Exception as e:
             print("failed to write something: %s" % (e))