Skip to content

Instantly share code, notes, and snippets.

@pllim
Created September 11, 2017 16:40
Show Gist options
  • Save pllim/ef0b8bc20b9b9e106913e82d8acb8297 to your computer and use it in GitHub Desktop.
Save pllim/ef0b8bc20b9b9e106913e82d8acb8297 to your computer and use it in GitHub Desktop.
Script written by @dborncamp to create CRC files for post-SM4 WFC subarrays
import os
import logging
import datetime
import shutil
import glob
from astropy.io import fits
from acstools import acs_destripe
from acstools import acsccd
from acstools import acscte
from acstools import acsrej
from acstools import acs2d
def _parse_crj_asn(asnfile):
"""
Parse the file names that go into the association file and return the root
file names that go into the association
"""
with fits.open(asnfile, 'readonly') as asnhdu:
asndata = asnhdu[1].data
rootnames = asndata['MEMNAME'][asndata['MEMTYPE'] == 'EXP-CRJ']
# strip off the potentially trailing whitespace and convert to list
rootnames = [name.strip(' ').lower() for name in rootnames.tolist()]
return rootnames
def updatehdr_asn(asnfile, dire='.'):
"""
Update the header for the files that are in an association
"""
rootnames = _parse_crj_asn(asnfile)
rawnames = [os.path.join(dire, name + '_raw.fits') for name in rootnames]
updatehdr(rawnames)
def updatehdr(rawlist):
"""
Update the header of raw files to use the right reference files
"""
for fil in rawlist:
with fits.open(fil, 'update') as hdu:
# warn the user if it is taken after flight software update
dateobs = hdu[0].header['date-obs']
dateobs = datetime.datetime.strptime(dateobs, '%Y-%m-%d')
fswupdate = datetime.datetime(2016, 10, 1)
if dateobs < fswupdate:
logging.warn('Not after flight software update. This may fail')
hdu[0].header['DRKCFILE'] = 'wfc_gain2_161017_dkc.fits'
hdu[0].header['DARKFILE'] = 'wfc_gain2_161017_drk.fits'
hdu[0].header['MDRIZTAB'] = 'jref$16r12191j_mdz.fits'
hdu[0].header['PCTECORR'] = 'PERFORM'
hdu[0].header['PCTETAB'] = 'jref$16k1747tj_cte.fits'
hdu[0].header['OSCNTAB'] = 'jref$17717071j_osc.fits'
hdu[0].header['SINKCORR'] = 'PERFORM'
hdu[0].header['SNKCFILE'] = 'jref$16q14176j_snk.fits'
def recal(asnfile, suffix='dstrp'):
"""
Run acs destripe and other calacs functions on the asn file for Ralph's
program
"""
# get the rootname
root = os.path.basename(asnfile).split('_asn.fits')[0]
updatehdr_asn(asnfile)
acsccd.acsccd(asnfile)
acs_destripe.clean('*blv_tmp.fits', suffix)
acscte.acscte('*blv_tmp_{}.fits'.format(suffix))
acsrej.acsrej('*blc_tmp.fits', '{}_crc_tmp.fits'.format(root))
acs2d.acs2d('*crc_tmp.fits')
# rename the crc file which is what Ralph wants
for crcfile in glob.iglob('*crc_tmp.fits'):
goodfile = crcfile.split('_tmp.fits')[0] + '.fits'
shutil.copy2(crcfile, goodfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment