32 from contextlib
import contextmanager
33 from lsstDebug
import getDebugFrame
43 from .
import isrFunctions
45 from .
import linearize
46 from .defects
import Defects
48 from .assembleCcdTask
import AssembleCcdTask
49 from .crosstalk
import CrosstalkTask, CrosstalkCalib
50 from .fringe
import FringeTask
51 from .isr
import maskNans
52 from .masking
import MaskingTask
53 from .overscan
import OverscanCorrectionTask
54 from .straylight
import StrayLightTask
55 from .vignette
import VignetteTask
56 from lsst.daf.butler
import DimensionGraph
59 __all__ = [
"IsrTask",
"IsrTaskConfig",
"RunIsrTask",
"RunIsrConfig"]
63 """Lookup function to identify crosstalkSource entries.
65 This should return an empty list under most circumstances. Only
66 when inter-chip crosstalk has been identified should this be
69 This will be unused until DM-25348 resolves the quantum graph
76 registry : `lsst.daf.butler.Registry`
77 Butler registry to query.
78 quantumDataId : `lsst.daf.butler.ExpandedDataCoordinate`
79 Data id to transform to identify crosstalkSources. The
80 ``detector`` entry will be stripped.
81 collections : `lsst.daf.butler.CollectionSearch`
82 Collections to search through.
86 results : `list` [`lsst.daf.butler.DatasetRef`]
87 List of datasets that match the query that will be used as
90 newDataId = quantumDataId.subset(DimensionGraph(registry.dimensions, names=[
"instrument",
"exposure"]))
91 results =
list(registry.queryDatasets(datasetType,
92 collections=collections,
100 dimensions={
"instrument",
"exposure",
"detector"},
101 defaultTemplates={}):
102 ccdExposure = cT.Input(
104 doc=
"Input exposure to process.",
105 storageClass=
"Exposure",
106 dimensions=[
"instrument",
"exposure",
"detector"],
108 camera = cT.PrerequisiteInput(
110 storageClass=
"Camera",
111 doc=
"Input camera to construct complete exposures.",
112 dimensions=[
"instrument"],
116 crosstalk = cT.PrerequisiteInput(
118 doc=
"Input crosstalk object",
119 storageClass=
"CrosstalkCalib",
120 dimensions=[
"instrument",
"detector"],
125 crosstalkSources = cT.PrerequisiteInput(
126 name=
"isrOverscanCorrected",
127 doc=
"Overscan corrected input images.",
128 storageClass=
"Exposure",
129 dimensions=[
"instrument",
"exposure",
"detector"],
132 lookupFunction=crosstalkSourceLookup,
134 bias = cT.PrerequisiteInput(
136 doc=
"Input bias calibration.",
137 storageClass=
"ExposureF",
138 dimensions=[
"instrument",
"detector"],
141 dark = cT.PrerequisiteInput(
143 doc=
"Input dark calibration.",
144 storageClass=
"ExposureF",
145 dimensions=[
"instrument",
"detector"],
148 flat = cT.PrerequisiteInput(
150 doc=
"Input flat calibration.",
151 storageClass=
"ExposureF",
152 dimensions=[
"instrument",
"physical_filter",
"detector"],
155 fringes = cT.PrerequisiteInput(
157 doc=
"Input fringe calibration.",
158 storageClass=
"ExposureF",
159 dimensions=[
"instrument",
"physical_filter",
"detector"],
162 strayLightData = cT.PrerequisiteInput(
164 doc=
"Input stray light calibration.",
165 storageClass=
"StrayLightData",
166 dimensions=[
"instrument",
"physical_filter",
"detector"],
169 bfKernel = cT.PrerequisiteInput(
171 doc=
"Input brighter-fatter kernel.",
172 storageClass=
"NumpyArray",
173 dimensions=[
"instrument"],
176 newBFKernel = cT.PrerequisiteInput(
177 name=
'brighterFatterKernel',
178 doc=
"Newer complete kernel + gain solutions.",
179 storageClass=
"BrighterFatterKernel",
180 dimensions=[
"instrument",
"detector"],
183 defects = cT.PrerequisiteInput(
185 doc=
"Input defect tables.",
186 storageClass=
"Defects",
187 dimensions=[
"instrument",
"detector"],
190 linearizer = cT.PrerequisiteInput(
192 storageClass=
"Linearizer",
193 doc=
"Linearity correction calibration.",
194 dimensions=[
"instrument",
"detector"],
197 opticsTransmission = cT.PrerequisiteInput(
198 name=
"transmission_optics",
199 storageClass=
"TransmissionCurve",
200 doc=
"Transmission curve due to the optics.",
201 dimensions=[
"instrument"],
204 filterTransmission = cT.PrerequisiteInput(
205 name=
"transmission_filter",
206 storageClass=
"TransmissionCurve",
207 doc=
"Transmission curve due to the filter.",
208 dimensions=[
"instrument",
"physical_filter"],
211 sensorTransmission = cT.PrerequisiteInput(
212 name=
"transmission_sensor",
213 storageClass=
"TransmissionCurve",
214 doc=
"Transmission curve due to the sensor.",
215 dimensions=[
"instrument",
"detector"],
218 atmosphereTransmission = cT.PrerequisiteInput(
219 name=
"transmission_atmosphere",
220 storageClass=
"TransmissionCurve",
221 doc=
"Transmission curve due to the atmosphere.",
222 dimensions=[
"instrument"],
225 illumMaskedImage = cT.PrerequisiteInput(
227 doc=
"Input illumination correction.",
228 storageClass=
"MaskedImageF",
229 dimensions=[
"instrument",
"physical_filter",
"detector"],
233 outputExposure = cT.Output(
235 doc=
"Output ISR processed exposure.",
236 storageClass=
"Exposure",
237 dimensions=[
"instrument",
"exposure",
"detector"],
239 preInterpExposure = cT.Output(
240 name=
'preInterpISRCCD',
241 doc=
"Output ISR processed exposure, with pixels left uninterpolated.",
242 storageClass=
"ExposureF",
243 dimensions=[
"instrument",
"exposure",
"detector"],
245 outputOssThumbnail = cT.Output(
247 doc=
"Output Overscan-subtracted thumbnail image.",
248 storageClass=
"Thumbnail",
249 dimensions=[
"instrument",
"exposure",
"detector"],
251 outputFlattenedThumbnail = cT.Output(
252 name=
"FlattenedThumb",
253 doc=
"Output flat-corrected thumbnail image.",
254 storageClass=
"Thumbnail",
255 dimensions=[
"instrument",
"exposure",
"detector"],
261 if config.doBias
is not True:
262 self.prerequisiteInputs.discard(
"bias")
263 if config.doLinearize
is not True:
264 self.prerequisiteInputs.discard(
"linearizer")
265 if config.doCrosstalk
is not True:
266 self.inputs.discard(
"crosstalkSources")
267 self.prerequisiteInputs.discard(
"crosstalk")
268 if config.doBrighterFatter
is not True:
269 self.prerequisiteInputs.discard(
"bfKernel")
270 self.prerequisiteInputs.discard(
"newBFKernel")
271 if config.doDefect
is not True:
272 self.prerequisiteInputs.discard(
"defects")
273 if config.doDark
is not True:
274 self.prerequisiteInputs.discard(
"dark")
275 if config.doFlat
is not True:
276 self.prerequisiteInputs.discard(
"flat")
277 if config.doAttachTransmissionCurve
is not True:
278 self.prerequisiteInputs.discard(
"opticsTransmission")
279 self.prerequisiteInputs.discard(
"filterTransmission")
280 self.prerequisiteInputs.discard(
"sensorTransmission")
281 self.prerequisiteInputs.discard(
"atmosphereTransmission")
282 if config.doUseOpticsTransmission
is not True:
283 self.prerequisiteInputs.discard(
"opticsTransmission")
284 if config.doUseFilterTransmission
is not True:
285 self.prerequisiteInputs.discard(
"filterTransmission")
286 if config.doUseSensorTransmission
is not True:
287 self.prerequisiteInputs.discard(
"sensorTransmission")
288 if config.doUseAtmosphereTransmission
is not True:
289 self.prerequisiteInputs.discard(
"atmosphereTransmission")
290 if config.doIlluminationCorrection
is not True:
291 self.prerequisiteInputs.discard(
"illumMaskedImage")
293 if config.doWrite
is not True:
294 self.outputs.discard(
"outputExposure")
295 self.outputs.discard(
"preInterpExposure")
296 self.outputs.discard(
"outputFlattenedThumbnail")
297 self.outputs.discard(
"outputOssThumbnail")
298 if config.doSaveInterpPixels
is not True:
299 self.outputs.discard(
"preInterpExposure")
300 if config.qa.doThumbnailOss
is not True:
301 self.outputs.discard(
"outputOssThumbnail")
302 if config.qa.doThumbnailFlattened
is not True:
303 self.outputs.discard(
"outputFlattenedThumbnail")
307 pipelineConnections=IsrTaskConnections):
308 """Configuration parameters for IsrTask.
310 Items are grouped in the order in which they are executed by the task.
312 datasetType = pexConfig.Field(
314 doc=
"Dataset type for input data; users will typically leave this alone, "
315 "but camera-specific ISR tasks will override it",
319 fallbackFilterName = pexConfig.Field(
321 doc=
"Fallback default filter name for calibrations.",
324 useFallbackDate = pexConfig.Field(
326 doc=
"Pass observation date when using fallback filter.",
329 expectWcs = pexConfig.Field(
332 doc=
"Expect input science images to have a WCS (set False for e.g. spectrographs)."
334 fwhm = pexConfig.Field(
336 doc=
"FWHM of PSF in arcseconds.",
339 qa = pexConfig.ConfigField(
341 doc=
"QA related configuration options.",
345 doConvertIntToFloat = pexConfig.Field(
347 doc=
"Convert integer raw images to floating point values?",
352 doSaturation = pexConfig.Field(
354 doc=
"Mask saturated pixels? NB: this is totally independent of the"
355 " interpolation option - this is ONLY setting the bits in the mask."
356 " To have them interpolated make sure doSaturationInterpolation=True",
359 saturatedMaskName = pexConfig.Field(
361 doc=
"Name of mask plane to use in saturation detection and interpolation",
364 saturation = pexConfig.Field(
366 doc=
"The saturation level to use if no Detector is present in the Exposure (ignored if NaN)",
367 default=float(
"NaN"),
369 growSaturationFootprintSize = pexConfig.Field(
371 doc=
"Number of pixels by which to grow the saturation footprints",
376 doSuspect = pexConfig.Field(
378 doc=
"Mask suspect pixels?",
381 suspectMaskName = pexConfig.Field(
383 doc=
"Name of mask plane to use for suspect pixels",
386 numEdgeSuspect = pexConfig.Field(
388 doc=
"Number of edge pixels to be flagged as untrustworthy.",
391 edgeMaskLevel = pexConfig.ChoiceField(
393 doc=
"Mask edge pixels in which coordinate frame: DETECTOR or AMP?",
396 'DETECTOR':
'Mask only the edges of the full detector.',
397 'AMP':
'Mask edges of each amplifier.',
402 doSetBadRegions = pexConfig.Field(
404 doc=
"Should we set the level of all BAD patches of the chip to the chip's average value?",
407 badStatistic = pexConfig.ChoiceField(
409 doc=
"How to estimate the average value for BAD regions.",
412 "MEANCLIP":
"Correct using the (clipped) mean of good data",
413 "MEDIAN":
"Correct using the median of the good data",
418 doOverscan = pexConfig.Field(
420 doc=
"Do overscan subtraction?",
423 overscan = pexConfig.ConfigurableField(
424 target=OverscanCorrectionTask,
425 doc=
"Overscan subtraction task for image segments.",
428 overscanFitType = pexConfig.ChoiceField(
430 doc=
"The method for fitting the overscan bias level.",
433 "POLY":
"Fit ordinary polynomial to the longest axis of the overscan region",
434 "CHEB":
"Fit Chebyshev polynomial to the longest axis of the overscan region",
435 "LEG":
"Fit Legendre polynomial to the longest axis of the overscan region",
436 "NATURAL_SPLINE":
"Fit natural spline to the longest axis of the overscan region",
437 "CUBIC_SPLINE":
"Fit cubic spline to the longest axis of the overscan region",
438 "AKIMA_SPLINE":
"Fit Akima spline to the longest axis of the overscan region",
439 "MEAN":
"Correct using the mean of the overscan region",
440 "MEANCLIP":
"Correct using a clipped mean of the overscan region",
441 "MEDIAN":
"Correct using the median of the overscan region",
442 "MEDIAN_PER_ROW":
"Correct using the median per row of the overscan region",
444 deprecated=(
"Please configure overscan via the OverscanCorrectionConfig interface."
445 " This option will no longer be used, and will be removed after v20.")
447 overscanOrder = pexConfig.Field(
449 doc=(
"Order of polynomial or to fit if overscan fit type is a polynomial, "
450 "or number of spline knots if overscan fit type is a spline."),
452 deprecated=(
"Please configure overscan via the OverscanCorrectionConfig interface."
453 " This option will no longer be used, and will be removed after v20.")
455 overscanNumSigmaClip = pexConfig.Field(
457 doc=
"Rejection threshold (sigma) for collapsing overscan before fit",
459 deprecated=(
"Please configure overscan via the OverscanCorrectionConfig interface."
460 " This option will no longer be used, and will be removed after v20.")
462 overscanIsInt = pexConfig.Field(
464 doc=
"Treat overscan as an integer image for purposes of overscan.FitType=MEDIAN"
465 " and overscan.FitType=MEDIAN_PER_ROW.",
467 deprecated=(
"Please configure overscan via the OverscanCorrectionConfig interface."
468 " This option will no longer be used, and will be removed after v20.")
471 overscanNumLeadingColumnsToSkip = pexConfig.Field(
473 doc=
"Number of columns to skip in overscan, i.e. those closest to amplifier",
476 overscanNumTrailingColumnsToSkip = pexConfig.Field(
478 doc=
"Number of columns to skip in overscan, i.e. those farthest from amplifier",
481 overscanMaxDev = pexConfig.Field(
483 doc=
"Maximum deviation from the median for overscan",
484 default=1000.0, check=
lambda x: x > 0
486 overscanBiasJump = pexConfig.Field(
488 doc=
"Fit the overscan in a piecewise-fashion to correct for bias jumps?",
491 overscanBiasJumpKeyword = pexConfig.Field(
493 doc=
"Header keyword containing information about devices.",
494 default=
"NO_SUCH_KEY",
496 overscanBiasJumpDevices = pexConfig.ListField(
498 doc=
"List of devices that need piecewise overscan correction.",
501 overscanBiasJumpLocation = pexConfig.Field(
503 doc=
"Location of bias jump along y-axis.",
508 doAssembleCcd = pexConfig.Field(
511 doc=
"Assemble amp-level exposures into a ccd-level exposure?"
513 assembleCcd = pexConfig.ConfigurableField(
514 target=AssembleCcdTask,
515 doc=
"CCD assembly task",
519 doAssembleIsrExposures = pexConfig.Field(
522 doc=
"Assemble amp-level calibration exposures into ccd-level exposure?"
524 doTrimToMatchCalib = pexConfig.Field(
527 doc=
"Trim raw data to match calibration bounding boxes?"
531 doBias = pexConfig.Field(
533 doc=
"Apply bias frame correction?",
536 biasDataProductName = pexConfig.Field(
538 doc=
"Name of the bias data product",
541 doBiasBeforeOverscan = pexConfig.Field(
543 doc=
"Reverse order of overscan and bias correction.",
548 doVariance = pexConfig.Field(
550 doc=
"Calculate variance?",
553 gain = pexConfig.Field(
555 doc=
"The gain to use if no Detector is present in the Exposure (ignored if NaN)",
556 default=float(
"NaN"),
558 readNoise = pexConfig.Field(
560 doc=
"The read noise to use if no Detector is present in the Exposure",
563 doEmpiricalReadNoise = pexConfig.Field(
566 doc=
"Calculate empirical read noise instead of value from AmpInfo data?"
570 doLinearize = pexConfig.Field(
572 doc=
"Correct for nonlinearity of the detector's response?",
577 doCrosstalk = pexConfig.Field(
579 doc=
"Apply intra-CCD crosstalk correction?",
582 doCrosstalkBeforeAssemble = pexConfig.Field(
584 doc=
"Apply crosstalk correction before CCD assembly, and before trimming?",
587 crosstalk = pexConfig.ConfigurableField(
588 target=CrosstalkTask,
589 doc=
"Intra-CCD crosstalk correction",
593 doDefect = pexConfig.Field(
595 doc=
"Apply correction for CCD defects, e.g. hot pixels?",
598 doNanMasking = pexConfig.Field(
600 doc=
"Mask non-finite (NAN, inf) pixels?",
603 doWidenSaturationTrails = pexConfig.Field(
605 doc=
"Widen bleed trails based on their width?",
610 doBrighterFatter = pexConfig.Field(
613 doc=
"Apply the brighter-fatter correction?"
615 brighterFatterLevel = pexConfig.ChoiceField(
618 doc=
"The level at which to correct for brighter-fatter.",
620 "AMP":
"Every amplifier treated separately.",
621 "DETECTOR":
"One kernel per detector",
624 brighterFatterMaxIter = pexConfig.Field(
627 doc=
"Maximum number of iterations for the brighter-fatter correction"
629 brighterFatterThreshold = pexConfig.Field(
632 doc=
"Threshold used to stop iterating the brighter-fatter correction. It is the "
633 "absolute value of the difference between the current corrected image and the one "
634 "from the previous iteration summed over all the pixels."
636 brighterFatterApplyGain = pexConfig.Field(
639 doc=
"Should the gain be applied when applying the brighter-fatter correction?"
641 brighterFatterMaskListToInterpolate = pexConfig.ListField(
643 doc=
"List of mask planes that should be interpolated over when applying the brighter-fatter "
645 default=[
"SAT",
"BAD",
"NO_DATA",
"UNMASKEDNAN"],
647 brighterFatterMaskGrowSize = pexConfig.Field(
650 doc=
"Number of pixels to grow the masks listed in config.brighterFatterMaskListToInterpolate "
651 "when brighter-fatter correction is applied."
655 doDark = pexConfig.Field(
657 doc=
"Apply dark frame correction?",
660 darkDataProductName = pexConfig.Field(
662 doc=
"Name of the dark data product",
667 doStrayLight = pexConfig.Field(
669 doc=
"Subtract stray light in the y-band (due to encoder LEDs)?",
672 strayLight = pexConfig.ConfigurableField(
673 target=StrayLightTask,
674 doc=
"y-band stray light correction"
678 doFlat = pexConfig.Field(
680 doc=
"Apply flat field correction?",
683 flatDataProductName = pexConfig.Field(
685 doc=
"Name of the flat data product",
688 flatScalingType = pexConfig.ChoiceField(
690 doc=
"The method for scaling the flat on the fly.",
693 "USER":
"Scale by flatUserScale",
694 "MEAN":
"Scale by the inverse of the mean",
695 "MEDIAN":
"Scale by the inverse of the median",
698 flatUserScale = pexConfig.Field(
700 doc=
"If flatScalingType is 'USER' then scale flat by this amount; ignored otherwise",
703 doTweakFlat = pexConfig.Field(
705 doc=
"Tweak flats to match observed amplifier ratios?",
710 doApplyGains = pexConfig.Field(
712 doc=
"Correct the amplifiers for their gains instead of applying flat correction",
715 normalizeGains = pexConfig.Field(
717 doc=
"Normalize all the amplifiers in each CCD to have the same median value.",
722 doFringe = pexConfig.Field(
724 doc=
"Apply fringe correction?",
727 fringe = pexConfig.ConfigurableField(
729 doc=
"Fringe subtraction task",
731 fringeAfterFlat = pexConfig.Field(
733 doc=
"Do fringe subtraction after flat-fielding?",
738 doMeasureBackground = pexConfig.Field(
740 doc=
"Measure the background level on the reduced image?",
745 doCameraSpecificMasking = pexConfig.Field(
747 doc=
"Mask camera-specific bad regions?",
750 masking = pexConfig.ConfigurableField(
757 doInterpolate = pexConfig.Field(
759 doc=
"Interpolate masked pixels?",
762 doSaturationInterpolation = pexConfig.Field(
764 doc=
"Perform interpolation over pixels masked as saturated?"
765 " NB: This is independent of doSaturation; if that is False this plane"
766 " will likely be blank, resulting in a no-op here.",
769 doNanInterpolation = pexConfig.Field(
771 doc=
"Perform interpolation over pixels masked as NaN?"
772 " NB: This is independent of doNanMasking; if that is False this plane"
773 " will likely be blank, resulting in a no-op here.",
776 doNanInterpAfterFlat = pexConfig.Field(
778 doc=(
"If True, ensure we interpolate NaNs after flat-fielding, even if we "
779 "also have to interpolate them before flat-fielding."),
782 maskListToInterpolate = pexConfig.ListField(
784 doc=
"List of mask planes that should be interpolated.",
785 default=[
'SAT',
'BAD'],
787 doSaveInterpPixels = pexConfig.Field(
789 doc=
"Save a copy of the pre-interpolated pixel values?",
794 fluxMag0T1 = pexConfig.DictField(
797 doc=
"The approximate flux of a zero-magnitude object in a one-second exposure, per filter.",
798 default=dict((f, pow(10.0, 0.4*m))
for f, m
in ((
"Unknown", 28.0),
801 defaultFluxMag0T1 = pexConfig.Field(
803 doc=
"Default value for fluxMag0T1 (for an unrecognized filter).",
804 default=pow(10.0, 0.4*28.0)
808 doVignette = pexConfig.Field(
810 doc=
"Apply vignetting parameters?",
813 vignette = pexConfig.ConfigurableField(
815 doc=
"Vignetting task.",
819 doAttachTransmissionCurve = pexConfig.Field(
822 doc=
"Construct and attach a wavelength-dependent throughput curve for this CCD image?"
824 doUseOpticsTransmission = pexConfig.Field(
827 doc=
"Load and use transmission_optics (if doAttachTransmissionCurve is True)?"
829 doUseFilterTransmission = pexConfig.Field(
832 doc=
"Load and use transmission_filter (if doAttachTransmissionCurve is True)?"
834 doUseSensorTransmission = pexConfig.Field(
837 doc=
"Load and use transmission_sensor (if doAttachTransmissionCurve is True)?"
839 doUseAtmosphereTransmission = pexConfig.Field(
842 doc=
"Load and use transmission_atmosphere (if doAttachTransmissionCurve is True)?"
846 doIlluminationCorrection = pexConfig.Field(
849 doc=
"Perform illumination correction?"
851 illuminationCorrectionDataProductName = pexConfig.Field(
853 doc=
"Name of the illumination correction data product.",
856 illumScale = pexConfig.Field(
858 doc=
"Scale factor for the illumination correction.",
861 illumFilters = pexConfig.ListField(
864 doc=
"Only perform illumination correction for these filters."
868 doWrite = pexConfig.Field(
870 doc=
"Persist postISRCCD?",
877 raise ValueError(
"You may not specify both doFlat and doApplyGains")
879 raise ValueError(
"You may not specify both doBiasBeforeOverscan and doTrimToMatchCalib")
888 class IsrTask(pipeBase.PipelineTask, pipeBase.CmdLineTask):
889 """Apply common instrument signature correction algorithms to a raw frame.
891 The process for correcting imaging data is very similar from
892 camera to camera. This task provides a vanilla implementation of
893 doing these corrections, including the ability to turn certain
894 corrections off if they are not needed. The inputs to the primary
895 method, `run()`, are a raw exposure to be corrected and the
896 calibration data products. The raw input is a single chip sized
897 mosaic of all amps including overscans and other non-science
898 pixels. The method `runDataRef()` identifies and defines the
899 calibration data products, and is intended for use by a
900 `lsst.pipe.base.cmdLineTask.CmdLineTask` and takes as input only a
901 `daf.persistence.butlerSubset.ButlerDataRef`. This task may be
902 subclassed for different camera, although the most camera specific
903 methods have been split into subtasks that can be redirected
906 The __init__ method sets up the subtasks for ISR processing, using
907 the defaults from `lsst.ip.isr`.
912 Positional arguments passed to the Task constructor. None used at this time.
913 kwargs : `dict`, optional
914 Keyword arguments passed on to the Task constructor. None used at this time.
916 ConfigClass = IsrTaskConfig
921 self.makeSubtask(
"assembleCcd")
922 self.makeSubtask(
"crosstalk")
923 self.makeSubtask(
"strayLight")
924 self.makeSubtask(
"fringe")
925 self.makeSubtask(
"masking")
926 self.makeSubtask(
"overscan")
927 self.makeSubtask(
"vignette")
930 inputs = butlerQC.get(inputRefs)
933 inputs[
'detectorNum'] = inputRefs.ccdExposure.dataId[
'detector']
934 except Exception
as e:
935 raise ValueError(
"Failure to find valid detectorNum value for Dataset %s: %s." %
938 inputs[
'isGen3'] =
True
940 detector = inputs[
'ccdExposure'].getDetector()
942 if self.config.doCrosstalk
is True:
945 if 'crosstalk' in inputs
and inputs[
'crosstalk']
is not None:
946 if not isinstance(inputs[
'crosstalk'], CrosstalkCalib):
947 inputs[
'crosstalk'] = CrosstalkCalib.fromTable(inputs[
'crosstalk'])
949 coeffVector = (self.config.crosstalk.crosstalkValues
950 if self.config.crosstalk.useConfigCoefficients
else None)
951 crosstalkCalib =
CrosstalkCalib().fromDetector(detector, coeffVector=coeffVector)
952 inputs[
'crosstalk'] = crosstalkCalib
953 if inputs[
'crosstalk'].interChip
and len(inputs[
'crosstalk'].interChip) > 0:
954 if 'crosstalkSources' not in inputs:
955 self.log.
warn(
"No crosstalkSources found for chip with interChip terms!")
958 if 'linearizer' in inputs:
959 if isinstance(inputs[
'linearizer'], dict):
961 linearizer.fromYaml(inputs[
'linearizer'])
962 self.log.
warn(
"Dictionary linearizers will be deprecated in DM-28741.")
963 elif isinstance(inputs[
'linearizer'], numpy.ndarray):
967 self.log.
warn(
"Bare lookup table linearizers will be deprecated in DM-28741.")
969 linearizer = inputs[
'linearizer']
970 linearizer.log = self.log
971 inputs[
'linearizer'] = linearizer
974 self.log.
warn(
"Constructing linearizer from cameraGeom information.")
976 if self.config.doDefect
is True:
977 if "defects" in inputs
and inputs[
'defects']
is not None:
980 if not isinstance(inputs[
"defects"], Defects):
981 inputs[
"defects"] = Defects.fromTable(inputs[
"defects"])
985 if self.config.doBrighterFatter:
986 brighterFatterKernel = inputs.pop(
'newBFKernel',
None)
987 if brighterFatterKernel
is None:
988 brighterFatterKernel = inputs.get(
'bfKernel',
None)
990 if brighterFatterKernel
is not None and not isinstance(brighterFatterKernel, numpy.ndarray):
991 detId = detector.getId()
992 inputs[
'bfGains'] = brighterFatterKernel.gain
995 if self.config.brighterFatterLevel ==
'DETECTOR':
996 if brighterFatterKernel.detectorKernel:
997 inputs[
'bfKernel'] = brighterFatterKernel.detectorKernel[detId]
998 elif brighterFatterKernel.detectorKernelFromAmpKernels:
999 inputs[
'bfKernel'] = brighterFatterKernel.detectorKernelFromAmpKernels[detId]
1001 raise RuntimeError(
"Failed to extract kernel from new-style BF kernel.")
1004 raise NotImplementedError(
"Per-amplifier brighter-fatter correction not implemented")
1006 if self.config.doFringe
is True and self.fringe.
checkFilter(inputs[
'ccdExposure']):
1008 inputs[
'fringes'] = self.fringe.loadFringes(inputs[
'fringes'],
1010 assembler=self.assembleCcd
1011 if self.config.doAssembleIsrExposures
else None)
1013 inputs[
'fringes'] = pipeBase.Struct(fringes=
None)
1015 if self.config.doStrayLight
is True and self.strayLight.
checkFilter(inputs[
'ccdExposure']):
1016 if 'strayLightData' not in inputs:
1017 inputs[
'strayLightData'] =
None
1019 outputs = self.
runrun(**inputs)
1020 butlerQC.put(outputs, outputRefs)
1023 """Retrieve necessary frames for instrument signature removal.
1025 Pre-fetching all required ISR data products limits the IO
1026 required by the ISR. Any conflict between the calibration data
1027 available and that needed for ISR is also detected prior to
1028 doing processing, allowing it to fail quickly.
1032 dataRef : `daf.persistence.butlerSubset.ButlerDataRef`
1033 Butler reference of the detector data to be processed
1034 rawExposure : `afw.image.Exposure`
1035 The raw exposure that will later be corrected with the
1036 retrieved calibration data; should not be modified in this
1041 result : `lsst.pipe.base.Struct`
1042 Result struct with components (which may be `None`):
1043 - ``bias``: bias calibration frame (`afw.image.Exposure`)
1044 - ``linearizer``: functor for linearization (`ip.isr.linearize.LinearizeBase`)
1045 - ``crosstalkSources``: list of possible crosstalk sources (`list`)
1046 - ``dark``: dark calibration frame (`afw.image.Exposure`)
1047 - ``flat``: flat calibration frame (`afw.image.Exposure`)
1048 - ``bfKernel``: Brighter-Fatter kernel (`numpy.ndarray`)
1049 - ``defects``: list of defects (`lsst.ip.isr.Defects`)
1050 - ``fringes``: `lsst.pipe.base.Struct` with components:
1051 - ``fringes``: fringe calibration frame (`afw.image.Exposure`)
1052 - ``seed``: random seed derived from the ccdExposureId for random
1053 number generator (`uint32`).
1054 - ``opticsTransmission``: `lsst.afw.image.TransmissionCurve`
1055 A ``TransmissionCurve`` that represents the throughput of the optics,
1056 to be evaluated in focal-plane coordinates.
1057 - ``filterTransmission`` : `lsst.afw.image.TransmissionCurve`
1058 A ``TransmissionCurve`` that represents the throughput of the filter
1059 itself, to be evaluated in focal-plane coordinates.
1060 - ``sensorTransmission`` : `lsst.afw.image.TransmissionCurve`
1061 A ``TransmissionCurve`` that represents the throughput of the sensor
1062 itself, to be evaluated in post-assembly trimmed detector coordinates.
1063 - ``atmosphereTransmission`` : `lsst.afw.image.TransmissionCurve`
1064 A ``TransmissionCurve`` that represents the throughput of the
1065 atmosphere, assumed to be spatially constant.
1066 - ``strayLightData`` : `object`
1067 An opaque object containing calibration information for
1068 stray-light correction. If `None`, no correction will be
1070 - ``illumMaskedImage`` : illumination correction image (`lsst.afw.image.MaskedImage`)
1074 NotImplementedError :
1075 Raised if a per-amplifier brighter-fatter kernel is requested by the configuration.
1078 dateObs = rawExposure.getInfo().getVisitInfo().getDate()
1079 dateObs = dateObs.toPython().isoformat()
1080 except RuntimeError:
1081 self.log.
warn(
"Unable to identify dateObs for rawExposure.")
1084 ccd = rawExposure.getDetector()
1085 filterLabel = rawExposure.getFilterLabel()
1086 rawExposure.mask.addMaskPlane(
"UNMASKEDNAN")
1087 biasExposure = (self.
getIsrExposuregetIsrExposure(dataRef, self.config.biasDataProductName)
1088 if self.config.doBias
else None)
1090 linearizer = (dataRef.get(
"linearizer", immediate=
True)
1092 if linearizer
is not None and not isinstance(linearizer, numpy.ndarray):
1093 linearizer.log = self.log
1094 if isinstance(linearizer, numpy.ndarray):
1097 crosstalkCalib =
None
1098 if self.config.doCrosstalk:
1100 crosstalkCalib = dataRef.get(
"crosstalk", immediate=
True)
1102 coeffVector = (self.config.crosstalk.crosstalkValues
1103 if self.config.crosstalk.useConfigCoefficients
else None)
1104 crosstalkCalib =
CrosstalkCalib().fromDetector(ccd, coeffVector=coeffVector)
1105 crosstalkSources = (self.crosstalk.prepCrosstalk(dataRef, crosstalkCalib)
1106 if self.config.doCrosstalk
else None)
1108 darkExposure = (self.
getIsrExposuregetIsrExposure(dataRef, self.config.darkDataProductName)
1109 if self.config.doDark
else None)
1110 flatExposure = (self.
getIsrExposuregetIsrExposure(dataRef, self.config.flatDataProductName,
1112 if self.config.doFlat
else None)
1114 brighterFatterKernel =
None
1115 brighterFatterGains =
None
1116 if self.config.doBrighterFatter
is True:
1121 brighterFatterKernel = dataRef.get(
"brighterFatterKernel")
1122 brighterFatterGains = brighterFatterKernel.gain
1123 self.log.
info(
"New style brighter-fatter kernel (brighterFatterKernel) loaded")
1126 brighterFatterKernel = dataRef.get(
"bfKernel")
1127 self.log.
info(
"Old style brighter-fatter kernel (np.array) loaded")
1129 brighterFatterKernel =
None
1130 if brighterFatterKernel
is not None and not isinstance(brighterFatterKernel, numpy.ndarray):
1133 if self.config.brighterFatterLevel ==
'DETECTOR':
1134 if brighterFatterKernel.detectorKernel:
1135 brighterFatterKernel = brighterFatterKernel.detectorKernel[ccd.getId()]
1136 elif brighterFatterKernel.detectorKernelFromAmpKernels:
1137 brighterFatterKernel = brighterFatterKernel.detectorKernelFromAmpKernels[ccd.getId()]
1139 raise RuntimeError(
"Failed to extract kernel from new-style BF kernel.")
1142 raise NotImplementedError(
"Per-amplifier brighter-fatter correction not implemented")
1144 defectList = (dataRef.get(
"defects")
1145 if self.config.doDefect
else None)
1146 fringeStruct = (self.fringe.readFringes(dataRef, assembler=self.assembleCcd
1147 if self.config.doAssembleIsrExposures
else None)
1148 if self.config.doFringe
and self.fringe.
checkFilter(rawExposure)
1149 else pipeBase.Struct(fringes=
None))
1151 if self.config.doAttachTransmissionCurve:
1152 opticsTransmission = (dataRef.get(
"transmission_optics")
1153 if self.config.doUseOpticsTransmission
else None)
1154 filterTransmission = (dataRef.get(
"transmission_filter")
1155 if self.config.doUseFilterTransmission
else None)
1156 sensorTransmission = (dataRef.get(
"transmission_sensor")
1157 if self.config.doUseSensorTransmission
else None)
1158 atmosphereTransmission = (dataRef.get(
"transmission_atmosphere")
1159 if self.config.doUseAtmosphereTransmission
else None)
1161 opticsTransmission =
None
1162 filterTransmission =
None
1163 sensorTransmission =
None
1164 atmosphereTransmission =
None
1166 if self.config.doStrayLight:
1167 strayLightData = self.strayLight.
readIsrData(dataRef, rawExposure)
1169 strayLightData =
None
1172 self.config.illuminationCorrectionDataProductName).getMaskedImage()
1173 if (self.config.doIlluminationCorrection
1174 and filterLabel
in self.config.illumFilters)
1178 return pipeBase.Struct(bias=biasExposure,
1179 linearizer=linearizer,
1180 crosstalk=crosstalkCalib,
1181 crosstalkSources=crosstalkSources,
1184 bfKernel=brighterFatterKernel,
1185 bfGains=brighterFatterGains,
1187 fringes=fringeStruct,
1188 opticsTransmission=opticsTransmission,
1189 filterTransmission=filterTransmission,
1190 sensorTransmission=sensorTransmission,
1191 atmosphereTransmission=atmosphereTransmission,
1192 strayLightData=strayLightData,
1193 illumMaskedImage=illumMaskedImage
1196 @pipeBase.timeMethod
1197 def run(self, ccdExposure, camera=None, bias=None, linearizer=None,
1198 crosstalk=None, crosstalkSources=None,
1199 dark=None, flat=None, bfKernel=None, bfGains=None, defects=None,
1200 fringes=pipeBase.Struct(fringes=
None), opticsTransmission=
None, filterTransmission=
None,
1201 sensorTransmission=
None, atmosphereTransmission=
None,
1202 detectorNum=
None, strayLightData=
None, illumMaskedImage=
None,
1205 """Perform instrument signature removal on an exposure.
1207 Steps included in the ISR processing, in order performed, are:
1208 - saturation and suspect pixel masking
1209 - overscan subtraction
1210 - CCD assembly of individual amplifiers
1212 - variance image construction
1213 - linearization of non-linear response
1215 - brighter-fatter correction
1218 - stray light subtraction
1220 - masking of known defects and camera specific features
1221 - vignette calculation
1222 - appending transmission curve and distortion model
1226 ccdExposure : `lsst.afw.image.Exposure`
1227 The raw exposure that is to be run through ISR. The
1228 exposure is modified by this method.
1229 camera : `lsst.afw.cameraGeom.Camera`, optional
1230 The camera geometry for this exposure. Required if ``isGen3`` is
1231 `True` and one or more of ``ccdExposure``, ``bias``, ``dark``, or
1232 ``flat`` does not have an associated detector.
1233 bias : `lsst.afw.image.Exposure`, optional
1234 Bias calibration frame.
1235 linearizer : `lsst.ip.isr.linearize.LinearizeBase`, optional
1236 Functor for linearization.
1237 crosstalk : `lsst.ip.isr.crosstalk.CrosstalkCalib`, optional
1238 Calibration for crosstalk.
1239 crosstalkSources : `list`, optional
1240 List of possible crosstalk sources.
1241 dark : `lsst.afw.image.Exposure`, optional
1242 Dark calibration frame.
1243 flat : `lsst.afw.image.Exposure`, optional
1244 Flat calibration frame.
1245 bfKernel : `numpy.ndarray`, optional
1246 Brighter-fatter kernel.
1247 bfGains : `dict` of `float`, optional
1248 Gains used to override the detector's nominal gains for the
1249 brighter-fatter correction. A dict keyed by amplifier name for
1250 the detector in question.
1251 defects : `lsst.ip.isr.Defects`, optional
1253 fringes : `lsst.pipe.base.Struct`, optional
1254 Struct containing the fringe correction data, with
1256 - ``fringes``: fringe calibration frame (`afw.image.Exposure`)
1257 - ``seed``: random seed derived from the ccdExposureId for random
1258 number generator (`uint32`)
1259 opticsTransmission: `lsst.afw.image.TransmissionCurve`, optional
1260 A ``TransmissionCurve`` that represents the throughput of the optics,
1261 to be evaluated in focal-plane coordinates.
1262 filterTransmission : `lsst.afw.image.TransmissionCurve`
1263 A ``TransmissionCurve`` that represents the throughput of the filter
1264 itself, to be evaluated in focal-plane coordinates.
1265 sensorTransmission : `lsst.afw.image.TransmissionCurve`
1266 A ``TransmissionCurve`` that represents the throughput of the sensor
1267 itself, to be evaluated in post-assembly trimmed detector coordinates.
1268 atmosphereTransmission : `lsst.afw.image.TransmissionCurve`
1269 A ``TransmissionCurve`` that represents the throughput of the
1270 atmosphere, assumed to be spatially constant.
1271 detectorNum : `int`, optional
1272 The integer number for the detector to process.
1273 isGen3 : bool, optional
1274 Flag this call to run() as using the Gen3 butler environment.
1275 strayLightData : `object`, optional
1276 Opaque object containing calibration information for stray-light
1277 correction. If `None`, no correction will be performed.
1278 illumMaskedImage : `lsst.afw.image.MaskedImage`, optional
1279 Illumination correction image.
1283 result : `lsst.pipe.base.Struct`
1284 Result struct with component:
1285 - ``exposure`` : `afw.image.Exposure`
1286 The fully ISR corrected exposure.
1287 - ``outputExposure`` : `afw.image.Exposure`
1288 An alias for `exposure`
1289 - ``ossThumb`` : `numpy.ndarray`
1290 Thumbnail image of the exposure after overscan subtraction.
1291 - ``flattenedThumb`` : `numpy.ndarray`
1292 Thumbnail image of the exposure after flat-field correction.
1297 Raised if a configuration option is set to True, but the
1298 required calibration data has not been specified.
1302 The current processed exposure can be viewed by setting the
1303 appropriate lsstDebug entries in the `debug.display`
1304 dictionary. The names of these entries correspond to some of
1305 the IsrTaskConfig Boolean options, with the value denoting the
1306 frame to use. The exposure is shown inside the matching
1307 option check and after the processing of that step has
1308 finished. The steps with debug points are:
1319 In addition, setting the "postISRCCD" entry displays the
1320 exposure after all ISR processing has finished.
1328 if detectorNum
is None:
1329 raise RuntimeError(
"Must supply the detectorNum if running as Gen3.")
1331 ccdExposure = self.
ensureExposureensureExposure(ccdExposure, camera, detectorNum)
1332 bias = self.
ensureExposureensureExposure(bias, camera, detectorNum)
1333 dark = self.
ensureExposureensureExposure(dark, camera, detectorNum)
1334 flat = self.
ensureExposureensureExposure(flat, camera, detectorNum)
1336 if isinstance(ccdExposure, ButlerDataRef):
1337 return self.
runDataRefrunDataRef(ccdExposure)
1339 ccd = ccdExposure.getDetector()
1340 filterLabel = ccdExposure.getFilterLabel()
1343 assert not self.config.doAssembleCcd,
"You need a Detector to run assembleCcd."
1344 ccd = [
FakeAmp(ccdExposure, self.config)]
1347 if self.config.doBias
and bias
is None:
1348 raise RuntimeError(
"Must supply a bias exposure if config.doBias=True.")
1349 if self.
doLinearizedoLinearize(ccd)
and linearizer
is None:
1350 raise RuntimeError(
"Must supply a linearizer if config.doLinearize=True for this detector.")
1351 if self.config.doBrighterFatter
and bfKernel
is None:
1352 raise RuntimeError(
"Must supply a kernel if config.doBrighterFatter=True.")
1353 if self.config.doDark
and dark
is None:
1354 raise RuntimeError(
"Must supply a dark exposure if config.doDark=True.")
1355 if self.config.doFlat
and flat
is None:
1356 raise RuntimeError(
"Must supply a flat exposure if config.doFlat=True.")
1357 if self.config.doDefect
and defects
is None:
1358 raise RuntimeError(
"Must supply defects if config.doDefect=True.")
1359 if (self.config.doFringe
and filterLabel
in self.fringe.config.filters
1360 and fringes.fringes
is None):
1365 raise RuntimeError(
"Must supply fringe exposure as a pipeBase.Struct.")
1366 if (self.config.doIlluminationCorrection
and filterLabel
in self.config.illumFilters
1367 and illumMaskedImage
is None):
1368 raise RuntimeError(
"Must supply an illumcor if config.doIlluminationCorrection=True.")
1371 if self.config.doConvertIntToFloat:
1372 self.log.
info(
"Converting exposure to floating point values.")
1375 if self.config.doBias
and self.config.doBiasBeforeOverscan:
1376 self.log.
info(
"Applying bias correction.")
1377 isrFunctions.biasCorrection(ccdExposure.getMaskedImage(), bias.getMaskedImage(),
1378 trimToFit=self.config.doTrimToMatchCalib)
1379 self.
debugViewdebugView(ccdExposure,
"doBias")
1385 if ccdExposure.getBBox().
contains(amp.getBBox()):
1387 badAmp = self.
maskAmplifiermaskAmplifier(ccdExposure, amp, defects)
1389 if self.config.doOverscan
and not badAmp:
1392 self.log.
debug(
"Corrected overscan for amplifier %s.", amp.getName())
1393 if overscanResults
is not None and \
1394 self.config.qa
is not None and self.config.qa.saveStats
is True:
1395 if isinstance(overscanResults.overscanFit, float):
1396 qaMedian = overscanResults.overscanFit
1397 qaStdev = float(
"NaN")
1400 afwMath.MEDIAN | afwMath.STDEVCLIP)
1401 qaMedian = qaStats.getValue(afwMath.MEDIAN)
1402 qaStdev = qaStats.getValue(afwMath.STDEVCLIP)
1404 self.metadata.
set(f
"FIT MEDIAN {amp.getName()}", qaMedian)
1405 self.metadata.
set(f
"FIT STDEV {amp.getName()}", qaStdev)
1406 self.log.
debug(
" Overscan stats for amplifer %s: %f +/- %f",
1407 amp.getName(), qaMedian, qaStdev)
1411 afwMath.MEDIAN | afwMath.STDEVCLIP)
1412 qaMedianAfter = qaStatsAfter.getValue(afwMath.MEDIAN)
1413 qaStdevAfter = qaStatsAfter.getValue(afwMath.STDEVCLIP)
1415 self.metadata.
set(f
"RESIDUAL MEDIAN {amp.getName()}", qaMedianAfter)
1416 self.metadata.
set(f
"RESIDUAL STDEV {amp.getName()}", qaStdevAfter)
1417 self.log.
debug(
" Overscan stats for amplifer %s after correction: %f +/- %f",
1418 amp.getName(), qaMedianAfter, qaStdevAfter)
1420 ccdExposure.getMetadata().
set(
'OVERSCAN',
"Overscan corrected")
1423 self.log.
warn(
"Amplifier %s is bad.", amp.getName())
1424 overscanResults =
None
1426 overscans.append(overscanResults
if overscanResults
is not None else None)
1428 self.log.
info(
"Skipped OSCAN for %s.", amp.getName())
1430 if self.config.doCrosstalk
and self.config.doCrosstalkBeforeAssemble:
1431 self.log.
info(
"Applying crosstalk correction.")
1432 self.crosstalk.
run(ccdExposure, crosstalk=crosstalk,
1433 crosstalkSources=crosstalkSources, camera=camera)
1434 self.
debugViewdebugView(ccdExposure,
"doCrosstalk")
1436 if self.config.doAssembleCcd:
1437 self.log.
info(
"Assembling CCD from amplifiers.")
1438 ccdExposure = self.assembleCcd.assembleCcd(ccdExposure)
1440 if self.config.expectWcs
and not ccdExposure.getWcs():
1441 self.log.
warn(
"No WCS found in input exposure.")
1442 self.
debugViewdebugView(ccdExposure,
"doAssembleCcd")
1445 if self.config.qa.doThumbnailOss:
1446 ossThumb = isrQa.makeThumbnail(ccdExposure, isrQaConfig=self.config.qa)
1448 if self.config.doBias
and not self.config.doBiasBeforeOverscan:
1449 self.log.
info(
"Applying bias correction.")
1450 isrFunctions.biasCorrection(ccdExposure.getMaskedImage(), bias.getMaskedImage(),
1451 trimToFit=self.config.doTrimToMatchCalib)
1452 self.
debugViewdebugView(ccdExposure,
"doBias")
1454 if self.config.doVariance:
1455 for amp, overscanResults
in zip(ccd, overscans):
1456 if ccdExposure.getBBox().
contains(amp.getBBox()):
1457 self.log.
debug(
"Constructing variance map for amplifer %s.", amp.getName())
1458 ampExposure = ccdExposure.Factory(ccdExposure, amp.getBBox())
1459 if overscanResults
is not None:
1461 overscanImage=overscanResults.overscanImage)
1465 if self.config.qa
is not None and self.config.qa.saveStats
is True:
1467 afwMath.MEDIAN | afwMath.STDEVCLIP)
1468 self.metadata.
set(f
"ISR VARIANCE {amp.getName()} MEDIAN",
1469 qaStats.getValue(afwMath.MEDIAN))
1470 self.metadata.
set(f
"ISR VARIANCE {amp.getName()} STDEV",
1471 qaStats.getValue(afwMath.STDEVCLIP))
1472 self.log.
debug(
" Variance stats for amplifer %s: %f +/- %f.",
1473 amp.getName(), qaStats.getValue(afwMath.MEDIAN),
1474 qaStats.getValue(afwMath.STDEVCLIP))
1477 self.log.
info(
"Applying linearizer.")
1478 linearizer.applyLinearity(image=ccdExposure.getMaskedImage().getImage(),
1479 detector=ccd, log=self.log)
1481 if self.config.doCrosstalk
and not self.config.doCrosstalkBeforeAssemble:
1482 self.log.
info(
"Applying crosstalk correction.")
1483 self.crosstalk.
run(ccdExposure, crosstalk=crosstalk,
1484 crosstalkSources=crosstalkSources, isTrimmed=
True)
1485 self.
debugViewdebugView(ccdExposure,
"doCrosstalk")
1489 if self.config.doDefect:
1490 self.log.
info(
"Masking defects.")
1491 self.
maskDefectmaskDefect(ccdExposure, defects)
1493 if self.config.numEdgeSuspect > 0:
1494 self.log.
info(
"Masking edges as SUSPECT.")
1495 self.
maskEdgesmaskEdges(ccdExposure, numEdgePixels=self.config.numEdgeSuspect,
1496 maskPlane=
"SUSPECT", level=self.config.edgeMaskLevel)
1498 if self.config.doNanMasking:
1499 self.log.
info(
"Masking non-finite (NAN, inf) value pixels.")
1500 self.
maskNanmaskNan(ccdExposure)
1502 if self.config.doWidenSaturationTrails:
1503 self.log.
info(
"Widening saturation trails.")
1504 isrFunctions.widenSaturationTrails(ccdExposure.getMaskedImage().getMask())
1506 if self.config.doCameraSpecificMasking:
1507 self.log.
info(
"Masking regions for camera specific reasons.")
1508 self.masking.
run(ccdExposure)
1510 if self.config.doBrighterFatter:
1519 interpExp = ccdExposure.clone()
1520 with self.
flatContextflatContext(interpExp, flat, dark):
1521 isrFunctions.interpolateFromMask(
1522 maskedImage=interpExp.getMaskedImage(),
1523 fwhm=self.config.fwhm,
1524 growSaturatedFootprints=self.config.growSaturationFootprintSize,
1525 maskNameList=
list(self.config.brighterFatterMaskListToInterpolate)
1527 bfExp = interpExp.clone()
1529 self.log.
info(
"Applying brighter-fatter correction using kernel type %s / gains %s.",
1531 bfResults = isrFunctions.brighterFatterCorrection(bfExp, bfKernel,
1532 self.config.brighterFatterMaxIter,
1533 self.config.brighterFatterThreshold,
1534 self.config.brighterFatterApplyGain,
1536 if bfResults[1] == self.config.brighterFatterMaxIter:
1537 self.log.
warn(
"Brighter-fatter correction did not converge, final difference %f.",
1540 self.log.
info(
"Finished brighter-fatter correction in %d iterations.",
1542 image = ccdExposure.getMaskedImage().getImage()
1543 bfCorr = bfExp.getMaskedImage().getImage()
1544 bfCorr -= interpExp.getMaskedImage().getImage()
1553 self.log.
info(
"Ensuring image edges are masked as EDGE to the brighter-fatter kernel size.")
1554 self.
maskEdgesmaskEdges(ccdExposure, numEdgePixels=numpy.max(bfKernel.shape) // 2,
1557 if self.config.brighterFatterMaskGrowSize > 0:
1558 self.log.
info(
"Growing masks to account for brighter-fatter kernel convolution.")
1559 for maskPlane
in self.config.brighterFatterMaskListToInterpolate:
1560 isrFunctions.growMasks(ccdExposure.getMask(),
1561 radius=self.config.brighterFatterMaskGrowSize,
1562 maskNameList=maskPlane,
1563 maskValue=maskPlane)
1565 self.
debugViewdebugView(ccdExposure,
"doBrighterFatter")
1567 if self.config.doDark:
1568 self.log.
info(
"Applying dark correction.")
1570 self.
debugViewdebugView(ccdExposure,
"doDark")
1572 if self.config.doFringe
and not self.config.fringeAfterFlat:
1573 self.log.
info(
"Applying fringe correction before flat.")
1574 self.fringe.
run(ccdExposure, **fringes.getDict())
1575 self.
debugViewdebugView(ccdExposure,
"doFringe")
1577 if self.config.doStrayLight
and self.strayLight.check(ccdExposure):
1578 self.log.
info(
"Checking strayLight correction.")
1579 self.strayLight.
run(ccdExposure, strayLightData)
1580 self.
debugViewdebugView(ccdExposure,
"doStrayLight")
1582 if self.config.doFlat:
1583 self.log.
info(
"Applying flat correction.")
1585 self.
debugViewdebugView(ccdExposure,
"doFlat")
1587 if self.config.doApplyGains:
1588 self.log.
info(
"Applying gain correction instead of flat.")
1589 isrFunctions.applyGains(ccdExposure, self.config.normalizeGains)
1591 if self.config.doFringe
and self.config.fringeAfterFlat:
1592 self.log.
info(
"Applying fringe correction after flat.")
1593 self.fringe.
run(ccdExposure, **fringes.getDict())
1595 if self.config.doVignette:
1596 self.log.
info(
"Constructing Vignette polygon.")
1599 if self.config.vignette.doWriteVignettePolygon:
1602 if self.config.doAttachTransmissionCurve:
1603 self.log.
info(
"Adding transmission curves.")
1604 isrFunctions.attachTransmissionCurve(ccdExposure, opticsTransmission=opticsTransmission,
1605 filterTransmission=filterTransmission,
1606 sensorTransmission=sensorTransmission,
1607 atmosphereTransmission=atmosphereTransmission)
1609 flattenedThumb =
None
1610 if self.config.qa.doThumbnailFlattened:
1611 flattenedThumb = isrQa.makeThumbnail(ccdExposure, isrQaConfig=self.config.qa)
1613 if self.config.doIlluminationCorrection
and filterLabel
in self.config.illumFilters:
1614 self.log.
info(
"Performing illumination correction.")
1615 isrFunctions.illuminationCorrection(ccdExposure.getMaskedImage(),
1616 illumMaskedImage, illumScale=self.config.illumScale,
1617 trimToFit=self.config.doTrimToMatchCalib)
1620 if self.config.doSaveInterpPixels:
1621 preInterpExp = ccdExposure.clone()
1636 if self.config.doSetBadRegions:
1637 badPixelCount, badPixelValue = isrFunctions.setBadRegions(ccdExposure)
1638 if badPixelCount > 0:
1639 self.log.
info(
"Set %d BAD pixels to %f.", badPixelCount, badPixelValue)
1641 if self.config.doInterpolate:
1642 self.log.
info(
"Interpolating masked pixels.")
1643 isrFunctions.interpolateFromMask(
1644 maskedImage=ccdExposure.getMaskedImage(),
1645 fwhm=self.config.fwhm,
1646 growSaturatedFootprints=self.config.growSaturationFootprintSize,
1647 maskNameList=
list(self.config.maskListToInterpolate)
1652 if self.config.doMeasureBackground:
1653 self.log.
info(
"Measuring background level.")
1656 if self.config.qa
is not None and self.config.qa.saveStats
is True:
1658 ampExposure = ccdExposure.Factory(ccdExposure, amp.getBBox())
1660 afwMath.MEDIAN | afwMath.STDEVCLIP)
1661 self.metadata.
set(
"ISR BACKGROUND {} MEDIAN".
format(amp.getName()),
1662 qaStats.getValue(afwMath.MEDIAN))
1663 self.metadata.
set(
"ISR BACKGROUND {} STDEV".
format(amp.getName()),
1664 qaStats.getValue(afwMath.STDEVCLIP))
1665 self.log.
debug(
" Background stats for amplifer %s: %f +/- %f",
1666 amp.getName(), qaStats.getValue(afwMath.MEDIAN),
1667 qaStats.getValue(afwMath.STDEVCLIP))
1669 self.
debugViewdebugView(ccdExposure,
"postISRCCD")
1671 return pipeBase.Struct(
1672 exposure=ccdExposure,
1674 flattenedThumb=flattenedThumb,
1676 preInterpolatedExposure=preInterpExp,
1677 outputExposure=ccdExposure,
1678 outputOssThumbnail=ossThumb,
1679 outputFlattenedThumbnail=flattenedThumb,
1682 @pipeBase.timeMethod
1684 """Perform instrument signature removal on a ButlerDataRef of a Sensor.
1686 This method contains the `CmdLineTask` interface to the ISR
1687 processing. All IO is handled here, freeing the `run()` method
1688 to manage only pixel-level calculations. The steps performed
1690 - Read in necessary detrending/isr/calibration data.
1691 - Process raw exposure in `run()`.
1692 - Persist the ISR-corrected exposure as "postISRCCD" if
1693 config.doWrite=True.
1697 sensorRef : `daf.persistence.butlerSubset.ButlerDataRef`
1698 DataRef of the detector data to be processed
1702 result : `lsst.pipe.base.Struct`
1703 Result struct with component:
1704 - ``exposure`` : `afw.image.Exposure`
1705 The fully ISR corrected exposure.
1710 Raised if a configuration option is set to True, but the
1711 required calibration data does not exist.
1714 self.log.
info(
"Performing ISR on sensor %s.", sensorRef.dataId)
1716 ccdExposure = sensorRef.get(self.config.datasetType)
1718 camera = sensorRef.get(
"camera")
1719 isrData = self.
readIsrDatareadIsrData(sensorRef, ccdExposure)
1721 result = self.
runrun(ccdExposure, camera=camera, **isrData.getDict())
1723 if self.config.doWrite:
1724 sensorRef.put(result.exposure,
"postISRCCD")
1725 if result.preInterpolatedExposure
is not None:
1726 sensorRef.put(result.preInterpolatedExposure,
"postISRCCD_uninterpolated")
1727 if result.ossThumb
is not None:
1728 isrQa.writeThumbnail(sensorRef, result.ossThumb,
"ossThumb")
1729 if result.flattenedThumb
is not None:
1730 isrQa.writeThumbnail(sensorRef, result.flattenedThumb,
"flattenedThumb")
1735 """Retrieve a calibration dataset for removing instrument signature.
1740 dataRef : `daf.persistence.butlerSubset.ButlerDataRef`
1741 DataRef of the detector data to find calibration datasets
1744 Type of dataset to retrieve (e.g. 'bias', 'flat', etc).
1745 dateObs : `str`, optional
1746 Date of the observation. Used to correct butler failures
1747 when using fallback filters.
1749 If True, disable butler proxies to enable error handling
1750 within this routine.
1754 exposure : `lsst.afw.image.Exposure`
1755 Requested calibration frame.
1760 Raised if no matching calibration frame can be found.
1763 exp = dataRef.get(datasetType, immediate=immediate)
1764 except Exception
as exc1:
1765 if not self.config.fallbackFilterName:
1766 raise RuntimeError(
"Unable to retrieve %s for %s: %s." % (datasetType, dataRef.dataId, exc1))
1768 if self.config.useFallbackDate
and dateObs:
1769 exp = dataRef.get(datasetType, filter=self.config.fallbackFilterName,
1770 dateObs=dateObs, immediate=immediate)
1772 exp = dataRef.get(datasetType, filter=self.config.fallbackFilterName, immediate=immediate)
1773 except Exception
as exc2:
1774 raise RuntimeError(
"Unable to retrieve %s for %s, even with fallback filter %s: %s AND %s." %
1775 (datasetType, dataRef.dataId, self.config.fallbackFilterName, exc1, exc2))
1776 self.log.
warn(
"Using fallback calibration from filter %s.", self.config.fallbackFilterName)
1778 if self.config.doAssembleIsrExposures:
1779 exp = self.assembleCcd.assembleCcd(exp)
1783 """Ensure that the data returned by Butler is a fully constructed exposure.
1785 ISR requires exposure-level image data for historical reasons, so if we did
1786 not recieve that from Butler, construct it from what we have, modifying the
1791 inputExp : `lsst.afw.image.Exposure`, `lsst.afw.image.DecoratedImageU`, or
1792 `lsst.afw.image.ImageF`
1793 The input data structure obtained from Butler.
1794 camera : `lsst.afw.cameraGeom.camera`
1795 The camera associated with the image. Used to find the appropriate
1798 The detector this exposure should match.
1802 inputExp : `lsst.afw.image.Exposure`
1803 The re-constructed exposure, with appropriate detector parameters.
1808 Raised if the input data cannot be used to construct an exposure.
1810 if isinstance(inputExp, afwImage.DecoratedImageU):
1812 elif isinstance(inputExp, afwImage.ImageF):
1814 elif isinstance(inputExp, afwImage.MaskedImageF):
1818 elif inputExp
is None:
1822 raise TypeError(
"Input Exposure is not known type in isrTask.ensureExposure: %s." %
1825 if inputExp.getDetector()
is None:
1826 inputExp.setDetector(camera[detectorNum])
1831 """Convert exposure image from uint16 to float.
1833 If the exposure does not need to be converted, the input is
1834 immediately returned. For exposures that are converted to use
1835 floating point pixels, the variance is set to unity and the
1840 exposure : `lsst.afw.image.Exposure`
1841 The raw exposure to be converted.
1845 newexposure : `lsst.afw.image.Exposure`
1846 The input ``exposure``, converted to floating point pixels.
1851 Raised if the exposure type cannot be converted to float.
1854 if isinstance(exposure, afwImage.ExposureF):
1856 self.log.
debug(
"Exposure already of type float.")
1858 if not hasattr(exposure,
"convertF"):
1859 raise RuntimeError(
"Unable to convert exposure (%s) to float." %
type(exposure))
1861 newexposure = exposure.convertF()
1862 newexposure.variance[:] = 1
1863 newexposure.mask[:] = 0x0
1868 """Identify bad amplifiers, saturated and suspect pixels.
1872 ccdExposure : `lsst.afw.image.Exposure`
1873 Input exposure to be masked.
1874 amp : `lsst.afw.table.AmpInfoCatalog`
1875 Catalog of parameters defining the amplifier on this
1877 defects : `lsst.ip.isr.Defects`
1878 List of defects. Used to determine if the entire
1884 If this is true, the entire amplifier area is covered by
1885 defects and unusable.
1888 maskedImage = ccdExposure.getMaskedImage()
1894 if defects
is not None:
1895 badAmp = bool(sum([v.getBBox().
contains(amp.getBBox())
for v
in defects]))
1900 dataView = afwImage.MaskedImageF(maskedImage, amp.getRawBBox(),
1902 maskView = dataView.getMask()
1903 maskView |= maskView.getPlaneBitMask(
"BAD")
1910 if self.config.doSaturation
and not badAmp:
1911 limits.update({self.config.saturatedMaskName: amp.getSaturation()})
1912 if self.config.doSuspect
and not badAmp:
1913 limits.update({self.config.suspectMaskName: amp.getSuspectLevel()})
1914 if math.isfinite(self.config.saturation):
1915 limits.update({self.config.saturatedMaskName: self.config.saturation})
1917 for maskName, maskThreshold
in limits.items():
1918 if not math.isnan(maskThreshold):
1919 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
1920 isrFunctions.makeThresholdMask(
1921 maskedImage=dataView,
1922 threshold=maskThreshold,
1928 maskView =
afwImage.Mask(maskedImage.getMask(), amp.getRawDataBBox(),
1930 maskVal = maskView.getPlaneBitMask([self.config.saturatedMaskName,
1931 self.config.suspectMaskName])
1932 if numpy.all(maskView.getArray() & maskVal > 0):
1934 maskView |= maskView.getPlaneBitMask(
"BAD")
1939 """Apply overscan correction in place.
1941 This method does initial pixel rejection of the overscan
1942 region. The overscan can also be optionally segmented to
1943 allow for discontinuous overscan responses to be fit
1944 separately. The actual overscan subtraction is performed by
1945 the `lsst.ip.isr.isrFunctions.overscanCorrection` function,
1946 which is called here after the amplifier is preprocessed.
1950 ccdExposure : `lsst.afw.image.Exposure`
1951 Exposure to have overscan correction performed.
1952 amp : `lsst.afw.cameraGeom.Amplifer`
1953 The amplifier to consider while correcting the overscan.
1957 overscanResults : `lsst.pipe.base.Struct`
1958 Result struct with components:
1959 - ``imageFit`` : scalar or `lsst.afw.image.Image`
1960 Value or fit subtracted from the amplifier image data.
1961 - ``overscanFit`` : scalar or `lsst.afw.image.Image`
1962 Value or fit subtracted from the overscan image data.
1963 - ``overscanImage`` : `lsst.afw.image.Image`
1964 Image of the overscan region with the overscan
1965 correction applied. This quantity is used to estimate
1966 the amplifier read noise empirically.
1971 Raised if the ``amp`` does not contain raw pixel information.
1975 lsst.ip.isr.isrFunctions.overscanCorrection
1977 if amp.getRawHorizontalOverscanBBox().isEmpty():
1978 self.log.
info(
"ISR_OSCAN: No overscan region. Not performing overscan correction.")
1982 statControl.setAndMask(ccdExposure.mask.getPlaneBitMask(
"SAT"))
1985 dataBBox = amp.getRawDataBBox()
1986 oscanBBox = amp.getRawHorizontalOverscanBBox()
1990 prescanBBox = amp.getRawPrescanBBox()
1991 if (oscanBBox.getBeginX() > prescanBBox.getBeginX()):
1992 dx0 += self.config.overscanNumLeadingColumnsToSkip
1993 dx1 -= self.config.overscanNumTrailingColumnsToSkip
1995 dx0 += self.config.overscanNumTrailingColumnsToSkip
1996 dx1 -= self.config.overscanNumLeadingColumnsToSkip
2002 if ((self.config.overscanBiasJump
2003 and self.config.overscanBiasJumpLocation)
2004 and (ccdExposure.getMetadata().exists(self.config.overscanBiasJumpKeyword)
2005 and ccdExposure.getMetadata().getScalar(self.config.overscanBiasJumpKeyword)
in
2006 self.config.overscanBiasJumpDevices)):
2007 if amp.getReadoutCorner()
in (ReadoutCorner.LL, ReadoutCorner.LR):
2008 yLower = self.config.overscanBiasJumpLocation
2009 yUpper = dataBBox.getHeight() - yLower
2011 yUpper = self.config.overscanBiasJumpLocation
2012 yLower = dataBBox.getHeight() - yUpper
2030 oscanBBox.getHeight())))
2033 for imageBBox, overscanBBox
in zip(imageBBoxes, overscanBBoxes):
2034 ampImage = ccdExposure.maskedImage[imageBBox]
2035 overscanImage = ccdExposure.maskedImage[overscanBBox]
2037 overscanArray = overscanImage.image.array
2038 median = numpy.ma.median(numpy.ma.masked_where(overscanImage.mask.array, overscanArray))
2039 bad = numpy.where(numpy.abs(overscanArray - median) > self.config.overscanMaxDev)
2040 overscanImage.mask.array[bad] = overscanImage.mask.getPlaneBitMask(
"SAT")
2043 statControl.setAndMask(ccdExposure.mask.getPlaneBitMask(
"SAT"))
2045 overscanResults = self.overscan.
run(ampImage.getImage(), overscanImage, amp)
2048 levelStat = afwMath.MEDIAN
2049 sigmaStat = afwMath.STDEVCLIP
2052 self.config.qa.flatness.nIter)
2053 metadata = ccdExposure.getMetadata()
2054 ampNum = amp.getName()
2056 if isinstance(overscanResults.overscanFit, float):
2057 metadata.set(
"ISR_OSCAN_LEVEL%s" % ampNum, overscanResults.overscanFit)
2058 metadata.set(
"ISR_OSCAN_SIGMA%s" % ampNum, 0.0)
2061 metadata.set(
"ISR_OSCAN_LEVEL%s" % ampNum, stats.getValue(levelStat))
2062 metadata.set(
"ISR_OSCAN_SIGMA%s" % ampNum, stats.getValue(sigmaStat))
2064 return overscanResults
2067 """Set the variance plane using the amplifier gain and read noise
2069 The read noise is calculated from the ``overscanImage`` if the
2070 ``doEmpiricalReadNoise`` option is set in the configuration; otherwise
2071 the value from the amplifier data is used.
2075 ampExposure : `lsst.afw.image.Exposure`
2076 Exposure to process.
2077 amp : `lsst.afw.table.AmpInfoRecord` or `FakeAmp`
2078 Amplifier detector data.
2079 overscanImage : `lsst.afw.image.MaskedImage`, optional.
2080 Image of overscan, required only for empirical read noise.
2084 lsst.ip.isr.isrFunctions.updateVariance
2086 maskPlanes = [self.config.saturatedMaskName, self.config.suspectMaskName]
2087 gain = amp.getGain()
2089 if math.isnan(gain):
2091 self.log.
warn(
"Gain set to NAN! Updating to 1.0 to generate Poisson variance.")
2094 self.log.
warn(
"Gain for amp %s == %g <= 0; setting to %f.",
2095 amp.getName(), gain, patchedGain)
2098 if self.config.doEmpiricalReadNoise
and overscanImage
is None:
2099 self.log.
info(
"Overscan is none for EmpiricalReadNoise.")
2101 if self.config.doEmpiricalReadNoise
and overscanImage
is not None:
2103 stats.setAndMask(overscanImage.mask.getPlaneBitMask(maskPlanes))
2105 self.log.
info(
"Calculated empirical read noise for amp %s: %f.",
2106 amp.getName(), readNoise)
2108 readNoise = amp.getReadNoise()
2110 isrFunctions.updateVariance(
2111 maskedImage=ampExposure.getMaskedImage(),
2113 readNoise=readNoise,
2117 """Apply dark correction in place.
2121 exposure : `lsst.afw.image.Exposure`
2122 Exposure to process.
2123 darkExposure : `lsst.afw.image.Exposure`
2124 Dark exposure of the same size as ``exposure``.
2125 invert : `Bool`, optional
2126 If True, re-add the dark to an already corrected image.
2131 Raised if either ``exposure`` or ``darkExposure`` do not
2132 have their dark time defined.
2136 lsst.ip.isr.isrFunctions.darkCorrection
2138 expScale = exposure.getInfo().getVisitInfo().getDarkTime()
2139 if math.isnan(expScale):
2140 raise RuntimeError(
"Exposure darktime is NAN.")
2141 if darkExposure.getInfo().getVisitInfo()
is not None \
2142 and not math.isnan(darkExposure.getInfo().getVisitInfo().getDarkTime()):
2143 darkScale = darkExposure.getInfo().getVisitInfo().getDarkTime()
2147 self.log.
warn(
"darkExposure.getInfo().getVisitInfo() does not exist. Using darkScale = 1.0.")
2150 isrFunctions.darkCorrection(
2151 maskedImage=exposure.getMaskedImage(),
2152 darkMaskedImage=darkExposure.getMaskedImage(),
2154 darkScale=darkScale,
2156 trimToFit=self.config.doTrimToMatchCalib
2160 """Check if linearization is needed for the detector cameraGeom.
2162 Checks config.doLinearize and the linearity type of the first
2167 detector : `lsst.afw.cameraGeom.Detector`
2168 Detector to get linearity type from.
2172 doLinearize : `Bool`
2173 If True, linearization should be performed.
2175 return self.config.doLinearize
and \
2176 detector.getAmplifiers()[0].getLinearityType() != NullLinearityType
2179 """Apply flat correction in place.
2183 exposure : `lsst.afw.image.Exposure`
2184 Exposure to process.
2185 flatExposure : `lsst.afw.image.Exposure`
2186 Flat exposure of the same size as ``exposure``.
2187 invert : `Bool`, optional
2188 If True, unflatten an already flattened image.
2192 lsst.ip.isr.isrFunctions.flatCorrection
2194 isrFunctions.flatCorrection(
2195 maskedImage=exposure.getMaskedImage(),
2196 flatMaskedImage=flatExposure.getMaskedImage(),
2197 scalingType=self.config.flatScalingType,
2198 userScale=self.config.flatUserScale,
2200 trimToFit=self.config.doTrimToMatchCalib
2204 """Detect saturated pixels and mask them using mask plane config.saturatedMaskName, in place.
2208 exposure : `lsst.afw.image.Exposure`
2209 Exposure to process. Only the amplifier DataSec is processed.
2210 amp : `lsst.afw.table.AmpInfoCatalog`
2211 Amplifier detector data.
2215 lsst.ip.isr.isrFunctions.makeThresholdMask
2217 if not math.isnan(amp.getSaturation()):
2218 maskedImage = exposure.getMaskedImage()
2219 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
2220 isrFunctions.makeThresholdMask(
2221 maskedImage=dataView,
2222 threshold=amp.getSaturation(),
2224 maskName=self.config.saturatedMaskName,
2228 """Interpolate over saturated pixels, in place.
2230 This method should be called after `saturationDetection`, to
2231 ensure that the saturated pixels have been identified in the
2232 SAT mask. It should also be called after `assembleCcd`, since
2233 saturated regions may cross amplifier boundaries.
2237 exposure : `lsst.afw.image.Exposure`
2238 Exposure to process.
2242 lsst.ip.isr.isrTask.saturationDetection
2243 lsst.ip.isr.isrFunctions.interpolateFromMask
2245 isrFunctions.interpolateFromMask(
2246 maskedImage=exposure.getMaskedImage(),
2247 fwhm=self.config.fwhm,
2248 growSaturatedFootprints=self.config.growSaturationFootprintSize,
2249 maskNameList=
list(self.config.saturatedMaskName),
2253 """Detect suspect pixels and mask them using mask plane config.suspectMaskName, in place.
2257 exposure : `lsst.afw.image.Exposure`
2258 Exposure to process. Only the amplifier DataSec is processed.
2259 amp : `lsst.afw.table.AmpInfoCatalog`
2260 Amplifier detector data.
2264 lsst.ip.isr.isrFunctions.makeThresholdMask
2268 Suspect pixels are pixels whose value is greater than amp.getSuspectLevel().
2269 This is intended to indicate pixels that may be affected by unknown systematics;
2270 for example if non-linearity corrections above a certain level are unstable
2271 then that would be a useful value for suspectLevel. A value of `nan` indicates
2272 that no such level exists and no pixels are to be masked as suspicious.
2274 suspectLevel = amp.getSuspectLevel()
2275 if math.isnan(suspectLevel):
2278 maskedImage = exposure.getMaskedImage()
2279 dataView = maskedImage.Factory(maskedImage, amp.getRawBBox())
2280 isrFunctions.makeThresholdMask(
2281 maskedImage=dataView,
2282 threshold=suspectLevel,
2284 maskName=self.config.suspectMaskName,
2288 """Mask defects using mask plane "BAD", in place.
2292 exposure : `lsst.afw.image.Exposure`
2293 Exposure to process.
2294 defectBaseList : `lsst.ip.isr.Defects` or `list` of
2295 `lsst.afw.image.DefectBase`.
2296 List of defects to mask.
2300 Call this after CCD assembly, since defects may cross amplifier boundaries.
2302 maskedImage = exposure.getMaskedImage()
2303 if not isinstance(defectBaseList, Defects):
2305 defectList =
Defects(defectBaseList)
2307 defectList = defectBaseList
2308 defectList.maskPixels(maskedImage, maskName=
"BAD")
2310 def maskEdges(self, exposure, numEdgePixels=0, maskPlane="SUSPECT", level='DETECTOR'):
2311 """Mask edge pixels with applicable mask plane.
2315 exposure : `lsst.afw.image.Exposure`
2316 Exposure to process.
2317 numEdgePixels : `int`, optional
2318 Number of edge pixels to mask.
2319 maskPlane : `str`, optional
2320 Mask plane name to use.
2321 level : `str`, optional
2322 Level at which to mask edges.
2324 maskedImage = exposure.getMaskedImage()
2325 maskBitMask = maskedImage.getMask().getPlaneBitMask(maskPlane)
2327 if numEdgePixels > 0:
2328 if level ==
'DETECTOR':
2329 boxes = [maskedImage.getBBox()]
2330 elif level ==
'AMP':
2331 boxes = [amp.getBBox()
for amp
in exposure.getDetector()]
2335 subImage = maskedImage[box]
2336 box.grow(-numEdgePixels)
2338 SourceDetectionTask.setEdgeBits(
2344 """Mask and interpolate defects using mask plane "BAD", in place.
2348 exposure : `lsst.afw.image.Exposure`
2349 Exposure to process.
2350 defectBaseList : `lsst.ip.isr.Defects` or `list` of
2351 `lsst.afw.image.DefectBase`.
2352 List of defects to mask and interpolate.
2356 lsst.ip.isr.isrTask.maskDefect
2358 self.
maskDefectmaskDefect(exposure, defectBaseList)
2359 self.
maskEdgesmaskEdges(exposure, numEdgePixels=self.config.numEdgeSuspect,
2360 maskPlane=
"SUSPECT", level=self.config.edgeMaskLevel)
2361 isrFunctions.interpolateFromMask(
2362 maskedImage=exposure.getMaskedImage(),
2363 fwhm=self.config.fwhm,
2364 growSaturatedFootprints=0,
2365 maskNameList=[
"BAD"],
2369 """Mask NaNs using mask plane "UNMASKEDNAN", in place.
2373 exposure : `lsst.afw.image.Exposure`
2374 Exposure to process.
2378 We mask over all non-finite values (NaN, inf), including those
2379 that are masked with other bits (because those may or may not be
2380 interpolated over later, and we want to remove all NaN/infs).
2381 Despite this behaviour, the "UNMASKEDNAN" mask plane is used to
2382 preserve the historical name.
2384 maskedImage = exposure.getMaskedImage()
2387 maskedImage.getMask().addMaskPlane(
"UNMASKEDNAN")
2388 maskVal = maskedImage.getMask().getPlaneBitMask(
"UNMASKEDNAN")
2389 numNans =
maskNans(maskedImage, maskVal)
2390 self.metadata.
set(
"NUMNANS", numNans)
2392 self.log.
warn(
"There were %d unmasked NaNs.", numNans)
2395 """"Mask and interpolate NaN/infs using mask plane "UNMASKEDNAN",
2400 exposure : `lsst.afw.image.Exposure`
2401 Exposure to process.
2405 lsst.ip.isr.isrTask.maskNan
2408 isrFunctions.interpolateFromMask(
2409 maskedImage=exposure.getMaskedImage(),
2410 fwhm=self.config.fwhm,
2411 growSaturatedFootprints=0,
2412 maskNameList=[
"UNMASKEDNAN"],
2416 """Measure the image background in subgrids, for quality control purposes.
2420 exposure : `lsst.afw.image.Exposure`
2421 Exposure to process.
2422 IsrQaConfig : `lsst.ip.isr.isrQa.IsrQaConfig`
2423 Configuration object containing parameters on which background
2424 statistics and subgrids to use.
2426 if IsrQaConfig
is not None:
2428 IsrQaConfig.flatness.nIter)
2429 maskVal = exposure.getMaskedImage().getMask().getPlaneBitMask([
"BAD",
"SAT",
"DETECTED"])
2430 statsControl.setAndMask(maskVal)
2431 maskedImage = exposure.getMaskedImage()
2433 skyLevel = stats.getValue(afwMath.MEDIAN)
2434 skySigma = stats.getValue(afwMath.STDEVCLIP)
2435 self.log.
info(
"Flattened sky level: %f +/- %f.", skyLevel, skySigma)
2436 metadata = exposure.getMetadata()
2437 metadata.set(
'SKYLEVEL', skyLevel)
2438 metadata.set(
'SKYSIGMA', skySigma)
2441 stat = afwMath.MEANCLIP
if IsrQaConfig.flatness.doClip
else afwMath.MEAN
2442 meshXHalf = int(IsrQaConfig.flatness.meshX/2.)
2443 meshYHalf = int(IsrQaConfig.flatness.meshY/2.)
2444 nX = int((exposure.getWidth() + meshXHalf) / IsrQaConfig.flatness.meshX)
2445 nY = int((exposure.getHeight() + meshYHalf) / IsrQaConfig.flatness.meshY)
2446 skyLevels = numpy.zeros((nX, nY))
2449 yc = meshYHalf + j * IsrQaConfig.flatness.meshY
2451 xc = meshXHalf + i * IsrQaConfig.flatness.meshX
2453 xLLC = xc - meshXHalf
2454 yLLC = yc - meshYHalf
2455 xURC = xc + meshXHalf - 1
2456 yURC = yc + meshYHalf - 1
2459 miMesh = maskedImage.Factory(exposure.getMaskedImage(), bbox, afwImage.LOCAL)
2463 good = numpy.where(numpy.isfinite(skyLevels))
2464 skyMedian = numpy.median(skyLevels[good])
2465 flatness = (skyLevels[good] - skyMedian) / skyMedian
2466 flatness_rms = numpy.std(flatness)
2467 flatness_pp = flatness.max() - flatness.min()
if len(flatness) > 0
else numpy.nan
2469 self.log.
info(
"Measuring sky levels in %dx%d grids: %f.", nX, nY, skyMedian)
2470 self.log.
info(
"Sky flatness in %dx%d grids - pp: %f rms: %f.",
2471 nX, nY, flatness_pp, flatness_rms)
2473 metadata.set(
'FLATNESS_PP', float(flatness_pp))
2474 metadata.set(
'FLATNESS_RMS', float(flatness_rms))
2475 metadata.set(
'FLATNESS_NGRIDS',
'%dx%d' % (nX, nY))
2476 metadata.set(
'FLATNESS_MESHX', IsrQaConfig.flatness.meshX)
2477 metadata.set(
'FLATNESS_MESHY', IsrQaConfig.flatness.meshY)
2480 """Set an approximate magnitude zero point for the exposure.
2484 exposure : `lsst.afw.image.Exposure`
2485 Exposure to process.
2487 filterLabel = exposure.getFilterLabel()
2488 if filterLabel
in self.config.fluxMag0T1:
2489 fluxMag0 = self.config.fluxMag0T1[filterLabel]
2491 self.log.
warn(
"No rough magnitude zero point set for filter %s.", filterLabel)
2492 fluxMag0 = self.config.defaultFluxMag0T1
2494 expTime = exposure.getInfo().getVisitInfo().getExposureTime()
2496 self.log.
warn(
"Non-positive exposure time; skipping rough zero point.")
2499 self.log.
info(
"Setting rough magnitude zero point: %f", 2.5*math.log10(fluxMag0*expTime))
2503 """Set the valid polygon as the intersection of fpPolygon and the ccd corners.
2507 ccdExposure : `lsst.afw.image.Exposure`
2508 Exposure to process.
2509 fpPolygon : `lsst.afw.geom.Polygon`
2510 Polygon in focal plane coordinates.
2513 ccd = ccdExposure.getDetector()
2514 fpCorners = ccd.getCorners(FOCAL_PLANE)
2515 ccdPolygon =
Polygon(fpCorners)
2518 intersect = ccdPolygon.intersectionSingle(fpPolygon)
2521 ccdPoints = ccd.transform(intersect, FOCAL_PLANE, PIXELS)
2522 validPolygon =
Polygon(ccdPoints)
2523 ccdExposure.getInfo().setValidPolygon(validPolygon)
2527 """Context manager that applies and removes flats and darks,
2528 if the task is configured to apply them.
2532 exp : `lsst.afw.image.Exposure`
2533 Exposure to process.
2534 flat : `lsst.afw.image.Exposure`
2535 Flat exposure the same size as ``exp``.
2536 dark : `lsst.afw.image.Exposure`, optional
2537 Dark exposure the same size as ``exp``.
2541 exp : `lsst.afw.image.Exposure`
2542 The flat and dark corrected exposure.
2544 if self.config.doDark
and dark
is not None:
2546 if self.config.doFlat:
2551 if self.config.doFlat:
2553 if self.config.doDark
and dark
is not None:
2557 """Utility function to examine ISR exposure at different stages.
2561 exposure : `lsst.afw.image.Exposure`
2564 State of processing to view.
2569 display.scale(
'asinh',
'zscale')
2570 display.mtv(exposure)
2571 prompt =
"Press Enter to continue [c]... "
2573 ans = input(prompt).lower()
2574 if ans
in (
"",
"c",):
2579 """A Detector-like object that supports returning gain and saturation level
2581 This is used when the input exposure does not have a detector.
2585 exposure : `lsst.afw.image.Exposure`
2586 Exposure to generate a fake amplifier for.
2587 config : `lsst.ip.isr.isrTaskConfig`
2588 Configuration to apply to the fake amplifier.
2592 self.
_bbox_bbox = exposure.getBBox(afwImage.LOCAL)
2594 self.
_gain_gain = config.gain
2599 return self.
_bbox_bbox
2602 return self.
_bbox_bbox
2608 return self.
_gain_gain
2621 isr = pexConfig.ConfigurableField(target=IsrTask, doc=
"Instrument signature removal")
2625 """Task to wrap the default IsrTask to allow it to be retargeted.
2627 The standard IsrTask can be called directly from a command line
2628 program, but doing so removes the ability of the task to be
2629 retargeted. As most cameras override some set of the IsrTask
2630 methods, this would remove those data-specific methods in the
2631 output post-ISR images. This wrapping class fixes the issue,
2632 allowing identical post-ISR images to be generated by both the
2633 processCcd and isrTask code.
2635 ConfigClass = RunIsrConfig
2636 _DefaultName =
"runIsr"
2640 self.makeSubtask(
"isr")
2646 dataRef : `lsst.daf.persistence.ButlerDataRef`
2647 data reference of the detector data to be processed
2651 result : `pipeBase.Struct`
2652 Result struct with component:
2654 - exposure : `lsst.afw.image.Exposure`
2655 Post-ISR processed exposure.
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Represent a 2-dimensional array of bitmask pixels.
Pass parameters to a Statistics object.
An integer coordinate rectangle.
def getRawHorizontalOverscanBBox(self)
def getSuspectLevel(self)
_RawHorizontalOverscanBBox
def __init__(self, exposure, config)
doSaturationInterpolation
def __init__(self, *config=None)
def flatCorrection(self, exposure, flatExposure, invert=False)
def maskAndInterpolateNan(self, exposure)
def saturationInterpolation(self, exposure)
def runDataRef(self, sensorRef)
def maskNan(self, exposure)
def maskAmplifier(self, ccdExposure, amp, defects)
def debugView(self, exposure, stepname)
def getIsrExposure(self, dataRef, datasetType, dateObs=None, immediate=True)
def saturationDetection(self, exposure, amp)
def maskDefect(self, exposure, defectBaseList)
def __init__(self, **kwargs)
def runQuantum(self, butlerQC, inputRefs, outputRefs)
def maskEdges(self, exposure, numEdgePixels=0, maskPlane="SUSPECT", level='DETECTOR')
def overscanCorrection(self, ccdExposure, amp)
def measureBackground(self, exposure, IsrQaConfig=None)
def roughZeroPoint(self, exposure)
def maskAndInterpolateDefects(self, exposure, defectBaseList)
def setValidPolygonIntersect(self, ccdExposure, fpPolygon)
def readIsrData(self, dataRef, rawExposure)
def ensureExposure(self, inputExp, camera, detectorNum)
def updateVariance(self, ampExposure, amp, overscanImage=None)
def doLinearize(self, detector)
def flatContext(self, exp, flat, dark=None)
def convertIntToFloat(self, exposure)
def suspectDetection(self, exposure, amp)
def run(self, ccdExposure, camera=None, bias=None, linearizer=None, crosstalk=None, crosstalkSources=None, dark=None, flat=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)
def darkCorrection(self, exposure, darkExposure, invert=False)
def __init__(self, *args, **kwargs)
def runDataRef(self, dataRef)
daf::base::PropertyList * list
daf::base::PropertySet * set
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values.
std::shared_ptr< Exposure< ImagePixelT, MaskPixelT, VariancePixelT > > makeExposure(MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > &mimage, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
A function to return an Exposure of the correct type (cf.
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
def checkFilter(exposure, filterList, log)
def crosstalkSourceLookup(datasetType, registry, quantumDataId, collections)
size_t maskNans(afw::image::MaskedImage< PixelT > const &mi, afw::image::MaskPixel maskVal, afw::image::MaskPixel allow=0)
Mask NANs in an image.
def getExposureId(self, dataRef)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def getDebugFrame(debugDisplay, name)