Skip to content

Instantly share code, notes, and snippets.

@meredith-durbin
Last active September 18, 2023 13:31
Show Gist options
  • Save meredith-durbin/b3e89f0a837c4fa88e83ac1af675a20e to your computer and use it in GitHub Desktop.
Save meredith-durbin/b3e89f0a837c4fa88e83ac1af675a20e to your computer and use it in GitHub Desktop.
deepCR for flc and drc images
import glob
import numpy as np
import os
import shutil
from deepCR import deepCR
from astropy.io import fits
# dq flag: http://www.stsci.edu/hst/instrumentation/acs/data-analysis/dq-flag-definitions
# dq flag: http://www.stsci.edu/hst/instrumentation/acs/data-analysis/dq-flag-definitions
def make_crclean(filename, model, threshold=0.5):
fext = filename.split('_')[-1].split('.fit')[0]
cleanfile = filename.replace(fext, 'crclean')
shutil.copy(filename, cleanfile)
is_drizzled = fext.startswith('dr')
with fits.open(cleanfile, mode='update') as hdulist:
if is_drizzled:
exptime = hdulist[0].header['TEXPTIME']
for i, ext in enumerate(hdulist):
if ext.name == 'SCI':
image = ext.data.copy()
if is_drizzled:
image *= exptime
image[~np.isfinite(image)] = 0.
mask, cleaned_image = model.clean(image, threshold=threshold, inpaint=True, n_jobs=1)
if is_drizzled:
cleaned_image[~np.isfinite(ext.data)] = np.nan
cleaned_image /= exptime
ext.data = cleaned_image.astype(np.float32)
hdulist.flush()
if ((len(hdulist) > i+2) & ~is_drizzled):
if (hdulist[i+2].name == 'DQ'):
hdulist[i+2].data[mask.astype(bool)] += 16384
model = deepCR(mask='ACS-WFC-F606W-2-4', inpaint='ACS-WFC-F606W-2-32', device='CPU')
files = glob.glob('data/*_??c.fits')
for f in files:
if (f.endswith('drc.fits') | f.endswith('flc.fits')):
if not os.path.exists(f.replace('drc','crclean').replace('flc','crclean')):
try:
make_crclean(f, model)
print(f'Made crcleaned copy of {f}')
except Exception as exc:
print(f'Failed on {f}')
print(exc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment