589 def compute_psf_and_ap_corr_map(self, visit, detector, exposure, src, isolated_source_table):
590 """Compute psf model and aperture correction map for a single exposure.
591
592 Parameters
593 ----------
594 visit : `int`
595 Visit number (for logging).
596 detector : `int`
597 Detector number (for logging).
598 exposure : `lsst.afw.image.ExposureF`
599 src : `lsst.afw.table.SourceCatalog`
600 isolated_source_table : `np.ndarray`
601
602 Returns
603 -------
604 psf : `lsst.meas.algorithms.ImagePsf`
605 PSF Model
606 ap_corr_map : `lsst.afw.image.ApCorrMap`
607 Aperture correction map.
608 measured_src : `lsst.afw.table.SourceCatalog`
609 Updated source catalog with measurements, flags and aperture corrections.
610 """
611
612 good_src = self.source_selector.selectSources(src)
613 if sum(good_src.selected) == 0:
614 self.log.warning('No good sources remain after cuts for visit %d, detector %d',
615 visit, detector)
616 return None, None, None
617
618
619
620
621
622
623 selection_mapper, selection_schema = self._make_selection_schema_mapper(src.schema)
624
626 selected_src.reserve(good_src.selected.sum())
627 selected_src.extend(src[good_src.selected], mapper=selection_mapper)
628
629
630
631 selected_src['calib_psf_candidate'] = np.zeros(len(selected_src), dtype=bool)
632 selected_src['calib_psf_used'] = np.zeros(len(selected_src), dtype=bool)
633 selected_src['calib_psf_reserved'] = np.zeros(len(selected_src), dtype=bool)
634
635
636 matched_src, matched_iso = esutil.numpy_util.match(
637 selected_src['id'],
638 isolated_source_table[self.config.id_column]
639 )
640
641 matched_arr = np.zeros(len(selected_src), dtype=bool)
642 matched_arr[matched_src] = True
643 selected_src['calib_psf_candidate'] = matched_arr
644
645 reserved_arr = np.zeros(len(selected_src), dtype=bool)
646 reserved_arr[matched_src] = isolated_source_table['reserved'][matched_iso]
647 selected_src['calib_psf_reserved'] = reserved_arr
648
649 selected_src = selected_src[selected_src['calib_psf_candidate']].copy(deep=True)
650
651
653 measured_src.reserve(len(selected_src))
654 measured_src.extend(selected_src, mapper=self.schema_mapper)
655
656
657 measured_src['calib_psf_candidate'] = selected_src['calib_psf_candidate']
658 measured_src['calib_psf_reserved'] = selected_src['calib_psf_reserved']
659
660
661 try:
662 psf_selection_result = self.make_psf_candidates.run(selected_src, exposure=exposure)
663 except Exception as e:
664 self.log.warning('Failed to make psf candidates for visit %d, detector %d: %s',
665 visit, detector, e)
666 return None, None, measured_src
667
668 psf_cand_cat = psf_selection_result.goodStarCat
669
670
671
672 psf_determiner_list = [cand for cand, use
673 in zip(psf_selection_result.psfCandidates,
674 ~psf_cand_cat['calib_psf_reserved']) if use]
675 flag_key = psf_cand_cat.schema['calib_psf_used'].asKey()
676 try:
677 psf, cell_set = self.psf_determiner.determinePsf(exposure,
678 psf_determiner_list,
679 self.metadata,
680 flagKey=flag_key)
681 except Exception as e:
682 self.log.warning('Failed to determine psf for visit %d, detector %d: %s',
683 visit, detector, e)
684 return None, None, measured_src
685
686
687 exposure.setPsf(psf)
688
689
690
691 matched_selected, matched_measured = esutil.numpy_util.match(
692 selected_src['id'],
693 measured_src['id']
694 )
695 measured_used = np.zeros(len(measured_src), dtype=bool)
696 measured_used[matched_measured] = selected_src['calib_psf_used'][matched_selected]
697 measured_src['calib_psf_used'] = measured_used
698
699
700 try:
701 self.measurement.run(measCat=measured_src, exposure=exposure)
702 except Exception as e:
703 self.log.warning('Failed to make measurements for visit %d, detector %d: %s',
704 visit, detector, e)
705 return psf, None, measured_src
706
707
708 try:
709 ap_corr_map = self.measure_ap_corr.run(exposure=exposure,
710 catalog=measured_src).apCorrMap
711 except Exception as e:
712 self.log.warning('Failed to compute aperture corrections for visit %d, detector %d: %s',
713 visit, detector, e)
714 return psf, None, measured_src
715
716 self.apply_ap_corr.run(catalog=measured_src, apCorrMap=ap_corr_map)
717
718 return psf, ap_corr_map, measured_src