lsst.ip.isr 22.0.1-31-g9cb7513+173247a6f9
isrTask.py
Go to the documentation of this file.
1# This file is part of ip_isr.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22import math
23import numpy
24
25import lsst.geom
26import lsst.afw.image as afwImage
27import lsst.afw.math as afwMath
28import lsst.pex.config as pexConfig
29import lsst.pipe.base as pipeBase
30import lsst.pipe.base.connectionTypes as cT
31
32from contextlib import contextmanager
33from lsstDebug import getDebugFrame
34
35from lsst.afw.cameraGeom import NullLinearityType, ReadoutCorner
36from lsst.afw.display import getDisplay
37from lsst.daf.persistence import ButlerDataRef
38from lsst.daf.persistence.butler import NoResults
39from lsst.meas.algorithms.detection import SourceDetectionTask
40from lsst.utils.timer import timeMethod
41
42from . import isrFunctions
43from . import isrQa
44from . import linearize
45from .defects import Defects
46
47from .assembleCcdTask import AssembleCcdTask
48from .crosstalk import CrosstalkTask, CrosstalkCalib
49from .fringe import FringeTask
50from .isr import maskNans
51from .masking import MaskingTask
52from .overscan import OverscanCorrectionTask
53from .straylight import StrayLightTask
54from .vignette import VignetteTask
55from .ampOffset import AmpOffsetTask
56from lsst.daf.butler import DimensionGraph
57
58
59__all__ = ["IsrTask", "IsrTaskConfig", "RunIsrTask", "RunIsrConfig"]
60
61
62def crosstalkSourceLookup(datasetType, registry, quantumDataId, collections):
63 """Lookup function to identify crosstalkSource entries.
64
65 This should return an empty list under most circumstances. Only
66 when inter-chip crosstalk has been identified should this be
67 populated.
68
69 Parameters
70 ----------
71 datasetType : `str`
72 Dataset to lookup.
73 registry : `lsst.daf.butler.Registry`
74 Butler registry to query.
75 quantumDataId : `lsst.daf.butler.ExpandedDataCoordinate`
76 Data id to transform to identify crosstalkSources. The
77 ``detector`` entry will be stripped.
78 collections : `lsst.daf.butler.CollectionSearch`
79 Collections to search through.
80
81 Returns
82 -------
83 results : `list` [`lsst.daf.butler.DatasetRef`]
84 List of datasets that match the query that will be used as
85 crosstalkSources.
86 """
87 newDataId = quantumDataId.subset(DimensionGraph(registry.dimensions, names=["instrument", "exposure"]))
88 results = set(registry.queryDatasets(datasetType, collections=collections, dataId=newDataId,
89 findFirst=True))
90 # In some contexts, calling `.expanded()` to expand all data IDs in the
91 # query results can be a lot faster because it vectorizes lookups. But in
92 # this case, expandDataId shouldn't need to hit the database at all in the
93 # steady state, because only the detector record is unknown and those are
94 # cached in the registry.
95 return [ref.expanded(registry.expandDataId(ref.dataId, records=newDataId.records)) for ref in results]
96
97
98class IsrTaskConnections(pipeBase.PipelineTaskConnections,
99 dimensions={"instrument", "exposure", "detector"},
100 defaultTemplates={}):
101 ccdExposure = cT.Input(
102 name="raw",
103 doc="Input exposure to process.",
104 storageClass="Exposure",
105 dimensions=["instrument", "exposure", "detector"],
106 )
107 camera = cT.PrerequisiteInput(
108 name="camera",
109 storageClass="Camera",
110 doc="Input camera to construct complete exposures.",
111 dimensions=["instrument"],
112 isCalibration=True,
113 )
114
115 crosstalk = cT.PrerequisiteInput(
116 name="crosstalk",
117 doc="Input crosstalk object",
118 storageClass="CrosstalkCalib",
119 dimensions=["instrument", "detector"],
120 isCalibration=True,
121 minimum=0, # can fall back to cameraGeom
122 )
123 crosstalkSources = cT.PrerequisiteInput(
124 name="isrOverscanCorrected",
125 doc="Overscan corrected input images.",
126 storageClass="Exposure",
127 dimensions=["instrument", "exposure", "detector"],
128 deferLoad=True,
129 multiple=True,
130 lookupFunction=crosstalkSourceLookup,
131 minimum=0, # not needed for all instruments, no config to control this
132 )
133 bias = cT.PrerequisiteInput(
134 name="bias",
135 doc="Input bias calibration.",
136 storageClass="ExposureF",
137 dimensions=["instrument", "detector"],
138 isCalibration=True,
139 )
140 dark = cT.PrerequisiteInput(
141 name='dark',
142 doc="Input dark calibration.",
143 storageClass="ExposureF",
144 dimensions=["instrument", "detector"],
145 isCalibration=True,
146 )
147 flat = cT.PrerequisiteInput(
148 name="flat",
149 doc="Input flat calibration.",
150 storageClass="ExposureF",
151 dimensions=["instrument", "physical_filter", "detector"],
152 isCalibration=True,
153 )
154 ptc = cT.PrerequisiteInput(
155 name="ptc",
156 doc="Input Photon Transfer Curve dataset",
157 storageClass="PhotonTransferCurveDataset",
158 dimensions=["instrument", "detector"],
159 isCalibration=True,
160 )
161 fringes = cT.PrerequisiteInput(
162 name="fringe",
163 doc="Input fringe calibration.",
164 storageClass="ExposureF",
165 dimensions=["instrument", "physical_filter", "detector"],
166 isCalibration=True,
167 minimum=0, # only needed for some bands, even when enabled
168 )
169 strayLightData = cT.PrerequisiteInput(
170 name='yBackground',
171 doc="Input stray light calibration.",
172 storageClass="StrayLightData",
173 dimensions=["instrument", "physical_filter", "detector"],
174 deferLoad=True,
175 isCalibration=True,
176 minimum=0, # only needed for some bands, even when enabled
177 )
178 bfKernel = cT.PrerequisiteInput(
179 name='bfKernel',
180 doc="Input brighter-fatter kernel.",
181 storageClass="NumpyArray",
182 dimensions=["instrument"],
183 isCalibration=True,
184 minimum=0, # can use either bfKernel or newBFKernel
185 )
186 newBFKernel = cT.PrerequisiteInput(
187 name='brighterFatterKernel',
188 doc="Newer complete kernel + gain solutions.",
189 storageClass="BrighterFatterKernel",
190 dimensions=["instrument", "detector"],
191 isCalibration=True,
192 minimum=0, # can use either bfKernel or newBFKernel
193 )
194 defects = cT.PrerequisiteInput(
195 name='defects',
196 doc="Input defect tables.",
197 storageClass="Defects",
198 dimensions=["instrument", "detector"],
199 isCalibration=True,
200 )
201 linearizer = cT.PrerequisiteInput(
202 name='linearizer',
203 storageClass="Linearizer",
204 doc="Linearity correction calibration.",
205 dimensions=["instrument", "detector"],
206 isCalibration=True,
207 minimum=0, # can fall back to cameraGeom
208 )
209 opticsTransmission = cT.PrerequisiteInput(
210 name="transmission_optics",
211 storageClass="TransmissionCurve",
212 doc="Transmission curve due to the optics.",
213 dimensions=["instrument"],
214 isCalibration=True,
215 )
216 filterTransmission = cT.PrerequisiteInput(
217 name="transmission_filter",
218 storageClass="TransmissionCurve",
219 doc="Transmission curve due to the filter.",
220 dimensions=["instrument", "physical_filter"],
221 isCalibration=True,
222 )
223 sensorTransmission = cT.PrerequisiteInput(
224 name="transmission_sensor",
225 storageClass="TransmissionCurve",
226 doc="Transmission curve due to the sensor.",
227 dimensions=["instrument", "detector"],
228 isCalibration=True,
229 )
230 atmosphereTransmission = cT.PrerequisiteInput(
231 name="transmission_atmosphere",
232 storageClass="TransmissionCurve",
233 doc="Transmission curve due to the atmosphere.",
234 dimensions=["instrument"],
235 isCalibration=True,
236 )
237 illumMaskedImage = cT.PrerequisiteInput(
238 name="illum",
239 doc="Input illumination correction.",
240 storageClass="MaskedImageF",
241 dimensions=["instrument", "physical_filter", "detector"],
242 isCalibration=True,
243 )
244
245 outputExposure = cT.Output(
246 name='postISRCCD',
247 doc="Output ISR processed exposure.",
248 storageClass="Exposure",
249 dimensions=["instrument", "exposure", "detector"],
250 )
251 preInterpExposure = cT.Output(
252 name='preInterpISRCCD',
253 doc="Output ISR processed exposure, with pixels left uninterpolated.",
254 storageClass="ExposureF",
255 dimensions=["instrument", "exposure", "detector"],
256 )
257 outputOssThumbnail = cT.Output(
258 name="OssThumb",
259 doc="Output Overscan-subtracted thumbnail image.",
260 storageClass="Thumbnail",
261 dimensions=["instrument", "exposure", "detector"],
262 )
263 outputFlattenedThumbnail = cT.Output(
264 name="FlattenedThumb",
265 doc="Output flat-corrected thumbnail image.",
266 storageClass="Thumbnail",
267 dimensions=["instrument", "exposure", "detector"],
268 )
269
270 def __init__(self, *, config=None):
271 super().__init__(config=config)
272
273 if config.doBias is not True:
274 self.prerequisiteInputs.discard("bias")
275 if config.doLinearize is not True:
276 self.prerequisiteInputs.discard("linearizer")
277 if config.doCrosstalk is not True:
278 self.prerequisiteInputs.discard("crosstalkSources")
279 self.prerequisiteInputs.discard("crosstalk")
280 if config.doBrighterFatter is not True:
281 self.prerequisiteInputs.discard("bfKernel")
282 self.prerequisiteInputs.discard("newBFKernel")
283 if config.doDefect is not True:
284 self.prerequisiteInputs.discard("defects")
285 if config.doDark is not True:
286 self.prerequisiteInputs.discard("dark")
287 if config.doFlat is not True:
288 self.prerequisiteInputs.discard("flat")
289 if config.doFringe is not True:
290 self.prerequisiteInputs.discard("fringe")
291 if config.doStrayLight is not True:
292 self.prerequisiteInputs.discard("strayLightData")
293 if config.usePtcGains is not True and config.usePtcReadNoise is not True:
294 self.prerequisiteInputs.discard("ptc")
295 if config.doAttachTransmissionCurve is not True:
296 self.prerequisiteInputs.discard("opticsTransmission")
297 self.prerequisiteInputs.discard("filterTransmission")
298 self.prerequisiteInputs.discard("sensorTransmission")
299 self.prerequisiteInputs.discard("atmosphereTransmission")
300 if config.doUseOpticsTransmission is not True:
301 self.prerequisiteInputs.discard("opticsTransmission")
302 if config.doUseFilterTransmission is not True:
303 self.prerequisiteInputs.discard("filterTransmission")
304 if config.doUseSensorTransmission is not True:
305 self.prerequisiteInputs.discard("sensorTransmission")
306 if config.doUseAtmosphereTransmission is not True:
307 self.prerequisiteInputs.discard("atmosphereTransmission")
308 if config.doIlluminationCorrection is not True:
309 self.prerequisiteInputs.discard("illumMaskedImage")
310
311 if config.doWrite is not True:
312 self.outputs.discard("outputExposure")
313 self.outputs.discard("preInterpExposure")
314 self.outputs.discard("outputFlattenedThumbnail")
315 self.outputs.discard("outputOssThumbnail")
316 if config.doSaveInterpPixels is not True:
317 self.outputs.discard("preInterpExposure")
318 if config.qa.doThumbnailOss is not True:
319 self.outputs.discard("outputOssThumbnail")
320 if config.qa.doThumbnailFlattened is not True:
321 self.outputs.discard("outputFlattenedThumbnail")
322
323
324class IsrTaskConfig(pipeBase.PipelineTaskConfig,
325 pipelineConnections=IsrTaskConnections):
326 """Configuration parameters for IsrTask.
327
328 Items are grouped in the order in which they are executed by the task.
329 """
330 datasetType = pexConfig.Field(
331 dtype=str,
332 doc="Dataset type for input data; users will typically leave this alone, "
333 "but camera-specific ISR tasks will override it",
334 default="raw",
335 )
336
337 fallbackFilterName = pexConfig.Field(
338 dtype=str,
339 doc="Fallback default filter name for calibrations.",
340 optional=True
341 )
342 useFallbackDate = pexConfig.Field(
343 dtype=bool,
344 doc="Pass observation date when using fallback filter.",
345 default=False,
346 )
347 expectWcs = pexConfig.Field(
348 dtype=bool,
349 default=True,
350 doc="Expect input science images to have a WCS (set False for e.g. spectrographs)."
351 )
352 fwhm = pexConfig.Field(
353 dtype=float,
354 doc="FWHM of PSF in arcseconds.",
355 default=1.0,
356 )
357 qa = pexConfig.ConfigField(
358 dtype=isrQa.IsrQaConfig,
359 doc="QA related configuration options.",
360 )
361
362 # Image conversion configuration
363 doConvertIntToFloat = pexConfig.Field(
364 dtype=bool,
365 doc="Convert integer raw images to floating point values?",
366 default=True,
367 )
368
369 # Saturated pixel handling.
370 doSaturation = pexConfig.Field(
371 dtype=bool,
372 doc="Mask saturated pixels? NB: this is totally independent of the"
373 " interpolation option - this is ONLY setting the bits in the mask."
374 " To have them interpolated make sure doSaturationInterpolation=True",
375 default=True,
376 )
377 saturatedMaskName = pexConfig.Field(
378 dtype=str,
379 doc="Name of mask plane to use in saturation detection and interpolation",
380 default="SAT",
381 )
382 saturation = pexConfig.Field(
383 dtype=float,
384 doc="The saturation level to use if no Detector is present in the Exposure (ignored if NaN)",
385 default=float("NaN"),
386 )
387 growSaturationFootprintSize = pexConfig.Field(
388 dtype=int,
389 doc="Number of pixels by which to grow the saturation footprints",
390 default=1,
391 )
392
393 # Suspect pixel handling.
394 doSuspect = pexConfig.Field(
395 dtype=bool,
396 doc="Mask suspect pixels?",
397 default=False,
398 )
399 suspectMaskName = pexConfig.Field(
400 dtype=str,
401 doc="Name of mask plane to use for suspect pixels",
402 default="SUSPECT",
403 )
404 numEdgeSuspect = pexConfig.Field(
405 dtype=int,
406 doc="Number of edge pixels to be flagged as untrustworthy.",
407 default=0,
408 )
409 edgeMaskLevel = pexConfig.ChoiceField(
410 dtype=str,
411 doc="Mask edge pixels in which coordinate frame: DETECTOR or AMP?",
412 default="DETECTOR",
413 allowed={
414 'DETECTOR': 'Mask only the edges of the full detector.',
415 'AMP': 'Mask edges of each amplifier.',
416 },
417 )
418
419 # Initial masking options.
420 doSetBadRegions = pexConfig.Field(
421 dtype=bool,
422 doc="Should we set the level of all BAD patches of the chip to the chip's average value?",
423 default=True,
424 )
425 badStatistic = pexConfig.ChoiceField(
426 dtype=str,
427 doc="How to estimate the average value for BAD regions.",
428 default='MEANCLIP',
429 allowed={
430 "MEANCLIP": "Correct using the (clipped) mean of good data",
431 "MEDIAN": "Correct using the median of the good data",
432 },
433 )
434
435 # Overscan subtraction configuration.
436 doOverscan = pexConfig.Field(
437 dtype=bool,
438 doc="Do overscan subtraction?",
439 default=True,
440 )
441 overscan = pexConfig.ConfigurableField(
442 target=OverscanCorrectionTask,
443 doc="Overscan subtraction task for image segments.",
444 )
445 overscanFitType = pexConfig.ChoiceField(
446 dtype=str,
447 doc="The method for fitting the overscan bias level.",
448 default='MEDIAN',
449 allowed={
450 "POLY": "Fit ordinary polynomial to the longest axis of the overscan region",
451 "CHEB": "Fit Chebyshev polynomial to the longest axis of the overscan region",
452 "LEG": "Fit Legendre polynomial to the longest axis of the overscan region",
453 "NATURAL_SPLINE": "Fit natural spline to the longest axis of the overscan region",
454 "CUBIC_SPLINE": "Fit cubic spline to the longest axis of the overscan region",
455 "AKIMA_SPLINE": "Fit Akima spline to the longest axis of the overscan region",
456 "MEAN": "Correct using the mean of the overscan region",
457 "MEANCLIP": "Correct using a clipped mean of the overscan region",
458 "MEDIAN": "Correct using the median of the overscan region",
459 "MEDIAN_PER_ROW": "Correct using the median per row of the overscan region",
460 },
461 deprecated=("Please configure overscan via the OverscanCorrectionConfig interface."
462 " This option will no longer be used, and will be removed after v20.")
463 )
464 overscanOrder = pexConfig.Field(
465 dtype=int,
466 doc=("Order of polynomial or to fit if overscan fit type is a polynomial, "
467 "or number of spline knots if overscan fit type is a spline."),
468 default=1,
469 deprecated=("Please configure overscan via the OverscanCorrectionConfig interface."
470 " This option will no longer be used, and will be removed after v20.")
471 )
472 overscanNumSigmaClip = pexConfig.Field(
473 dtype=float,
474 doc="Rejection threshold (sigma) for collapsing overscan before fit",
475 default=3.0,
476 deprecated=("Please configure overscan via the OverscanCorrectionConfig interface."
477 " This option will no longer be used, and will be removed after v20.")
478 )
479 overscanIsInt = pexConfig.Field(
480 dtype=bool,
481 doc="Treat overscan as an integer image for purposes of overscan.FitType=MEDIAN"
482 " and overscan.FitType=MEDIAN_PER_ROW.",
483 default=True,
484 deprecated=("Please configure overscan via the OverscanCorrectionConfig interface."
485 " This option will no longer be used, and will be removed after v20.")
486 )
487 # These options do not get deprecated, as they define how we slice up the
488 # image data.
489 overscanNumLeadingColumnsToSkip = pexConfig.Field(
490 dtype=int,
491 doc="Number of columns to skip in overscan, i.e. those closest to amplifier",
492 default=0,
493 )
494 overscanNumTrailingColumnsToSkip = pexConfig.Field(
495 dtype=int,
496 doc="Number of columns to skip in overscan, i.e. those farthest from amplifier",
497 default=0,
498 )
499 overscanMaxDev = pexConfig.Field(
500 dtype=float,
501 doc="Maximum deviation from the median for overscan",
502 default=1000.0, check=lambda x: x > 0
503 )
504 overscanBiasJump = pexConfig.Field(
505 dtype=bool,
506 doc="Fit the overscan in a piecewise-fashion to correct for bias jumps?",
507 default=False,
508 )
509 overscanBiasJumpKeyword = pexConfig.Field(
510 dtype=str,
511 doc="Header keyword containing information about devices.",
512 default="NO_SUCH_KEY",
513 )
514 overscanBiasJumpDevices = pexConfig.ListField(
515 dtype=str,
516 doc="List of devices that need piecewise overscan correction.",
517 default=(),
518 )
519 overscanBiasJumpLocation = pexConfig.Field(
520 dtype=int,
521 doc="Location of bias jump along y-axis.",
522 default=0,
523 )
524
525 # Amplifier to CCD assembly configuration
526 doAssembleCcd = pexConfig.Field(
527 dtype=bool,
528 default=True,
529 doc="Assemble amp-level exposures into a ccd-level exposure?"
530 )
531 assembleCcd = pexConfig.ConfigurableField(
532 target=AssembleCcdTask,
533 doc="CCD assembly task",
534 )
535
536 # General calibration configuration.
537 doAssembleIsrExposures = pexConfig.Field(
538 dtype=bool,
539 default=False,
540 doc="Assemble amp-level calibration exposures into ccd-level exposure?"
541 )
542 doTrimToMatchCalib = pexConfig.Field(
543 dtype=bool,
544 default=False,
545 doc="Trim raw data to match calibration bounding boxes?"
546 )
547
548 # Bias subtraction.
549 doBias = pexConfig.Field(
550 dtype=bool,
551 doc="Apply bias frame correction?",
552 default=True,
553 )
554 biasDataProductName = pexConfig.Field(
555 dtype=str,
556 doc="Name of the bias data product",
557 default="bias",
558 )
559 doBiasBeforeOverscan = pexConfig.Field(
560 dtype=bool,
561 doc="Reverse order of overscan and bias correction.",
562 default=False
563 )
564
565 # Variance construction
566 doVariance = pexConfig.Field(
567 dtype=bool,
568 doc="Calculate variance?",
569 default=True
570 )
571 gain = pexConfig.Field(
572 dtype=float,
573 doc="The gain to use if no Detector is present in the Exposure (ignored if NaN)",
574 default=float("NaN"),
575 )
576 readNoise = pexConfig.Field(
577 dtype=float,
578 doc="The read noise to use if no Detector is present in the Exposure",
579 default=0.0,
580 )
581 doEmpiricalReadNoise = pexConfig.Field(
582 dtype=bool,
583 default=False,
584 doc="Calculate empirical read noise instead of value from AmpInfo data?"
585 )
586 usePtcReadNoise = pexConfig.Field(
587 dtype=bool,
588 default=False,
589 doc="Use readnoise values from the Photon Transfer Curve?"
590 )
591 maskNegativeVariance = pexConfig.Field(
592 dtype=bool,
593 default=True,
594 doc="Mask pixels that claim a negative variance? This likely indicates a failure "
595 "in the measurement of the overscan at an edge due to the data falling off faster "
596 "than the overscan model can account for it."
597 )
598 negativeVarianceMaskName = pexConfig.Field(
599 dtype=str,
600 default="BAD",
601 doc="Mask plane to use to mark pixels with negative variance, if `maskNegativeVariance` is True.",
602 )
603 # Linearization.
604 doLinearize = pexConfig.Field(
605 dtype=bool,
606 doc="Correct for nonlinearity of the detector's response?",
607 default=True,
608 )
609
610 # Crosstalk.
611 doCrosstalk = pexConfig.Field(
612 dtype=bool,
613 doc="Apply intra-CCD crosstalk correction?",
614 default=False,
615 )
616 doCrosstalkBeforeAssemble = pexConfig.Field(
617 dtype=bool,
618 doc="Apply crosstalk correction before CCD assembly, and before trimming?",
619 default=False,
620 )
621 crosstalk = pexConfig.ConfigurableField(
622 target=CrosstalkTask,
623 doc="Intra-CCD crosstalk correction",
624 )
625
626 # Masking options.
627 doDefect = pexConfig.Field(
628 dtype=bool,
629 doc="Apply correction for CCD defects, e.g. hot pixels?",
630 default=True,
631 )
632 doNanMasking = pexConfig.Field(
633 dtype=bool,
634 doc="Mask non-finite (NAN, inf) pixels?",
635 default=True,
636 )
637 doWidenSaturationTrails = pexConfig.Field(
638 dtype=bool,
639 doc="Widen bleed trails based on their width?",
640 default=True
641 )
642
643 # Brighter-Fatter correction.
644 doBrighterFatter = pexConfig.Field(
645 dtype=bool,
646 default=False,
647 doc="Apply the brighter-fatter correction?"
648 )
649 brighterFatterLevel = pexConfig.ChoiceField(
650 dtype=str,
651 default="DETECTOR",
652 doc="The level at which to correct for brighter-fatter.",
653 allowed={
654 "AMP": "Every amplifier treated separately.",
655 "DETECTOR": "One kernel per detector",
656 }
657 )
658 brighterFatterMaxIter = pexConfig.Field(
659 dtype=int,
660 default=10,
661 doc="Maximum number of iterations for the brighter-fatter correction"
662 )
663 brighterFatterThreshold = pexConfig.Field(
664 dtype=float,
665 default=1000,
666 doc="Threshold used to stop iterating the brighter-fatter correction. It is the "
667 "absolute value of the difference between the current corrected image and the one "
668 "from the previous iteration summed over all the pixels."
669 )
670 brighterFatterApplyGain = pexConfig.Field(
671 dtype=bool,
672 default=True,
673 doc="Should the gain be applied when applying the brighter-fatter correction?"
674 )
675 brighterFatterMaskListToInterpolate = pexConfig.ListField(
676 dtype=str,
677 doc="List of mask planes that should be interpolated over when applying the brighter-fatter "
678 "correction.",
679 default=["SAT", "BAD", "NO_DATA", "UNMASKEDNAN"],
680 )
681 brighterFatterMaskGrowSize = pexConfig.Field(
682 dtype=int,
683 default=0,
684 doc="Number of pixels to grow the masks listed in config.brighterFatterMaskListToInterpolate "
685 "when brighter-fatter correction is applied."
686 )
687
688 # Dark subtraction.
689 doDark = pexConfig.Field(
690 dtype=bool,
691 doc="Apply dark frame correction?",
692 default=True,
693 )
694 darkDataProductName = pexConfig.Field(
695 dtype=str,
696 doc="Name of the dark data product",
697 default="dark",
698 )
699
700 # Camera-specific stray light removal.
701 doStrayLight = pexConfig.Field(
702 dtype=bool,
703 doc="Subtract stray light in the y-band (due to encoder LEDs)?",
704 default=False,
705 )
706 strayLight = pexConfig.ConfigurableField(
707 target=StrayLightTask,
708 doc="y-band stray light correction"
709 )
710
711 # Flat correction.
712 doFlat = pexConfig.Field(
713 dtype=bool,
714 doc="Apply flat field correction?",
715 default=True,
716 )
717 flatDataProductName = pexConfig.Field(
718 dtype=str,
719 doc="Name of the flat data product",
720 default="flat",
721 )
722 flatScalingType = pexConfig.ChoiceField(
723 dtype=str,
724 doc="The method for scaling the flat on the fly.",
725 default='USER',
726 allowed={
727 "USER": "Scale by flatUserScale",
728 "MEAN": "Scale by the inverse of the mean",
729 "MEDIAN": "Scale by the inverse of the median",
730 },
731 )
732 flatUserScale = pexConfig.Field(
733 dtype=float,
734 doc="If flatScalingType is 'USER' then scale flat by this amount; ignored otherwise",
735 default=1.0,
736 )
737 doTweakFlat = pexConfig.Field(
738 dtype=bool,
739 doc="Tweak flats to match observed amplifier ratios?",
740 default=False
741 )
742
743 # Amplifier normalization based on gains instead of using flats
744 # configuration.
745 doApplyGains = pexConfig.Field(
746 dtype=bool,
747 doc="Correct the amplifiers for their gains instead of applying flat correction",
748 default=False,
749 )
750 usePtcGains = pexConfig.Field(
751 dtype=bool,
752 doc="Use the gain values from the Photon Transfer Curve?",
753 default=False,
754 )
755 normalizeGains = pexConfig.Field(
756 dtype=bool,
757 doc="Normalize all the amplifiers in each CCD to have the same median value.",
758 default=False,
759 )
760
761 # Fringe correction.
762 doFringe = pexConfig.Field(
763 dtype=bool,
764 doc="Apply fringe correction?",
765 default=True,
766 )
767 fringe = pexConfig.ConfigurableField(
768 target=FringeTask,
769 doc="Fringe subtraction task",
770 )
771 fringeAfterFlat = pexConfig.Field(
772 dtype=bool,
773 doc="Do fringe subtraction after flat-fielding?",
774 default=True,
775 )
776
777 # Amp offset correction.
778 doAmpOffset = pexConfig.Field(
779 doc="Calculate and apply amp offset corrections?",
780 dtype=bool,
781 default=False,
782 )
783 ampOffset = pexConfig.ConfigurableField(
784 doc="Amp offset correction task.",
785 target=AmpOffsetTask,
786 )
787
788 # Initial CCD-level background statistics options.
789 doMeasureBackground = pexConfig.Field(
790 dtype=bool,
791 doc="Measure the background level on the reduced image?",
792 default=False,
793 )
794
795 # Camera-specific masking configuration.
796 doCameraSpecificMasking = pexConfig.Field(
797 dtype=bool,
798 doc="Mask camera-specific bad regions?",
799 default=False,
800 )
801 masking = pexConfig.ConfigurableField(
802 target=MaskingTask,
803 doc="Masking task."
804 )
805
806 # Interpolation options.
807 doInterpolate = pexConfig.Field(
808 dtype=bool,
809 doc="Interpolate masked pixels?",
810 default=True,
811 )
812 doSaturationInterpolation = pexConfig.Field(
813 dtype=bool,
814 doc="Perform interpolation over pixels masked as saturated?"
815 " NB: This is independent of doSaturation; if that is False this plane"
816 " will likely be blank, resulting in a no-op here.",
817 default=True,
818 )
819 doNanInterpolation = pexConfig.Field(
820 dtype=bool,
821 doc="Perform interpolation over pixels masked as NaN?"
822 " NB: This is independent of doNanMasking; if that is False this plane"
823 " will likely be blank, resulting in a no-op here.",
824 default=True,
825 )
826 doNanInterpAfterFlat = pexConfig.Field(
827 dtype=bool,
828 doc=("If True, ensure we interpolate NaNs after flat-fielding, even if we "
829 "also have to interpolate them before flat-fielding."),
830 default=False,
831 )
832 maskListToInterpolate = pexConfig.ListField(
833 dtype=str,
834 doc="List of mask planes that should be interpolated.",
835 default=['SAT', 'BAD'],
836 )
837 doSaveInterpPixels = pexConfig.Field(
838 dtype=bool,
839 doc="Save a copy of the pre-interpolated pixel values?",
840 default=False,
841 )
842
843 # Default photometric calibration options.
844 fluxMag0T1 = pexConfig.DictField(
845 keytype=str,
846 itemtype=float,
847 doc="The approximate flux of a zero-magnitude object in a one-second exposure, per filter.",
848 default=dict((f, pow(10.0, 0.4*m)) for f, m in (("Unknown", 28.0),
849 ))
850 )
851 defaultFluxMag0T1 = pexConfig.Field(
852 dtype=float,
853 doc="Default value for fluxMag0T1 (for an unrecognized filter).",
854 default=pow(10.0, 0.4*28.0)
855 )
856
857 # Vignette correction configuration.
858 doVignette = pexConfig.Field(
859 dtype=bool,
860 doc=("Compute and attach the validPolygon defining the unvignetted region to the exposure "
861 "according to vignetting parameters?"),
862 default=False,
863 )
864 doMaskVignettePolygon = pexConfig.Field(
865 dtype=bool,
866 doc=("Add a mask bit for pixels within the vignetted region. Ignored if doVignette "
867 "is False"),
868 default=True,
869 )
870 vignetteValue = pexConfig.Field(
871 dtype=float,
872 doc="Value to replace image array pixels with in the vignetted region? Ignored if None.",
873 optional=True,
874 default=None,
875 )
876 vignette = pexConfig.ConfigurableField(
877 target=VignetteTask,
878 doc="Vignetting task.",
879 )
880
881 # Transmission curve configuration.
882 doAttachTransmissionCurve = pexConfig.Field(
883 dtype=bool,
884 default=False,
885 doc="Construct and attach a wavelength-dependent throughput curve for this CCD image?"
886 )
887 doUseOpticsTransmission = pexConfig.Field(
888 dtype=bool,
889 default=True,
890 doc="Load and use transmission_optics (if doAttachTransmissionCurve is True)?"
891 )
892 doUseFilterTransmission = pexConfig.Field(
893 dtype=bool,
894 default=True,
895 doc="Load and use transmission_filter (if doAttachTransmissionCurve is True)?"
896 )
897 doUseSensorTransmission = pexConfig.Field(
898 dtype=bool,
899 default=True,
900 doc="Load and use transmission_sensor (if doAttachTransmissionCurve is True)?"
901 )
902 doUseAtmosphereTransmission = pexConfig.Field(
903 dtype=bool,
904 default=True,
905 doc="Load and use transmission_atmosphere (if doAttachTransmissionCurve is True)?"
906 )
907
908 # Illumination correction.
909 doIlluminationCorrection = pexConfig.Field(
910 dtype=bool,
911 default=False,
912 doc="Perform illumination correction?"
913 )
914 illuminationCorrectionDataProductName = pexConfig.Field(
915 dtype=str,
916 doc="Name of the illumination correction data product.",
917 default="illumcor",
918 )
919 illumScale = pexConfig.Field(
920 dtype=float,
921 doc="Scale factor for the illumination correction.",
922 default=1.0,
923 )
924 illumFilters = pexConfig.ListField(
925 dtype=str,
926 default=[],
927 doc="Only perform illumination correction for these filters."
928 )
929
930 # Write the outputs to disk. If ISR is run as a subtask, this may not
931 # be needed.
932 doWrite = pexConfig.Field(
933 dtype=bool,
934 doc="Persist postISRCCD?",
935 default=True,
936 )
937
938 def validate(self):
939 super().validate()
940 if self.doFlatdoFlat and self.doApplyGainsdoApplyGains:
941 raise ValueError("You may not specify both doFlat and doApplyGains")
942 if self.doBiasBeforeOverscandoBiasBeforeOverscan and self.doTrimToMatchCalibdoTrimToMatchCalib:
943 raise ValueError("You may not specify both doBiasBeforeOverscan and doTrimToMatchCalib")
944 if self.doSaturationInterpolationdoSaturationInterpolation and self.saturatedMaskNamesaturatedMaskName not in self.maskListToInterpolatemaskListToInterpolate:
945 self.maskListToInterpolatemaskListToInterpolate.append(self.saturatedMaskNamesaturatedMaskName)
946 if not self.doSaturationInterpolationdoSaturationInterpolation and self.saturatedMaskNamesaturatedMaskName in self.maskListToInterpolatemaskListToInterpolate:
947 self.maskListToInterpolatemaskListToInterpolate.remove(self.saturatedMaskNamesaturatedMaskName)
948 if self.doNanInterpolationdoNanInterpolation and "UNMASKEDNAN" not in self.maskListToInterpolatemaskListToInterpolate:
949 self.maskListToInterpolatemaskListToInterpolate.append("UNMASKEDNAN")
950
951
952class IsrTask(pipeBase.PipelineTask, pipeBase.CmdLineTask):
953 """Apply common instrument signature correction algorithms to a raw frame.
954
955 The process for correcting imaging data is very similar from
956 camera to camera. This task provides a vanilla implementation of
957 doing these corrections, including the ability to turn certain
958 corrections off if they are not needed. The inputs to the primary
959 method, `run()`, are a raw exposure to be corrected and the
960 calibration data products. The raw input is a single chip sized
961 mosaic of all amps including overscans and other non-science
962 pixels. The method `runDataRef()` identifies and defines the
963 calibration data products, and is intended for use by a
964 `lsst.pipe.base.cmdLineTask.CmdLineTask` and takes as input only a
966 subclassed for different camera, although the most camera specific
967 methods have been split into subtasks that can be redirected
968 appropriately.
969
970 The __init__ method sets up the subtasks for ISR processing, using
971 the defaults from `lsst.ip.isr`.
972
973 Parameters
974 ----------
975 args : `list`
976 Positional arguments passed to the Task constructor.
977 None used at this time.
978 kwargs : `dict`, optional
979 Keyword arguments passed on to the Task constructor.
980 None used at this time.
981 """
982 ConfigClass = IsrTaskConfig
983 _DefaultName = "isr"
984
985 def __init__(self, **kwargs):
986 super().__init__(**kwargs)
987 self.makeSubtask("assembleCcd")
988 self.makeSubtask("crosstalk")
989 self.makeSubtask("strayLight")
990 self.makeSubtask("fringe")
991 self.makeSubtask("masking")
992 self.makeSubtask("overscan")
993 self.makeSubtask("vignette")
994 self.makeSubtask("ampOffset")
995
996 def runQuantum(self, butlerQC, inputRefs, outputRefs):
997 inputs = butlerQC.get(inputRefs)
998
999 try:
1000 inputs['detectorNum'] = inputRefs.ccdExposure.dataId['detector']
1001 except Exception as e:
1002 raise ValueError("Failure to find valid detectorNum value for Dataset %s: %s." %
1003 (inputRefs, e))
1004
1005 inputs['isGen3'] = True
1006
1007 detector = inputs['ccdExposure'].getDetector()
1008
1009 if self.config.doCrosstalk is True:
1010 # Crosstalk sources need to be defined by the pipeline
1011 # yaml if they exist.
1012 if 'crosstalk' in inputs and inputs['crosstalk'] is not None:
1013 if not isinstance(inputs['crosstalk'], CrosstalkCalib):
1014 inputs['crosstalk'] = CrosstalkCalib.fromTable(inputs['crosstalk'])
1015 else:
1016 coeffVector = (self.config.crosstalk.crosstalkValues
1017 if self.config.crosstalk.useConfigCoefficients else None)
1018 crosstalkCalib = CrosstalkCalib().fromDetector(detector, coeffVector=coeffVector)
1019 inputs['crosstalk'] = crosstalkCalib
1020 if inputs['crosstalk'].interChip and len(inputs['crosstalk'].interChip) > 0:
1021 if 'crosstalkSources' not in inputs:
1022 self.log.warning("No crosstalkSources found for chip with interChip terms!")
1023
1024 if self.doLinearizedoLinearize(detector) is True:
1025 if 'linearizer' in inputs:
1026 if isinstance(inputs['linearizer'], dict):
1027 linearizer = linearize.Linearizer(detector=detector, log=self.log)
1028 linearizer.fromYaml(inputs['linearizer'])
1029 self.log.warning("Dictionary linearizers will be deprecated in DM-28741.")
1030 elif isinstance(inputs['linearizer'], numpy.ndarray):
1031 linearizer = linearize.Linearizer(table=inputs.get('linearizer', None),
1032 detector=detector,
1033 log=self.log)
1034 self.log.warning("Bare lookup table linearizers will be deprecated in DM-28741.")
1035 else:
1036 linearizer = inputs['linearizer']
1037 linearizer.log = self.log
1038 inputs['linearizer'] = linearizer
1039 else:
1040 inputs['linearizer'] = linearize.Linearizer(detector=detector, log=self.log)
1041 self.log.warning("Constructing linearizer from cameraGeom information.")
1042
1043 if self.config.doDefect is True:
1044 if "defects" in inputs and inputs['defects'] is not None:
1045 # defects is loaded as a BaseCatalog with columns
1046 # x0, y0, width, height. Masking expects a list of defects
1047 # defined by their bounding box
1048 if not isinstance(inputs["defects"], Defects):
1049 inputs["defects"] = Defects.fromTable(inputs["defects"])
1050
1051 # Load the correct style of brighter-fatter kernel, and repack
1052 # the information as a numpy array.
1053 if self.config.doBrighterFatter:
1054 brighterFatterKernel = inputs.pop('newBFKernel', None)
1055 if brighterFatterKernel is None:
1056 brighterFatterKernel = inputs.get('bfKernel', None)
1057
1058 if brighterFatterKernel is not None and not isinstance(brighterFatterKernel, numpy.ndarray):
1059 # This is a ISR calib kernel
1060 detName = detector.getName()
1061 level = brighterFatterKernel.level
1062
1063 # This is expected to be a dictionary of amp-wise gains.
1064 inputs['bfGains'] = brighterFatterKernel.gain
1065 if self.config.brighterFatterLevel == 'DETECTOR':
1066 if level == 'DETECTOR':
1067 if detName in brighterFatterKernel.detKernels:
1068 inputs['bfKernel'] = brighterFatterKernel.detKernels[detName]
1069 else:
1070 raise RuntimeError("Failed to extract kernel from new-style BF kernel.")
1071 elif level == 'AMP':
1072 self.log.warning("Making DETECTOR level kernel from AMP based brighter "
1073 "fatter kernels.")
1074 brighterFatterKernel.makeDetectorKernelFromAmpwiseKernels(detName)
1075 inputs['bfKernel'] = brighterFatterKernel.detKernels[detName]
1076 elif self.config.brighterFatterLevel == 'AMP':
1077 raise NotImplementedError("Per-amplifier brighter-fatter correction not implemented")
1078
1079 if self.config.doFringe is True and self.fringe.checkFilter(inputs['ccdExposure']):
1080 expId = inputs['ccdExposure'].info.id
1081 inputs['fringes'] = self.fringe.loadFringes(inputs['fringes'],
1082 expId=expId,
1083 assembler=self.assembleCcd
1084 if self.config.doAssembleIsrExposures else None)
1085 else:
1086 inputs['fringes'] = pipeBase.Struct(fringes=None)
1087
1088 if self.config.doStrayLight is True and self.strayLight.checkFilter(inputs['ccdExposure']):
1089 if 'strayLightData' not in inputs:
1090 inputs['strayLightData'] = None
1091
1092 outputs = self.runrun(**inputs)
1093 butlerQC.put(outputs, outputRefs)
1094
1095 def readIsrData(self, dataRef, rawExposure):
1096 """Retrieve necessary frames for instrument signature removal.
1097
1098 Pre-fetching all required ISR data products limits the IO
1099 required by the ISR. Any conflict between the calibration data
1100 available and that needed for ISR is also detected prior to
1101 doing processing, allowing it to fail quickly.
1102
1103 Parameters
1104 ----------
1106 Butler reference of the detector data to be processed
1107 rawExposure : `afw.image.Exposure`
1108 The raw exposure that will later be corrected with the
1109 retrieved calibration data; should not be modified in this
1110 method.
1111
1112 Returns
1113 -------
1114 result : `lsst.pipe.base.Struct`
1115 Result struct with components (which may be `None`):
1116 - ``bias``: bias calibration frame (`afw.image.Exposure`)
1117 - ``linearizer``: functor for linearization
1119 - ``crosstalkSources``: list of possible crosstalk sources (`list`)
1120 - ``dark``: dark calibration frame (`afw.image.Exposure`)
1121 - ``flat``: flat calibration frame (`afw.image.Exposure`)
1122 - ``bfKernel``: Brighter-Fatter kernel (`numpy.ndarray`)
1123 - ``defects``: list of defects (`lsst.ip.isr.Defects`)
1124 - ``fringes``: `lsst.pipe.base.Struct` with components:
1125 - ``fringes``: fringe calibration frame (`afw.image.Exposure`)
1126 - ``seed``: random seed derived from the ccdExposureId for random
1127 number generator (`uint32`).
1128 - ``opticsTransmission``: `lsst.afw.image.TransmissionCurve`
1129 A ``TransmissionCurve`` that represents the throughput of the
1130 optics, to be evaluated in focal-plane coordinates.
1131 - ``filterTransmission`` : `lsst.afw.image.TransmissionCurve`
1132 A ``TransmissionCurve`` that represents the throughput of the
1133 filter itself, to be evaluated in focal-plane coordinates.
1134 - ``sensorTransmission`` : `lsst.afw.image.TransmissionCurve`
1135 A ``TransmissionCurve`` that represents the throughput of the
1136 sensor itself, to be evaluated in post-assembly trimmed
1137 detector coordinates.
1138 - ``atmosphereTransmission`` : `lsst.afw.image.TransmissionCurve`
1139 A ``TransmissionCurve`` that represents the throughput of the
1140 atmosphere, assumed to be spatially constant.
1141 - ``strayLightData`` : `object`
1142 An opaque object containing calibration information for
1143 stray-light correction. If `None`, no correction will be
1144 performed.
1145 - ``illumMaskedImage`` : illumination correction image
1147
1148 Raises
1149 ------
1150 NotImplementedError :
1151 Raised if a per-amplifier brighter-fatter kernel is requested by
1152 the configuration.
1153 """
1154 try:
1155 dateObs = rawExposure.getInfo().getVisitInfo().getDate()
1156 dateObs = dateObs.toPython().isoformat()
1157 except RuntimeError:
1158 self.log.warning("Unable to identify dateObs for rawExposure.")
1159 dateObs = None
1160
1161 ccd = rawExposure.getDetector()
1162 filterLabel = rawExposure.getFilterLabel()
1163 physicalFilter = isrFunctions.getPhysicalFilter(filterLabel, self.log)
1164 rawExposure.mask.addMaskPlane("UNMASKEDNAN") # needed to match pre DM-15862 processing.
1165 biasExposure = (self.getIsrExposuregetIsrExposure(dataRef, self.config.biasDataProductName)
1166 if self.config.doBias else None)
1167 # immediate=True required for functors and linearizers are functors
1168 # see ticket DM-6515
1169 linearizer = (dataRef.get("linearizer", immediate=True)
1170 if self.doLinearizedoLinearize(ccd) else None)
1171 if linearizer is not None and not isinstance(linearizer, numpy.ndarray):
1172 linearizer.log = self.log
1173 if isinstance(linearizer, numpy.ndarray):
1174 linearizer = linearize.Linearizer(table=linearizer, detector=ccd)
1175
1176 crosstalkCalib = None
1177 if self.config.doCrosstalk:
1178 try:
1179 crosstalkCalib = dataRef.get("crosstalk", immediate=True)
1180 except NoResults:
1181 coeffVector = (self.config.crosstalk.crosstalkValues
1182 if self.config.crosstalk.useConfigCoefficients else None)
1183 crosstalkCalib = CrosstalkCalib().fromDetector(ccd, coeffVector=coeffVector)
1184 crosstalkSources = (self.crosstalk.prepCrosstalk(dataRef, crosstalkCalib)
1185 if self.config.doCrosstalk else None)
1186
1187 darkExposure = (self.getIsrExposuregetIsrExposure(dataRef, self.config.darkDataProductName)
1188 if self.config.doDark else None)
1189 flatExposure = (self.getIsrExposuregetIsrExposure(dataRef, self.config.flatDataProductName,
1190 dateObs=dateObs)
1191 if self.config.doFlat else None)
1192
1193 brighterFatterKernel = None
1194 brighterFatterGains = None
1195 if self.config.doBrighterFatter is True:
1196 try:
1197 # Use the new-style cp_pipe version of the kernel if it exists
1198 # If using a new-style kernel, always use the self-consistent
1199 # gains, i.e. the ones inside the kernel object itself
1200 brighterFatterKernel = dataRef.get("brighterFatterKernel")
1201 brighterFatterGains = brighterFatterKernel.gain
1202 self.log.info("New style brighter-fatter kernel (brighterFatterKernel) loaded")
1203 except NoResults:
1204 try: # Fall back to the old-style numpy-ndarray style kernel if necessary.
1205 brighterFatterKernel = dataRef.get("bfKernel")
1206 self.log.info("Old style brighter-fatter kernel (bfKernel) loaded")
1207 except NoResults:
1208 brighterFatterKernel = None
1209 if brighterFatterKernel is not None and not isinstance(brighterFatterKernel, numpy.ndarray):
1210 # If the kernel is not an ndarray, it's the cp_pipe version
1211 # so extract the kernel for this detector, or raise an error
1212 if self.config.brighterFatterLevel == 'DETECTOR':
1213 if brighterFatterKernel.detKernels:
1214 brighterFatterKernel = brighterFatterKernel.detKernels[ccd.getName()]
1215 else:
1216 raise RuntimeError("Failed to extract kernel from new-style BF kernel.")
1217 else:
1218 # TODO DM-15631 for implementing this
1219 raise NotImplementedError("Per-amplifier brighter-fatter correction not implemented")
1220
1221 defectList = (dataRef.get("defects")
1222 if self.config.doDefect else None)
1223 expId = rawExposure.info.id
1224 fringeStruct = (self.fringe.readFringes(dataRef, expId=expId, assembler=self.assembleCcd
1225 if self.config.doAssembleIsrExposures else None)
1226 if self.config.doFringe and self.fringe.checkFilter(rawExposure)
1227 else pipeBase.Struct(fringes=None))
1228
1229 if self.config.doAttachTransmissionCurve:
1230 opticsTransmission = (dataRef.get("transmission_optics")
1231 if self.config.doUseOpticsTransmission else None)
1232 filterTransmission = (dataRef.get("transmission_filter")
1233 if self.config.doUseFilterTransmission else None)
1234 sensorTransmission = (dataRef.get("transmission_sensor")
1235 if self.config.doUseSensorTransmission else None)
1236 atmosphereTransmission = (dataRef.get("transmission_atmosphere")
1237 if self.config.doUseAtmosphereTransmission else None)
1238 else:
1239 opticsTransmission = None
1240 filterTransmission = None
1241 sensorTransmission = None
1242 atmosphereTransmission = None
1243
1244 if self.config.doStrayLight:
1245 strayLightData = self.strayLight.readIsrData(dataRef, rawExposure)
1246 else:
1247 strayLightData = None
1248
1249 illumMaskedImage = (self.getIsrExposuregetIsrExposure(dataRef,
1250 self.config.illuminationCorrectionDataProductName).getMaskedImage()
1251 if (self.config.doIlluminationCorrection
1252 and physicalFilter in self.config.illumFilters)
1253 else None)
1254
1255 # Struct should include only kwargs to run()
1256 return pipeBase.Struct(bias=biasExposure,
1257 linearizer=linearizer,
1258 crosstalk=crosstalkCalib,
1259 crosstalkSources=crosstalkSources,
1260 dark=darkExposure,
1261 flat=flatExposure,
1262 bfKernel=brighterFatterKernel,
1263 bfGains=brighterFatterGains,
1264 defects=defectList,
1265 fringes=fringeStruct,
1266 opticsTransmission=opticsTransmission,
1267 filterTransmission=filterTransmission,
1268 sensorTransmission=sensorTransmission,
1269 atmosphereTransmission=atmosphereTransmission,
1270 strayLightData=strayLightData,
1271 illumMaskedImage=illumMaskedImage
1272 )
1273
1274 @timeMethod
1275 def run(self, ccdExposure, *, camera=None, bias=None, linearizer=None,
1276 crosstalk=None, crosstalkSources=None,
1277 dark=None, flat=None, ptc=None, bfKernel=None, bfGains=None, defects=None,
1278 fringes=pipeBase.Struct(fringes=None), opticsTransmission=None, filterTransmission=None,
1279 sensorTransmission=None, atmosphereTransmission=None,
1280 detectorNum=None, strayLightData=None, illumMaskedImage=None,
1281 isGen3=False,
1282 ):
1283 """Perform instrument signature removal on an exposure.
1284
1285 Steps included in the ISR processing, in order performed, are:
1286 - saturation and suspect pixel masking
1287 - overscan subtraction
1288 - CCD assembly of individual amplifiers
1289 - bias subtraction
1290 - variance image construction
1291 - linearization of non-linear response
1292 - crosstalk masking
1293 - brighter-fatter correction
1294 - dark subtraction
1295 - fringe correction
1296 - stray light subtraction
1297 - flat correction
1298 - masking of known defects and camera specific features
1299 - vignette calculation
1300 - appending transmission curve and distortion model
1301
1302 Parameters
1303 ----------
1304 ccdExposure : `lsst.afw.image.Exposure`
1305 The raw exposure that is to be run through ISR. The
1306 exposure is modified by this method.
1307 camera : `lsst.afw.cameraGeom.Camera`, optional
1308 The camera geometry for this exposure. Required if
1309 one or more of ``ccdExposure``, ``bias``, ``dark``, or
1310 ``flat`` does not have an associated detector.
1311 bias : `lsst.afw.image.Exposure`, optional
1312 Bias calibration frame.
1313 linearizer : `lsst.ip.isr.linearize.LinearizeBase`, optional
1314 Functor for linearization.
1315 crosstalk : `lsst.ip.isr.crosstalk.CrosstalkCalib`, optional
1316 Calibration for crosstalk.
1317 crosstalkSources : `list`, optional
1318 List of possible crosstalk sources.
1319 dark : `lsst.afw.image.Exposure`, optional
1320 Dark calibration frame.
1321 flat : `lsst.afw.image.Exposure`, optional
1322 Flat calibration frame.
1324 Photon transfer curve dataset, with, e.g., gains
1325 and read noise.
1326 bfKernel : `numpy.ndarray`, optional
1327 Brighter-fatter kernel.
1328 bfGains : `dict` of `float`, optional
1329 Gains used to override the detector's nominal gains for the
1330 brighter-fatter correction. A dict keyed by amplifier name for
1331 the detector in question.
1332 defects : `lsst.ip.isr.Defects`, optional
1333 List of defects.
1334 fringes : `lsst.pipe.base.Struct`, optional
1335 Struct containing the fringe correction data, with
1336 elements:
1337 - ``fringes``: fringe calibration frame (`afw.image.Exposure`)
1338 - ``seed``: random seed derived from the ccdExposureId for random
1339 number generator (`uint32`)
1340 opticsTransmission: `lsst.afw.image.TransmissionCurve`, optional
1341 A ``TransmissionCurve`` that represents the throughput of the,
1342 optics, to be evaluated in focal-plane coordinates.
1343 filterTransmission : `lsst.afw.image.TransmissionCurve`
1344 A ``TransmissionCurve`` that represents the throughput of the
1345 filter itself, to be evaluated in focal-plane coordinates.
1346 sensorTransmission : `lsst.afw.image.TransmissionCurve`
1347 A ``TransmissionCurve`` that represents the throughput of the
1348 sensor itself, to be evaluated in post-assembly trimmed detector
1349 coordinates.
1350 atmosphereTransmission : `lsst.afw.image.TransmissionCurve`
1351 A ``TransmissionCurve`` that represents the throughput of the
1352 atmosphere, assumed to be spatially constant.
1353 detectorNum : `int`, optional
1354 The integer number for the detector to process.
1355 isGen3 : bool, optional
1356 Flag this call to run() as using the Gen3 butler environment.
1357 strayLightData : `object`, optional
1358 Opaque object containing calibration information for stray-light
1359 correction. If `None`, no correction will be performed.
1360 illumMaskedImage : `lsst.afw.image.MaskedImage`, optional
1361 Illumination correction image.
1362
1363 Returns
1364 -------
1365 result : `lsst.pipe.base.Struct`
1366 Result struct with component:
1367 - ``exposure`` : `afw.image.Exposure`
1368 The fully ISR corrected exposure.
1369 - ``outputExposure`` : `afw.image.Exposure`
1370 An alias for `exposure`
1371 - ``ossThumb`` : `numpy.ndarray`
1372 Thumbnail image of the exposure after overscan subtraction.
1373 - ``flattenedThumb`` : `numpy.ndarray`
1374 Thumbnail image of the exposure after flat-field correction.
1375
1376 Raises
1377 ------
1378 RuntimeError
1379 Raised if a configuration option is set to True, but the
1380 required calibration data has not been specified.
1381
1382 Notes
1383 -----
1384 The current processed exposure can be viewed by setting the
1385 appropriate lsstDebug entries in the `debug.display`
1386 dictionary. The names of these entries correspond to some of
1387 the IsrTaskConfig Boolean options, with the value denoting the
1388 frame to use. The exposure is shown inside the matching
1389 option check and after the processing of that step has
1390 finished. The steps with debug points are:
1391
1392 doAssembleCcd
1393 doBias
1394 doCrosstalk
1395 doBrighterFatter
1396 doDark
1397 doFringe
1398 doStrayLight
1399 doFlat
1400
1401 In addition, setting the "postISRCCD" entry displays the
1402 exposure after all ISR processing has finished.
1403
1404 """
1405
1406 if isGen3 is True:
1407 # Gen3 currently cannot automatically do configuration overrides.
1408 # DM-15257 looks to discuss this issue.
1409 # Configure input exposures;
1410
1411 ccdExposure = self.ensureExposureensureExposure(ccdExposure, camera, detectorNum)
1412 bias = self.ensureExposureensureExposure(bias, camera, detectorNum)
1413 dark = self.ensureExposureensureExposure(dark, camera, detectorNum)
1414 flat = self.ensureExposureensureExposure(flat, camera, detectorNum)
1415 else:
1416 if isinstance(ccdExposure, ButlerDataRef):
1417 return self.runDataRefrunDataRef(ccdExposure)
1418
1419 ccd = ccdExposure.getDetector()
1420 filterLabel = ccdExposure.getFilterLabel()
1421 physicalFilter = isrFunctions.getPhysicalFilter(filterLabel, self.log)
1422
1423 if not ccd:
1424 assert not self.config.doAssembleCcd, "You need a Detector to run assembleCcd."
1425 ccd = [FakeAmp(ccdExposure, self.config)]
1426
1427 # Validate Input
1428 if self.config.doBias and bias is None:
1429 raise RuntimeError("Must supply a bias exposure if config.doBias=True.")
1430 if self.doLinearizedoLinearize(ccd) and linearizer is None:
1431 raise RuntimeError("Must supply a linearizer if config.doLinearize=True for this detector.")
1432 if self.config.doBrighterFatter and bfKernel is None:
1433 raise RuntimeError("Must supply a kernel if config.doBrighterFatter=True.")
1434 if self.config.doDark and dark is None:
1435 raise RuntimeError("Must supply a dark exposure if config.doDark=True.")
1436 if self.config.doFlat and flat is None:
1437 raise RuntimeError("Must supply a flat exposure if config.doFlat=True.")
1438 if self.config.doDefect and defects is None:
1439 raise RuntimeError("Must supply defects if config.doDefect=True.")
1440 if (self.config.doFringe and physicalFilter in self.fringe.config.filters
1441 and fringes.fringes is None):
1442 # The `fringes` object needs to be a pipeBase.Struct, as
1443 # we use it as a `dict` for the parameters of
1444 # `FringeTask.run()`. The `fringes.fringes` `list` may
1445 # not be `None` if `doFringe=True`. Otherwise, raise.
1446 raise RuntimeError("Must supply fringe exposure as a pipeBase.Struct.")
1447 if (self.config.doIlluminationCorrection and physicalFilter in self.config.illumFilters
1448 and illumMaskedImage is None):
1449 raise RuntimeError("Must supply an illumcor if config.doIlluminationCorrection=True.")
1450
1451 # Begin ISR processing.
1452 if self.config.doConvertIntToFloat:
1453 self.log.info("Converting exposure to floating point values.")
1454 ccdExposure = self.convertIntToFloatconvertIntToFloat(ccdExposure)
1455
1456 if self.config.doBias and self.config.doBiasBeforeOverscan:
1457 self.log.info("Applying bias correction.")
1458 isrFunctions.biasCorrection(ccdExposure.getMaskedImage(), bias.getMaskedImage(),
1459 trimToFit=self.config.doTrimToMatchCalib)
1460 self.debugViewdebugView(ccdExposure, "doBias")
1461
1462 # Amplifier level processing.
1463 overscans = []
1464 for amp in ccd:
1465 # if ccdExposure is one amp,
1466 # check for coverage to prevent performing ops multiple times
1467 if ccdExposure.getBBox().contains(amp.getBBox()):
1468 # Check for fully masked bad amplifiers,
1469 # and generate masks for SUSPECT and SATURATED values.
1470 badAmp = self.maskAmplifiermaskAmplifier(ccdExposure, amp, defects)
1471
1472 if self.config.doOverscan and not badAmp:
1473 # Overscan correction on amp-by-amp basis.
1474 overscanResults = self.overscanCorrectionoverscanCorrection(ccdExposure, amp)
1475 self.log.debug("Corrected overscan for amplifier %s.", amp.getName())
1476 if overscanResults is not None and \
1477 self.config.qa is not None and self.config.qa.saveStats is True:
1478 if isinstance(overscanResults.overscanFit, float):
1479 qaMedian = overscanResults.overscanFit
1480 qaStdev = float("NaN")
1481 else:
1482 qaStats = afwMath.makeStatistics(overscanResults.overscanFit,
1483 afwMath.MEDIAN | afwMath.STDEVCLIP)
1484 qaMedian = qaStats.getValue(afwMath.MEDIAN)
1485 qaStdev = qaStats.getValue(afwMath.STDEVCLIP)
1486
1487 self.metadata[f"FIT MEDIAN {amp.getName()}"] = qaMedian
1488 self.metadata[f"FIT STDEV {amp.getName()}"] = qaStdev
1489 self.log.debug(" Overscan stats for amplifer %s: %f +/- %f",
1490 amp.getName(), qaMedian, qaStdev)
1491
1492 # Residuals after overscan correction
1493 qaStatsAfter = afwMath.makeStatistics(overscanResults.overscanImage,
1494 afwMath.MEDIAN | afwMath.STDEVCLIP)
1495 qaMedianAfter = qaStatsAfter.getValue(afwMath.MEDIAN)
1496 qaStdevAfter = qaStatsAfter.getValue(afwMath.STDEVCLIP)
1497
1498 self.metadata[f"RESIDUAL MEDIAN {amp.getName()}"] = qaMedianAfter
1499 self.metadata[f"RESIDUAL STDEV {amp.getName()}"] = qaStdevAfter
1500 self.log.debug(" Overscan stats for amplifer %s after correction: %f +/- %f",
1501 amp.getName(), qaMedianAfter, qaStdevAfter)
1502
1503 ccdExposure.getMetadata().set('OVERSCAN', "Overscan corrected")
1504 else:
1505 if badAmp:
1506 self.log.warning("Amplifier %s is bad.", amp.getName())
1507 overscanResults = None
1508
1509 overscans.append(overscanResults if overscanResults is not None else None)
1510 else:
1511 self.log.info("Skipped OSCAN for %s.", amp.getName())
1512
1513 if self.config.doCrosstalk and self.config.doCrosstalkBeforeAssemble:
1514 self.log.info("Applying crosstalk correction.")
1515 self.crosstalk.run(ccdExposure, crosstalk=crosstalk,
1516 crosstalkSources=crosstalkSources, camera=camera)
1517 self.debugViewdebugView(ccdExposure, "doCrosstalk")
1518
1519 if self.config.doAssembleCcd:
1520 self.log.info("Assembling CCD from amplifiers.")
1521 ccdExposure = self.assembleCcd.assembleCcd(ccdExposure)
1522
1523 if self.config.expectWcs and not ccdExposure.getWcs():
1524 self.log.warning("No WCS found in input exposure.")
1525 self.debugViewdebugView(ccdExposure, "doAssembleCcd")
1526
1527 ossThumb = None
1528 if self.config.qa.doThumbnailOss:
1529 ossThumb = isrQa.makeThumbnail(ccdExposure, isrQaConfig=self.config.qa)
1530
1531 if self.config.doBias and not self.config.doBiasBeforeOverscan:
1532 self.log.info("Applying bias correction.")
1533 isrFunctions.biasCorrection(ccdExposure.getMaskedImage(), bias.getMaskedImage(),
1534 trimToFit=self.config.doTrimToMatchCalib)
1535 self.debugViewdebugView(ccdExposure, "doBias")
1536
1537 if self.config.doVariance:
1538 for amp, overscanResults in zip(ccd, overscans):
1539 if ccdExposure.getBBox().contains(amp.getBBox()):
1540 self.log.debug("Constructing variance map for amplifer %s.", amp.getName())
1541 ampExposure = ccdExposure.Factory(ccdExposure, amp.getBBox())
1542 if overscanResults is not None:
1543 self.updateVarianceupdateVariance(ampExposure, amp,
1544 overscanImage=overscanResults.overscanImage,
1545 ptcDataset=ptc)
1546 else:
1547 self.updateVarianceupdateVariance(ampExposure, amp,
1548 overscanImage=None,
1549 ptcDataset=ptc)
1550 if self.config.qa is not None and self.config.qa.saveStats is True:
1551 qaStats = afwMath.makeStatistics(ampExposure.getVariance(),
1552 afwMath.MEDIAN | afwMath.STDEVCLIP)
1553 self.metadata[f"ISR VARIANCE {amp.getName()} MEDIAN"] = \
1554 qaStats.getValue(afwMath.MEDIAN)
1555 self.metadata[f"ISR VARIANCE {amp.getName()} STDEV"] = \
1556 qaStats.getValue(afwMath.STDEVCLIP)
1557 self.log.debug(" Variance stats for amplifer %s: %f +/- %f.",
1558 amp.getName(), qaStats.getValue(afwMath.MEDIAN),
1559 qaStats.getValue(afwMath.STDEVCLIP))
1560 if self.config.maskNegativeVariance:
1561 self.maskNegativeVariancemaskNegativeVariance(ccdExposure)
1562
1563 if self.doLinearizedoLinearize(ccd):
1564 self.log.info("Applying linearizer.")
1565 linearizer.applyLinearity(image=ccdExposure.getMaskedImage().getImage(),
1566 detector=ccd, log=self.log)
1567
1568 if self.config.doCrosstalk and not self.config.doCrosstalkBeforeAssemble:
1569 self.log.info("Applying crosstalk correction.")
1570 self.crosstalk.run(ccdExposure, crosstalk=crosstalk,
1571 crosstalkSources=crosstalkSources, isTrimmed=True)
1572 self.debugViewdebugView(ccdExposure, "doCrosstalk")
1573
1574 # Masking block. Optionally mask known defects, NAN/inf pixels,
1575 # widen trails, and do anything else the camera needs. Saturated and
1576 # suspect pixels have already been masked.
1577 if self.config.doDefect:
1578 self.log.info("Masking defects.")
1579 self.maskDefectmaskDefect(ccdExposure, defects)
1580
1581 if self.config.numEdgeSuspect > 0:
1582 self.log.info("Masking edges as SUSPECT.")
1583 self.maskEdgesmaskEdges(ccdExposure, numEdgePixels=self.config.numEdgeSuspect,
1584 maskPlane="SUSPECT", level=self.config.edgeMaskLevel)
1585
1586 if self.config.doNanMasking:
1587 self.log.info("Masking non-finite (NAN, inf) value pixels.")
1588 self.maskNanmaskNan(ccdExposure)
1589
1590 if self.config.doWidenSaturationTrails:
1591 self.log.info("Widening saturation trails.")
1592 isrFunctions.widenSaturationTrails(ccdExposure.getMaskedImage().getMask())
1593
1594 if self.config.doCameraSpecificMasking:
1595 self.log.info("Masking regions for camera specific reasons.")
1596 self.masking.run(ccdExposure)
1597
1598 if self.config.doBrighterFatter:
1599 # We need to apply flats and darks before we can interpolate, and
1600 # we need to interpolate before we do B-F, but we do B-F without
1601 # the flats and darks applied so we can work in units of electrons
1602 # or holes. This context manager applies and then removes the darks
1603 # and flats.
1604 #
1605 # We also do not want to interpolate values here, so operate on
1606 # temporary images so we can apply only the BF-correction and roll
1607 # back the interpolation.
1608 interpExp = ccdExposure.clone()
1609 with self.flatContextflatContext(interpExp, flat, dark):
1610 isrFunctions.interpolateFromMask(
1611 maskedImage=interpExp.getMaskedImage(),
1612 fwhm=self.config.fwhm,
1613 growSaturatedFootprints=self.config.growSaturationFootprintSize,
1614 maskNameList=list(self.config.brighterFatterMaskListToInterpolate)
1615 )
1616 bfExp = interpExp.clone()
1617
1618 self.log.info("Applying brighter-fatter correction using kernel type %s / gains %s.",
1619 type(bfKernel), type(bfGains))
1620 bfResults = isrFunctions.brighterFatterCorrection(bfExp, bfKernel,
1621 self.config.brighterFatterMaxIter,
1622 self.config.brighterFatterThreshold,
1623 self.config.brighterFatterApplyGain,
1624 bfGains)
1625 if bfResults[1] == self.config.brighterFatterMaxIter:
1626 self.log.warning("Brighter-fatter correction did not converge, final difference %f.",
1627 bfResults[0])
1628 else:
1629 self.log.info("Finished brighter-fatter correction in %d iterations.",
1630 bfResults[1])
1631 image = ccdExposure.getMaskedImage().getImage()
1632 bfCorr = bfExp.getMaskedImage().getImage()
1633 bfCorr -= interpExp.getMaskedImage().getImage()
1634 image += bfCorr
1635
1636 # Applying the brighter-fatter correction applies a
1637 # convolution to the science image. At the edges this
1638 # convolution may not have sufficient valid pixels to
1639 # produce a valid correction. Mark pixels within the size
1640 # of the brighter-fatter kernel as EDGE to warn of this
1641 # fact.
1642 self.log.info("Ensuring image edges are masked as EDGE to the brighter-fatter kernel size.")
1643 self.maskEdgesmaskEdges(ccdExposure, numEdgePixels=numpy.max(bfKernel.shape) // 2,
1644 maskPlane="EDGE")
1645
1646 if self.config.brighterFatterMaskGrowSize > 0:
1647 self.log.info("Growing masks to account for brighter-fatter kernel convolution.")
1648 for maskPlane in self.config.brighterFatterMaskListToInterpolate:
1649 isrFunctions.growMasks(ccdExposure.getMask(),
1650 radius=self.config.brighterFatterMaskGrowSize,
1651 maskNameList=maskPlane,
1652 maskValue=maskPlane)
1653
1654 self.debugViewdebugView(ccdExposure, "doBrighterFatter")
1655
1656 if self.config.doDark:
1657 self.log.info("Applying dark correction.")
1658 self.darkCorrectiondarkCorrection(ccdExposure, dark)
1659 self.debugViewdebugView(ccdExposure, "doDark")
1660
1661 if self.config.doFringe and not self.config.fringeAfterFlat:
1662 self.log.info("Applying fringe correction before flat.")
1663 self.fringe.run(ccdExposure, **fringes.getDict())
1664 self.debugViewdebugView(ccdExposure, "doFringe")
1665
1666 if self.config.doStrayLight and self.strayLight.check(ccdExposure):
1667 self.log.info("Checking strayLight correction.")
1668 self.strayLight.run(ccdExposure, strayLightData)
1669 self.debugViewdebugView(ccdExposure, "doStrayLight")
1670
1671 if self.config.doFlat:
1672 self.log.info("Applying flat correction.")
1673 self.flatCorrectionflatCorrection(ccdExposure, flat)
1674 self.debugViewdebugView(ccdExposure, "doFlat")
1675
1676 if self.config.doApplyGains:
1677 self.log.info("Applying gain correction instead of flat.")
1678 if self.config.usePtcGains:
1679 self.log.info("Using gains from the Photon Transfer Curve.")
1680 isrFunctions.applyGains(ccdExposure, self.config.normalizeGains,
1681 ptcGains=ptc.gain)
1682 else:
1683 isrFunctions.applyGains(ccdExposure, self.config.normalizeGains)
1684
1685 if self.config.doFringe and self.config.fringeAfterFlat:
1686 self.log.info("Applying fringe correction after flat.")
1687 self.fringe.run(ccdExposure, **fringes.getDict())
1688
1689 if self.config.doVignette:
1690 if self.config.doMaskVignettePolygon:
1691 self.log.info("Constructing, attaching, and masking vignette polygon.")
1692 else:
1693 self.log.info("Constructing and attaching vignette polygon.")
1694 self.vignettePolygonvignettePolygon = self.vignette.run(
1695 exposure=ccdExposure, doUpdateMask=self.config.doMaskVignettePolygon,
1696 vignetteValue=self.config.vignetteValue, log=self.log)
1697
1698 if self.config.doAttachTransmissionCurve:
1699 self.log.info("Adding transmission curves.")
1700 isrFunctions.attachTransmissionCurve(ccdExposure, opticsTransmission=opticsTransmission,
1701 filterTransmission=filterTransmission,
1702 sensorTransmission=sensorTransmission,
1703 atmosphereTransmission=atmosphereTransmission)
1704
1705 flattenedThumb = None
1706 if self.config.qa.doThumbnailFlattened:
1707 flattenedThumb = isrQa.makeThumbnail(ccdExposure, isrQaConfig=self.config.qa)
1708
1709 if self.config.doIlluminationCorrection and physicalFilter in self.config.illumFilters:
1710 self.log.info("Performing illumination correction.")
1711 isrFunctions.illuminationCorrection(ccdExposure.getMaskedImage(),
1712 illumMaskedImage, illumScale=self.config.illumScale,
1713 trimToFit=self.config.doTrimToMatchCalib)
1714
1715 preInterpExp = None
1716 if self.config.doSaveInterpPixels:
1717 preInterpExp = ccdExposure.clone()
1718
1719 # Reset and interpolate bad pixels.
1720 #
1721 # Large contiguous bad regions (which should have the BAD mask
1722 # bit set) should have their values set to the image median.
1723 # This group should include defects and bad amplifiers. As the
1724 # area covered by these defects are large, there's little
1725 # reason to expect that interpolation would provide a more
1726 # useful value.
1727 #
1728 # Smaller defects can be safely interpolated after the larger
1729 # regions have had their pixel values reset. This ensures
1730 # that the remaining defects adjacent to bad amplifiers (as an
1731 # example) do not attempt to interpolate extreme values.
1732 if self.config.doSetBadRegions:
1733 badPixelCount, badPixelValue = isrFunctions.setBadRegions(ccdExposure)
1734 if badPixelCount > 0:
1735 self.log.info("Set %d BAD pixels to %f.", badPixelCount, badPixelValue)
1736
1737 if self.config.doInterpolate:
1738 self.log.info("Interpolating masked pixels.")
1739 isrFunctions.interpolateFromMask(
1740 maskedImage=ccdExposure.getMaskedImage(),
1741 fwhm=self.config.fwhm,
1742 growSaturatedFootprints=self.config.growSaturationFootprintSize,
1743 maskNameList=list(self.config.maskListToInterpolate)
1744 )
1745
1746 self.roughZeroPointroughZeroPoint(ccdExposure)
1747
1748 # correct for amp offsets within the CCD
1749 if self.config.doAmpOffset:
1750 self.log.info("Correcting amp offsets.")
1751 self.ampOffset.run(ccdExposure)
1752
1753 if self.config.doMeasureBackground:
1754 self.log.info("Measuring background level.")
1755 self.measureBackgroundmeasureBackground(ccdExposure, self.config.qa)
1756
1757 if self.config.qa is not None and self.config.qa.saveStats is True:
1758 for amp in ccd:
1759 ampExposure = ccdExposure.Factory(ccdExposure, amp.getBBox())
1760 qaStats = afwMath.makeStatistics(ampExposure.getImage(),
1761 afwMath.MEDIAN | afwMath.STDEVCLIP)
1762 self.metadata[f"ISR BACKGROUND {amp.getName()} MEDIAN"] = qaStats.getValue(afwMath.MEDIAN)
1763 self.metadata[f"ISR BACKGROUND {amp.getName()} STDEV"] = \
1764 qaStats.getValue(afwMath.STDEVCLIP)
1765 self.log.debug(" Background stats for amplifer %s: %f +/- %f",
1766 amp.getName(), qaStats.getValue(afwMath.MEDIAN),
1767 qaStats.getValue(afwMath.STDEVCLIP))
1768
1769 self.debugViewdebugView(ccdExposure, "postISRCCD")
1770
1771 return pipeBase.Struct(
1772 exposure=ccdExposure,
1773 ossThumb=ossThumb,
1774 flattenedThumb=flattenedThumb,
1775
1776 preInterpExposure=preInterpExp,
1777 outputExposure=ccdExposure,
1778 outputOssThumbnail=ossThumb,
1779 outputFlattenedThumbnail=flattenedThumb,
1780 )
1781
1782 @timeMethod
1783 def runDataRef(self, sensorRef):
1784 """Perform instrument signature removal on a ButlerDataRef of a Sensor.
1785
1786 This method contains the `CmdLineTask` interface to the ISR
1787 processing. All IO is handled here, freeing the `run()` method
1788 to manage only pixel-level calculations. The steps performed
1789 are:
1790 - Read in necessary detrending/isr/calibration data.
1791 - Process raw exposure in `run()`.
1792 - Persist the ISR-corrected exposure as "postISRCCD" if
1793 config.doWrite=True.
1794
1795 Parameters
1796 ----------
1798 DataRef of the detector data to be processed
1799
1800 Returns
1801 -------
1802 result : `lsst.pipe.base.Struct`
1803 Result struct with component:
1804 - ``exposure`` : `afw.image.Exposure`
1805 The fully ISR corrected exposure.
1806
1807 Raises
1808 ------
1809 RuntimeError
1810 Raised if a configuration option is set to True, but the
1811 required calibration data does not exist.
1812
1813 """
1814 self.log.info("Performing ISR on sensor %s.", sensorRef.dataId)
1815
1816 ccdExposure = sensorRef.get(self.config.datasetType)
1817
1818 camera = sensorRef.get("camera")
1819 isrData = self.readIsrDatareadIsrData(sensorRef, ccdExposure)
1820
1821 result = self.runrun(ccdExposure, camera=camera, **isrData.getDict())
1822
1823 if self.config.doWrite:
1824 sensorRef.put(result.exposure, "postISRCCD")
1825 if result.preInterpExposure is not None:
1826 sensorRef.put(result.preInterpExposure, "postISRCCD_uninterpolated")
1827 if result.ossThumb is not None:
1828 isrQa.writeThumbnail(sensorRef, result.ossThumb, "ossThumb")
1829 if result.flattenedThumb is not None:
1830 isrQa.writeThumbnail(sensorRef, result.flattenedThumb, "flattenedThumb")
1831
1832 return result
1833
1834 def getIsrExposure(self, dataRef, datasetType, dateObs=None, immediate=True):
1835 """Retrieve a calibration dataset for removing instrument signature.
1836
1837 Parameters
1838 ----------
1839
1841 DataRef of the detector data to find calibration datasets
1842 for.
1843 datasetType : `str`
1844 Type of dataset to retrieve (e.g. 'bias', 'flat', etc).
1845 dateObs : `str`, optional
1846 Date of the observation. Used to correct butler failures
1847 when using fallback filters.
1848 immediate : `Bool`
1849 If True, disable butler proxies to enable error handling
1850 within this routine.
1851
1852 Returns
1853 -------
1854 exposure : `lsst.afw.image.Exposure`
1855 Requested calibration frame.
1856
1857 Raises
1858 ------
1859 RuntimeError
1860 Raised if no matching calibration frame can be found.
1861 """
1862 try:
1863 exp = dataRef.get(datasetType, immediate=immediate)
1864 except Exception as exc1:
1865 if not self.config.fallbackFilterName:
1866 raise RuntimeError("Unable to retrieve %s for %s: %s." % (datasetType, dataRef.dataId, exc1))
1867 try:
1868 if self.config.useFallbackDate and dateObs:
1869 exp = dataRef.get(datasetType, filter=self.config.fallbackFilterName,
1870 dateObs=dateObs, immediate=immediate)
1871 else:
1872 exp = dataRef.get(datasetType, filter=self.config.fallbackFilterName, immediate=immediate)
1873 except Exception as exc2:
1874 raise RuntimeError("Unable to retrieve %s for %s, even with fallback filter %s: %s AND %s." %
1875 (datasetType, dataRef.dataId, self.config.fallbackFilterName, exc1, exc2))
1876 self.log.warning("Using fallback calibration from filter %s.", self.config.fallbackFilterName)
1877
1878 if self.config.doAssembleIsrExposures:
1879 exp = self.assembleCcd.assembleCcd(exp)
1880 return exp
1881
1882 def ensureExposure(self, inputExp, camera=None, detectorNum=None):
1883 """Ensure that the data returned by Butler is a fully constructed exp.
1884
1885 ISR requires exposure-level image data for historical reasons, so if we
1886 did not recieve that from Butler, construct it from what we have,
1887 modifying the input in place.
1888
1889 Parameters
1890 ----------
1891 inputExp : `lsst.afw.image.Exposure`, `lsst.afw.image.DecoratedImageU`,
1892 or `lsst.afw.image.ImageF`
1893 The input data structure obtained from Butler.
1894 camera : `lsst.afw.cameraGeom.camera`, optional
1895 The camera associated with the image. Used to find the appropriate
1896 detector if detector is not already set.
1897 detectorNum : `int`, optional
1898 The detector in the camera to attach, if the detector is not
1899 already set.
1900
1901 Returns
1902 -------
1903 inputExp : `lsst.afw.image.Exposure`
1904 The re-constructed exposure, with appropriate detector parameters.
1905
1906 Raises
1907 ------
1908 TypeError
1909 Raised if the input data cannot be used to construct an exposure.
1910 """
1911 if isinstance(inputExp, afwImage.DecoratedImageU):
1912 inputExp = afwImage.makeExposure(afwImage.makeMaskedImage(inputExp))
1913 elif isinstance(inputExp, afwImage.ImageF):
1914 inputExp = afwImage.makeExposure(afwImage.makeMaskedImage(inputExp))
1915 elif isinstance(inputExp, afwImage.MaskedImageF):
1916 inputExp = afwImage.makeExposure(inputExp)
1917 elif isinstance(inputExp, afwImage.Exposure):
1918 pass
1919 elif inputExp is None:
1920 # Assume this will be caught by the setup if it is a problem.
1921 return inputExp
1922 else:
1923 raise TypeError("Input Exposure is not known type in isrTask.ensureExposure: %s." %
1924 (type(inputExp), ))
1925
1926 if inputExp.getDetector() is None:
1927 if camera is None or detectorNum is None:
1928 raise RuntimeError('Must supply both a camera and detector number when using exposures '
1929 'without a detector set.')
1930 inputExp.setDetector(camera[detectorNum])
1931
1932 return inputExp
1933
1934 def convertIntToFloat(self, exposure):
1935 """Convert exposure image from uint16 to float.
1936
1937 If the exposure does not need to be converted, the input is
1938 immediately returned. For exposures that are converted to use
1939 floating point pixels, the variance is set to unity and the
1940 mask to zero.
1941
1942 Parameters
1943 ----------
1944 exposure : `lsst.afw.image.Exposure`
1945 The raw exposure to be converted.
1946
1947 Returns
1948 -------
1949 newexposure : `lsst.afw.image.Exposure`
1950 The input ``exposure``, converted to floating point pixels.
1951
1952 Raises
1953 ------
1954 RuntimeError
1955 Raised if the exposure type cannot be converted to float.
1956
1957 """
1958 if isinstance(exposure, afwImage.ExposureF):
1959 # Nothing to be done
1960 self.log.debug("Exposure already of type float.")
1961 return exposure
1962 if not hasattr(exposure, "convertF"):
1963 raise RuntimeError("Unable to convert exposure (%s) to float." % type(exposure))
1964
1965 newexposure = exposure.convertF()
1966 newexposure.variance[:] = 1
1967 newexposure.mask[:] = 0x0
1968
1969 return newexposure
1970
1971 def maskAmplifier(self, ccdExposure, amp, defects):
1972 """Identify bad amplifiers, saturated and suspect pixels.
1973
1974 Parameters
1975 ----------
1976 ccdExposure : `lsst.afw.image.Exposure`
1977 Input exposure to be masked.
1979 Catalog of parameters defining the amplifier on this
1980 exposure to mask.
1981 defects : `lsst.ip.isr.Defects`
1982 List of defects. Used to determine if the entire
1983 amplifier is bad.
1984
1985 Returns
1986 -------
1987 badAmp : `Bool`
1988 If this is true, the entire amplifier area is covered by
1989 defects and unusable.
1990
1991 """
1992 maskedImage = ccdExposure.getMaskedImage()
1993
1994 badAmp = False
1995
1996 # Check if entire amp region is defined as a defect
1997 # NB: need to use amp.getBBox() for correct comparison with current
1998 # defects definition.
1999 if defects is not None:
2000 badAmp = bool(sum([v.getBBox().contains(amp.getBBox()) for v in defects]))
2001
2002 # In the case of a bad amp, we will set mask to "BAD"
2003 # (here use amp.getRawBBox() for correct association with pixels in
2004 # current ccdExposure).
2005 if badAmp:
2006 dataView = afwImage.MaskedImageF(maskedImage, amp.getRawBBox(),
2007 afwImage.PARENT)
2008 maskView = dataView.getMask()
2009 maskView |= maskView.getPlaneBitMask("BAD")
2010 del maskView
2011 return badAmp
2012
2013 # Mask remaining defects after assembleCcd() to allow for defects that
2014 # cross amplifier boundaries. Saturation and suspect pixels can be
2015 # masked now, though.
2016 limits = dict()
2017 if self.config.doSaturation and not badAmp:
2018 limits.update({self.config.saturatedMaskName: amp.getSaturation()})
2019 if self.config.doSuspect and not badAmp:
2020 limits.update({self.config.suspectMaskName: amp.getSuspectLevel()})
2021 if math.isfinite(self.config.saturation):
2022 limits.update({self.config.saturatedMaskName: self.config.saturation})
2023
2024 for maskName, maskThreshold in limits.items():
2025 if not math.isnan(maskThreshold):
2026 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
2027 isrFunctions.makeThresholdMask(
2028 maskedImage=dataView,
2029 threshold=maskThreshold,
2030 growFootprints=0,
2031 maskName=maskName
2032 )
2033
2034 # Determine if we've fully masked this amplifier with SUSPECT and
2035 # SAT pixels.
2036 maskView = afwImage.Mask(maskedImage.getMask(), amp.getRawDataBBox(),
2037 afwImage.PARENT)
2038 maskVal = maskView.getPlaneBitMask([self.config.saturatedMaskName,
2039 self.config.suspectMaskName])
2040 if numpy.all(maskView.getArray() & maskVal > 0):
2041 badAmp = True
2042 maskView |= maskView.getPlaneBitMask("BAD")
2043
2044 return badAmp
2045
2046 def overscanCorrection(self, ccdExposure, amp):
2047 """Apply overscan correction in place.
2048
2049 This method does initial pixel rejection of the overscan
2050 region. The overscan can also be optionally segmented to
2051 allow for discontinuous overscan responses to be fit
2052 separately. The actual overscan subtraction is performed by
2053 the `lsst.ip.isr.isrFunctions.overscanCorrection` function,
2054 which is called here after the amplifier is preprocessed.
2055
2056 Parameters
2057 ----------
2058 ccdExposure : `lsst.afw.image.Exposure`
2059 Exposure to have overscan correction performed.
2060 amp : `lsst.afw.cameraGeom.Amplifer`
2061 The amplifier to consider while correcting the overscan.
2062
2063 Returns
2064 -------
2065 overscanResults : `lsst.pipe.base.Struct`
2066 Result struct with components:
2067 - ``imageFit`` : scalar or `lsst.afw.image.Image`
2068 Value or fit subtracted from the amplifier image data.
2069 - ``overscanFit`` : scalar or `lsst.afw.image.Image`
2070 Value or fit subtracted from the overscan image data.
2071 - ``overscanImage`` : `lsst.afw.image.Image`
2072 Image of the overscan region with the overscan
2073 correction applied. This quantity is used to estimate
2074 the amplifier read noise empirically.
2075
2076 Raises
2077 ------
2078 RuntimeError
2079 Raised if the ``amp`` does not contain raw pixel information.
2080
2081 See Also
2082 --------
2083 lsst.ip.isr.isrFunctions.overscanCorrection
2084 """
2085 if amp.getRawHorizontalOverscanBBox().isEmpty():
2086 self.log.info("ISR_OSCAN: No overscan region. Not performing overscan correction.")
2087 return None
2088
2089 statControl = afwMath.StatisticsControl()
2090 statControl.setAndMask(ccdExposure.mask.getPlaneBitMask("SAT"))
2091
2092 # Determine the bounding boxes
2093 dataBBox = amp.getRawDataBBox()
2094 oscanBBox = amp.getRawHorizontalOverscanBBox()
2095 dx0 = 0
2096 dx1 = 0
2097
2098 prescanBBox = amp.getRawPrescanBBox()
2099 if (oscanBBox.getBeginX() > prescanBBox.getBeginX()): # amp is at the right
2100 dx0 += self.config.overscanNumLeadingColumnsToSkip
2101 dx1 -= self.config.overscanNumTrailingColumnsToSkip
2102 else:
2103 dx0 += self.config.overscanNumTrailingColumnsToSkip
2104 dx1 -= self.config.overscanNumLeadingColumnsToSkip
2105
2106 # Determine if we need to work on subregions of the amplifier
2107 # and overscan.
2108 imageBBoxes = []
2109 overscanBBoxes = []
2110
2111 if ((self.config.overscanBiasJump
2112 and self.config.overscanBiasJumpLocation)
2113 and (ccdExposure.getMetadata().exists(self.config.overscanBiasJumpKeyword)
2114 and ccdExposure.getMetadata().getScalar(self.config.overscanBiasJumpKeyword) in
2115 self.config.overscanBiasJumpDevices)):
2116 if amp.getReadoutCorner() in (ReadoutCorner.LL, ReadoutCorner.LR):
2117 yLower = self.config.overscanBiasJumpLocation
2118 yUpper = dataBBox.getHeight() - yLower
2119 else:
2120 yUpper = self.config.overscanBiasJumpLocation
2121 yLower = dataBBox.getHeight() - yUpper
2122
2123 imageBBoxes.append(lsst.geom.Box2I(dataBBox.getBegin(),
2124 lsst.geom.Extent2I(dataBBox.getWidth(), yLower)))
2125 overscanBBoxes.append(lsst.geom.Box2I(oscanBBox.getBegin() + lsst.geom.Extent2I(dx0, 0),
2126 lsst.geom.Extent2I(oscanBBox.getWidth() - dx0 + dx1,
2127 yLower)))
2128
2129 imageBBoxes.append(lsst.geom.Box2I(dataBBox.getBegin() + lsst.geom.Extent2I(0, yLower),
2130 lsst.geom.Extent2I(dataBBox.getWidth(), yUpper)))
2131 overscanBBoxes.append(lsst.geom.Box2I(oscanBBox.getBegin() + lsst.geom.Extent2I(dx0, yLower),
2132 lsst.geom.Extent2I(oscanBBox.getWidth() - dx0 + dx1,
2133 yUpper)))
2134 else:
2135 imageBBoxes.append(lsst.geom.Box2I(dataBBox.getBegin(),
2136 lsst.geom.Extent2I(dataBBox.getWidth(), dataBBox.getHeight())))
2137 overscanBBoxes.append(lsst.geom.Box2I(oscanBBox.getBegin() + lsst.geom.Extent2I(dx0, 0),
2138 lsst.geom.Extent2I(oscanBBox.getWidth() - dx0 + dx1,
2139 oscanBBox.getHeight())))
2140
2141 # Perform overscan correction on subregions, ensuring saturated
2142 # pixels are masked.
2143 for imageBBox, overscanBBox in zip(imageBBoxes, overscanBBoxes):
2144 ampImage = ccdExposure.maskedImage[imageBBox]
2145 overscanImage = ccdExposure.maskedImage[overscanBBox]
2146
2147 overscanArray = overscanImage.image.array
2148 median = numpy.ma.median(numpy.ma.masked_where(overscanImage.mask.array, overscanArray))
2149 bad = numpy.where(numpy.abs(overscanArray - median) > self.config.overscanMaxDev)
2150 overscanImage.mask.array[bad] = overscanImage.mask.getPlaneBitMask("SAT")
2151
2152 statControl = afwMath.StatisticsControl()
2153 statControl.setAndMask(ccdExposure.mask.getPlaneBitMask("SAT"))
2154
2155 overscanResults = self.overscan.run(ampImage.getImage(), overscanImage, amp)
2156
2157 # Measure average overscan levels and record them in the metadata.
2158 levelStat = afwMath.MEDIAN
2159 sigmaStat = afwMath.STDEVCLIP
2160
2161 sctrl = afwMath.StatisticsControl(self.config.qa.flatness.clipSigma,
2162 self.config.qa.flatness.nIter)
2163 metadata = ccdExposure.getMetadata()
2164 ampNum = amp.getName()
2165 # if self.config.overscanFitType in ("MEDIAN", "MEAN", "MEANCLIP"):
2166 if isinstance(overscanResults.overscanFit, float):
2167 metadata[f"ISR_OSCAN_LEVEL{ampNum}"] = overscanResults.overscanFit
2168 metadata[f"ISR_OSCAN_SIGMA{ampNum}"] = 0.0
2169 else:
2170 stats = afwMath.makeStatistics(overscanResults.overscanFit, levelStat | sigmaStat, sctrl)
2171 metadata[f"ISR_OSCAN_LEVEL{ampNum}"] = stats.getValue(levelStat)
2172 metadata[f"ISR_OSCAN_SIGMA%{ampNum}"] = stats.getValue(sigmaStat)
2173
2174 return overscanResults
2175
2176 def updateVariance(self, ampExposure, amp, overscanImage=None, ptcDataset=None):
2177 """Set the variance plane using the gain and read noise
2178
2179 The read noise is calculated from the ``overscanImage`` if the
2180 ``doEmpiricalReadNoise`` option is set in the configuration; otherwise
2181 the value from the amplifier data is used.
2182
2183 Parameters
2184 ----------
2185 ampExposure : `lsst.afw.image.Exposure`
2186 Exposure to process.
2187 amp : `lsst.afw.table.AmpInfoRecord` or `FakeAmp`
2188 Amplifier detector data.
2189 overscanImage : `lsst.afw.image.MaskedImage`, optional.
2190 Image of overscan, required only for empirical read noise.
2191 ptcDataset : `lsst.ip.isr.PhotonTransferCurveDataset`, optional
2192 PTC dataset containing the gains and read noise.
2193
2194
2195 Raises
2196 ------
2197 RuntimeError
2198 Raised if either ``usePtcGains`` of ``usePtcReadNoise``
2199 are ``True``, but ptcDataset is not provided.
2200
2201 Raised if ```doEmpiricalReadNoise`` is ``True`` but
2202 ``overscanImage`` is ``None``.
2203
2204 See also
2205 --------
2206 lsst.ip.isr.isrFunctions.updateVariance
2207 """
2208 maskPlanes = [self.config.saturatedMaskName, self.config.suspectMaskName]
2209 if self.config.usePtcGains:
2210 if ptcDataset is None:
2211 raise RuntimeError("No ptcDataset provided to use PTC gains.")
2212 else:
2213 gain = ptcDataset.gain[amp.getName()]
2214 self.log.info("Using gain from Photon Transfer Curve.")
2215 else:
2216 gain = amp.getGain()
2217
2218 if math.isnan(gain):
2219 gain = 1.0
2220 self.log.warning("Gain set to NAN! Updating to 1.0 to generate Poisson variance.")
2221 elif gain <= 0:
2222 patchedGain = 1.0
2223 self.log.warning("Gain for amp %s == %g <= 0; setting to %f.",
2224 amp.getName(), gain, patchedGain)
2225 gain = patchedGain
2226
2227 if self.config.doEmpiricalReadNoise and overscanImage is None:
2228 raise RuntimeError("Overscan is none for EmpiricalReadNoise.")
2229
2230 if self.config.doEmpiricalReadNoise and overscanImage is not None:
2231 stats = afwMath.StatisticsControl()
2232 stats.setAndMask(overscanImage.mask.getPlaneBitMask(maskPlanes))
2233 readNoise = afwMath.makeStatistics(overscanImage, afwMath.STDEVCLIP, stats).getValue()
2234 self.log.info("Calculated empirical read noise for amp %s: %f.",
2235 amp.getName(), readNoise)
2236 elif self.config.usePtcReadNoise:
2237 if ptcDataset is None:
2238 raise RuntimeError("No ptcDataset provided to use PTC readnoise.")
2239 else:
2240 readNoise = ptcDataset.noise[amp.getName()]
2241 self.log.info("Using read noise from Photon Transfer Curve.")
2242 else:
2243 readNoise = amp.getReadNoise()
2244
2245 isrFunctions.updateVariance(
2246 maskedImage=ampExposure.getMaskedImage(),
2247 gain=gain,
2248 readNoise=readNoise,
2249 )
2250
2251 def maskNegativeVariance(self, exposure):
2252 """Identify and mask pixels with negative variance values.
2253
2254 Parameters
2255 ----------
2256 exposure : `lsst.afw.image.Exposure`
2257 Exposure to process.
2258
2259 See Also
2260 --------
2261 lsst.ip.isr.isrFunctions.updateVariance
2262 """
2263 maskPlane = exposure.getMask().getPlaneBitMask(self.config.negativeVarianceMaskName)
2264 bad = numpy.where(exposure.getVariance().getArray() <= 0.0)
2265 exposure.mask.array[bad] |= maskPlane
2266
2267 def darkCorrection(self, exposure, darkExposure, invert=False):
2268 """Apply dark correction in place.
2269
2270 Parameters
2271 ----------
2272 exposure : `lsst.afw.image.Exposure`
2273 Exposure to process.
2274 darkExposure : `lsst.afw.image.Exposure`
2275 Dark exposure of the same size as ``exposure``.
2276 invert : `Bool`, optional
2277 If True, re-add the dark to an already corrected image.
2278
2279 Raises
2280 ------
2281 RuntimeError
2282 Raised if either ``exposure`` or ``darkExposure`` do not
2283 have their dark time defined.
2284
2285 See Also
2286 --------
2287 lsst.ip.isr.isrFunctions.darkCorrection
2288 """
2289 expScale = exposure.getInfo().getVisitInfo().getDarkTime()
2290 if math.isnan(expScale):
2291 raise RuntimeError("Exposure darktime is NAN.")
2292 if darkExposure.getInfo().getVisitInfo() is not None \
2293 and not math.isnan(darkExposure.getInfo().getVisitInfo().getDarkTime()):
2294 darkScale = darkExposure.getInfo().getVisitInfo().getDarkTime()
2295 else:
2296 # DM-17444: darkExposure.getInfo.getVisitInfo() is None
2297 # so getDarkTime() does not exist.
2298 self.log.warning("darkExposure.getInfo().getVisitInfo() does not exist. Using darkScale = 1.0.")
2299 darkScale = 1.0
2300
2301 isrFunctions.darkCorrection(
2302 maskedImage=exposure.getMaskedImage(),
2303 darkMaskedImage=darkExposure.getMaskedImage(),
2304 expScale=expScale,
2305 darkScale=darkScale,
2306 invert=invert,
2307 trimToFit=self.config.doTrimToMatchCalib
2308 )
2309
2310 def doLinearize(self, detector):
2311 """Check if linearization is needed for the detector cameraGeom.
2312
2313 Checks config.doLinearize and the linearity type of the first
2314 amplifier.
2315
2316 Parameters
2317 ----------
2318 detector : `lsst.afw.cameraGeom.Detector`
2319 Detector to get linearity type from.
2320
2321 Returns
2322 -------
2323 doLinearize : `Bool`
2324 If True, linearization should be performed.
2325 """
2326 return self.config.doLinearize and \
2327 detector.getAmplifiers()[0].getLinearityType() != NullLinearityType
2328
2329 def flatCorrection(self, exposure, flatExposure, invert=False):
2330 """Apply flat correction in place.
2331
2332 Parameters
2333 ----------
2334 exposure : `lsst.afw.image.Exposure`
2335 Exposure to process.
2336 flatExposure : `lsst.afw.image.Exposure`
2337 Flat exposure of the same size as ``exposure``.
2338 invert : `Bool`, optional
2339 If True, unflatten an already flattened image.
2340
2341 See Also
2342 --------
2343 lsst.ip.isr.isrFunctions.flatCorrection
2344 """
2345 isrFunctions.flatCorrection(
2346 maskedImage=exposure.getMaskedImage(),
2347 flatMaskedImage=flatExposure.getMaskedImage(),
2348 scalingType=self.config.flatScalingType,
2349 userScale=self.config.flatUserScale,
2350 invert=invert,
2351 trimToFit=self.config.doTrimToMatchCalib
2352 )
2353
2354 def saturationDetection(self, exposure, amp):
2355 """Detect and mask saturated pixels in config.saturatedMaskName.
2356
2357 Parameters
2358 ----------
2359 exposure : `lsst.afw.image.Exposure`
2360 Exposure to process. Only the amplifier DataSec is processed.
2362 Amplifier detector data.
2363
2364 See Also
2365 --------
2366 lsst.ip.isr.isrFunctions.makeThresholdMask
2367 """
2368 if not math.isnan(amp.getSaturation()):
2369 maskedImage = exposure.getMaskedImage()
2370 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
2371 isrFunctions.makeThresholdMask(
2372 maskedImage=dataView,
2373 threshold=amp.getSaturation(),
2374 growFootprints=0,
2375 maskName=self.config.saturatedMaskName,
2376 )
2377
2378 def saturationInterpolation(self, exposure):
2379 """Interpolate over saturated pixels, in place.
2380
2381 This method should be called after `saturationDetection`, to
2382 ensure that the saturated pixels have been identified in the
2383 SAT mask. It should also be called after `assembleCcd`, since
2384 saturated regions may cross amplifier boundaries.
2385
2386 Parameters
2387 ----------
2388 exposure : `lsst.afw.image.Exposure`
2389 Exposure to process.
2390
2391 See Also
2392 --------
2393 lsst.ip.isr.isrTask.saturationDetection
2394 lsst.ip.isr.isrFunctions.interpolateFromMask
2395 """
2396 isrFunctions.interpolateFromMask(
2397 maskedImage=exposure.getMaskedImage(),
2398 fwhm=self.config.fwhm,
2399 growSaturatedFootprints=self.config.growSaturationFootprintSize,
2400 maskNameList=list(self.config.saturatedMaskName),
2401 )
2402
2403 def suspectDetection(self, exposure, amp):
2404 """Detect and mask suspect pixels in config.suspectMaskName.
2405
2406 Parameters
2407 ----------
2408 exposure : `lsst.afw.image.Exposure`
2409 Exposure to process. Only the amplifier DataSec is processed.
2411 Amplifier detector data.
2412
2413 See Also
2414 --------
2415 lsst.ip.isr.isrFunctions.makeThresholdMask
2416
2417 Notes
2418 -----
2419 Suspect pixels are pixels whose value is greater than
2420 amp.getSuspectLevel(). This is intended to indicate pixels that may be
2421 affected by unknown systematics; for example if non-linearity
2422 corrections above a certain level are unstable then that would be a
2423 useful value for suspectLevel. A value of `nan` indicates that no such
2424 level exists and no pixels are to be masked as suspicious.
2425 """
2426 suspectLevel = amp.getSuspectLevel()
2427 if math.isnan(suspectLevel):
2428 return
2429
2430 maskedImage = exposure.getMaskedImage()
2431 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
2432 isrFunctions.makeThresholdMask(
2433 maskedImage=dataView,
2434 threshold=suspectLevel,
2435 growFootprints=0,
2436 maskName=self.config.suspectMaskName,
2437 )
2438
2439 def maskDefect(self, exposure, defectBaseList):
2440 """Mask defects using mask plane "BAD", in place.
2441
2442 Parameters
2443 ----------
2444 exposure : `lsst.afw.image.Exposure`
2445 Exposure to process.
2446 defectBaseList : `lsst.ip.isr.Defects` or `list` of
2448 List of defects to mask.
2449
2450 Notes
2451 -----
2452 Call this after CCD assembly, since defects may cross amplifier
2453 boundaries.
2454 """
2455 maskedImage = exposure.getMaskedImage()
2456 if not isinstance(defectBaseList, Defects):
2457 # Promotes DefectBase to Defect
2458 defectList = Defects(defectBaseList)
2459 else:
2460 defectList = defectBaseList
2461 defectList.maskPixels(maskedImage, maskName="BAD")
2462
2463 def maskEdges(self, exposure, numEdgePixels=0, maskPlane="SUSPECT", level='DETECTOR'):
2464 """Mask edge pixels with applicable mask plane.
2465
2466 Parameters
2467 ----------
2468 exposure : `lsst.afw.image.Exposure`
2469 Exposure to process.
2470 numEdgePixels : `int`, optional
2471 Number of edge pixels to mask.
2472 maskPlane : `str`, optional
2473 Mask plane name to use.
2474 level : `str`, optional
2475 Level at which to mask edges.
2476 """
2477 maskedImage = exposure.getMaskedImage()
2478 maskBitMask = maskedImage.getMask().getPlaneBitMask(maskPlane)
2479
2480 if numEdgePixels > 0:
2481 if level == 'DETECTOR':
2482 boxes = [maskedImage.getBBox()]
2483 elif level == 'AMP':
2484 boxes = [amp.getBBox() for amp in exposure.getDetector()]
2485
2486 for box in boxes:
2487 # This makes a bbox numEdgeSuspect pixels smaller than the
2488 # image on each side
2489 subImage = maskedImage[box]
2490 box.grow(-numEdgePixels)
2491 # Mask pixels outside box
2492 SourceDetectionTask.setEdgeBits(
2493 subImage,
2494 box,
2495 maskBitMask)
2496
2497 def maskAndInterpolateDefects(self, exposure, defectBaseList):
2498 """Mask and interpolate defects using mask plane "BAD", in place.
2499
2500 Parameters
2501 ----------
2502 exposure : `lsst.afw.image.Exposure`
2503 Exposure to process.
2504 defectBaseList : `lsst.ip.isr.Defects` or `list` of
2506 List of defects to mask and interpolate.
2507
2508 See Also
2509 --------
2510 lsst.ip.isr.isrTask.maskDefect
2511 """
2512 self.maskDefectmaskDefect(exposure, defectBaseList)
2513 self.maskEdgesmaskEdges(exposure, numEdgePixels=self.config.numEdgeSuspect,
2514 maskPlane="SUSPECT", level=self.config.edgeMaskLevel)
2515 isrFunctions.interpolateFromMask(
2516 maskedImage=exposure.getMaskedImage(),
2517 fwhm=self.config.fwhm,
2518 growSaturatedFootprints=0,
2519 maskNameList=["BAD"],
2520 )
2521
2522 def maskNan(self, exposure):
2523 """Mask NaNs using mask plane "UNMASKEDNAN", in place.
2524
2525 Parameters
2526 ----------
2527 exposure : `lsst.afw.image.Exposure`
2528 Exposure to process.
2529
2530 Notes
2531 -----
2532 We mask over all non-finite values (NaN, inf), including those
2533 that are masked with other bits (because those may or may not be
2534 interpolated over later, and we want to remove all NaN/infs).
2535 Despite this behaviour, the "UNMASKEDNAN" mask plane is used to
2536 preserve the historical name.
2537 """
2538 maskedImage = exposure.getMaskedImage()
2539
2540 # Find and mask NaNs
2541 maskedImage.getMask().addMaskPlane("UNMASKEDNAN")
2542 maskVal = maskedImage.getMask().getPlaneBitMask("UNMASKEDNAN")
2543 numNans = maskNans(maskedImage, maskVal)
2544 self.metadata["NUMNANS"] = numNans
2545 if numNans > 0:
2546 self.log.warning("There were %d unmasked NaNs.", numNans)
2547
2548 def maskAndInterpolateNan(self, exposure):
2549 """"Mask and interpolate NaN/infs using mask plane "UNMASKEDNAN",
2550 in place.
2551
2552 Parameters
2553 ----------
2554 exposure : `lsst.afw.image.Exposure`
2555 Exposure to process.
2556
2557 See Also
2558 --------
2559 lsst.ip.isr.isrTask.maskNan
2560 """
2561 self.maskNanmaskNan(exposure)
2562 isrFunctions.interpolateFromMask(
2563 maskedImage=exposure.getMaskedImage(),
2564 fwhm=self.config.fwhm,
2565 growSaturatedFootprints=0,
2566 maskNameList=["UNMASKEDNAN"],
2567 )
2568
2569 def measureBackground(self, exposure, IsrQaConfig=None):
2570 """Measure the image background in subgrids, for quality control.
2571
2572 Parameters
2573 ----------
2574 exposure : `lsst.afw.image.Exposure`
2575 Exposure to process.
2576 IsrQaConfig : `lsst.ip.isr.isrQa.IsrQaConfig`
2577 Configuration object containing parameters on which background
2578 statistics and subgrids to use.
2579 """
2580 if IsrQaConfig is not None:
2581 statsControl = afwMath.StatisticsControl(IsrQaConfig.flatness.clipSigma,
2582 IsrQaConfig.flatness.nIter)
2583 maskVal = exposure.getMaskedImage().getMask().getPlaneBitMask(["BAD", "SAT", "DETECTED"])
2584 statsControl.setAndMask(maskVal)
2585 maskedImage = exposure.getMaskedImage()
2586 stats = afwMath.makeStatistics(maskedImage, afwMath.MEDIAN | afwMath.STDEVCLIP, statsControl)
2587 skyLevel = stats.getValue(afwMath.MEDIAN)
2588 skySigma = stats.getValue(afwMath.STDEVCLIP)
2589 self.log.info("Flattened sky level: %f +/- %f.", skyLevel, skySigma)
2590 metadata = exposure.getMetadata()
2591 metadata["SKYLEVEL"] = skyLevel
2592 metadata["SKYSIGMA"] = skySigma
2593
2594 # calcluating flatlevel over the subgrids
2595 stat = afwMath.MEANCLIP if IsrQaConfig.flatness.doClip else afwMath.MEAN
2596 meshXHalf = int(IsrQaConfig.flatness.meshX/2.)
2597 meshYHalf = int(IsrQaConfig.flatness.meshY/2.)
2598 nX = int((exposure.getWidth() + meshXHalf) / IsrQaConfig.flatness.meshX)
2599 nY = int((exposure.getHeight() + meshYHalf) / IsrQaConfig.flatness.meshY)
2600 skyLevels = numpy.zeros((nX, nY))
2601
2602 for j in range(nY):
2603 yc = meshYHalf + j * IsrQaConfig.flatness.meshY
2604 for i in range(nX):
2605 xc = meshXHalf + i * IsrQaConfig.flatness.meshX
2606
2607 xLLC = xc - meshXHalf
2608 yLLC = yc - meshYHalf
2609 xURC = xc + meshXHalf - 1
2610 yURC = yc + meshYHalf - 1
2611
2612 bbox = lsst.geom.Box2I(lsst.geom.Point2I(xLLC, yLLC), lsst.geom.Point2I(xURC, yURC))
2613 miMesh = maskedImage.Factory(exposure.getMaskedImage(), bbox, afwImage.LOCAL)
2614
2615 skyLevels[i, j] = afwMath.makeStatistics(miMesh, stat, statsControl).getValue()
2616
2617 good = numpy.where(numpy.isfinite(skyLevels))
2618 skyMedian = numpy.median(skyLevels[good])
2619 flatness = (skyLevels[good] - skyMedian) / skyMedian
2620 flatness_rms = numpy.std(flatness)
2621 flatness_pp = flatness.max() - flatness.min() if len(flatness) > 0 else numpy.nan
2622
2623 self.log.info("Measuring sky levels in %dx%d grids: %f.", nX, nY, skyMedian)
2624 self.log.info("Sky flatness in %dx%d grids - pp: %f rms: %f.",
2625 nX, nY, flatness_pp, flatness_rms)
2626
2627 metadata["FLATNESS_PP"] = float(flatness_pp)
2628 metadata["FLATNESS_RMS"] = float(flatness_rms)
2629 metadata["FLATNESS_NGRIDS"] = '%dx%d' % (nX, nY)
2630 metadata["FLATNESS_MESHX"] = IsrQaConfig.flatness.meshX
2631 metadata["FLATNESS_MESHY"] = IsrQaConfig.flatness.meshY
2632
2633 def roughZeroPoint(self, exposure):
2634 """Set an approximate magnitude zero point for the exposure.
2635
2636 Parameters
2637 ----------
2638 exposure : `lsst.afw.image.Exposure`
2639 Exposure to process.
2640 """
2641 filterLabel = exposure.getFilterLabel()
2642 physicalFilter = isrFunctions.getPhysicalFilter(filterLabel, self.log)
2643
2644 if physicalFilter in self.config.fluxMag0T1:
2645 fluxMag0 = self.config.fluxMag0T1[physicalFilter]
2646 else:
2647 self.log.warning("No rough magnitude zero point defined for filter %s.", physicalFilter)
2648 fluxMag0 = self.config.defaultFluxMag0T1
2649
2650 expTime = exposure.getInfo().getVisitInfo().getExposureTime()
2651 if not expTime > 0: # handle NaN as well as <= 0
2652 self.log.warning("Non-positive exposure time; skipping rough zero point.")
2653 return
2654
2655 self.log.info("Setting rough magnitude zero point for filter %s: %f",
2656 physicalFilter, 2.5*math.log10(fluxMag0*expTime))
2657 exposure.setPhotoCalib(afwImage.makePhotoCalibFromCalibZeroPoint(fluxMag0*expTime, 0.0))
2658
2659 @contextmanager
2660 def flatContext(self, exp, flat, dark=None):
2661 """Context manager that applies and removes flats and darks,
2662 if the task is configured to apply them.
2663
2664 Parameters
2665 ----------
2667 Exposure to process.
2669 Flat exposure the same size as ``exp``.
2670 dark : `lsst.afw.image.Exposure`, optional
2671 Dark exposure the same size as ``exp``.
2672
2673 Yields
2674 ------
2676 The flat and dark corrected exposure.
2677 """
2678 if self.config.doDark and dark is not None:
2679 self.darkCorrectiondarkCorrection(exp, dark)
2680 if self.config.doFlat:
2681 self.flatCorrectionflatCorrection(exp, flat)
2682 try:
2683 yield exp
2684 finally:
2685 if self.config.doFlat:
2686 self.flatCorrectionflatCorrection(exp, flat, invert=True)
2687 if self.config.doDark and dark is not None:
2688 self.darkCorrectiondarkCorrection(exp, dark, invert=True)
2689
2690 def debugView(self, exposure, stepname):
2691 """Utility function to examine ISR exposure at different stages.
2692
2693 Parameters
2694 ----------
2695 exposure : `lsst.afw.image.Exposure`
2696 Exposure to view.
2697 stepname : `str`
2698 State of processing to view.
2699 """
2700 frame = getDebugFrame(self._display, stepname)
2701 if frame:
2702 display = getDisplay(frame)
2703 display.scale('asinh', 'zscale')
2704 display.mtv(exposure)
2705 prompt = "Press Enter to continue [c]... "
2706 while True:
2707 ans = input(prompt).lower()
2708 if ans in ("", "c",):
2709 break
2710
2711
2712class FakeAmp(object):
2713 """A Detector-like object that supports returning gain and saturation level
2714
2715 This is used when the input exposure does not have a detector.
2716
2717 Parameters
2718 ----------
2719 exposure : `lsst.afw.image.Exposure`
2720 Exposure to generate a fake amplifier for.
2721 config : `lsst.ip.isr.isrTaskConfig`
2722 Configuration to apply to the fake amplifier.
2723 """
2724
2725 def __init__(self, exposure, config):
2726 self._bbox_bbox = exposure.getBBox(afwImage.LOCAL)
2727 self._RawHorizontalOverscanBBox_RawHorizontalOverscanBBox = lsst.geom.Box2I()
2728 self._gain_gain = config.gain
2729 self._readNoise_readNoise = config.readNoise
2730 self._saturation_saturation = config.saturation
2731
2732 def getBBox(self):
2733 return self._bbox_bbox
2734
2735 def getRawBBox(self):
2736 return self._bbox_bbox
2737
2739 return self._RawHorizontalOverscanBBox_RawHorizontalOverscanBBox
2740
2741 def getGain(self):
2742 return self._gain_gain
2743
2744 def getReadNoise(self):
2745 return self._readNoise_readNoise
2746
2747 def getSaturation(self):
2748 return self._saturation_saturation
2749
2751 return float("NaN")
2752
2753
2754class RunIsrConfig(pexConfig.Config):
2755 isr = pexConfig.ConfigurableField(target=IsrTask, doc="Instrument signature removal")
2756
2757
2758class RunIsrTask(pipeBase.CmdLineTask):
2759 """Task to wrap the default IsrTask to allow it to be retargeted.
2760
2761 The standard IsrTask can be called directly from a command line
2762 program, but doing so removes the ability of the task to be
2763 retargeted. As most cameras override some set of the IsrTask
2764 methods, this would remove those data-specific methods in the
2765 output post-ISR images. This wrapping class fixes the issue,
2766 allowing identical post-ISR images to be generated by both the
2767 processCcd and isrTask code.
2768 """
2769 ConfigClass = RunIsrConfig
2770 _DefaultName = "runIsr"
2771
2772 def __init__(self, *args, **kwargs):
2773 super().__init__(*args, **kwargs)
2774 self.makeSubtask("isr")
2775
2776 def runDataRef(self, dataRef):
2777 """
2778 Parameters
2779 ----------
2780 dataRef : `lsst.daf.persistence.ButlerDataRef`
2781 data reference of the detector data to be processed
2782
2783 Returns
2784 -------
2785 result : `pipeBase.Struct`
2786 Result struct with component:
2787
2788 - exposure : `lsst.afw.image.Exposure`
2789 Post-ISR processed exposure.
2790 """
2791 return self.isr.runDataRef(dataRef)
def getRawHorizontalOverscanBBox(self)
Definition: isrTask.py:2738
def __init__(self, exposure, config)
Definition: isrTask.py:2725
def __init__(self, *config=None)
Definition: isrTask.py:270
def flatCorrection(self, exposure, flatExposure, invert=False)
Definition: isrTask.py:2329
def maskAndInterpolateNan(self, exposure)
Definition: isrTask.py:2548
def saturationInterpolation(self, exposure)
Definition: isrTask.py:2378
def runDataRef(self, sensorRef)
Definition: isrTask.py:1783
def maskNan(self, exposure)
Definition: isrTask.py:2522
def maskAmplifier(self, ccdExposure, amp, defects)
Definition: isrTask.py:1971
def debugView(self, exposure, stepname)
Definition: isrTask.py:2690
def ensureExposure(self, inputExp, camera=None, detectorNum=None)
Definition: isrTask.py:1882
def getIsrExposure(self, dataRef, datasetType, dateObs=None, immediate=True)
Definition: isrTask.py:1834
def maskNegativeVariance(self, exposure)
Definition: isrTask.py:2251
def saturationDetection(self, exposure, amp)
Definition: isrTask.py:2354
def maskDefect(self, exposure, defectBaseList)
Definition: isrTask.py:2439
def __init__(self, **kwargs)
Definition: isrTask.py:985
def runQuantum(self, butlerQC, inputRefs, outputRefs)
Definition: isrTask.py:996
def maskEdges(self, exposure, numEdgePixels=0, maskPlane="SUSPECT", level='DETECTOR')
Definition: isrTask.py:2463
def overscanCorrection(self, ccdExposure, amp)
Definition: isrTask.py:2046
def measureBackground(self, exposure, IsrQaConfig=None)
Definition: isrTask.py:2569
def roughZeroPoint(self, exposure)
Definition: isrTask.py:2633
def maskAndInterpolateDefects(self, exposure, defectBaseList)
Definition: isrTask.py:2497
def readIsrData(self, dataRef, rawExposure)
Definition: isrTask.py:1095
def run(self, ccdExposure, *camera=None, bias=None, linearizer=None, crosstalk=None, crosstalkSources=None, dark=None, flat=None, ptc=None, bfKernel=None, bfGains=None, defects=None, fringes=pipeBase.Struct(fringes=None), opticsTransmission=None, filterTransmission=None, sensorTransmission=None, atmosphereTransmission=None, detectorNum=None, strayLightData=None, illumMaskedImage=None, isGen3=False)
Definition: isrTask.py:1282
def doLinearize(self, detector)
Definition: isrTask.py:2310
def flatContext(self, exp, flat, dark=None)
Definition: isrTask.py:2660
def convertIntToFloat(self, exposure)
Definition: isrTask.py:1934
def suspectDetection(self, exposure, amp)
Definition: isrTask.py:2403
def updateVariance(self, ampExposure, amp, overscanImage=None, ptcDataset=None)
Definition: isrTask.py:2176
def darkCorrection(self, exposure, darkExposure, invert=False)
Definition: isrTask.py:2267
def __init__(self, *args, **kwargs)
Definition: isrTask.py:2772
def runDataRef(self, dataRef)
Definition: isrTask.py:2776
def checkFilter(exposure, filterList, log)
def crosstalkSourceLookup(datasetType, registry, quantumDataId, collections)
Definition: isrTask.py:62
size_t maskNans(afw::image::MaskedImage< PixelT > const &mi, afw::image::MaskPixel maskVal, afw::image::MaskPixel allow=0)
Mask NANs in an image.
Definition: Isr.cc:35