# noao.imred.crutil¶

The noao.imred.crutil package contains various algorithms for finding and replacing cosmic rays in single images or image sets.

## Notes¶

For questions or comments please see our github page. We encourage and appreciate user feedback.

Most of these notebooks rely on basic knowledge of the Astropy FITS I/O module. If you are unfamiliar with this module please see the Astropy FITS I/O user documentation before using this documentation.

Contents: * crgrow * crmedian

## crgrow¶

Please review the Notes section above before running any examples in this notebook

The crgrow replacement uses the skimage.morphology package to grow the values in any numpy array. The dilation task is a wrapper around scipy.ndimage.grey_dilation. You can insert any kernal type where disk is called in this example. See the skimage.morphology.dilation for more information. More kernel shapes are also listed on this page.

# Standard Imports
from skimage.morphology import disk,dilation

# Astronomy Specific Imports
from astropy.io import fits
from astroquery.mast import Observations

# Download test file using astroquery, this only needs to be run once
# and can be skipped if using your own data.
obsid = '2004615006'

INFO: Found cached file ./mastDownload/HST/ICZGS3YGQ/iczgs3ygq_flt.fits with expected size 16534080. [astroquery.query]

Table length=1
Local PathStatusMessageURL
str47str5str87str93
# Change this value to your desired data file
out_file = 'crgrow.fits'

# Read in your fits file, when using some fits file, the byteswap call is required to
# make sure your array data type is correct when the dilation function is used. This
# may be due to a bug in the dilation funciton.
# For this example we will work with the 3rd extensions, the DQ array
hdu = fits.open(test_data,mode='update')
hdu.info()
dq1 = hdu[3].data.byteswap().newbyteorder('=')

# Dilation used to grow the CR flags, here we use the disk radius 2 shape kernel
grownDQ = dilation(dq1, disk(2))

# Re-assign the changed array to our original fits file and close save the updated FITS to a new file.
hdu[3].data = grownDQ
hdu.writeto(out_file, overwrite=True)

Filename: ./mastDownload/HST/ICZGS3YGQ/iczgs3ygq_flt.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
0  PRIMARY       1 PrimaryHDU     266   ()
1  SCI           1 ImageHDU       140   (1014, 1014)   float32
2  ERR           1 ImageHDU        51   (1014, 1014)   float32
3  DQ            1 ImageHDU        43   (1014, 1014)   int16
4  SAMP          1 ImageHDU        37   (1014, 1014)   int16
5  TIME          1 ImageHDU        37   (1014, 1014)   float32
6  WCSCORR       1 BinTableHDU     59   7R x 24C   [40A, I, A, 24A, 24A, 24A, 24A, D, D, D, D, D, D, D, D, 24A, 24A, D, D, D, D, J, 40A, 128A]


## crmedian¶

Please review the Notes section above before running any examples in this notebook

The crmedian task is a way to identify and replace cosmic rays in a single image by detecting pixels that deviate a statistically significant amount from the median by comparing to a median filtered version of the image. The identified cosmic rays can then be replaced by the median filtered value. A similar algorithm has been used in ccdproc.cosmicray_median. In ccdproc.cosmicray_median you also have the option of using an error array. If none is provided the standard deviation of the data is used. Ccdproc is an evolving package, please see their documentation for more information on usage.

# Astronomy Specific Imports
from astropy.io import fits
from astropy import units
from astroquery.mast import Observations

# Download test file using astroquery, this only needs to be run once
# and can be skipped if using your own data.
obsid = '2004615003'

INFO: Found cached file ./mastDownload/HST/ICZGS3Y5Q/iczgs3y5q_flt.fits with expected size 16534080. [astroquery.query]

Table length=1
Local PathStatusMessageURL
str47str5str87str93
# Change these values to your desired data files

# First we need to pull out the science and error(uncertainty) array to
# create CCDData objects. Our acutal unit is electrons/sec, this is not
# accepted by the current set of units
image_data = fits_ccddata_reader(test_data, hdu=1, unit=units.electron/units.s, hdu_uncertainty=2)
error_data = image_data.uncertainty.array

# Now we run cosmicray_median, since we input a CCDData type, a CCDData type is returned
# If a numpy.ndarray if the input data type, it will return a numpy.ndarray
newdata = cosmicray_median(image_data, error_image=error_data, thresh=5, mbox=11, rbox=11, gbox=3)

INFO: using the unit electron / s passed to the FITS reader instead of the unit ELECTRONS/S in the FITS file. [astropy.nddata.ccddata]

/Users/ogaz/miniconda3/envs/irafdev/lib/python3.5/site-packages/ccdproc/core.py:1565: RuntimeWarning: divide by zero encountered in true_divide
rarr = (data - marr) / error_image


## Not Replacing¶

• cosmicrays - Remove cosmic rays using flux ratio algorithm.

• craverage - Detect CRs against average and avoid objects.

• crcombine - Combine multiple exposures to eliminate cosmic rays.

• credit - Interactively edit cosmic rays using an image display.

• crfix - Fix cosmic rays in images using cosmic ray masks.

• crnebula - Detect and replace cosmic rays in nebular data.