ccdproc.cosmicray_lacosmic(ccd, sigclip=4.5, sigfrac=0.3, objlim=5.0, gain=1.0, readnoise=6.5, satlevel=65535.0, pssl=0.0, niter=4, sepmed=True, cleantype='meanmask', fsmode='median', psfmodel='gauss', psffwhm=2.5, psfsize=7, psfk=None, psfbeta=4.765, verbose=False, gain_apply=True, inbkg=None, invar=None)[source]

Identify cosmic rays through the L.A. Cosmic technique. The L.A. Cosmic technique identifies cosmic rays by identifying pixels based on a variation of the Laplacian edge detection. The algorithm is an implementation of the code describe in van Dokkum (2001) [1] as implemented by McCully (2014) [2]. If you use this algorithm, please cite these two works.

ccdCCDData or numpy.ndarray

Data to have cosmic ray cleaned.

gain_applybool, optional

If True, return gain-corrected data, with correct units, otherwise do not gain-correct the data. Default is True to preserve backwards compatibility.

sigclipfloat, optional

Laplacian-to-noise limit for cosmic ray detection. Lower values will flag more pixels as cosmic rays. Default: 4.5.

sigfracfloat, optional

Fractional detection limit for neighboring pixels. For cosmic ray neighbor pixels, a Laplacian-to-noise detection limit of sigfrac * sigclip will be used. Default: 0.3.

objlimfloat, 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.

inbkgfloat 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.

invarfloat 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.

psslfloat, optional

Previously subtracted sky level in ADU. We always need to work in electrons for cosmic ray detection, so we need to know the sky level that has been subtracted so we can add it back in. Default: 0.0.

gainfloat or Quantity, optional

Gain of the image (electrons / ADU). We always need to work in electrons for cosmic ray detection. Default: 1.0

readnoisefloat, optional

Read noise of the image (electrons). Used to generate the noise model of the image. Default: 6.5.

satlevelfloat, optional

Saturation 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: 65535.0.

niterint, optional

Number of iterations of the LA Cosmic algorithm to perform. Default: 4.

sepmedbool, 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, the separable median filter is significantly faster, and still detects cosmic rays well. Note, this is a performance feature, and not part of the original L.A. Cosmic. Default: True

cleantypestr, optional

Set which clean algorithm is used:

  • "median": An unmasked 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".

fsmodestr, 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.

Default: "median".

psfmodelstr, 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".

psffwhmfloat, optional

Full Width Half Maximum of the PSF to use to generate the kernel. Default: 2.5.

psfsizeint, optional

Size of the kernel to calculate. Returned kernel will have size psfsize x psfsize. psfsize should be odd. Default: 7.

psfknumpy.ndarray (with float dtype) or None, 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.

psfbetafloat, optional

Moffat beta parameter. Only used if fsmode=='convolve' and psfmodel=='moffat'. Default: 4.765.

verbosebool, optional

Print to the screen or not. Default: False.

nccdCCDData or numpy.ndarray

An object of the same type as ccd is returned. If it is a CCDData, the mask attribute will also be updated with areas identified with cosmic rays masked. By default, the image is multiplied by the gain. You can control this behavior with the gain_apply argument.


If an numpy.ndarray is provided as ccd, a boolean ndarray with the cosmic rays identified will also be returned.


Implementation of the cosmic ray identification L.A.Cosmic:



van Dokkum, P; 2001, “Cosmic-Ray Rejection by Laplacian Edge Detection”. The Publications of the Astronomical Society of the Pacific, Volume 113, Issue 789, pp. 1420-1427. doi: 10.1086/323894


McCully, C., 2014, “Astro-SCRAPPY”,


  1. Given an numpy.ndarray object, the syntax for running cosmicrar_lacosmic would be:

    >>> newdata, mask = cosmicray_lacosmic(data, sigclip=5)  

    where the error is an array that is the same shape as data but includes the pixel error. This would return a data array, newdata, with the bad pixels replaced by the local median from a box of 11 pixels; and it would return a mask indicating the bad pixels.

  2. Given an CCDData object with an uncertainty frame, the syntax for running cosmicrar_lacosmic would be:

    >>> newccd = cosmicray_lacosmic(ccd, sigclip=5)   

    The newccd object will have bad pixels in its data array replace and the mask of the object will be created if it did not previously exist or be updated with the detected cosmic rays.