Despike#

The goal of despiking is to remove cosmic ray hits from images.

Concept#

Despiking is carried out by our implementation of Astropy’s Astro-SCRAPPY algorithm. The algorithm works on a single image at a time and is based on Pieter van Dokkum’s L.A.Cosmic algorithm. It identifies cosmic rays in images by exploiting their sharp, high-contrast edges using a Laplacian edge detection algorithm. This approach distinguishes cosmic rays from real astronomical objects by their characteristic sharpness and lack of spatial coherence. The technique enhances the Laplacian-filtered image, isolates significant outliers, and then removes or replaces the cosmic ray pixels to preserve image integrity.

Applying correction#

The correction is carried out primarily using the astroscrappy.detect_cosmics function:

astroscrappy.detect_cosmics(indat, inmask=None, inbkg=None, invar=None, sigclip=4.5, sigfrac=0.3, objlim=5.0, gain=1.0, readnoise=6.5, satlevel=65536.0, niter=4, sepmed=True, cleantype='meanmask', fsmode='median', psfmodel='gauss', psffwhm=2.5, psfsize=7, psfk=None, psfbeta=4.765, verbose=False)
detect_cosmics(indat, inmask=None, inbkg=None, invar=None, sigclip=4.5,

sigfrac=0.3, objlim=5.0, gain=1.0, readnoise=6.5, satlevel=65536.0, niter=4, sepmed=True, cleantype=’meanmask’, fsmode=’median’, psfmodel=’gauss’, psffwhm=2.5, psfsize=7, psfk=None, psfbeta=4.765, verbose=False)

Detect cosmic rays in a numpy array.

If you use this code, please cite the Zendo DOI: https://zenodo.org/record/1482019

Please cite the original paper which can be found at: http://www.astro.yale.edu/dokkum/lacosmic/

van Dokkum 2001, PASP, 113, 789, 1420 (article : http://adsabs.harvard.edu/abs/2001PASP..113.1420V)

Parameters:
  • indat (float numpy array) – Input data array that will be used for cosmic ray detection. This should include the sky background (or a mean background level, added back in after sky subtraction), so that noise can be estimated correctly from the data values. This should be in units of “counts”.

  • inmask (boolean numpy array, optional) – Input bad pixel mask. Values of True will be ignored in the cosmic ray detection/cleaning process. Default: None.

  • inbkg (float numpy array, optional) – A pre-determined background image, to be subtracted from indat before running the main detection algorithm. This is used primarily with spectroscopic data, to remove sky lines and the cross-section of an object continuum during iteration, “protecting” them from spurious rejection (see the above paper). This background is not removed from the final, cleaned output (cleanarr). This should be in units of “counts”, the same units of indat. This inbkg should be free from cosmic rays. When estimating the cosmic-ray free noise of the image, we will treat inbkg as a constant Poisson contribution to the variance.

  • invar (float numpy array, optional) – A pre-determined estimate of the data variance (ie. noise squared) in each pixel, generated by previous processing of indat. If provided, this is used in place of an internal noise model based on indat, gain and readnoise. This still gets median filtered and cleaned internally, to estimate what the noise in each pixel would be in the absence of cosmic rays. This should be in units of “counts” squared.

  • sigclip (float, optional) – Laplacian-to-noise limit for cosmic ray detection. Lower values will flag more pixels as cosmic rays. Default: 4.5.

  • sigfrac (float, optional) – Fractional detection limit for neighboring pixels. For cosmic ray neighbor pixels, a lapacian-to-noise detection limit of sigfrac * sigclip will be used. Default: 0.3.

  • objlim (float, optional) – Minimum contrast between Laplacian image and the fine structure image. Increase this value if cores of bright stars are flagged as cosmic rays. Default: 5.0.

  • gain (float, optional) – Gain of the image (electrons / ADU). We always need to work in electrons for cosmic ray detection. Default: 1.0

  • readnoise (float, optional) – Read noise of the image (electrons). Used to generate the noise model of the image. Default: 6.5.

  • satlevel (float, optional) – Saturation of level of the image (electrons). This value is used to detect saturated stars and pixels at or above this level are added to the mask. Default: 65536.0.

  • niter (int, optional) – Number of iterations of the LA Cosmic algorithm to perform. Default: 4.

  • sepmed (boolean, optional) – Use the separable median filter instead of the full median filter. The separable median is not identical to the full median filter, but they are approximately the same and the separable median filter is significantly faster and still detects cosmic rays well. Default: True

  • cleantype ({'median', 'medmask', 'meanmask', 'idw'}, optional) –

    Set which clean algorithm is used:

    ’median’: An umasked 5x5 median filter

    ’medmask’: A masked 5x5 median filter

    ’meanmask’: A masked 5x5 mean filter

    ’idw’: A masked 5x5 inverse distance weighted interpolation

    Default: “meanmask”.

  • fsmode ({'median', 'convolve'}, optional) –

    Method to build the fine structure image:

    ’median’: Use the median filter in the standard LA Cosmic algorithm ‘convolve’: Convolve the image with the psf kernel to calculate the fine structure image using a matched filter technique. Default: ‘median’.

  • psfmodel ({'gauss', 'gaussx', 'gaussy', 'moffat'}, optional) – Model to use to generate the psf kernel if fsmode == ‘convolve’ and psfk is None. The current choices are Gaussian and Moffat profiles. ‘gauss’ and ‘moffat’ produce circular PSF kernels. The ‘gaussx’ and ‘gaussy’ produce Gaussian kernels in the x and y directions respectively. Default: “gauss”.

  • psffwhm (float, optional) – Full Width Half Maximum of the PSF to use to generate the kernel. Default: 2.5.

  • psfsize (int, optional) – Size of the kernel to calculate. Returned kernel will have size psfsize x psfsize. psfsize should be odd. Default: 7.

  • psfk (float numpy array, optional) – PSF kernel array to use for the fine structure image if fsmode == ‘convolve’. If None and fsmode == ‘convolve’, we calculate the psf kernel using ‘psfmodel’. Default: None.

  • psfbeta (float, optional) – Moffat beta parameter. Only used if fsmode==’convolve’ and psfmodel==’moffat’. Default: 4.765.

  • verbose (boolean, optional) – Print to the screen or not. Default: False.

Returns:

  • crmask (boolean numpy array) – The cosmic ray mask (boolean) array with values of True where there are cosmic ray detections.

  • cleanarr (float numpy array) – The cleaned data array.

Notes

To reproduce the most similar behavior to the original LA Cosmic (written in IRAF), set inmask = None, satlevel = np.inf, sepmed=False, cleantype=’medmask’, and fsmode=’median’.

The original IRAF version distinguishes between spectroscopic and imaging data. This version does not.

For best results on spectra, we recommend that you include an estimate of the background. One can generally obtain this by fitting columns with a smooth function. To efficiently identify cosmic rays, LA Cosmic and therefore astroscrappy estimates the cosmic ray free noise by smoothing the variance using a median filter. To minimize false positives on bright sky lines, if inbkg is provided, we do not smooth the variance contribution from the provided background. We only smooth the variance that is in addition to the Poisson contribution from the background so that we do not underestimate the noise (and therefore run the risk of flagging false positives) near narrow, bright sky lines.

If you wish to incorporate this as a Prefect task in a custom pipeline, using something like the punchbowl.level1.despike.despike_task is recommended.