254 input_handle_dict = butlerQC.get(inputRefs)
256 band = butlerQC.quantum.dataId[
'band']
257 visit = butlerQC.quantum.dataId[
'visit']
259 src_dict_temp = {handle.dataId[
'detector']: handle
260 for handle
in input_handle_dict[
'srcs']}
261 calexp_dict_temp = {handle.dataId[
'detector']: handle
262 for handle
in input_handle_dict[
'calexps']}
263 isolated_star_cat_dict_temp = {handle.dataId[
'tract']: handle
264 for handle
in input_handle_dict[
'isolated_star_cats']}
265 isolated_star_source_dict_temp = {handle.dataId[
'tract']: handle
266 for handle
in input_handle_dict[
'isolated_star_sources']}
269 src_dict = {detector: src_dict_temp[detector]
for
270 detector
in sorted(src_dict_temp.keys())}
271 calexp_dict = {detector: calexp_dict_temp[detector]
for
272 detector
in sorted(calexp_dict_temp.keys())}
273 isolated_star_cat_dict = {tract: isolated_star_cat_dict_temp[tract]
for
274 tract
in sorted(isolated_star_cat_dict_temp.keys())}
275 isolated_star_source_dict = {tract: isolated_star_source_dict_temp[tract]
for
276 tract
in sorted(isolated_star_source_dict_temp.keys())}
278 struct = self.
run(visit,
280 isolated_star_cat_dict,
281 isolated_star_source_dict,
285 butlerQC.put(struct.psf_ap_corr_cat,
286 outputRefs.finalized_psf_ap_corr_cat)
287 butlerQC.put(pd.DataFrame(struct.output_table),
288 outputRefs.finalized_src_table)
290 def run(self, visit, band, isolated_star_cat_dict, isolated_star_source_dict, src_dict, calexp_dict):
292 Run the FinalizeCharacterizationTask.
297 Visit number. Used in the output catalogs.
299 Band name. Used to select reserved stars.
300 isolated_star_cat_dict : `dict`
301 Per-tract dict of isolated star catalog handles.
302 isolated_star_source_dict : `dict`
303 Per-tract dict of isolated star source catalog handles.
305 Per-detector dict of src catalog handles.
307 Per-detector dict of calibrated exposure handles.
311 struct : `lsst.pipe.base.struct`
312 Struct with outputs for persistence.
318 isolated_star_cat_dict,
319 isolated_star_source_dict
322 exposure_cat_schema = afwTable.ExposureTable.makeMinimalSchema()
323 exposure_cat_schema.addField(
'visit', type=
'L', doc=
'Visit number')
325 metadata = dafBase.PropertyList()
326 metadata.add(
"COMMENT",
"Catalog id is detector id, sorted.")
327 metadata.add(
"COMMENT",
"Only detectors with data have entries.")
329 psf_ap_corr_cat = afwTable.ExposureCatalog(exposure_cat_schema)
330 psf_ap_corr_cat.setMetadata(metadata)
332 measured_src_tables = []
334 for detector
in src_dict:
335 src = src_dict[detector].get()
336 exposure = calexp_dict[detector].get()
343 isolated_source_table
347 record = psf_ap_corr_cat.addNew()
348 record[
'id'] = int(detector)
349 record[
'visit'] = visit
352 if ap_corr_map
is not None:
353 record.setApCorrMap(ap_corr_map)
355 measured_src[
'visit'][:] = visit
356 measured_src[
'detector'][:] = detector
358 measured_src_tables.append(measured_src.asAstropy().as_array())
360 measured_src_table = np.concatenate(measured_src_tables)
362 return pipeBase.Struct(psf_ap_corr_cat=psf_ap_corr_cat,
363 output_table=measured_src_table)
366 """Make the schema mapper from the input schema to the output schema.
370 input_schema : `lsst.afw.table.Schema`
375 mapper : `lsst.afw.table.SchemaMapper`
377 output_schema : `lsst.afw.table.Schema`
378 Output schema (with alias map)
380 mapper = afwTable.SchemaMapper(input_schema)
381 mapper.addMinimalSchema(afwTable.SourceTable.makeMinimalSchema())
382 mapper.addMapping(input_schema[
'slot_Centroid_x'].asKey())
383 mapper.addMapping(input_schema[
'slot_Centroid_y'].asKey())
386 aper_fields = input_schema.extract(
'base_CircularApertureFlux_*')
387 for field, item
in aper_fields.items():
388 mapper.addMapping(item.key)
391 apflux_fields = input_schema.extract(
'slot_ApFlux_*')
392 for field, item
in apflux_fields.items():
393 mapper.addMapping(item.key)
395 calibflux_fields = input_schema.extract(
'slot_CalibFlux_*')
396 for field, item
in calibflux_fields.items():
397 mapper.addMapping(item.key)
400 input_schema[self.config.source_selector.active.signalToNoise.fluxField].asKey(),
401 'calib_psf_selection_flux')
403 input_schema[self.config.source_selector.active.signalToNoise.errField].asKey(),
404 'calib_psf_selection_flux_err')
406 output_schema = mapper.getOutputSchema()
408 output_schema.addField(
409 'calib_psf_candidate',
411 doc=(
'set if the source was a candidate for PSF determination, '
412 'as determined from FinalizeCharacterizationTask.'),
414 output_schema.addField(
415 'calib_psf_reserved',
417 doc=(
'set if source was reserved from PSF determination by '
418 'FinalizeCharacterizationTask.'),
420 output_schema.addField(
423 doc=(
'set if source was used in the PSF determination by '
424 'FinalizeCharacterizationTask.'),
426 output_schema.addField(
429 doc=
'Visit number for the sources.',
431 output_schema.addField(
434 doc=
'Detector number for the sources.',
437 alias_map = input_schema.getAliasMap()
438 alias_map_output = afwTable.AliasMap()
439 alias_map_output.set(
'slot_Centroid', alias_map.get(
'slot_Centroid'))
440 alias_map_output.set(
'slot_ApFlux', alias_map.get(
'slot_ApFlux'))
441 alias_map_output.set(
'slot_CalibFlux', alias_map.get(
'slot_CalibFlux'))
443 output_schema.setAliasMap(alias_map_output)
445 return mapper, output_schema
473 Concatenate isolated star catalogs and make reserve selection.
478 Band name. Used to select reserved stars.
479 isolated_star_cat_dict : `dict`
480 Per-tract dict of isolated star catalog handles.
481 isolated_star_source_dict : `dict`
482 Per-tract dict of isolated star source catalog handles.
486 isolated_table : `np.ndarray` (N,)
487 Table of isolated stars, with indexes to isolated sources.
488 isolated_source_table : `np.ndarray` (M,)
489 Table of isolated sources, with indexes to isolated stars.
492 isolated_sources = []
493 merge_cat_counter = 0
494 merge_source_counter = 0
496 for tract
in isolated_star_cat_dict:
497 df_cat = isolated_star_cat_dict[tract].get()
498 table_cat = df_cat.to_records()
500 df_source = isolated_star_source_dict[tract].get(
501 parameters={
'columns': [self.config.id_column,
504 table_source = df_source.to_records()
507 (use_band,) = (table_cat[f
'nsource_{band}'] > 0).nonzero()
509 if len(use_band) == 0:
511 self.log.info(
"No sources found in %s band in tract %d.", band, tract)
516 obj_index = table_source[
'obj_index'][:]
517 a, b = esutil.numpy_util.match(use_band, obj_index)
520 table_source[
'obj_index'][b] = a
521 _, index_new = np.unique(a, return_index=
True)
522 table_cat[f
'source_cat_index_{band}'][use_band] = index_new
533 table_source = table_source[b]
534 table_cat = table_cat[use_band]
537 table_cat = np.lib.recfunctions.append_fields(
540 np.zeros(table_cat.size, dtype=bool),
543 table_source = np.lib.recfunctions.append_fields(
546 np.zeros(table_source.size, dtype=bool),
551 table_cat[
'reserved'][:] = self.reserve_selection.run(
553 extra=f
'{band}_{tract}',
555 table_source[
'reserved'][:] = table_cat[
'reserved'][table_source[
'obj_index']]
558 table_cat[f
'source_cat_index_{band}'] += merge_source_counter
559 table_source[
'obj_index'] += merge_cat_counter
561 isolated_tables.append(table_cat)
562 isolated_sources.append(table_source)
564 merge_cat_counter += len(table_cat)
565 merge_source_counter += len(table_source)
567 isolated_table = np.concatenate(isolated_tables)
568 isolated_source_table = np.concatenate(isolated_sources)
570 return isolated_table, isolated_source_table
573 """Compute psf model and aperture correction map for a single exposure.
578 Visit number (for logging).
580 Detector number (for logging).
581 exposure : `lsst.afw.image.ExposureF`
582 src : `lsst.afw.table.SourceCatalog`
583 isolated_source_table : `np.ndarray`
587 psf : `lsst.meas.algorithms.ImagePsf`
589 ap_corr_map : `lsst.afw.image.ApCorrMap`
590 Aperture correction map.
591 measured_src : `lsst.afw.table.SourceCatalog`
592 Updated source catalog with measurements, flags and aperture corrections.
595 good_src = self.source_selector.selectSources(src)
604 selected_src = afwTable.SourceCatalog(selection_schema)
605 selected_src.reserve(good_src.selected.sum())
606 selected_src.extend(src[good_src.selected], mapper=selection_mapper)
610 selected_src[
'calib_psf_candidate'] = np.zeros(len(selected_src), dtype=bool)
611 selected_src[
'calib_psf_used'] = np.zeros(len(selected_src), dtype=bool)
612 selected_src[
'calib_psf_reserved'] = np.zeros(len(selected_src), dtype=bool)
615 matched_src, matched_iso = esutil.numpy_util.match(
617 isolated_source_table[self.config.id_column]
620 matched_arr = np.zeros(len(selected_src), dtype=bool)
621 matched_arr[matched_src] =
True
622 selected_src[
'calib_psf_candidate'] = matched_arr
624 reserved_arr = np.zeros(len(selected_src), dtype=bool)
625 reserved_arr[matched_src] = isolated_source_table[
'reserved'][matched_iso]
626 selected_src[
'calib_psf_reserved'] = reserved_arr
628 selected_src = selected_src[selected_src[
'calib_psf_candidate']].copy(deep=
True)
631 measured_src = afwTable.SourceCatalog(self.
schema)
632 measured_src.reserve(len(selected_src))
636 measured_src[
'calib_psf_candidate'] = selected_src[
'calib_psf_candidate']
637 measured_src[
'calib_psf_reserved'] = selected_src[
'calib_psf_reserved']
641 psf_selection_result = self.make_psf_candidates.run(selected_src, exposure=exposure)
642 except Exception
as e:
643 self.log.warning(
'Failed to make psf candidates for visit %d, detector %d: %s',
645 return None,
None, measured_src
647 psf_cand_cat = psf_selection_result.goodStarCat
651 psf_determiner_list = [cand
for cand, use
652 in zip(psf_selection_result.psfCandidates,
653 ~psf_cand_cat[
'calib_psf_reserved'])
if use]
654 flag_key = psf_cand_cat.schema[
'calib_psf_used'].asKey()
656 psf, cell_set = self.psf_determiner.determinePsf(exposure,
660 except Exception
as e:
661 self.log.warning(
'Failed to determine psf for visit %d, detector %d: %s',
663 return None,
None, measured_src
670 matched_selected, matched_measured = esutil.numpy_util.match(
674 measured_used = np.zeros(len(measured_src), dtype=bool)
675 measured_used[matched_measured] = selected_src[
'calib_psf_used'][matched_selected]
676 measured_src[
'calib_psf_used'] = measured_used
680 self.measurement.run(measCat=measured_src, exposure=exposure)
681 except Exception
as e:
682 self.log.warning(
'Failed to make measurements for visit %d, detector %d: %s',
684 return psf,
None, measured_src
688 ap_corr_map = self.measure_ap_corr.run(exposure=exposure,
689 catalog=measured_src).apCorrMap
690 except Exception
as e:
691 self.log.warning(
'Failed to compute aperture corrections for visit %d, detector %d: %s',
693 return psf,
None, measured_src
695 self.apply_ap_corr.run(catalog=measured_src, apCorrMap=ap_corr_map)
697 return psf, ap_corr_map, measured_src