378 photoRefObjLoader=None, icSourceSchema=None,
379 initInputs=None, **kwargs):
382 if initInputs
is not None:
383 icSourceSchema = initInputs[
'icSourceSchema'].schema
385 if icSourceSchema
is not None:
388 minimumSchema = afwTable.SourceTable.makeMinimalSchema()
389 self.
schemaMapper.addMinimalSchema(minimumSchema,
False)
397 afwTable.Field[
"Flag"](
"calib_detected",
398 "Source was detected as an icSource"))
399 missingFieldNames = []
400 for fieldName
in self.config.icSourceFieldsToCopy:
402 schemaItem = icSourceSchema.find(fieldName)
404 missingFieldNames.append(fieldName)
409 if missingFieldNames:
410 raise RuntimeError(
"isSourceCat is missing fields {} "
411 "specified in icSourceFieldsToCopy"
412 .format(missingFieldNames))
419 self.
schema = afwTable.SourceTable.makeMinimalSchema()
420 self.makeSubtask(
'detection', schema=self.
schema)
424 if self.config.doDeblend:
425 self.makeSubtask(
"deblend", schema=self.
schema)
426 if self.config.doSkySources:
427 self.makeSubtask(
"skySources")
429 self.makeSubtask(
'measurement', schema=self.
schema,
431 self.makeSubtask(
'postCalibrationMeasurement', schema=self.
schema,
433 self.makeSubtask(
"setPrimaryFlags", schema=self.
schema, isSingleFrame=
True)
434 if self.config.doApCorr:
435 self.makeSubtask(
'applyApCorr', schema=self.
schema)
436 self.makeSubtask(
'catalogCalculation', schema=self.
schema)
438 if self.config.doAstrometry:
439 self.makeSubtask(
"astrometry", refObjLoader=astromRefObjLoader,
441 if self.config.doPhotoCal:
442 self.makeSubtask(
"photoCal", refObjLoader=photoRefObjLoader,
444 if self.config.doComputeSummaryStats:
445 self.makeSubtask(
'computeSummaryStats')
447 if initInputs
is not None and (astromRefObjLoader
is not None or photoRefObjLoader
is not None):
448 raise RuntimeError(
"PipelineTask form of this task should not be initialized with "
449 "reference object loaders.")
454 self.
schema.checkUnits(parse_strict=self.config.checkUnitsParseStrict)
456 sourceCatSchema = afwTable.SourceCatalog(self.
schema)
461 inputs = butlerQC.get(inputRefs)
462 inputs[
'idGenerator'] = self.config.idGenerator.apply(butlerQC.quantum.dataId)
464 if self.config.doAstrometry:
465 refObjLoader = ReferenceObjectLoader(dataIds=[ref.datasetRef.dataId
466 for ref
in inputRefs.astromRefCat],
467 refCats=inputs.pop(
'astromRefCat'),
468 name=self.config.connections.astromRefCat,
469 config=self.config.astromRefObjLoader, log=self.log)
470 self.astrometry.setRefObjLoader(refObjLoader)
472 if self.config.doPhotoCal:
473 photoRefObjLoader = ReferenceObjectLoader(dataIds=[ref.datasetRef.dataId
474 for ref
in inputRefs.photoRefCat],
475 refCats=inputs.pop(
'photoRefCat'),
476 name=self.config.connections.photoRefCat,
477 config=self.config.photoRefObjLoader,
479 self.photoCal.match.setRefObjLoader(photoRefObjLoader)
481 outputs = self.
run(**inputs)
483 if self.config.doWriteMatches
and self.config.doAstrometry:
484 if outputs.astromMatches
is not None:
485 normalizedMatches = afwTable.packMatches(outputs.astromMatches)
486 normalizedMatches.table.setMetadata(outputs.matchMeta)
487 if self.config.doWriteMatchesDenormalized:
488 denormMatches = denormalizeMatches(outputs.astromMatches, outputs.matchMeta)
489 outputs.matchesDenormalized = denormMatches
490 outputs.matches = normalizedMatches
492 del outputRefs.matches
493 if self.config.doWriteMatchesDenormalized:
494 del outputRefs.matchesDenormalized
495 butlerQC.put(outputs, outputRefs)
498 def run(self, exposure, exposureIdInfo=None, background=None,
499 icSourceCat=None, idGenerator=None):
500 """Calibrate an exposure.
504 exposure : `lsst.afw.image.ExposureF`
505 Exposure to calibrate.
506 exposureIdInfo : `lsst.obs.baseExposureIdInfo`, optional
507 Exposure ID info. Deprecated in favor of ``idGenerator``, and
508 ignored if that is provided.
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 if exposureIdInfo
is not None:
540 idGenerator = IdGenerator._from_exposure_id_info(exposureIdInfo)
542 idGenerator = IdGenerator()
544 if background
is None:
545 background = BackgroundList()
546 table = SourceTable.make(self.
schema, idGenerator.make_table_id_factory())
549 detRes = self.detection.run(table=table, exposure=exposure,
551 sourceCat = detRes.sources
552 if detRes.background:
553 for bg
in detRes.background:
554 background.append(bg)
555 if self.config.doSkySources:
556 skySourceFootprints = self.skySources.run(mask=exposure.mask, seed=idGenerator.catalog_id)
557 if skySourceFootprints:
558 for foot
in skySourceFootprints:
559 s = sourceCat.addNew()
562 if self.config.doDeblend:
563 self.deblend.run(exposure=exposure, sources=sourceCat)
564 self.measurement.run(
567 exposureId=idGenerator.catalog_id,
569 if self.config.doApCorr:
570 apCorrMap = exposure.getInfo().getApCorrMap()
571 if apCorrMap
is None:
572 self.log.warning(
"Image does not have valid aperture correction map for %r; "
573 "skipping aperture correction", idGenerator)
575 self.applyApCorr.run(
579 self.catalogCalculation.run(sourceCat)
581 self.setPrimaryFlags.run(sourceCat)
583 if icSourceCat
is not None and \
584 len(self.config.icSourceFieldsToCopy) > 0:
592 if not sourceCat.isContiguous():
593 sourceCat = sourceCat.copy(deep=
True)
599 if self.config.doAstrometry:
600 astromRes = self.astrometry.run(
604 astromMatches = astromRes.matches
605 matchMeta = astromRes.matchMeta
606 if exposure.getWcs()
is None:
607 if self.config.requireAstrometry:
608 raise RuntimeError(f
"WCS fit failed for {idGenerator} and requireAstrometry "
611 self.log.warning(
"Unable to perform astrometric calibration for %r but "
612 "requireAstrometry is False: attempting to proceed...",
616 if self.config.doPhotoCal:
617 if np.all(np.isnan(sourceCat[
"coord_ra"]))
or np.all(np.isnan(sourceCat[
"coord_dec"])):
618 if self.config.requirePhotoCal:
619 raise RuntimeError(f
"Astrometry failed for {idGenerator}, so cannot do "
620 "photoCal, but requirePhotoCal is True.")
621 self.log.warning(
"Astrometry failed for %r, so cannot do photoCal. requirePhotoCal "
622 "is False, so skipping photometric calibration and setting photoCalib "
623 "to None. Attempting to proceed...", idGenerator)
624 exposure.setPhotoCalib(
None)
628 photoRes = self.photoCal.run(
629 exposure, sourceCat=sourceCat, expId=idGenerator.catalog_id
631 exposure.setPhotoCalib(photoRes.photoCalib)
634 self.log.info(
"Photometric zero-point: %f",
635 photoRes.photoCalib.instFluxToMagnitude(1.0))
636 self.
setMetadata(exposure=exposure, photoRes=photoRes)
637 except Exception
as e:
638 if self.config.requirePhotoCal:
640 self.log.warning(
"Unable to perform photometric calibration "
641 "(%s): attempting to proceed", e)
644 self.postCalibrationMeasurement.run(
647 exposureId=idGenerator.catalog_id,
650 if self.config.doComputeSummaryStats:
651 summary = self.computeSummaryStats.run(exposure=exposure,
653 background=background)
654 exposure.getInfo().setSummaryStats(summary)
656 frame = getDebugFrame(self._display,
"calibrate")
661 matches=astromMatches,
666 return pipeBase.Struct(
668 astromMatches=astromMatches,
670 outputExposure=exposure,
672 outputBackground=background,
712 """Match sources in an icSourceCat and a sourceCat and copy fields.
714 The fields copied are those specified by
715 ``config.icSourceFieldsToCopy``.
719 icSourceCat : `lsst.afw.table.SourceCatalog`
720 Catalog from which to copy fields.
721 sourceCat : `lsst.afw.table.SourceCatalog`
722 Catalog to which to copy fields.
727 Raised if any of the following occur:
728 - icSourceSchema and icSourceKeys are not specified.
729 - icSourceCat and sourceCat are not specified.
730 - icSourceFieldsToCopy is empty.
733 raise RuntimeError(
"To copy icSource fields you must specify "
734 "icSourceSchema and icSourceKeys when "
735 "constructing this task")
736 if icSourceCat
is None or sourceCat
is None:
737 raise RuntimeError(
"icSourceCat and sourceCat must both be "
739 if len(self.config.icSourceFieldsToCopy) == 0:
740 self.log.warning(
"copyIcSourceFields doing nothing because "
741 "icSourceFieldsToCopy is empty")
744 mc = afwTable.MatchControl()
745 mc.findOnlyClosest =
False
746 matches = afwTable.matchXy(icSourceCat, sourceCat,
747 self.config.matchRadiusPix, mc)
748 if self.config.doDeblend:
749 deblendKey = sourceCat.schema[
"deblend_nChild"].asKey()
751 matches = [m
for m
in matches
if m[1].get(deblendKey) == 0]
758 for m0, m1, d
in matches:
760 match = bestMatches.get(id0)
761 if match
is None or d <= match[2]:
762 bestMatches[id0] = (m0, m1, d)
763 matches = list(bestMatches.values())
768 numMatches = len(matches)
769 numUniqueSources = len(set(m[1].getId()
for m
in matches))
770 if numUniqueSources != numMatches:
771 self.log.warning(
"%d icSourceCat sources matched only %d sourceCat "
772 "sources", numMatches, numUniqueSources)
774 self.log.info(
"Copying flags from icSourceCat to sourceCat for "
775 "%d sources", numMatches)
779 for icSrc, src, d
in matches:
785 icSrcFootprint = icSrc.getFootprint()
787 icSrc.setFootprint(src.getFootprint())
790 icSrc.setFootprint(icSrcFootprint)