Work through the reduction of a single dataset


In [2]:
# imports
import os
import glob
import pdb
#from imp import reload
#from importlib import reload

from astropy.io import fits

from cosredux import utils as cr_utils
from cosredux import trace as cr_trace
from cosredux import darks as cr_darks
from cosredux import io as cr_io
from cosredux import science as cr_science

In [ ]:
# python setup.py develop

Setup files

Copy files from Dropbox to local, working folder
cd 'working_folder'
# Darks, if needed
cp -rp ~/Dropbox/COS-LRG/darksall .
# Calibs, as needed
cp -rp ~/Dropbox/COS-LRG/calibfilesmast .
# Subset of the raw and object calibration files
mkdir LCYA01010
cd LCYA01010
cp -rp ~/Dropbox/COS-LRG/LCYA01010/*rawtag* .
cp -rp ~/Dropbox/COS-LRG/LCYA01010/lcya01010_asn.fits .
cp -rp ~/Dropbox/COS-LRG/LCYA01010/lcya01010_trl.fits .
cp -rp ~/Dropbox/COS-LRG/LCYA01010/lcya01010_j*.fits .

In [3]:
rdx_path = '/Users/xavier/HST/COS/LRG_Redux/'
#rdx_path = '/home/marijana/ReductionCOS/'
science_folder = 'LCYA01010/'
dark_folder = 'darksall/'
#calib_folder = 'calibfilesmast/'
calib_folder = 'calibs/'
root_out = 'lcya01010'

In [4]:
# Default values
defaults = {}
defaults['pha_mnx'] = (2,15)
defaults['apert'] = 25.
defaults['ndays'] = 90.

Customize for calcos

Science frames
LP2 calib file

Science frames


In [3]:
cr_utils.modify_rawtag_for_calcos(rdx_path+science_folder)


Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_rawtag_a.fits

Calibs -- May not need to repeat this (i.e. if done before)


In [4]:
cr_utils.modify_LP2_1dx_calib(rdx_path+calib_folder)


WARNING: UnitsWarning: The unit 'angstrom' has been deprecated in the FITS standard. Suggested: 10**-1 nm. [astropy.units.format.utils]

Run calcos

Launch pyraf

cd ureka
xgterm (or xterm)
export lref=/home/marijana/ReductionCOS/calibfilesmast/
# Working in xgterm window
ur_setup
mkiraf #?
pyraf

Execute

cd folder_with_data  (e.g. LCYA01010)
stsdas
hst_calib
hstcos
#
calcos [dataset prefix]_asn.fits (e.g. calcos lcya01010_asn.fits)

Crashing and rerunning

If you have to rerun, remove the corrtag and .tra files

Coadd corrtag files


In [3]:
corrtag_files_a = glob.glob(rdx_path+science_folder + '*_corrtag_a.fits')
corrtag_files_b = glob.glob(rdx_path+science_folder + '*_corrtag_b.fits')

In [4]:
corrtag_files_a


Out[4]:
['/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_corrtag_a.fits']

In [24]:
_ = cr_utils.coadd_bintables(corrtag_files_a, outfile=rdx_path+science_folder+root_out+'_coaddcorr_woPHA_a.fits')
_ = cr_utils.coadd_bintables(corrtag_files_b, outfile=rdx_path+science_folder+root_out+'_coaddcorr_woPHA_b.fits')


Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_woPHA_a.fits
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_woPHA_b.fits

Traces


In [25]:
reload(cr_trace)
traces_a=cr_trace.traces(rdx_path+science_folder+root_out+'_coaddcorr_woPHA_a.fits', rdx_path+calib_folder, 'FUVA', clobber=True)
traces_b=cr_trace.traces(rdx_path+science_folder+root_out+'_coaddcorr_woPHA_b.fits', rdx_path+calib_folder, 'FUVB', clobber=True)


Updated trace for segment=FUVA in /Users/xavier/HST/COS/LRG_Redux/calibs/x6q17586l_1dx.fits
Wrote Traces to {:s} /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_woPHA_a_traces.json
Updated trace for segment=FUVB in /Users/xavier/HST/COS/LRG_Redux/calibs/x6q17586l_1dx.fits
Wrote Traces to {:s} /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_woPHA_b_traces.json

Ignoring Sun and Limb

Get HVLEVELs


In [18]:
reload(cr_utils)
hva_a, hvb_a = cr_utils.get_hvlevels(corrtag_files_a)
hva_b, hvb_b = cr_utils.get_hvlevels(corrtag_files_b)


HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_corrtag_a.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_corrtag_a.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_corrtag_a.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_corrtag_a.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_corrtag_b.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_corrtag_b.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_corrtag_b.fits
HVA=167 and HVB=169 in file /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_corrtag_b.fits

Change PHA

Examine PHA values near the trace of a single exposure (OPTIONAL)


In [91]:
reload(cr_science)
ex_region = cr_science.set_extraction_region(traces_a[0], 'FUVA', corrtag_files_a[0], check=True)

In [94]:
reload(cr_darks)
pha_values_a, _, _ = cr_darks.get_pha_values_science(ex_region, corrtag_files_a[0], background=False)

In [95]:
from xastropy.xutils import xdebug as xdb
xdb.xhist(pha_values_a)

Edit


In [97]:
# Reset pha_mnx above if you wish

In [23]:
reload(cr_utils)
cr_utils.change_pha(rdx_path+calib_folder, low=defaults['pha_mnx'][0], up=defaults['pha_mnx'][1])


Updating PHA values in /home/marijana/ReductionCOS/calibfilesmast/wc318317l_pha.fits

Modify PHACORR


In [25]:
reload(cr_utils)
cr_utils.modify_phacorr(rdx_path+science_folder)


Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01ggq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_rawtag_a.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01fyq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01gjq_rawtag_b.fits
Modifying header cards for rawtag file: /home/marijana/ReductionCOS/LCYA01010/lcya01g0q_rawtag_a.fits

Clean for CALCOS


In [9]:
reload(cr_utils)
cr_utils.clean_for_calcos_phafiltering(rdx_path+science_folder)

Run Calcos as above -- with PHA restricted

Coadd new PHA frames


In [4]:
corrtag_files_a = glob.glob(rdx_path+science_folder + '*_corrtag_a.fits')
corrtag_files_b = glob.glob(rdx_path+science_folder + '*_corrtag_b.fits')

In [5]:
corrtag_files_a


Out[5]:
['/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_corrtag_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_corrtag_a.fits']

In [24]:
_ = cr_utils.coadd_bintables(corrtag_files_a, outfile=rdx_path+science_folder+root_out+'_coaddcorr_withPHA_a.fits')
_ = cr_utils.coadd_bintables(corrtag_files_b, outfile=rdx_path+science_folder+root_out+'_coaddcorr_withPHA_b.fits')


Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_withPHA_a.fits
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01010_coaddcorr_withPHA_b.fits

Reduce Darks

Find Darks


In [23]:
reload(cr_darks)
subf_a = cr_darks.setup_for_calcos(rdx_path+dark_folder, corrtag_files_a[0], 'FUVA')


HVA=167 and HVB=169 in file /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_a.fits
Copying dark frames into sub folder: darks_a_167
Editing dark frame headers
Wrote calcos script: /Users/xavier/HST/COS/LRG_Redux/LCYA01010/darks_a_167/darks_a_167.cl

Run calcos

In new sub-folder for darks
Use the script

Clean unnecessary (large) files


In [27]:
reload(cr_darks)
cr_darks.clean_after_calcos(rdx_path+science_folder+subf_a)

Measure Background

Set background region

Set chk=True to show plots
Iterate as needed

In [31]:
# Read traces (if needed)
traces_a = cr_io.read_traces(rdx_path+science_folder+root_out+'_coaddcorr_woPHA_a.fits')
traces_b = cr_io.read_traces(rdx_path+science_folder+root_out+'_coaddcorr_woPHA_b.fits')

In [32]:
reload(cr_darks)
chk = True
bg_region_a = cr_darks.set_background_region(traces_a[0], 'FUVA', rdx_path+science_folder+root_out+'_coaddcorr_woPHA_a.fits', check=chk)
bg_region_b = cr_darks.set_background_region(traces_b[0], 'FUVB', rdx_path+science_folder+root_out+'_coaddcorr_woPHA_b.fits', check=chk)
bg_region_a, bg_region_b


Out[32]:
({u'lower': (1200.0, 2445.5107, 431.1351318359375, 381.1351318359375),
  u'upper': (1200.0, 2445.5107, 491.1351318359375, 541.1351318359375)},
 {u'lower': (50.0, 1209.924, 490.27716064453125, 440.27716064453125)})

Loop on Exposures without PHA

Will generate background spectra, one per exposure per segment
Below we only do FUVA

In [28]:
corrtag_woPHA_a = glob.glob(rdx_path+science_folder + '*_corrtag_woPHA_a.fits')
corrtag_woPHA_a


Out[28]:
['/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_woPHA_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_corrtag_woPHA_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_corrtag_woPHA_a.fits',
 '/Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_corrtag_woPHA_a.fits']

In [35]:
reload(cr_darks)
reload(cr_utils)
cr_darks.dark_to_exposures(corrtag_woPHA_a, bg_region_a, traces_a[0], 'FUVA', defaults)


HVA=167 and HVB=169 in file /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_woPHA_a.fits
HVA=167 and HVB=169 in file /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_corrtag_woPHA_a.fits
HVA=167 and HVB=169 in file /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_corrtag_woPHA_a.fits
HVA=167 and HVB=169 in file /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_corrtag_woPHA_a.fits
Working on exposure: /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_corrtag_woPHA_a.fits
Matching darks to the exposure
We have 14 darks to use
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/darks_a_167/lcya01fyq_darks.fits
Will use scale factor = 0.079273
Extracting..
Wrote background spectrum to /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01fyq_a_bkgd.fits
Working on exposure: /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_corrtag_woPHA_a.fits
Matching darks to the exposure
We have 25 darks to use
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/darks_a_167/lcya01g0q_darks.fits
Will use scale factor = 0.0378727
Extracting..
Wrote background spectrum to /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01g0q_a_bkgd.fits
Working on exposure: /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_corrtag_woPHA_a.fits
Matching darks to the exposure
We have 31 darks to use
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/darks_a_167/lcya01ggq_darks.fits
Will use scale factor = 0.0450641
Extracting..
Wrote background spectrum to /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01ggq_a_bkgd.fits
Working on exposure: /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_corrtag_woPHA_a.fits
Matching darks to the exposure
We have 22 darks to use
Wrote outfile /Users/xavier/HST/COS/LRG_Redux/LCYA01010/darks_a_167/lcya01gjq_darks.fits
Will use scale factor = 0.0571152
Extracting..
Wrote background spectrum to /Users/xavier/HST/COS/LRG_Redux/LCYA01010/lcya01gjq_a_bkgd.fits

Coadd


In [5]:
x1d_files = glob.glob(rdx_path+science_folder + '*_x1d.fits')

In [6]:
reload(cr_science)
cr_science.coadd_exposures(x1d_files, 'FUVA', rdx_path+science_folder+'LCYA01010_coadd.fits')


WARNING: UnitsWarning: The unit 'angstrom' has been deprecated in the FITS standard. Suggested: 10**-1 nm. [astropy.units.format.utils]
WARNING: UnitsWarning: The unit 'erg' has been deprecated in the FITS standard. Suggested: cm2 g s-2. [astropy.units.format.utils]
WARNING: UnitsWarning: 'erg /s /cm**2 /angstrom' contains multiple slashes, which is discouraged by the FITS standard [astropy.units.format.generic]
Wrote /Users/xavier/HST/COS/LRG_Redux/LCYA01010/LCYA01010_coadd.fits

Bin to 2 pixels


In [10]:
reload(cr_science)
cr_science.coadd_exposures(x1d_files, 'FUVA', rdx_path+science_folder+'LCYA01010_coadd_bin2.fits', bin=2)


Wrote /Users/xavier/HST/COS/LRG_Redux/LCYA01010/LCYA01010_coadd_bin2.fits

In [ ]: