def test(): import threedhst.prep_flt_astrodrizzle as prep asn_file = 'HOPS183B-10-140-G141_asn.fits' #asn_file = 'HOPS183B-10-140-F160W_asn.fits' threedhst.process_grism.fresh_flt_files(asn_file, from_path='../RAW/', preserve_dq=False) asn = threedhst.utils.ASNFile(asn_file) for exp in asn.exposures: prep.subtract_fixed_background(flt_file='%s_flt.fits' %(exp), path_to_raw='../RAW/')
def m82(): import glob import os import threedhst import threedhst.prep_flt_astrodrizzle as init from threedhst import catIO import unicorn import research.hawkiff import stsci.convolve import research.pab.align as align ### Make ACS associations unicorn.candels.make_asn_files(uniquename=True, translate={'-ROT': ''}) threedhst.options['FLT_PERSISTENCE_PATH'] = '../Persistence/' #### IR files = glob.glob('*-F1*asn.fits') radec = None for asn_file in files: #init.prep_direct_grism_pair(direct_asn=asn_file, grism_asn=None, radec=radec, ACS=False, align_threshold=5, raw_path='../RAW/', order=0) threedhst.process_grism.fresh_flt_files(asn_file) asn = threedhst.utils.ASNFile(asn_file) for exp in asn.exposures: init.subtract_fixed_background(flt_file='%s_flt.fits' % (exp)) #drizzlepac.astrodrizzle.AstroDrizzle(asn_file, static=False, skysub=False, driz_separate=True, driz_sep_wcs=False, median=True, blot=True, driz_cr=True, driz_combine=True, final_wht_type='IVM', clean=True, final_wcs=True, final_rot=0, final_scale=0.128, final_pixfrac=1, context=False, resetbits=0, final_bits=64, preserve=False) # ims = [] hdrs = [] for asn_file in files: asn = threedhst.utils.ASNFile(asn_file) ### cross correlation for exp in asn.exposures: flt = pyfits.open('%s_flt.fits' % (exp)) slx, sly = slice(400, 600), slice(400, 600) #slx, sly = slice(450,550), slice(450,550) ims.append((flt[1].data * (flt[3].data == 0))[sly, slx]) hdrs.append(flt[1].header) shifts = [[0, 0]] for i in range(1, len(ims)): ccorr = stsci.convolve.correlate2d(ims[0], ims[i], fft=1) from astropy.modeling import models, fitting poly = models.Polynomial2D(degree=4) lsq = fitting.LevMarLSQFitter() yp, xp = np.indices(ccorr.shape) pfit = lsq(poly, xp, yp, ccorr) #ix = np.unravel_index(np.argmax(ccorr-pfit(xp, yp)), ccorr.shape) sh = ccorr.shape ix = [sh[0] / 2, sh[1] / 2] ccorr[ix[0], ix[1]] = np.nan ds9.view(ccorr - pfit(xp, yp)) status = raw_input('Mark center of ccorr peak') xcf = np.cast[float](ds9.get('pan image').split()) xc = np.cast[int](np.round(xcf)) N = 10 sly2, slx2 = slice(xc[1] - N - 1, xc[1] + N - 1), slice(xc[0] - N - 1, xc[0] + N - 1) sub = (ccorr - pfit(xp, yp))[sly2, slx2] flat = models.Polynomial2D(degree=1) gauss = models.Gaussian2D(100000, xcf[0], xcf[1], 1, 1, 0, bounds={ 'x_mean': [xcf[0] - 5, xcf[0] + 5], 'y_mean': [xcf[1] - 5, xcf[1] + 5] }) ok = np.isfinite(sub) gfit = lsq(flat + gauss, xp[sly2, slx2][ok], yp[sly2, slx2][ok], sub[ok]) shifts.append([ix[1] - gfit.x_mean_1, ix[0] - gfit.y_mean_1]) print shifts[-1] wcs = pywcs.WCS(hdrs[0]) crpix = [[hdrs[0]['CRPIX1'], hdrs[0]['CRPIX2']]] for i in range(1, len(ims)): wcs = pywcs.WCS(hdrs[i]) xy = wcs.all_world2pix([hdrs[0]['CRVAL1']], [hdrs[0]['CRVAL2']], 1) crpix.append([xy[0][0], xy[1][0]]) print crpix[-1] dx = np.array(shifts) - (np.array(crpix) - crpix[0]) ### M82 dx = np.array([[0., 0.], [0.1692337, -0.03445068], [0.11051269, 0.26955819], [0.04652894, 0.30199132]]) i0 = 0 for asn_file in files: asn = threedhst.utils.ASNFile(asn_file) fp = open(asn_file.replace('_asn.fits', '_shifts.txt'), 'w') fp.write("""# frame: output # refimage: %s_flt.fits[1] # form: delta # units: pixels """ % (asn.exposures[0])) for exp in asn.exposures: fp.write('%s_flt.fits %.4f %.4f 0, 1.0 0.1 0.1\n' % (exp, dx[i0][0], dx[i0][1])) flt = pyfits.open('%s_flt.fits' % (exp), mode='update') flt[1].header['CRPIX1'] += dx[i0][0] flt[1].header['CRPIX2'] += dx[i0][1] flt.flush() i0 += 1 fp.close() for asn_file in files: drizzlepac.astrodrizzle.AstroDrizzle(asn_file, static=False, skysub=True, driz_separate=True, driz_sep_wcs=False, median=True, blot=True, driz_cr=True, driz_combine=True, final_wht_type='IVM', clean=True, final_wcs=True, final_rot=0, final_scale=0.1, final_pixfrac=1, context=False, resetbits=4096, final_bits=64, preserve=False) for asn_file in files: align.coarse_align(image=asn_file.replace('_asn.fits', '_drz_sci.fits'), ref='h_m82_v_s20_drz_sci.fits')
def m82(): import glob import os import threedhst import threedhst.prep_flt_astrodrizzle as init from threedhst import catIO import unicorn import research.hawkiff import stsci.convolve import research.pab.align as align ### Make ACS associations unicorn.candels.make_asn_files(uniquename=True, translate={'-ROT': ''}) threedhst.options['FLT_PERSISTENCE_PATH'] = '../Persistence/' #### IR files = glob.glob('*-F1*asn.fits') radec = None for asn_file in files: #init.prep_direct_grism_pair(direct_asn=asn_file, grism_asn=None, radec=radec, ACS=False, align_threshold=5, raw_path='../RAW/', order=0) threedhst.process_grism.fresh_flt_files(asn_file) asn = threedhst.utils.ASNFile(asn_file) for exp in asn.exposures: init.subtract_fixed_background(flt_file='%s_flt.fits' % (exp)) #drizzlepac.astrodrizzle.AstroDrizzle(asn_file, static=False, skysub=False, driz_separate=True, driz_sep_wcs=False, median=True, blot=True, driz_cr=True, driz_combine=True, final_wht_type='IVM', clean=True, final_wcs=True, final_rot=0, final_scale=0.128, final_pixfrac=1, context=False, resetbits=0, final_bits=64, preserve=False) # ims = [] hdrs = [] for asn_file in files: asn = threedhst.utils.ASNFile(asn_file) ### cross correlation for exp in asn.exposures: flt = pyfits.open('%s_flt.fits' % (exp)) slx, sly = slice(400, 600), slice(400, 600) #slx, sly = slice(450,550), slice(450,550) ims.append((flt[1].data * (flt[3].data == 0))[sly, slx]) hdrs.append(flt[1].header) shifts = [[0, 0]] for i in range(1, len(ims)): ccorr = stsci.convolve.correlate2d(ims[0], ims[i], fft=1) from astropy.modeling import models, fitting poly = models.Polynomial2D(degree=4) lsq = fitting.LevMarLSQFitter() yp, xp = np.indices(ccorr.shape) pfit = lsq(poly, xp, yp, ccorr) #ix = np.unravel_index(np.argmax(ccorr-pfit(xp, yp)), ccorr.shape) sh = ccorr.shape ix = [sh[0] / 2, sh[1] / 2] ccorr[ix[0], ix[1]] = np.nan ds9.view(ccorr - pfit(xp, yp)) status = raw_input('Mark center of ccorr peak') xcf = np.cast[float](ds9.get('pan image').split()) xc = np.cast[int](np.round(xcf)) N = 10 sly2, slx2 = slice(xc[1] - N - 1, xc[1] + N - 1), slice( xc[0] - N - 1, xc[0] + N - 1) sub = (ccorr - pfit(xp, yp))[sly2, slx2] flat = models.Polynomial2D(degree=1) gauss = models.Gaussian2D( 100000, xcf[0], xcf[1], 1, 1, 0, bounds={ 'x_mean': [xcf[0] - 5, xcf[0] + 5], 'y_mean': [xcf[1] - 5, xcf[1] + 5] }) ok = np.isfinite(sub) gfit = lsq(flat + gauss, xp[sly2, slx2][ok], yp[sly2, slx2][ok], sub[ok]) shifts.append([ix[1] - gfit.x_mean_1, ix[0] - gfit.y_mean_1]) print shifts[-1] wcs = pywcs.WCS(hdrs[0]) crpix = [[hdrs[0]['CRPIX1'], hdrs[0]['CRPIX2']]] for i in range(1, len(ims)): wcs = pywcs.WCS(hdrs[i]) xy = wcs.all_world2pix([hdrs[0]['CRVAL1']], [hdrs[0]['CRVAL2']], 1) crpix.append([xy[0][0], xy[1][0]]) print crpix[-1] dx = np.array(shifts) - (np.array(crpix) - crpix[0]) ### M82 dx = np.array([[0., 0.], [0.1692337, -0.03445068], [0.11051269, 0.26955819], [0.04652894, 0.30199132]]) i0 = 0 for asn_file in files: asn = threedhst.utils.ASNFile(asn_file) fp = open(asn_file.replace('_asn.fits', '_shifts.txt'), 'w') fp.write("""# frame: output # refimage: %s_flt.fits[1] # form: delta # units: pixels """ % (asn.exposures[0])) for exp in asn.exposures: fp.write('%s_flt.fits %.4f %.4f 0, 1.0 0.1 0.1\n' % (exp, dx[i0][0], dx[i0][1])) flt = pyfits.open('%s_flt.fits' % (exp), mode='update') flt[1].header['CRPIX1'] += dx[i0][0] flt[1].header['CRPIX2'] += dx[i0][1] flt.flush() i0 += 1 fp.close() for asn_file in files: drizzlepac.astrodrizzle.AstroDrizzle( asn_file, static=False, skysub=True, driz_separate=True, driz_sep_wcs=False, median=True, blot=True, driz_cr=True, driz_combine=True, final_wht_type='IVM', clean=True, final_wcs=True, final_rot=0, final_scale=0.1, final_pixfrac=1, context=False, resetbits=4096, final_bits=64, preserve=False) for asn_file in files: align.coarse_align( image=asn_file.replace('_asn.fits', '_drz_sci.fits'), ref='h_m82_v_s20_drz_sci.fits')