Data_Reduction/Imaging_Data_Reduction
import os
import glob
# input files with the absolute path.
infiles = {
"J1": {
"b1": ["/data/det_b/SWSB00008031.fits", "/data/det_b/SWSB00008041.fits", "/data/det_b/SWSB00008051.fits", "/data/det_b/SWSB00008061.fits"],
"b2": ["/data/det_b/SWSB00008032.fits", "/data/det_b/SWSB00008042.fits", "/data/det_b/SWSB00008052.fits", "/data/det_b/SWSB00008062.fits"]
},
"H1": {
"r1": ["/data/det_r/SWSR00009021.fits", "/data/det_r/SWSR00009031.fits", "/data/det_r/SWSR00009041.fits", "/data/det_r/SWSR00009051.fits"],
"r2": ["/data/det_r/SWSR00009022.fits", "/data/det_r/SWSR00009032.fits", "/data/det_r/SWSR00009042.fits", "/data/det_r/SWSR00009052.fits"]
}
}
################################################################################
# essential parameters
n_iter=2; n_start=0; mag_min_wcs=14; mag_max_wcs=20; detect_thresh_wcs=3; detect_thresh_thru=3; astref_catalog="PANSTARRS-1"; astref_band="z"; max_sep_wcs=0.2; max_sep_thru=0.5; self_flux_calib=False; two_step_segmap=True;
mag_min_thrus = {"J1": 15.0, "H1": 16.0}
mag_max_thrus = {"J1": 17.0, "H1": 17.5}
pwd = os.getcwd()
for band in infiles.keys():
# prepare a directory for each band data.
work_dir = os.path.join(pwd, band)
if not os.path.exists(work_dir):
os.makedirs(work_dir)
os.chdir(work_dir)
mag_min_thru = mag_min_thrus[band.upper()]
mag_max_thru = mag_max_thrus[band.upper()]
# reduce data for each detector.
for armch in infiles[band].keys():
in_arr = infiles[band][armch]
exec(open("/work/swsred/reduce_all.py").read())
# -> flat-fielded, sky-subtracted, and wcs-corrected frames ('wc' files) created.
# collect all the indiviaual frames reduced for all detectors.
wc_arr = glob.glob("wcSWS*.fits")
wc_arr.sort()
# filenames of the corresponding weight maps.
wht_arr = [in_fits.replace("wcSWS", "SWS").replace(".fits", "_wht.fits") for in_fits in wc_arr]
# filenames for the mosaicked products to be created.
out_fits = "fs12_{}.fits".format(band.lower())
out_wht_fits = out_fits.replace(".fits", "_wht.fits")
out_exp_fits = out_fits.replace(".fits", "_exp.fits")
out_clip_log = out_fits.replace(".fits", "_clip.log")
frame = swsred.frame_info.FrameInfo(out_fits)
# mosaic the 'wc' frames.
swarp_config = swsred.swarp.get_default_config()
swarp_config["IMAGEOUT_NAME"] = out_fits
swarp_config["WEIGHTOUT_NAME"] = out_wht_fits
swarp_config["WEIGHT_TYPE"] = "MAP_WEIGHT"
swarp_config["WEIGHT_IMAGE"] = wht_arr
swarp_config["RESCALE_WEIGHTS"] = "N"
swarp_config["WRITE_FILEINFO"] = "N"
swarp_config["CLIP_LOGNAME"] = out_clip_log
swsred.swarp.swarp(wc_arr, swarp_config)
# make an exposure map for the mosaicked frame.
swsred.make_exposure_map.make_exposure_map(wc_arr, out_exp_fits, wht_arr, clipped_log=out_clip_log)
swsred.hedit.hedit_1(out_fits, {"EXP_TOT": (astropy.io.fits.getdata(out_exp_fits).max(), "total (maximum) exposure time of frames stacked.")})
# fetch 2MASS catalog.
refcat_thru = swsred.tmc.TMC()
refcat_thru.keep_file = False
refcat_thru.fetch_by_fits(out_fits, crop=True)
refcat_thru.clean()
refcat_thru.poserr_cut()
refcat_thru.ra.unit = astropy.units.degree
refcat_thru.dec.unit = astropy.units.degree
refcat_thru_ra = refcat_thru.ra.copy()
refcat_thru_dec = refcat_thru.dec.copy()
refcat_thru_mag = refcat_thru.magAB_at(frame.lam_c).copy()
refcat_thru_e_mag = refcat_thru.e_magAB_at(frame.lam_c).copy()
# correct for positional offset (not sure but exists).
refcat_thru_ra -= 0.1 / 3600
refcat_thru_dec -= 0.2 / 3600
# evaluate a throughput of the mosaicked frame.
sex_params = swsred.evaluate.SEX_PARAMS_THRU
sex_config = swsred.sextractor.get_default_config()
sex_config["DETECT_THRESH"] = detect_thresh_thru
sex_config["CATALOG_TYPE"] = "ASCII_HEAD"
sex_config["WEIGHT_TYPE"] = "MAP_WEIGHT"
sex_config["PHOT_APERTURES"] = d_aper
sex_config["WEIGHT_IMAGE"] = out_wht_fits
sex_tbl = swsred.sextractor.sextractor_1(out_fits, params=sex_params, config=sex_config, set_satur_level=True)
sex_tbl.remove_rows(sex_tbl["FLAGS"] > 4)
sex_tbl.remove_rows(sex_tbl["FLAGS"] == 2) # blended source
tbl = swsred.evaluate.thru_1(out_fits, sex_tbl["XWIN_IMAGE"], sex_tbl["YWIN_IMAGE"], sex_tbl["ALPHAWIN_J2000"], sex_tbl["DELTAWIN_J2000"], sex_tbl["FWHM_IMAGE"], sex_tbl["FLUX_APER"], sex_tbl["FLUXERR_APER"], max_sep=0.5, ref_ra=refcat_thru_ra, ref_dec=refcat_thru_dec, ref_mag=refcat_thru_mag, ref_e_mag=refcat_thru_e_mag, mag_min=mag_min_thru, mag_max=mag_max_thru)
# evaluate a limiting magnitude of the mosaicked frame.
swsred.calc_lim_mag.calc_lim_mag(out_fits, mask_fits=out_wht_fits, seg_fits=None, r_aper=0.5/0.095, mask_thresh=0.3, n_aper=40000, check_hist=False, check_ds9=False)