astroscrappy.detect_cosmics(indat, inmask=None, bkg=None, var=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:

Please cite the original paper which can be found at:

van Dokkum 2001, PASP, 113, 789, 1420 (article :

indatfloat 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”.

inmaskboolean numpy array, optional

Input bad pixel mask. Values of True will be ignored in the cosmic ray detection/cleaning process. Default: None.

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.

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

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

niterint, optional

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

sepmedboolean, 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. 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”.

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.

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

psfbetafloat, optional

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

verboseboolean, optional

Print to the screen or not. Default: False.

crmaskboolean numpy array

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

cleanarrfloat numpy array

The cleaned data array.


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.