380 photoRefObjLoader=None, icSourceSchema=None,
381 initInputs=None, **kwargs):
384 if initInputs
is not None:
385 icSourceSchema = initInputs[
'icSourceSchema'].schema
387 if icSourceSchema
is not None:
390 minimumSchema = afwTable.SourceTable.makeMinimalSchema()
391 self.
schemaMapper.addMinimalSchema(minimumSchema,
False)
399 afwTable.Field[
"Flag"](
"calib_detected",
400 "Source was detected as an icSource"))
401 missingFieldNames = []
402 for fieldName
in self.config.icSourceFieldsToCopy:
404 schemaItem = icSourceSchema.find(fieldName)
406 missingFieldNames.append(fieldName)
411 if missingFieldNames:
412 raise RuntimeError(
"isSourceCat is missing fields {} "
413 "specified in icSourceFieldsToCopy"
414 .format(missingFieldNames))
421 self.
schema = afwTable.SourceTable.makeMinimalSchema()
422 afwTable.CoordKey.addErrorFields(self.
schema)
423 self.makeSubtask(
'detection', schema=self.
schema)
427 if self.config.doDeblend:
428 self.makeSubtask(
"deblend", schema=self.
schema)
429 if self.config.doSkySources:
430 self.makeSubtask(
"skySources")
432 self.makeSubtask(
'measurement', schema=self.
schema,
434 self.makeSubtask(
'postCalibrationMeasurement', schema=self.
schema,
436 self.makeSubtask(
"setPrimaryFlags", schema=self.
schema, isSingleFrame=
True)
437 if self.config.doApCorr:
438 self.makeSubtask(
'applyApCorr', schema=self.
schema)
439 self.makeSubtask(
'catalogCalculation', schema=self.
schema)
441 if self.config.doAstrometry:
442 self.makeSubtask(
"astrometry", refObjLoader=astromRefObjLoader,
444 if self.config.doPhotoCal:
445 self.makeSubtask(
"photoCal", refObjLoader=photoRefObjLoader,
447 if self.config.doComputeSummaryStats:
448 self.makeSubtask(
'computeSummaryStats')
450 if initInputs
is not None and (astromRefObjLoader
is not None or photoRefObjLoader
is not None):
451 raise RuntimeError(
"PipelineTask form of this task should not be initialized with "
452 "reference object loaders.")
457 self.
schema.checkUnits(parse_strict=self.config.checkUnitsParseStrict)
459 sourceCatSchema = afwTable.SourceCatalog(self.
schema)
464 inputs = butlerQC.get(inputRefs)
465 inputs[
'idGenerator'] = self.config.idGenerator.apply(butlerQC.quantum.dataId)
467 if self.config.doAstrometry:
468 refObjLoader = ReferenceObjectLoader(dataIds=[ref.datasetRef.dataId
469 for ref
in inputRefs.astromRefCat],
470 refCats=inputs.pop(
'astromRefCat'),
471 name=self.config.connections.astromRefCat,
472 config=self.config.astromRefObjLoader, log=self.log)
473 self.astrometry.setRefObjLoader(refObjLoader)
475 if self.config.doPhotoCal:
476 photoRefObjLoader = ReferenceObjectLoader(dataIds=[ref.datasetRef.dataId
477 for ref
in inputRefs.photoRefCat],
478 refCats=inputs.pop(
'photoRefCat'),
479 name=self.config.connections.photoRefCat,
480 config=self.config.photoRefObjLoader,
482 self.photoCal.match.setRefObjLoader(photoRefObjLoader)
484 outputs = self.
run(**inputs)
486 if self.config.doWriteMatches
and self.config.doAstrometry:
487 if outputs.astromMatches
is not None:
488 normalizedMatches = afwTable.packMatches(outputs.astromMatches)
489 normalizedMatches.table.setMetadata(outputs.matchMeta)
490 if self.config.doWriteMatchesDenormalized:
491 denormMatches = denormalizeMatches(outputs.astromMatches, outputs.matchMeta)
492 outputs.matchesDenormalized = denormMatches
493 outputs.matches = normalizedMatches
495 del outputRefs.matches
496 if self.config.doWriteMatchesDenormalized:
497 del outputRefs.matchesDenormalized
498 butlerQC.put(outputs, outputRefs)
501 def run(self, exposure, background=None,
502 icSourceCat=None, idGenerator=None):
503 """Calibrate an exposure.
507 exposure : `lsst.afw.image.ExposureF`
508 Exposure to calibrate.
509 background : `lsst.afw.math.BackgroundList`, optional
510 Initial model of background already subtracted from exposure.
511 icSourceCat : `lsst.afw.image.SourceCatalog`, optional
512 SourceCatalog from CharacterizeImageTask from which we can copy
514 idGenerator : `lsst.meas.base.IdGenerator`, optional
515 Object that generates source IDs and provides RNG seeds.
519 result : `lsst.pipe.base.Struct`
520 Results as a struct with attributes:
523 Characterized exposure (`lsst.afw.image.ExposureF`).
525 Detected sources (`lsst.afw.table.SourceCatalog`).
527 Model of subtracted background (`lsst.afw.math.BackgroundList`).
529 List of source/ref matches from astrometry solver.
531 Metadata from astrometry matches.
533 Another reference to ``exposure`` for compatibility.
535 Another reference to ``sourceCat`` for compatibility.
538 if idGenerator
is None:
539 idGenerator = IdGenerator()
541 if background
is None:
542 background = BackgroundList()
543 table = SourceTable.make(self.
schema, idGenerator.make_table_id_factory())
546 detRes = self.detection.run(table=table, exposure=exposure,
548 sourceCat = detRes.sources
549 if detRes.background:
550 for bg
in detRes.background:
551 background.append(bg)
552 if self.config.doSkySources:
553 skySourceFootprints = self.skySources.run(mask=exposure.mask, seed=idGenerator.catalog_id)
554 if skySourceFootprints:
555 for foot
in skySourceFootprints:
556 s = sourceCat.addNew()
559 if self.config.doDeblend:
560 self.deblend.run(exposure=exposure, sources=sourceCat)
561 self.measurement.run(
564 exposureId=idGenerator.catalog_id,
566 if self.config.doApCorr:
567 apCorrMap = exposure.getInfo().getApCorrMap()
568 if apCorrMap
is None:
569 self.log.warning(
"Image does not have valid aperture correction map for %r; "
570 "skipping aperture correction", idGenerator)
572 self.applyApCorr.run(
576 self.catalogCalculation.run(sourceCat)
578 self.setPrimaryFlags.run(sourceCat)
580 if icSourceCat
is not None and \
581 len(self.config.icSourceFieldsToCopy) > 0:
589 if not sourceCat.isContiguous():
590 sourceCat = sourceCat.copy(deep=
True)
596 if self.config.doAstrometry:
597 astromRes = self.astrometry.run(
601 astromMatches = astromRes.matches
602 matchMeta = astromRes.matchMeta
603 if exposure.getWcs()
is None:
604 if self.config.requireAstrometry:
605 raise RuntimeError(f
"WCS fit failed for {idGenerator} and requireAstrometry "
608 self.log.warning(
"Unable to perform astrometric calibration for %r but "
609 "requireAstrometry is False: attempting to proceed...",
613 if self.config.doPhotoCal:
614 if np.all(np.isnan(sourceCat[
"coord_ra"]))
or np.all(np.isnan(sourceCat[
"coord_dec"])):
615 if self.config.requirePhotoCal:
616 raise RuntimeError(f
"Astrometry failed for {idGenerator}, so cannot do "
617 "photoCal, but requirePhotoCal is True.")
618 self.log.warning(
"Astrometry failed for %r, so cannot do photoCal. requirePhotoCal "
619 "is False, so skipping photometric calibration and setting photoCalib "
620 "to None. Attempting to proceed...", idGenerator)
621 exposure.setPhotoCalib(
None)
625 photoRes = self.photoCal.run(
626 exposure, sourceCat=sourceCat, expId=idGenerator.catalog_id
628 exposure.setPhotoCalib(photoRes.photoCalib)
631 self.log.info(
"Photometric zero-point: %f",
632 photoRes.photoCalib.instFluxToMagnitude(1.0))
633 self.
setMetadata(exposure=exposure, photoRes=photoRes)
634 except Exception
as e:
635 if self.config.requirePhotoCal:
637 self.log.warning(
"Unable to perform photometric calibration "
638 "(%s): attempting to proceed", e)
641 self.postCalibrationMeasurement.run(
644 exposureId=idGenerator.catalog_id,
647 if self.config.doComputeSummaryStats:
648 summary = self.computeSummaryStats.run(exposure=exposure,
650 background=background)
651 exposure.getInfo().setSummaryStats(summary)
653 frame = getDebugFrame(self._display,
"calibrate")
658 matches=astromMatches,
663 return pipeBase.Struct(
665 astromMatches=astromMatches,
667 outputExposure=exposure,
669 outputBackground=background,
709 """Match sources in an icSourceCat and a sourceCat and copy fields.
711 The fields copied are those specified by
712 ``config.icSourceFieldsToCopy``.
716 icSourceCat : `lsst.afw.table.SourceCatalog`
717 Catalog from which to copy fields.
718 sourceCat : `lsst.afw.table.SourceCatalog`
719 Catalog to which to copy fields.
724 Raised if any of the following occur:
725 - icSourceSchema and icSourceKeys are not specified.
726 - icSourceCat and sourceCat are not specified.
727 - icSourceFieldsToCopy is empty.
730 raise RuntimeError(
"To copy icSource fields you must specify "
731 "icSourceSchema and icSourceKeys when "
732 "constructing this task")
733 if icSourceCat
is None or sourceCat
is None:
734 raise RuntimeError(
"icSourceCat and sourceCat must both be "
736 if len(self.config.icSourceFieldsToCopy) == 0:
737 self.log.warning(
"copyIcSourceFields doing nothing because "
738 "icSourceFieldsToCopy is empty")
741 mc = afwTable.MatchControl()
742 mc.findOnlyClosest =
False
743 matches = afwTable.matchXy(icSourceCat, sourceCat,
744 self.config.matchRadiusPix, mc)
745 if self.config.doDeblend:
746 deblendKey = sourceCat.schema[
"deblend_nChild"].asKey()
748 matches = [m
for m
in matches
if m[1].get(deblendKey) == 0]
755 for m0, m1, d
in matches:
757 match = bestMatches.get(id0)
758 if match
is None or d <= match[2]:
759 bestMatches[id0] = (m0, m1, d)
760 matches = list(bestMatches.values())
765 numMatches = len(matches)
766 numUniqueSources = len(set(m[1].getId()
for m
in matches))
767 if numUniqueSources != numMatches:
768 self.log.warning(
"%d icSourceCat sources matched only %d sourceCat "
769 "sources", numMatches, numUniqueSources)
771 self.log.info(
"Copying flags from icSourceCat to sourceCat for "
772 "%d sources", numMatches)
776 for icSrc, src, d
in matches:
782 icSrcFootprint = icSrc.getFootprint()
784 icSrc.setFootprint(src.getFootprint())
787 icSrc.setFootprint(icSrcFootprint)