Beispiel #1
0
def hopr_brodwin():
    
    import unicorn
    import wfc3_gyro.prepare        
    import threedhst.prep_flt_astrodrizzle as init
        
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_BRODWIN/REDUCE/')
    unicorn.candels.make_asn_files()        
    RAW_PATH = '../RAW'
    
    prep_brodwin_drifted()
    os.system('rsync -av {}/ib2u72*_flt.fits.gz .'.format(RAW_PATH))
    os.system('gunzip -f ib2u72*_flt.fits.gz')
    
    init.prep_direct_grism_pair(direct_asn='BRODWIN-72-260-F160W_asn.fits', grism_asn=None, radec='../REF/radec.cat',
    	raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = 0.06,
    	skip_direct=False, ACS=False, align_threshold=6)

    for master_root in ['BRODWIN-22-051-F160W','BRODWIN-72-260-F160W']:
        asn = threedhst.utils.ASNFile('{}_asn.fits'.format(master_root))    
        for root in asn.exposures:
            wfc3_gyro.prepare.split_IMA(root=root)
            wfc3_gyro.prepare.make_pointing_asn(root=root, master_root=master_root)
            wfc3_gyro.prepare.subtract_background_reads(root=root, master_root=master_root)
            wfc3_gyro.prepare.fix_cosmic_rays(root=root, master_root=master_root)
            wfc3_gyro.prepare.align_reads(root=root, refimage='../REF/BRODWIN-72-260-F160W_drz_sci.fits', final_scale=0.12,
                master_catalog='../REF/BRODWIN-72-260-F160W_drz_sci.cat', refxcol = 13, refycol = 14)


    for root in ['ib2u72','ib2u22']:
        drizzle_flts(root=root)
Beispiel #2
0
def prep_cooper_repeat():
    
    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_COOPER/PREP/')

    unicorn.candels.make_asn_files()
    
    init.prep_direct_grism_pair(direct_asn='EGS13034445-55-081-F140W_asn.fits', grism_asn=None, radec=None,
    	raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = 0.06,
    	skip_direct=False, ACS=False, align_threshold=6)
    
    os.system('rsync -av EGS13034445-55-081-F140W_drz* ../REF/')
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_COOPER/REF/')
    wfc3_gyro.prepare.run_sextractor(mosaic='EGS13034445-55-081-F140W_drz_sci.fits', 
        weight='EGS13034445-55-081-F140W_drz_wht.fits')
    threedhst.sex.sexcatRegions('EGS13034445-55-081-F140W_drz_sci.cat','EGS13034445-55-081-F140W_drz_sci.reg', format=1)
    ### make an radec cat from sextractor cat
    
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_COOPER/PREP/')
    
    init.prep_direct_grism_pair(direct_asn='EGS13034445-55-081-F140W_asn.fits', grism_asn=None, radec='../REF/radec.cat',
    	raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = 0.06,
    	skip_direct=False, ACS=False, align_threshold=6)
Beispiel #3
0
def align_all(visits=[], make_asn = False, program=14227):
    """
    The catalog for ERSPRIME is South.
    """
    print paths
    print visits

    shutil.copy('../RAW/files.info', 'files.info')
    cleanup_outputs()
    fetch_gz(visits)

    if make_asn:
        unicorn.candels.make_asn_files()
        # Lower case the filenames
        asn_files = glob.glob('*asn.fits')
        for asn in asn_files:
            os.rename(asn, '{}_asn.fits'.format(asn.split('_')[0].upper()))
           #os.rename(asn, asn.upper())

    direct_files = glob.glob('*105*_asn.fits')

    print direct_files

    for direct in direct_files:
        # Check that not accidently looping over a program 13420 visit if
        # processing a 14227 visit of same number.
        if program == 14227 and 'GDN' not in direct:
            grism = direct.replace('F105W', 'G102')
            print " "
            print direct, grism

            #if 'GDN' in grism:
            #    visit = int(grism.split('-')[1])
            #else:
            #    visit = int(grism[4:6])
            visit = int(grism.split('-')[1])
            print "Visit: {}".format(visit)

            # Only reprocess the visits in the list, or reprocess
            # everything if the list is empty.
            if ((visits != []) and (visit in visits)) or (visits == []):

                if 'GN' in direct.upper() or 'GDN' in direct.upper():
                    cat_file = 'goodsn_radec.cat'
                    print "Using radec cat for NORTH, {}".format(cat_file)
                elif 'GS' in direct.upper() or 'ERSPRIME' in direct.upper():
                    cat_file = 'goodss_3dhst.v4.1.radec.cat'
                    print "Using radec cat for SOUTH, {}".format(cat_file)

                init.prep_direct_grism_pair(direct_asn=direct, grism_asn=grism, 
                    radec=os.path.join(paths['path_to_ref_files'], 'REF', cat_file),
                    raw_path = paths['path_to_RAW'], mask_grow=8, 
                    scattered_light=False, final_scale = None,
                    skip_direct=False, ACS=False, align_threshold=6)

    cleanup_gz()
Beispiel #4
0
def prep_clear(field, make_asn = True, check_background = False, 
    run_prep = True):

    import threedhst.prep_flt_astrodrizzle as init
    import unicorn.interlace_test as test

    if make_asn:

        unicorn.candels.make_asn_files()

    if check_background:

        #os.chdir('/Users/imomcheva/Work/CLEAR/RAW')
        os.chdir(paths['path_to_RAW'])
        asn_files = glob.glob('*asn.fits')
        for asn in asn_files:
            root = asn.split('_asn')[0]
            png = glob.glob(root+'*orbit.*')
            if (len(png) > 0):
                continue
            try:
                mywfc3.bg.show_orbit_limbangle(asn = [root])
            except:
                fp = open(root+'_orbit.failed','w')
                fp.write('---')
                fp.close()
        os.chdir(paths['path_to_PREPARE'])
        
    if remove_satelites:
        
        #os.chdir('/Users/imomcheva/Work/CLEAR/RAW/')
        os.chdir(paths['path_to_RAW'])
        unicorn.prepare.make_IMA_FLT(raw='icxt31r3q_raw.fits', pop_reads=[2])    
        os.chdir(paths['path_to_PREPARE'])
        
    if run_prep:

        os.chdir(paths['path_to_PREPARE'])

        grism_files = glob.glob(field + '*G102_asn.fits')
        direct_files = glob.glob(field + '*F105W_asn.fits')

        for direct, grism in zip(direct_files, grism_files):
            init.prep_direct_grism_pair(direct_asn=direct, grism_asn=grism, 
                radec=paths['path_to_ref_files']+'REF/goodss_3dhst.v4.1.radec.cat',
                raw_path = paths['path_to_RAW'], mask_grow=8, 
                scattered_light=False, final_scale = None,
                skip_direct=False, ACS=False, align_threshold=6)
Beispiel #5
0
def prep_brodwin_repeat():
    
    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_BRODWIN/PREP/')

    init.prep_direct_grism_pair(direct_asn='BRODWIN-72-260-F160W_asn.fits', grism_asn=None, radec=None,
    	raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = 0.06,
    	skip_direct=False, ACS=False, align_threshold=6)
    
    os.system('rsync -av BRODWIN-72-260-F160W_drz* ../REF/')
    ### make an radec cat from sextractor cat
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_BRODWIN/REF/')
    wfc3_gyro.prepare.run_sextractor(mosaic='BRODWIN-72-260-F160W_drz_sci.fits',weight='BRODWIN-72-260-F160W_drz_wht.fits')
    threedhst.sex.sexcatRegions('BRODWIN-72-260-F160W_drz_sci.cat','BRODWIN-72-260-F160W_drz_sci.reg', format=1)
    
    os.chdir('/3DHST/Spectra/Work/14114/TEST_HOPR_BRODWIN/PREP/')
    
    init.prep_direct_grism_pair(direct_asn='BRODWIN-72-260-F160W_asn.fits', grism_asn=None, radec='../REF/radec.cat',
    	raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = 0.06,
    	skip_direct=False, ACS=False, align_threshold=6)
Beispiel #6
0
def acs():
    """
    Script to reduce ACS Cluster data
    """
    
    import glob
    import os
    import drizzlepac
    import numpy as np
    
    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    from threedhst import catIO
    
    import unicorn
    import unicorn.interlace_acs
    
    
    field = 'MACS*1149'
    
    ### First, in PREP_FLT, run "flt_info.sh"
    
    ### Make ACS associations
    unicorn.candels.make_asn_files(uniquename=True, translate={'-ROT':''})
    
    ### Need to take out "_flc" extension from the ASN files
    files=glob.glob('*asn.fits')
    for asn_file in files:
        asn = threedhst.utils.ASNFile(asn_file)
        ## print asn.exposures
        for i in range(len(asn.exposures)):
            asn.exposures[i] = asn.exposures[i].split('_flc')[0]
        #
        asn.write(asn_file)
    #
    #### Combine parallels
    for par in [1,2]:
        for filter in ['F775W','F850LP']:
            files=glob.glob(field+'*-ACSPAR%d-[AB]*-%s*' %(par, filter))
            print files
            list = []
            for file in files:
                asn = threedhst.utils.ASNFile(file)
                list.extend(asn.exposures)
                os.remove(file)
                
            asn.exposures = list
            asn.product = 'MACS0416-ACSPAR%d-%s' %(par, filter)
            asn.write('%s_asn.fits' %(asn.product))
            
    files, radec = glob.glob('MACS0416-ACSPA*asn.fits'), 'subaru.radec'
    files, radec = glob.glob('MACS0416-2403*asn.fits'), 'clash_radec.dat'
    files, radec = glob.glob('*0717*F814W*asn.fits'), 'subaru_macs0717.radec'
    files, radec = glob.glob(field+'*POS5-1?*814*asn.fits'), 'macs0717_subaru_radec.dat'
    files, radec = glob.glob(field+'*PAR1*775*asn.fits'), 'macs0416_subaru_radec.dat'
    files, radec = glob.glob(field+'*814*asn.fits'), 'macs1149_radec.dat'
    refimage='hlsp_XXX'
    
    files, radec = glob.glob('*asn.fits'), 'clash_radec.dat'
    files, radec = glob.glob('*LENS*asn.fits'), 'clash_radec.dat'
    
    from socket import gethostname as hostname
    for asn_file in files:
        #if os.path.exists(asn_file.replace('asn', 'drc_sci')):
            #continue
        #
        init.prep_direct_grism_pair(direct_asn=asn_file, grism_asn=None, radec=radec, ACS=True, align_threshold=5)
        if 'science' in hostname():
            asn = threedhst.utils.ASNFile(asn_file)
            for exp in asn.exposures:
                 os.remove('../RAW/%s_flc.fits.gz' %(exp))
                
    ##### Combine visits on the cluster to flag CRs
    info = catIO.Table("files.info")
    filters = np.unique(info['FILTER'])
    
    for filter in filters:
        #### Get all FLTs
        flt_list = []
        files=glob.glob('MACS*0416-2403-??-???-%s_asn.fits' %(filter))
        #files=glob.glob('MACS*1149*%s_asn.fits' %(filter))
        for asn_file in files:
            asn = threedhst.utils.ASNFile(asn_file)
            for exp in asn.exposures:
                flt_list.append('%s_flc.fits' %(exp))
        #
        bits = 96 #+512
        skyuser = '******'
        #### Ignore MDRIZSKY
        fp = open('ACS.skyfile','w')
        fp.writelines(['%s 0.0\n' %(file) for file in flt_list])
        fp.close()
        #
        #### Run AstroDrizzle to flag CRs on both orients
        drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS0717+3745-%s_total_comb' %(filter), clean=True, context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=True, driz_sep_wcs=True, median=True, blot=True, driz_cr=True, driz_combine=True, final_wcs=True, final_refimage=None, final_scale=0.065, final_pixfrac=0.8, final_rot=0, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM')
        #
        #drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS0416-2403-30mas-%s' %(filter), clean=True, context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, final_refimage='hlsp_clash_hst_acs-30mas_macs0416_f475w_v1_drz.fits', final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM')
        #
        #drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS1149-2223-Mosaic-%s' %(filter), clean=True, context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, final_scale=0.05, final_rot=0, final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM')
        
    #### MACS0717 mosaic
    for filter in filters:
        flt_list = []
        #files=glob.glob('MACS0416-2403-??-???-%s_asn.fits' %(filter))
        files=glob.glob(field+'*%s_asn.fits' %(filter))
        for asn_file in files:
            asn = threedhst.utils.ASNFile(asn_file)
            for exp in asn.exposures:
                if os.path.exists('%s_flc.fits' %(exp)):
                    flt_list.append('%s_flc.fits' %(exp))
        #
        bits = 96 #+512
        skyuser = '******'
        #### Ignore MDRIZSKY
        fp = open('ACS.skyfile','w')
        fp.writelines(['%s 0.0\n' %(file) for file in flt_list])
        fp.close()
        #
        #drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS0717-Mosaic-%s' %(filter), clean=True, context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, final_rot=0, final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM', final_ra=1.093574923885E+02, final_dec=3.778490152063E+01, final_scale=0.1798, final_outnx=4185, final_outny=4670)
        ### Here, specify the output image size, pixel scale, etc.
        #drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS0717-Mosaic-%s' %(filter), clean=True, 
        #        context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, 
        #        driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, 
        #        final_rot=0, final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM', 
        #        final_ra=1.093574923885E+02, final_dec=3.778490152063E+01, final_scale=0.1, final_outnx=7524, final_outny=8397)
        ### Here, force the output to match the FF mosaic
        #drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS1149-Mosaic-%s' %(filter), clean=True, 
        #        context=False, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, 
        #        driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, 
        #        final_rot=0, final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM',
        #        final_refimage=refimage)
        drizzlepac.astrodrizzle.AstroDrizzle(flt_list, output='MACS1149-Mosaic-v2-%s' %(filter), clean=True, 
                context=True, preserve=False, skysub=True, skyfile='ACS.skyfile', driz_separate=False, 
                driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_combine=True, final_wcs=True, 
                final_rot=0, final_pixfrac=0.8, final_kernel='square', resetbits=0, final_bits=bits, final_wht_type='IVM',
                final_ra=177.405988, final_dec=22.343847, final_outnx=11658, final_outny=16355)
Beispiel #7
0
def prep():

    import glob
    import os

    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    from threedhst import catIO

    import unicorn
    import research.hawkiff

    ### 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)

    ###
    for asn_file in files:
        drizzlepac.astrodrizzle.AstroDrizzle(
            asn_file,
            static=False,
            skysub=False,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            final_wht_type='IVM',
            clean=True,
            final_wcs=True,
            final_refimage='IRAS23436+5257-87-304-F814W_drc_sci.fits',
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

    ### Match WCS of NB observations
    init.copy_adriz_headerlets(glob.glob('*-F1*[MW]*asn.fits')[0],
                               glob.glob('*-F1*[N]*asn.fits')[0],
                               order=[1, 0])

    ### Flag negative pixels
    for asn_file in files:
        asn = threedhst.utils.ASNFile(asn_file)
        for exp in asn.exposures:
            im = pyfits.open('%s_flt.fits' % (exp), mode='update')
            neg = im['SCI'].data < -3 * im['ERR'].data
            print '%s #negative = %d' % (exp, neg.sum())
            im['DQ'].data[neg] |= 4096
            im.flush()
        #
        drizzlepac.astrodrizzle.AstroDrizzle(asn_file,
                                             skysub=True,
                                             static=True,
                                             driz_separate=True,
                                             driz_sep_wcs=True,
                                             median=True,
                                             blot=True,
                                             driz_cr=True,
                                             driz_combine=True,
                                             clean=True,
                                             final_wcs=True,
                                             final_scale=0.1,
                                             final_rot=0,
                                             final_pixfrac=1,
                                             context=False,
                                             resetbits=0,
                                             final_bits=64,
                                             preserve=False)
        sci = pyfits.open(asn_file.replace('_asn', '_drz_sci'), mode='update')
        wht = pyfits.open(asn_file.replace('_asn', '_drz_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### ACS / UVIS
    files = glob.glob('*-F6*asn.fits')
    files = glob.glob('*-F[48]*asn.fits')
    files.sort()
    for asn_file in files[::-1]:
        asn = threedhst.utils.ASNFile(asn_file)
        for i in range(len(asn.exposures)):
            asn.exposures[i] = asn.exposures[i].split('_flc')[0]

        print asn_file, asn.exposures
        asn.write(asn_file)

        if ('814' in asn_file) | ('625' in asn_file):
            radec = None
        else:
            root = glob.glob('*-F[86]*W*sci.fits')[0].split('_sci')[0]
            if not os.path.exists('%s_sci.cat' % (root)):
                os.system(
                    'cp /user/brammer/software/share/gauss_5.0_9x9.conv .')
                research.hawkiff.make_catalog(root=root,
                                              sci_ext='sci',
                                              threshold=3,
                                              use_rms=False,
                                              subtract_background=True)
                cat = catIO.Table('%s_sci.cat' % (root))
                cat['X_WORLD',
                    'Y_WORLD'].write('ref_opt.radec',
                                     format='ascii.commented_header')

            radec = 'ref_opt.radec'

        radec = None
        init.prep_direct_grism_pair(direct_asn=asn_file,
                                    grism_asn=None,
                                    radec=radec,
                                    ACS=True,
                                    align_threshold=5,
                                    raw_path='../RAW/')
    #
    for asn_file in files:
        radec = None
        init.prep_direct_grism_pair(direct_asn=asn_file,
                                    grism_asn=None,
                                    radec=radec,
                                    ACS=True,
                                    align_threshold=5,
                                    raw_path='../RAW/')

    files = glob.glob('*sci.fits')
    ims = {}
    for file in files:
        im = pyfits.open(file)
        filter = file.split('-')[-1][:5]
        pix = np.sqrt(im[0].header['CD1_1']**2 +
                      im[0].header['CD1_2']**2) * 3600
        scl = im[0].header['PHOTFLAM'] / 1.e-19 * (0.1 / pix)**2
        im[0].data *= scl
        ims[filter] = im

    ### Try Multidrizzle CR with combined image
    import drizzlepac

    asn_files = glob.glob('*-F[4-8]*asn.fits')
    exposures = []
    for file in asn_files:
        asn = threedhst.utils.ASNFile(file)
        exposures.extend(asn.exposures)

    target = '-'.join(file.split('-')[:-1])
    asn.exposures = exposures
    asn.product = target
    asn_file = '%s_asn.fits' % (asn.product)
    asn.write(asn_file, clobber=True)

    ### modify:  make copy of FLCs, make new mosaic and then copy FLCs back
    ##xxx

    drizzlepac.astrodrizzle.AstroDrizzle(asn_file,
                                         skysub=False,
                                         clean=True,
                                         final_wcs=True,
                                         final_scale=0.05,
                                         final_pixfrac=0.8,
                                         context=False,
                                         resetbits=4096,
                                         final_bits=576,
                                         preserve=False)

    #
    bits = 576

    drizzlepac.astrodrizzle.AstroDrizzle(asn_file,
                                         static=False,
                                         skysub=False,
                                         driz_separate=False,
                                         driz_sep_wcs=False,
                                         median=False,
                                         blot=False,
                                         driz_cr=False,
                                         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=0,
                                         final_bits=bits,
                                         preserve=False)

    #### rerun final mosaics
    refimage = '%s_drz_sci.fits' % (target)

    for file in asn_files:
        drizzlepac.astrodrizzle.AstroDrizzle(file,
                                             static=False,
                                             skysub=False,
                                             driz_separate=False,
                                             driz_sep_wcs=False,
                                             median=False,
                                             blot=False,
                                             driz_cr=False,
                                             driz_combine=True,
                                             final_wht_type='IVM',
                                             clean=True,
                                             final_wcs=True,
                                             final_refimage=refimage,
                                             final_pixfrac=1,
                                             context=False,
                                             resetbits=0,
                                             final_bits=bits,
                                             preserve=False)

    for file in asn_files:
        print file
        sci = pyfits.open(glob.glob(file.replace('_asn', '_dr?_sci'))[0],
                          mode='update')
        wht = pyfits.open(sci.filename().replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Try own driz CR
    import stwcs
    from drizzlepac import astrodrizzle, quickDeriv
    import scipy.ndimage as nd

    ref_file = 'ESO550-IG02-13-083-F435W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083-F814W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083_drc_sci.fits'
    ref_ext = 0
    ref = pyfits.open(ref_file)
    ref_wcs = stwcs.wcsutil.HSTWCS(ref, ext=ref_ext)

    asn_file = 'ESO550-IG02-13-083-F814W_asn.fits'
    asn = threedhst.utils.ASNFile(asn_file)
    for exp in asn.exposures:
        flt = pyfits.open('%s_flc.fits' % (exp))  #, mode='update')
        for ext in [1, 2]:
            flt_wcs = stwcs.wcsutil.HSTWCS(flt, ext=('sci', ext))
            blotted_ref = astrodrizzle.ablot.do_blot(ref[ref_ext].data,
                                                     ref_wcs,
                                                     flt_wcs,
                                                     1,
                                                     coeffs=True,
                                                     interp='nearest',
                                                     sinscl=1.0,
                                                     stepsize=10,
                                                     wcsmap=None)
            #
            blotDeriv = quickDeriv.qderiv(blotted_ref)

            scl = flt['sci', ext].header['PHOTFLAM'] / ref[0].header[
                'PHOTFLAM'] / flt[0].header['EXPTIME']
            noise = flt['err', ext].data

            driz_scale = 1.2
            driz_snr = 3.5

            driz_scale = 1.0
            driz_snr = 6
            driz_f = 2

            # |data_image - blotted_image| > scale x deriv + SNR x noise
            abs = np.abs(flt['sci', ext].data * scl - blotted_ref * driz_f)
            xderiv = driz_scale * blotDeriv
            xsnr = driz_snr * scl * noise

            mask = abs > (xderiv + xsnr)
            new_dq = mask & ((flt['dq', ext].data & 4096) == 0)

    ### Try "cosmics" LA Cosmic
    im = pyfits.open('ESO550-IG02-13-083-F814W_drc_sci.fits')
    slx, sly = slice(1500, 2600), slice(1800, 2800)
    ext = 0
    subim = im[ext].data[sly, slx]
    h = im[ext].header
    subim *= im[0].header['EXPTIME']

    #### FLT
    im = pyfits.open('j9cv13pcq_flc.fits')
    ext = 0
    slx, sly = slice(1500, 2600), slice(0, 400)
    subim = im[4].data[sly, slx]
    h = im[0].header

    import cosmics
    c = cosmics.cosmicsimage(subim,
                             pssl=20.0,
                             gain=1,
                             readnoise=h['READNSEA'],
                             sigclip=4.0,
                             sigfrac=0.3,
                             objlim=3.8,
                             satlevel=84700.0,
                             verbose=True)
    c.run(maxiter=4)

    crflux = subim * c.mask
    plt.hist(np.log10(crflux[c.mask]), range=[1, 6], bins=100, alpha=0.5)

    ####
    import research.pab.pab
    files = glob.glob('*-F*asn.fits')
    #files=glob.glob('IC*-F814*asn.fits')
    for file in files:
        asn = threedhst.utils.ASNFile(file)
        for exp in asn.exposures:
            flc = pyfits.open('%s_flc.fits' % (exp))
            research.pab.pab.run_lacosmic('%s_flc.fits' % (exp),
                                          split=2048,
                                          sigclip=5,
                                          pssl=flc[1].header['MDRIZSK0'])

        #
        drizzlepac.updatehdr.update_from_shiftfile(
            file.replace('asn.fits', 'shifts.txt'))
        drizzlepac.astrodrizzle.AstroDrizzle(file,
                                             skysub=True,
                                             clean=True,
                                             final_wcs=True,
                                             final_scale=0.05,
                                             final_pixfrac=0.8,
                                             context=False,
                                             resetbits=0,
                                             final_bits=576,
                                             preserve=False)

        drizzlepac.astrodrizzle.AstroDrizzle(file,
                                             output='sub_' +
                                             file.split('_asn')[0],
                                             skysub=True,
                                             static=True,
                                             driz_separate=False,
                                             driz_sep_wcs=False,
                                             median=False,
                                             blot=False,
                                             driz_cr=False,
                                             driz_combine=True,
                                             clean=True,
                                             final_wcs=True,
                                             final_scale=0.08,
                                             final_ra=60.635233,
                                             final_dec=60.344597,
                                             final_outnx=800,
                                             final_outny=800,
                                             final_rot=0,
                                             final_pixfrac=0.8,
                                             context=False,
                                             resetbits=0,
                                             final_bits=576,
                                             preserve=False)

    ### Final mosaics
    f110w = glob.glob('*110W*asn.fits')[0]
    drizzlepac.astrodrizzle.AstroDrizzle(f110w,
                                         output='sub_' +
                                         f110w.split('_asn')[0],
                                         skysub=True,
                                         static=True,
                                         driz_separate=False,
                                         driz_sep_wcs=False,
                                         median=False,
                                         blot=False,
                                         driz_cr=False,
                                         driz_combine=True,
                                         clean=True,
                                         final_wcs=True,
                                         final_scale=0.1,
                                         final_rot=0,
                                         final_pixfrac=1,
                                         context=False,
                                         resetbits=0,
                                         final_bits=576 - 512,
                                         preserve=False)

    files = glob.glob('*-F*asn.fits')
    for file in files:
        if 'F110W' in file:
            continue
        #
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            output='sub_' + file.split('_asn')[0],
            skysub=True,
            static=True,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_refimage='sub_' + f110w.replace('_asn', '_drz_sci'),
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576 - 512 * ('-F1' in file),
            preserve=False)

    for sci_im in glob.glob('sub*sci.fits'):
        print sci_im
        sci = pyfits.open(sci_im, mode='update')
        wht = pyfits.open(sci_im.replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Test alignment
    import align
    ra_list, de_list = np.loadtxt('f814w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F673N_drc_sci.fits',
            wht='sub_ESO550-IG025-41-115-F673N_drc_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)

    #drizzlepac.tweakback.tweakback('ESO550-IG025-41-115-F673N_drc_sci.fits', force=True, verbose=True) #, origwcs='DRZWCS')

    ra_list, de_list = np.loadtxt('f110w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F132N_drz_sci.fits',
            wht='sub_ESO550-IG025-41-115-F132N_drz_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)
Beispiel #8
0
def run_wfc3(run_prep=True,
             run_interlace=True,
             run_redshifts=True,
             align_threshold=6):
    """
    Run end-to-end WFC3 test
    """
    import os
    import glob
    import time

    import astropy.io.fits as pyfits
    from astropy.table import Table as table

    import threedhst
    import threedhst.prep_flt_astrodrizzle as init

    import unicorn
    import unicorn.interlace_test as test

    #### Preparation steps
    #run_prep = True

    root = 'goodss-34'

    if run_prep:

        #### Make ASN files
        unicorn.candels.make_asn_files(uniquename=False)

        #### Run main preparation wrapper to align to reference and
        #### subtract backgrounds
        init.prep_direct_grism_pair(direct_asn=root + '-F140W_asn.fits',
                                    grism_asn=root + '-G141_asn.fits',
                                    radec='../Catalog/goodss_radec.dat',
                                    raw_path='../RAW/',
                                    mask_grow=8,
                                    scattered_light=False,
                                    final_scale=None,
                                    skip_direct=False,
                                    ACS=False,
                                    align_threshold=align_threshold)

        #### test
        # if True:
        #     import threedhst.prep_flt_astrodrizzle as init
        #     init.subtract_flt_background(root=root+'-F140W', scattered_light=False)

    #### Interlaced images + reference
    #run_interlace = True
    if run_interlace:

        #### Interlace images themselves
        NGROWX = 180
        NGROWY = 30
        grow, auto_off = 2, False
        #grow, auto_off = 1, True

        unicorn.reduce.interlace_combine(root=root + '-F140W',
                                         view=False,
                                         use_error=True,
                                         make_undistorted=False,
                                         pad=60,
                                         NGROWX=NGROWX,
                                         NGROWY=NGROWY,
                                         ddx=0,
                                         ddy=0,
                                         growx=grow,
                                         growy=grow,
                                         auto_offsets=auto_off,
                                         ref_exp=0)
        unicorn.reduce.interlace_combine(root=root + '-G141',
                                         view=False,
                                         use_error=True,
                                         make_undistorted=False,
                                         pad=60,
                                         NGROWX=NGROWX,
                                         NGROWY=NGROWY,
                                         ddx=0,
                                         ddy=0,
                                         growx=grow,
                                         growy=grow,
                                         auto_offsets=auto_off,
                                         ref_exp=0)

        #### Interlaced reference
        adriz_blot = unicorn.reduce.adriz_blot_from_reference
        adriz_blot(pointing=root + '-F140W',
                   pad=60,
                   NGROWX=NGROWX,
                   NGROWY=NGROWY,
                   growx=grow,
                   growy=grow,
                   auto_offsets=auto_off,
                   ref_exp=0,
                   ref_image='../Catalog/goodss_3dhst.v4.0.IR_cutout_sci.fits',
                   ref_ext=0,
                   ref_filter='F140W',
                   seg_image='../Catalog/goodss_3dhst.v4.0.IR_cutout_seg.fits',
                   cat_file='../Catalog/goodss_3dhst.v4.0.IR_cutout.cat')

        #### Make a model to 25 AB
        model = unicorn.reduce.process_GrismModel(
            root=root,
            grow_factor=grow,
            growx=grow,
            growy=grow,
            MAG_LIMIT=25,
            REFINE_MAG_LIMIT=21,
            make_zeroth_model=False,
            use_segm=False,
            model_slope=0,
            model_list=None,
            direct='F140W',
            grism='G141',
            BEAMS=['A', 'B', 'C', 'D', 'E'],
            align_reference=True)

        #### Remove residual backgroudn with model mask
        model.refine_mask_background(threshold=0.002,
                                     grow_mask=14,
                                     update=True,
                                     resid_threshold=4,
                                     clip_left=640,
                                     save_figure=True,
                                     interlace=True)

    #run_redshifts = True

    if run_redshifts:

        model = unicorn.reduce.process_GrismModel(
            root=root,
            grow_factor=2,
            growx=2,
            growy=2,
            MAG_LIMIT=25,
            REFINE_MAG_LIMIT=21,
            make_zeroth_model=False,
            use_segm=False,
            model_slope=0,
            model_list=None,
            direct='F140W',
            grism='G141',
            BEAMS=['A', 'B', 'C', 'D', 'E'])

        #### Some objects with z_spec, em lines; v4.0 IDs
        ids = [
            25778, 25909, 26087, 26550, 28052, 28870, 29122, 30309, 30520,
            30662
        ]
        for id in ids:
            if os.path.exists('%s_%05d.2D.fits' % (model.baseroot, id)):
                continue
            #
            model.twod_spectrum(id=id,
                                grow=1,
                                miny=-36,
                                maxy=None,
                                CONTAMINATING_MAGLIMIT=23,
                                refine=False,
                                verbose=False,
                                force_refine_nearby=False,
                                USE_REFERENCE_THUMB=True,
                                USE_FLUX_RADIUS_SCALE=3,
                                BIG_THUMB=False,
                                extract_1d=True)
            #
            ### Original fit
            gris = unicorn.interlace_fit.GrismSpectrumFit(
                root='%s_%05d' % (model.baseroot, id),
                FIGURE_FORMAT='png',
                verbose=True,
                lowz_thresh=0.4,
                fix_direct_thumbnail=True,
                RELEASE=False,
                OUTPUT_PATH='./',
                BASE_PATH='./',
                skip_photometric=False,
                p_flat=0.0001,
                use_mag_prior=True,
                dr_match=1.0)
            gris.fit_in_steps(dzfirst=0.003,
                              zrfirst=(0.3, 3.3),
                              dzsecond=0.0002,
                              save=True,
                              make_plot=True,
                              skip_second=False,
                              oned=False)
            ### New fit
            gris = test.SimultaneousFit(root='%s_%05d' % (model.baseroot, id),
                                        FIGURE_FORMAT='png',
                                        lowz_thresh=0.4)
            gris.new_fit_constrained(zrfirst=[0.6, 3.3],
                                     dzfirst=0.005,
                                     dzsecond=0.0005,
                                     make_plot=True,
                                     ignore_photometry=False,
                                     ignore_spectrum=False,
                                     refit_norm=False,
                                     get_tilt=True,
                                     faint_limit=24)
Beispiel #9
0
def run_acs(run_prep=True, run_interlace=True, run_redshifts=True):
    import glob

    import threedhst
    import threedhst.prep_flt_astrodrizzle as init

    import unicorn
    import unicorn.interlace_acs

    ### Make ACS associations
    files = glob.glob('../RAW/j*asn.fits')
    for file in files:
        pointing = threedhst.prep_flt_files.make_targname_asn(
            file, newfile=True, field='goodss-34', ext='flc')

    if run_prep:
        ### Alignment and background subtraction
        init.prep_direct_grism_pair(direct_asn='goodss-34-03-F814W_asn.fits',
                                    grism_asn='goodss-34-03-G800L_asn.fits',
                                    radec='../Catalog/goodss_radec.dat',
                                    ACS=True)

    if run_interlace:
        #### Interlace images
        interlace_acs = unicorn.interlace_acs.interlace_combine_acs
        for chip in [1, 2]:
            interlace_acs(root='goodss-34-03-F814W',
                          view=False,
                          chip=chip,
                          filter='F814W',
                          outroot='goodss-34-03',
                          center=True,
                          pad=100,
                          growx=1,
                          growy=1)
            interlace_acs(root='goodss-34-03-G800L',
                          view=False,
                          chip=chip,
                          filter='G800L',
                          outroot='goodss-34-03',
                          center=True,
                          pad=100,
                          growx=1,
                          growy=1)

        #### Interlaced reference, self as reference image
        adriz_blot = unicorn.reduce.adriz_blot_from_reference
        adriz_blot(pointing='goodss-34-03-F814W',
                   ACS=True,
                   pad=100,
                   NGROWX=0,
                   NGROWY=0,
                   growx=1,
                   growy=1,
                   auto_offsets=False,
                   ref_exp=0,
                   ref_image='goodss-34-03-F814W_drc_sci.fits',
                   ref_ext=0,
                   ref_filter='F814W',
                   seg_image='Catalog/goodss_3dhst.v4.0.IR_cutout_seg.fits',
                   cat_file='Catalog/goodss_3dhst.v4.0.IR_cutout.cat')

        ### test blot from UDF reference [works]
        #adriz_blot(pointing='goodss-34-03-F814W', ACS=True, pad=100, NGROW=0, growx=1, growy=1, auto_offsets=False, ref_exp=0, ref_image='hlsp_xdf_hst_acswfc-60mas_hudf_f814w_v1_sci.fits', ref_ext=0, ref_filter='F140W', seg_image='Catalog/goodss_3dhst.v4.0.IR_cutout_seg.fits', cat_file='Catalog/goodss_3dhst.v4.0.IR_cutout.cat')

    if run_redshifts:
        import unicorn.interlace_test
        ids = {1: [27549, 30367, 30861], 2: []}
        for chip in ids.keys():
            model = unicorn.reduce.process_GrismModel(
                root='goodss-34-03-chip%d' % (chip),
                grow_factor=1,
                growx=1,
                growy=1,
                MAG_LIMIT=21,
                REFINE_MAG_LIMIT=21,
                make_zeroth_model=False,
                use_segm=False,
                model_slope=0,
                model_list=None,
                direct='F814W',
                grism='G800L',
                BEAMS=['A', 'B', 'C', 'D', 'E', 'F', 'G'])
            #
            # model = unicorn.reduce.GrismModel('goodss-34-03-chip%d' %(chip), grow_factor=1, growx=1, growy=1, MAG_LIMIT=28, use_segm=False, grism='G800L', direct='F814W')
            # MLIM = [21,21]
            # model.compute_full_model(refine=False, MAG_LIMIT=MLIM[0], save_pickle=False, model_slope=0, BEAMS=['A','B','C','D','E','F','G'])
            # ### For the brighter galaxies, refine the model with the observed spectrum
            # model.compute_full_model(refine=True, MAG_LIMIT=MLIM[1], save_pickle=True, model_slope=0, BEAMS=['A','B','C','D','E','F','G'])
            #
            for id in ids[chip]:
                model.twod_spectrum(id=id,
                                    grow=1,
                                    miny=-40,
                                    maxy=None,
                                    CONTAMINATING_MAGLIMIT=23,
                                    refine=False,
                                    verbose=False,
                                    force_refine_nearby=False,
                                    USE_REFERENCE_THUMB=True,
                                    USE_FLUX_RADIUS_SCALE=3,
                                    BIG_THUMB=False,
                                    extract_1d=True)
                model.show_2d(savePNG=True, verbose=False)
                #
                gris = unicorn.interlace_fit.GrismSpectrumFit(
                    root='%s_%05d' % (model.baseroot, id),
                    FIGURE_FORMAT='png',
                    verbose=True,
                    lowz_thresh=0.05,
                    fix_direct_thumbnail=True,
                    RELEASE=False,
                    OUTPUT_PATH='./',
                    BASE_PATH='./',
                    skip_photometric=False,
                    p_flat=0.0001,
                    use_mag_prior=True,
                    dr_match=1.0)
                #
                gris = unicorn.interlace_test.SimultaneousFit(
                    root='%s_%05d' % (model.baseroot, id),
                    FIGURE_FORMAT='png',
                    verbose=True,
                    lowz_thresh=0.05,
                    fix_direct_thumbnail=True,
                    RELEASE=False,
                    OUTPUT_PATH='./',
                    BASE_PATH='./',
                    skip_photometric=False,
                    p_flat=0.0001,
                    use_mag_prior=True,
                    dr_match=1.0)
                gris.new_fit_constrained(zrfirst=[0.05, 0.8],
                                         dzfirst=0.005,
                                         dzsecond=0.0005,
                                         make_plot=True,
                                         ignore_photometry=False,
                                         ignore_spectrum=False,
                                         refit_norm=False,
                                         get_tilt=True,
                                         faint_limit=24)
Beispiel #10
0
def figs_gn1(make_asn = True, check_background = True, run_prep = True, run_interlace=True, make_model=True):

    import mywfc3.bg
    import threedhst.prep_flt_astrodrizzle as init
    from unicorn.reduce import adriz_blot_from_reference as adriz_blot
    import unicorn.interlace_test as test

    if make_asn:

		unicorn.candels.make_asn_files()

    if check_background:

		os.chdir('/Users/imomcheva/Work/FIGS/RAW')
		asn_files = glob.glob('*asn.fits')
		for asn in asn_files:
			root = asn.split('_asn')[0]
			png = glob.glob(root+'*orbit.*')
			if (len(png) > 0):
				continue
			try:
				mywfc3.bg.show_orbit_limbangle(asn = [root])
			except:
				fp = open(root+'_orbit.failed','w')
				fp.write('---')
				fp.close()
	    
		os.chdir('/Users/imomcheva/Work/FIGS/PREPARE/')

    if run_prep:

		os.chdir('/Users/imomcheva/Work/FIGS/PREPARE/')

		grism_files = glob.glob('FIGS-GN1*G102_asn.fits')
		direct_files = glob.glob('FIGS-GN1*F105W_asn.fits')

		for direct, grism in zip(direct_files, grism_files):
			init.prep_direct_grism_pair(direct_asn=direct, grism_asn=grism, radec='../REF/goodsn_3dhst.v4.1.radec.cat',
				raw_path = '../RAW/', mask_grow=8, scattered_light=False, final_scale = None,
				skip_direct=False, ACS=False, align_threshold=6)

    if run_interlace:

		NGROWX = 200
		NGROWY = 30
		pad = 60

		CATALOG = '/3DHST/Photometry/Release/v4.0/GOODS-N/Detection/goodsn_3dhst.v4.0.F125W_conv.cat'
		REF_IMAGE = '../REF/goodsn_3dhst.v4.0.F125W_orig_sci.fits'
		REF_EXT = 0
		SEG_IMAGE = '/3DHST/Photometry/Release/v4.0/GOODS-N/Detection/goodsn_3dhst.v4.0.F160W_seg.fits'
		REF_FILTER = 'F125W'

		grism = glob.glob('FIGS-GN1*G102_asn.fits')
        
		special_point = ['FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102',\
            'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102',\
            'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102', 'FIGS-GN1-3E-217-G102',\
            'FIGS-GN1-3E-217-G102']
		
		"""
        for i in range(len(grism)):
		    pointing=grism[i].split('_asn')[0]
            
            if pointing in special_point: 
                ref_exp = 3
            else: 
                ref_exp = 0
    
            adriz_blot(pointing=pointing, pad=pad, NGROWX=NGROWX, NGROWY=NGROWY, growx=1, 
            	growy=1, auto_offsets=True, ref_exp=ref_exp, ref_image=REF_IMAGE, ref_ext=REF_EXT, 
            	ref_filter=REF_FILTER, seg_image=SEG_IMAGE, cat_file=CATALOG, grism='G102')                                                                                  
            unicorn.reduce.interlace_combine(pointing.replace('G102','F105W'), view=False, use_error=True, 
            	make_undistorted=False, pad=pad, NGROWX=NGROWX, NGROWY=NGROWY, ddx=0, ddy=0, 
            	growx=1, growy=1, auto_offsets=True, ref_exp=0)
            unicorn.reduce.interlace_combine(pointing, view=False, use_error=True, 
            	make_undistorted=False, pad=pad, NGROWX=NGROWX, NGROWY=NGROWY, ddx=0, ddy=0, 
            	growx=1, growy=1, auto_offsets=True, ref_exp=0)
		"""
        
    if make_model:

		grism = glob.glob('FIGS-GN1*G102_asn.fits')

		for i in range(len(grism)):
			root = grism[i].split('-G102')[0]
			m0 = unicorn.reduce.GrismModel(root=root,grow_factor=1, growx=1, growy=1, 
				direct='F105W', grism='G102', MAG_LIMIT=27)
			model_list = m0.get_eazy_templates(dr_min=0.5, MAG_LIMIT=25)
			model = unicorn.reduce.process_GrismModel(root=root, grow_factor=1, growx=1, growy=1, 
				MAG_LIMIT=27, REFINE_MAG_LIMIT=21, make_zeroth_model=False, use_segm=False, 
				model_slope=0, direct='F105W', grism='G102', BEAMS=['A', 'B', 'C', 'D', 'E'], 
				align_reference=True)
			if not os.path.exists(os.path.basename(model.root) + '-G141_maskbg.dat'):
				model.refine_mask_background(threshold=0.002, grow_mask=14, update=True, resid_threshold=4, 
					clip_left=640, save_figure=True, interlace=True)

    if extract:
    
        id = 32031
        
        grism = glob.glob('FIGS-GN1*G102_asn.fits')
        
        for i in range(len(grism)):            
            root = grism[i].split('-G102')[0]
            model = unicorn.reduce.process_GrismModel(root=root, grow_factor=1, growx=1, growy=1, 
                MAG_LIMIT=27, REFINE_MAG_LIMIT=21, make_zeroth_model=False, use_segm=False, 
                model_slope=0, direct='F105W', grism='G102', BEAMS=['A', 'B', 'C', 'D', 'E'])
            model.twod_spectrum(id=id, grow=1, miny=-36, maxy=None, CONTAMINATING_MAGLIMIT=27, 
                refine=True, verbose=False, force_refine_nearby=True, USE_REFERENCE_THUMB=True, 
                USE_FLUX_RADIUS_SCALE=3, BIG_THUMB=False, extract_1d=True)
            model.show_2d(savePNG=True)
Beispiel #11
0
def prep():

    import glob
    import os

    import threedhst
    import threedhst.prep_flt_astrodrizzle as init
    from threedhst import catIO

    import unicorn
    import research.hawkiff

    ### 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)

    ###
    for asn_file in files:
        drizzlepac.astrodrizzle.AstroDrizzle(
            asn_file,
            static=False,
            skysub=False,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            final_wht_type='IVM',
            clean=True,
            final_wcs=True,
            final_refimage='IRAS23436+5257-87-304-F814W_drc_sci.fits',
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

    ### Match WCS of NB observations
    init.copy_adriz_headerlets(
        glob.glob('*-F1*[MW]*asn.fits')[0],
        glob.glob('*-F1*[N]*asn.fits')[0],
        order=[1, 0])

    ### Flag negative pixels
    for asn_file in files:
        asn = threedhst.utils.ASNFile(asn_file)
        for exp in asn.exposures:
            im = pyfits.open('%s_flt.fits' % (exp), mode='update')
            neg = im['SCI'].data < -3 * im['ERR'].data
            print '%s #negative = %d' % (exp, neg.sum())
            im['DQ'].data[neg] |= 4096
            im.flush()
        #
        drizzlepac.astrodrizzle.AstroDrizzle(
            asn_file,
            skysub=True,
            static=True,
            driz_separate=True,
            driz_sep_wcs=True,
            median=True,
            blot=True,
            driz_cr=True,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_scale=0.1,
            final_rot=0,
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=64,
            preserve=False)
        sci = pyfits.open(asn_file.replace('_asn', '_drz_sci'), mode='update')
        wht = pyfits.open(asn_file.replace('_asn', '_drz_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### ACS / UVIS
    files = glob.glob('*-F6*asn.fits')
    files = glob.glob('*-F[48]*asn.fits')
    files.sort()
    for asn_file in files[::-1]:
        asn = threedhst.utils.ASNFile(asn_file)
        for i in range(len(asn.exposures)):
            asn.exposures[i] = asn.exposures[i].split('_flc')[0]

        print asn_file, asn.exposures
        asn.write(asn_file)

        if ('814' in asn_file) | ('625' in asn_file):
            radec = None
        else:
            root = glob.glob('*-F[86]*W*sci.fits')[0].split('_sci')[0]
            if not os.path.exists('%s_sci.cat' % (root)):
                os.system(
                    'cp /user/brammer/software/share/gauss_5.0_9x9.conv .')
                research.hawkiff.make_catalog(
                    root=root,
                    sci_ext='sci',
                    threshold=3,
                    use_rms=False,
                    subtract_background=True)
                cat = catIO.Table('%s_sci.cat' % (root))
                cat['X_WORLD', 'Y_WORLD'].write(
                    'ref_opt.radec', format='ascii.commented_header')

            radec = 'ref_opt.radec'

        radec = None
        init.prep_direct_grism_pair(
            direct_asn=asn_file,
            grism_asn=None,
            radec=radec,
            ACS=True,
            align_threshold=5,
            raw_path='../RAW/')
    #
    for asn_file in files:
        radec = None
        init.prep_direct_grism_pair(
            direct_asn=asn_file,
            grism_asn=None,
            radec=radec,
            ACS=True,
            align_threshold=5,
            raw_path='../RAW/')

    files = glob.glob('*sci.fits')
    ims = {}
    for file in files:
        im = pyfits.open(file)
        filter = file.split('-')[-1][:5]
        pix = np.sqrt(im[0].header['CD1_1']**2 +
                      im[0].header['CD1_2']**2) * 3600
        scl = im[0].header['PHOTFLAM'] / 1.e-19 * (0.1 / pix)**2
        im[0].data *= scl
        ims[filter] = im

    ### Try Multidrizzle CR with combined image
    import drizzlepac

    asn_files = glob.glob('*-F[4-8]*asn.fits')
    exposures = []
    for file in asn_files:
        asn = threedhst.utils.ASNFile(file)
        exposures.extend(asn.exposures)

    target = '-'.join(file.split('-')[:-1])
    asn.exposures = exposures
    asn.product = target
    asn_file = '%s_asn.fits' % (asn.product)
    asn.write(asn_file, clobber=True)

    ### modify:  make copy of FLCs, make new mosaic and then copy FLCs back
    ##xxx

    drizzlepac.astrodrizzle.AstroDrizzle(
        asn_file,
        skysub=False,
        clean=True,
        final_wcs=True,
        final_scale=0.05,
        final_pixfrac=0.8,
        context=False,
        resetbits=4096,
        final_bits=576,
        preserve=False)

    #
    bits = 576

    drizzlepac.astrodrizzle.AstroDrizzle(
        asn_file,
        static=False,
        skysub=False,
        driz_separate=False,
        driz_sep_wcs=False,
        median=False,
        blot=False,
        driz_cr=False,
        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=0,
        final_bits=bits,
        preserve=False)

    #### rerun final mosaics
    refimage = '%s_drz_sci.fits' % (target)

    for file in asn_files:
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            static=False,
            skysub=False,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            final_wht_type='IVM',
            clean=True,
            final_wcs=True,
            final_refimage=refimage,
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=bits,
            preserve=False)

    for file in asn_files:
        print file
        sci = pyfits.open(
            glob.glob(file.replace('_asn', '_dr?_sci'))[0], mode='update')
        wht = pyfits.open(sci.filename().replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Try own driz CR
    import stwcs
    from drizzlepac import astrodrizzle, quickDeriv
    import scipy.ndimage as nd

    ref_file = 'ESO550-IG02-13-083-F435W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083-F814W_drc_sci.fits'
    ref_file = 'ESO550-IG02-13-083_drc_sci.fits'
    ref_ext = 0
    ref = pyfits.open(ref_file)
    ref_wcs = stwcs.wcsutil.HSTWCS(ref, ext=ref_ext)

    asn_file = 'ESO550-IG02-13-083-F814W_asn.fits'
    asn = threedhst.utils.ASNFile(asn_file)
    for exp in asn.exposures:
        flt = pyfits.open('%s_flc.fits' % (exp))  #, mode='update')
        for ext in [1, 2]:
            flt_wcs = stwcs.wcsutil.HSTWCS(flt, ext=('sci', ext))
            blotted_ref = astrodrizzle.ablot.do_blot(
                ref[ref_ext].data,
                ref_wcs,
                flt_wcs,
                1,
                coeffs=True,
                interp='nearest',
                sinscl=1.0,
                stepsize=10,
                wcsmap=None)
            #
            blotDeriv = quickDeriv.qderiv(blotted_ref)

            scl = flt['sci', ext].header['PHOTFLAM'] / ref[0].header[
                'PHOTFLAM'] / flt[0].header['EXPTIME']
            noise = flt['err', ext].data

            driz_scale = 1.2
            driz_snr = 3.5

            driz_scale = 1.0
            driz_snr = 6
            driz_f = 2

            # |data_image - blotted_image| > scale x deriv + SNR x noise
            abs = np.abs(flt['sci', ext].data * scl - blotted_ref * driz_f)
            xderiv = driz_scale * blotDeriv
            xsnr = driz_snr * scl * noise

            mask = abs > (xderiv + xsnr)
            new_dq = mask & ((flt['dq', ext].data & 4096) == 0)

    ### Try "cosmics" LA Cosmic
    im = pyfits.open('ESO550-IG02-13-083-F814W_drc_sci.fits')
    slx, sly = slice(1500, 2600), slice(1800, 2800)
    ext = 0
    subim = im[ext].data[sly, slx]
    h = im[ext].header
    subim *= im[0].header['EXPTIME']

    #### FLT
    im = pyfits.open('j9cv13pcq_flc.fits')
    ext = 0
    slx, sly = slice(1500, 2600), slice(0, 400)
    subim = im[4].data[sly, slx]
    h = im[0].header

    import cosmics
    c = cosmics.cosmicsimage(
        subim,
        pssl=20.0,
        gain=1,
        readnoise=h['READNSEA'],
        sigclip=4.0,
        sigfrac=0.3,
        objlim=3.8,
        satlevel=84700.0,
        verbose=True)
    c.run(maxiter=4)

    crflux = subim * c.mask
    plt.hist(np.log10(crflux[c.mask]), range=[1, 6], bins=100, alpha=0.5)

    ####
    import research.pab.pab
    files = glob.glob('*-F*asn.fits')
    #files=glob.glob('IC*-F814*asn.fits')
    for file in files:
        asn = threedhst.utils.ASNFile(file)
        for exp in asn.exposures:
            flc = pyfits.open('%s_flc.fits' % (exp))
            research.pab.pab.run_lacosmic(
                '%s_flc.fits' % (exp),
                split=2048,
                sigclip=5,
                pssl=flc[1].header['MDRIZSK0'])

        #
        drizzlepac.updatehdr.update_from_shiftfile(
            file.replace('asn.fits', 'shifts.txt'))
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            skysub=True,
            clean=True,
            final_wcs=True,
            final_scale=0.05,
            final_pixfrac=0.8,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            output='sub_' + file.split('_asn')[0],
            skysub=True,
            static=True,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_scale=0.08,
            final_ra=60.635233,
            final_dec=60.344597,
            final_outnx=800,
            final_outny=800,
            final_rot=0,
            final_pixfrac=0.8,
            context=False,
            resetbits=0,
            final_bits=576,
            preserve=False)

    ### Final mosaics
    f110w = glob.glob('*110W*asn.fits')[0]
    drizzlepac.astrodrizzle.AstroDrizzle(
        f110w,
        output='sub_' + f110w.split('_asn')[0],
        skysub=True,
        static=True,
        driz_separate=False,
        driz_sep_wcs=False,
        median=False,
        blot=False,
        driz_cr=False,
        driz_combine=True,
        clean=True,
        final_wcs=True,
        final_scale=0.1,
        final_rot=0,
        final_pixfrac=1,
        context=False,
        resetbits=0,
        final_bits=576 - 512,
        preserve=False)

    files = glob.glob('*-F*asn.fits')
    for file in files:
        if 'F110W' in file:
            continue
        #
        drizzlepac.astrodrizzle.AstroDrizzle(
            file,
            output='sub_' + file.split('_asn')[0],
            skysub=True,
            static=True,
            driz_separate=False,
            driz_sep_wcs=False,
            median=False,
            blot=False,
            driz_cr=False,
            driz_combine=True,
            clean=True,
            final_wcs=True,
            final_refimage='sub_' + f110w.replace('_asn', '_drz_sci'),
            final_pixfrac=1,
            context=False,
            resetbits=0,
            final_bits=576 - 512 * ('-F1' in file),
            preserve=False)

    for sci_im in glob.glob('sub*sci.fits'):
        print sci_im
        sci = pyfits.open(sci_im, mode='update')
        wht = pyfits.open(sci_im.replace('_sci', '_wht'))
        sci[0].data[wht[0].data == 0] = 0
        sci.flush()

    #### Test alignment
    import align
    ra_list, de_list = np.loadtxt('f814w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F673N_drc_sci.fits',
            wht='sub_ESO550-IG025-41-115-F673N_drc_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)

    #drizzlepac.tweakback.tweakback('ESO550-IG025-41-115-F673N_drc_sci.fits', force=True, verbose=True) #, origwcs='DRZWCS')

    ra_list, de_list = np.loadtxt('f110w.radec', unpack=True)
    for geom in ['shift', 'shift', 'rxyscale', 'shift', 'shift']:
        align.get_align_to_subaru(
            sci='sub_ESO550-IG025-41-115-F132N_drz_sci.fits',
            wht='sub_ESO550-IG025-41-115-F132N_drz_wht.fits',
            verbose=False,
            fitgeometry=geom,
            align_data=(ra_list, de_list, ra_list * 0),
            THRESH=1.2)