22 import lsst.pex.config 
as pexConfig
 
   27 from lsstDebug 
import getDebugFrame
 
   33     doInterpolate = pexConfig.Field(
 
   35         doc=
"Interpolate over defects? (ignored unless you provide a list of defects)",
 
   38     doCosmicRay = pexConfig.Field(
 
   40         doc=
"Find and mask out cosmic rays?",
 
   43     cosmicray = pexConfig.ConfigField(
 
   44         dtype=measAlg.FindCosmicRaysConfig,
 
   45         doc=
"Options for finding and masking cosmic rays",
 
   47     interp = pexConfig.ConfigurableField(
 
   48         target=InterpImageTask,
 
   49         doc=
"Interpolate over bad image pixels",
 
   53         self.
interp.useFallbackValueAtEdge = 
True 
   54         self.
interp.fallbackValueType = 
"MEANCLIP" 
   55         self.
interp.negativeFallbackAllowed = 
True 
   69     @brief Interpolate over defects in an exposure and handle cosmic rays 
   71     @section pipe_tasks_repair_Contents Contents 
   73      - @ref pipe_tasks_repair_Purpose 
   74      - @ref pipe_tasks_repair_Initialize 
   75      - @ref pipe_tasks_repair_IO 
   76      - @ref pipe_tasks_repair_Config 
   77      - @ref pipe_tasks_repair_Debug 
   78      - @ref pipe_tasks_repair_Example 
   80     @section pipe_tasks_repair_Purpose Description 
   84     This task operates on an lsst.afw.image.Exposure in place to interpolate over a set of 
   85     lsst.meas.algorithms.Defect objects. 
   86     It will also, optionally, find and interpolate any cosmic rays in the lsst.afw.image.Exposure. 
   88     @section pipe_tasks_repair_Initialize Task initialization 
   90     See: lsst.pipe.base.task.Task.__init__ 
   92     @section pipe_tasks_repair_IO Inputs/Outputs to the run method 
   96     @section pipe_tasks_repair_Config Configuration parameters 
  100     @section pipe_tasks_repair_Debug Debug variables 
  102     The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a 
  103     flag @c -d to import @b debug.py from your @c PYTHONPATH; see <a 
  104     href="http://lsst-web.ncsa.illinois.edu/~buildbot/doxygen/x_masterDoxyDoc/base_debug.html"> 
  105     Using lsstDebug to control debugging output</a> for more about @b debug.py files. 
  107     The available variables in RepairTask are: 
  110       <DD> A dictionary containing debug point names as keys with frame number as value. Valid keys are: 
  113           <DD> display image before any repair is done 
  115           <DD> display image after cosmic ray and defect correction 
  118       <DD> If True, display the exposure on display's frame 1 and overlay bounding boxes around detects CRs. 
  120     @section pipe_tasks_repair_Example A complete example of using RepairTask 
  122     This code is in runRepair.py in the examples directory, and can be run as @em e.g. 
  124     examples/runRepair.py 
  126     @dontinclude runRepair.py 
  127     Import the task.  There are other imports.  Read the source file for more info. 
  130     For this example, we manufacture a test image to run on. 
  132     First, create a pure Poisson noise image and a Psf to go with it.  The mask plane 
  133     and variance can be constructed at the same time. 
  137     Inject some cosmic rays and generate the Exposure.  Exposures are MaskedImages (image + mask + variance) 
  138     with other metadata (e.g. Psf and Wcs objects). 
  142     Defects are represented as bad columns of random lengths.  A defect list must be constructed to pass 
  143     on to the RepairTask. 
  144     @bug This is addressed in <a href="https://jira.lsstcorp.org/browse/DM-963"> DM-963</a> 
  149     Finally, the exposure can be repaired.  Create an instance of the task and run it.  The exposure 
  150     is modified in place. 
  155     To investigate the @ref pipe_tasks_repair_Debug, put something like 
  159         di = lsstDebug.getInfo(name)        # N.b. lsstDebug.Info(name) would call us recursively 
  160         if name == "lsst.pipe.tasks.repair": 
  161             di.display = {'repair.before':2, 'repair.after':3} 
  165     lsstDebug.Info = DebugInfo 
  167     into your debug.py file and run runRepair.py with the @c --debug flag. 
  171         Display code should be updated once we settle on a standard way of controlling what is displayed. 
  173     ConfigClass = RepairConfig
 
  174     _DefaultName = 
"repair" 
  177         pipeBase.Task.__init__(self, **kwargs)
 
  178         if self.config.doInterpolate:
 
  179             self.makeSubtask(
"interp")
 
  182     def run(self, exposure, defects=None, keepCRs=None):
 
  183         """!Repair an Exposure's defects and cosmic rays 
  185         @param[in, out] exposure  lsst.afw.image.Exposure to process.  Exposure must have a valid Psf. 
  187         @param[in]      defects   an lsst.meas.algorithms.DefectListT object.  If None, do no 
  189         @param[in]      keepCRs   don't interpolate over the CR pixels (defer to RepairConfig if None) 
  191         @throws AssertionError with the following strings: 
  194           <DT> No exposure provided 
  195           <DD> The object provided as exposure evaluates to False 
  197           <DD> The Exposure has no associated Psf 
  200         assert exposure, 
"No exposure provided" 
  201         psf = exposure.getPsf()
 
  202         assert psf, 
"No PSF provided" 
  206             afwDisplay.Display(frame).
mtv(exposure)
 
  208         if defects 
is not None and self.config.doInterpolate:
 
  209             self.interp.
run(exposure, defects=defects)
 
  211         if self.config.doCosmicRay:
 
  212             self.
cosmicRay(exposure, keepCRs=keepCRs)
 
  216             afwDisplay.Display(frame).
mtv(exposure)
 
  221         @param[in,out] exposure Exposure to process 
  222         @param[in]     keepCRs  Don't interpolate over the CR pixels (defer to pex_config if None) 
  228         assert exposure, 
"No exposure provided" 
  229         psf = exposure.getPsf()
 
  230         assert psf, 
"No psf provided" 
  234             mask = exposure.getMaskedImage().getMask()
 
  235             crBit = mask.getMaskPlane(
"CR")
 
  236             mask.clearMaskPlane(crBit)
 
  241         binSize = self.config.cosmicray.background.binSize
 
  242         nx, ny = exposure.getWidth()/binSize, exposure.getHeight()/binSize
 
  252             exposure = exposure.Factory(exposure, 
True)
 
  253             subtractBackgroundTask = measAlg.SubtractBackgroundTask(config=self.config.cosmicray.background)
 
  254             modelBg = subtractBackgroundTask.run(exposure).background
 
  258             keepCRs = self.config.cosmicray.keepCRs
 
  260             crs = measAlg.findCosmicRays(exposure.getMaskedImage(), psf, medianBg,
 
  261                                          pexConfig.makePropertySet(self.config.cosmicray), keepCRs)
 
  264                 img = exposure.getMaskedImage()
 
  265                 img += modelBg.getImageF()
 
  268                 exposure0.setMaskedImage(exposure.getMaskedImage())
 
  272                 afwDisplay.Display().
mtv(exposure0, title=
"Failed CR")
 
  277             mask = exposure0.getMaskedImage().getMask()
 
  278             crBit = mask.getPlaneBitMask(
"CR")
 
  279             afwDet.setMaskFromFootprintList(mask, crs, crBit)
 
  282             if display 
and displayCR:
 
  283                 disp = afwDisplay.Display()
 
  284                 disp.incrDefaultFrame()
 
  285                 disp.mtv(exposure0, title=
"Post-CR")
 
  287                 with disp.Buffering():
 
  289                         afwDisplay.utils.drawBBox(cr.getBBox(), borderWidth=0.55)
 
  291         self.log.
info(
"Identified %s cosmic rays." % (num,))