Search

Incorporating masks into calibrated science images

There are three ways of determining which pixels in a CCD image may need to be masked (this is in addition to whatever mask or bit fields the observatory at which you are taking images may provide).

Two of them are the same for all of the science images:

The third, identifying cosmic rays, discussed in Cosmic ray removal, will by its nature be different for each science image.

The first two masks could be added to science images at the time the science images are calibrated, if desired. They are added to the science images here, as a separate step, because in many situations it is fine to omit masking entirely and there is no particular advantage to introducing it earlier.

We begin, as usual, with a couple of imports.

from pathlib import Path

from astropy import units as u
from astropy.nddata import CCDData

import ccdproc as ccdp

Read masks that are the same for all of the science images

Click here to comment on this section on GitHub (opens in new tab).

In previous notebooks we constructed a mask based on the dark current and a mask created by ccdmask from a flat image. Displaying the summary of the the information about the reduced images is a handy way to determine which files are the masks.

ex2_path = Path('example2-reduced')

ifc = ccdp.ImageFileCollection(ex2_path)
ifc.summary['file', 'imagetyp']
Table masked=True length=38
fileimagetyp
str35str9
AutoFlat-PANoRot-r-Bin1-001.fitFLAT
AutoFlat-PANoRot-r-Bin1-002.fitFLAT
AutoFlat-PANoRot-r-Bin1-003.fitFLAT
AutoFlat-PANoRot-r-Bin1-004.fitFLAT
AutoFlat-PANoRot-r-Bin1-005.fitFLAT
AutoFlat-PANoRot-r-Bin1-006.fitFLAT
AutoFlat-PANoRot-r-Bin1-007.fitFLAT
AutoFlat-PANoRot-r-Bin1-008.fitFLAT
AutoFlat-PANoRot-r-Bin1-009.fitFLAT
AutoFlat-PANoRot-r-Bin1-010.fitFLAT
......
Dark-S001-R001-C009-NoFilt copy.fitDARK
Dark-S001-R001-C009-NoFilt.fitDARK
Dark-S001-R001-C020-NoFilt.fitDARK
combined_bias.fitBIAS
combined_dark_90.000.fitDARK
combined_flat_filter_r.fitFLAT
kelt-16-b-S001-R001-C084-r.fitLIGHT
kelt-16-b-S001-R001-C125-r.fitLIGHT
mask_from_ccdmask.fitsflat mask
mask_from_dark_current.fitsdark mask

We read each of those in below, converting the mask to boolean after we read it.

mask_ccdmask = CCDData.read(ex2_path / 'mask_from_ccdmask.fits', unit=u.dimensionless_unscaled)
mask_ccdmask.data = mask_ccdmask.data.astype('bool')

mask_hot_pix = CCDData.read(ex2_path / 'mask_from_dark_current.fits', unit=u.dimensionless_unscaled)
mask_hot_pix.data = mask_hot_pix.data.astype('bool')

Combining the masks

Click here to comment on this section on GitHub (opens in new tab).

We combine the masks using a logical "or" since we want to mask out pixels that unit=u.dimensionless_unscaled bad for any reason.

combined_mask = mask_ccdmask.data | mask_hot_pix.data

It turns out we are masking roughly 0.056% of the pixels so far.

combined_mask.sum()
9422

Detect cosmic rays

Click here to comment on this section on GitHub (opens in new tab).

Cosmic ray detection was discussed in detail in an earlier section. Here we loop over all of the calibrated science images and:

  • detect cosmic rays in them,
  • combine the cosmic ray mask with the mask that applies to all images,
  • set the mask of the image to the overall mask, and
  • save the image, overwriting the calibrated science image without the mask.

Since the cosmic ray detection takes a while, a status message is displayed before each image is processed.

ifc.files_filtered()
for ccd, file_name in ifc.ccds(imagetyp='light', return_fname=True):
    print('Working on file {}'.format(file_name))
    new_ccd = ccdp.cosmicray_lacosmic(ccd, readnoise=10, sigclip=8, verbose=True)
    overall_mask = new_ccd.mask | combined_mask
    # If there was already a mask, keep it.
    if ccd.mask is not None:
        ccd.mask = ccd.mask | overall_mask
    else:
        ccd.mask = overall_mask
    # Files can be overwritten only with an explicit option
    ccd.write(ifc.location / file_name, overwrite=True)
WARNING: FITSFixedWarning: RADECSYS= 'FK5 ' / Equatorial coordinate system 
the RADECSYS keyword is deprecated, use RADESYSa. [astropy.wcs.wcs]
WARNING:astropy:FITSFixedWarning: RADECSYS= 'FK5 ' / Equatorial coordinate system 
the RADECSYS keyword is deprecated, use RADESYSa.
Working on file kelt-16-b-S001-R001-C084-r.fit
Starting 4 L.A.Cosmic iterations
Iteration 1:
211 cosmic pixels this iteration
Iteration 2:
111 cosmic pixels this iteration
Iteration 3:
112 cosmic pixels this iteration
Iteration 4:
86 cosmic pixels this iteration
Working on file kelt-16-b-S001-R001-C125-r.fit
Starting 4 L.A.Cosmic iterations
Iteration 1:
98 cosmic pixels this iteration
Iteration 2:
62 cosmic pixels this iteration
Iteration 3:
59 cosmic pixels this iteration
Iteration 4:
67 cosmic pixels this iteration