def do_astrodrizzle(filters, imdrizzlepath): """ Runs astrodrizzle in order to stack all images. Writes output as <filter>_drz.fits Note that we haven't dithered; the stack is just a higher S/N image of our FoV INPUTS: -------- filters : dictionary {filter : filelist} imdrizzle path : string path where full frame images have ben stored for stacking """ teal.unlearn( 'astrodrizzle' ) #teal mimics IRAF functionality, unlearn sets all parameters to default for filter in filters: if filter[0].lower() == 'f': #Check that we only have images if not os.path.isfile( os.path.join(imdrizzlepath, filter + '_drz.fits')): old_dir = os.getcwd() os.chdir( imdrizzlepath) #do the stacking in the imdrizzle directory astrodrizzle.AstroDrizzle('@%s.lis' % (filter), output=filter, build='yes', skysub=False) os.chdir(old_dir) #switch back to original working directory
def mytweakreg_quad_main(path, refim): os.chdir(path) os.system('pwd') updatewcs.updatewcs('*flc.fits') teal.unlearn('tweakreg') findpars = { 'computesig': True, 'threshold': 100., 'conv_width': 3.5, 'peakmin': 100., 'peakmax': 90000., 'nsigma': 1.5, 'ratio': 1.0, 'theta': 0.0, 'dqbits': None, 'use_sharp_round': False, 'ylimit': 0.1 } tweakreg.TweakReg(glob.glob('*flc.fits'), expand_refcat=False, enforce_user_order=False, updatehdr=False, shiftfile=True, writecat=True, clean=False, interactive=True, verbose=False, headerlet=False, minobj=15, searchrad=250.0, searchunits='pixels', use2dhist=True, see2dplot=True, separation=0.5, fitgeometry='rscale', residplot='both', labelsize=8, nclip=5, sigma=3.0, refimage=refim, imagefindcfg=findpars, refimagefindcfg=findpars)
def do_drizzle(asns, outname, working_dir='sameasoutname', cte=True, **options): """ Note that this requires Ureka to be activated A good baseline of options for 2-image ACS/WFC is: ``final_pixfrac=0.6,final_scale=.03, final_wcs=True`` `working_dir` can have the special value "sameasoutname", or anything ending with an "_" will have `outname` added at the end """ from stsci.tools import teal from drizzlepac import astrodrizzle if working_dir == 'sameasoutname': working_dir = outname if working_dir.endswith('_'): working_dir = working_dir + outname #toprocess_fns are *relative* to the working_dir, not absolute paths toprocess_fns = copy_files(asns, working_dir, cte=cte, incldrz=False, allowexistingdata=True) #reset to defaults teal.unlearn('astrodrizzle') options.setdefault('preserve', False) options.setdefault('restore', False) if len(set([asn.filter for asn in asns])) > 1: if 'final_wht_type' not in options: print("You are using multiple filters at the same time but didn't " "set a wheight type. You probably don't want EXP, so we'll " "try IVM") options['final_wht_type'] = 'IVM' olddir = os.path.abspath(os.curdir) try: os.chdir(working_dir) astrodrizzle.AstroDrizzle(toprocess_fns, output=outname, **options) finally: os.chdir(olddir) return working_dir
input_images = sorted(glob.glob('i*flc.fits')) threshold = 100. elif detector == 'ir': input_images = sorted(glob.glob('i*flt.fits')) threshold = 20. cw = 2.5 # Parallelized option p = Pool(8) derp = p.map(updatewcs.updatewcs, input_images) p.close() p.join() cat = 'gaia.cat' wcsname ='GAIA' teal.unlearn('tweakreg') teal.unlearn('imagefindpars') tweakreg.TweakReg(input_images, # Pass input images updatehdr=True, # update header with new WCS solution imagefindcfg={'threshold':threshold,'conv_width':cw},# Detection parameters, threshold varies for different data separation=0., # Allow for very small shifts refcat=cat, # Use user supplied catalog (Gaia) clean=True, # Get rid of intermediate files interactive=False, see2dplot=True, shiftfile=True, # Save out shift file (so we can look at shifts later) wcsname=wcsname, # Give our WCS a new name reusename=True, fitgeometry='general') # Use the 6 parameter fit os.rename('shifts.txt','shifts_{}.txt'.format(detector))
'''code written by Matt Wilde to efficiently drizzle already tweaked images suitable for photometry and blinking. Must run in the /Users/mattwilde/47Tuc/BLINK/ ... with appropriate detectrfilter ''' from glob import glob #from astropy.io import fits from drizzlepac import * from stsci.tools import teal teal.unlearn('updatenpol') teal.unlearn('astrodrizzle') ''' need to implement this to be extensible to other directories: updatenpol.update(list_of_images,refdir='/path/to/reference/files/',\ local=True) ''' ###seams to only work in python, not pyraf... def drizzler(filter,detector): '''function to auto generate drizzle functions based on a given filter and detector. Takes in a filter and detector as arguments in quotations seperated by a comma. Ex: drizzler('F435W','WFPC2') ''' epochs1 = glob('9*') epochs2 = glob('0*')
USE: python run_tweakback.py -drz drz.fits --> apply WCS solution from drz.fits to constituent flts (must be located in cwd). ''' __author__='D.M. HAMMER' __version__= 0.1 import os, glob, argparse, pyfits, pdb from pyraf import iraf import numpy as np from drizzlepac import tweakback from stsci.tools import teal if __name__=='__main__': # -- Parse input parameters parser = argparse.ArgumentParser(description='Run tweakback to apply WCS from drz to fl? images.') parser.add_argument('-drz', '--drzim',default='NONE', type=str, help='Input drizzled image (no wildcards). \ Default is NONE - requires input.') options = parser.parse_args() # -- initialize names of drizzled/flt(c) images if options.drzim == 'NONE': raise Exception('Must input a drizzled image.') drzim = options.drzim # -- run tweakback iraf.unlearn('tweakback') teal.unlearn('tweakback') tweakback.tweakback(drzim,verbose=True)
f = open('imlist.dat', 'w') for ff in imlist: f.write(ff+'\n') f.close() # -- get instrument and filter name instrum = pyfits.getheader(imlist[0])['INSTRUME'] if instrum == 'WFC3': filtname = pyfits.getheader(imlist[0])['FILTER'] elif instrum == 'ACS': filtname = pyfits.getheader(imlist[0])['FILTER1'] if filtname[0] == 'C': filtname = pyfits.getheader(imlist[0])['FILTER2'] # -- run AstroDrizzle teal.unlearn('astrodrizzle') iraf.unlearn('astrodrizzle') #astrodrizzle.AstroDrizzle('@imlist.dat',configobj='drizzle_'+instrum+'.cfg') if instrum == 'WFC3': astrodrizzle.AstroDrizzle('@imlist.dat',output=filtname.lower(),num_cores=4,final_bits='64', in_memory=True,clean=True, combine_type='imedian',preseve=False, \ final_wcs=True,final_rot=0.0,final_scale=0.05,final_pixfrac=0.4) elif instrum == 'ACS': astrodrizzle.AstroDrizzle('@imlist.dat',output=filtname.lower(),num_cores=4,final_bits='64,32',in_memory=True,clean=True, combine_type=imedian,preserve=False,\ final_wcs=True,final_rot=0.0,final_scale=0.05,final_pixfrac=0.7, final_wht_type='IVM') # weigh type dependent on avail. of rms maps else: raise Exception('Instrument '+instrum+' not covered in our case list.') # -- remove unwanted astrodrizzle files tmp=np.concatenate((glob.glob('*single*fits'),glob.glob('*mask.fits'),glob.glob('tmp*.fits'), glob.glob(*blt.fits'))) for ff in tmp: os.remove(ff)
import drizzlepac from drizzlepac import astrodrizzle from drizzlepac import tweakreg from drizzlepac import tweakback import pylab as p import numpy from numpy import ma, finfo, float32 from stsci.tools import teal teal.unlearn('astrodrizzle') teal.unlearn('tweakreg') adriz = astrodrizzle.AstroDrizzle twreg = tweakreg.TweakReg twback = tweakback.tweakback from utils import basic_params import numpy as np from astropy.io import fits import shutil import glob import os filt = np.array(['f125', 'f140', 'f150', 'f165']) filt_num = np.array(['125', '140', '150', '165']) def drizzle_SBC(params, params_gal, i): """Entry point for combining different extensions of FLT images Args: params: (dict) Dictionary of 'basic' section of config file (common for all ULIRGs) params_gal: (dict) Dictionary of galaxy parameters from config file i : (int) ULIRG number (It goes from 0 to 4)
# -- initialize "conv_width" based on filter name (IR = 2.5; optical=3.5) fheader = pyfits.getheader(im[0]) instr = fheader['INSTRUME'] if instr == 'WFC3': filtname = fheader['FILTER'] elif instr == 'ACS': filtname = fheader['FILTER1'] if filtname[0] == 'C': filtname = fheader['FILTER2'] else: raise Exception('Instrument '+instr+' not covered in case list.') if filtname[0:2] == 'F1': conv_wid = 2.5 else: conv_wid = 3.5 # -- run tweakreg teal.unlearn('tweakreg') if USE_REF: tweakreg.TweakReg('@imlist.dat',runfile=logfile,shiftfile=True,outshifts='shift.dat',updatehdr=True, \ fitgeometry=fittype,catfile=catfilename,xcol=xcol,ycol=ycol, \ refimage=irefim,refcat=irefcat,refxyunits='pixels',refxcol=rxcol,refycol=rycol, \ conv_width=conv_wid,searchrad=3.0,nclip=7, \ see2dplot=False,residplot='No plot') else: tweakreg.TweakReg('@imlist.dat',runfile=logfile,shiftfile=True,outshifts='shift.dat',updatehdr=True, \ fitgeometry=fittype,catfile=catfilename,xcol=xcol,ycol=ycol, \ conv_width=conv_wid,searchrad=3.0,nclip=7, \ see2dplot=False,residplot='No plot') # -- remove unwanted tweakreg files
for ff in imlist: f.write(ff+'\n') f.close() # -- get instrument and filter name instrum = pyfits.getheader(imlist[0])['INSTRUME'] if instrum == 'WFC3': filtname = pyfits.getheader(imlist[0])['FILTER'] if pixscale < 0: pixscale = 0.1283 elif instrum == 'ACS': filtname = pyfits.getheader(imlist[0])['FILTER1'] if filtname[0] == 'C': filtname = pyfits.getheader(imlist[0])['FILTER2'] if pixscale < 0: pixscale = 0.0496 # -- run AstroDrizzle teal.unlearn('astrodrizzle') iraf.unlearn('astrodrizzle') if instrum == 'WFC3': astrodrizzle.AstroDrizzle('@imlist.dat',output=filtname.lower(),num_cores=NCORES,final_bits='64', in_memory=True,clean=True, combine_type=imedian,preseve=False, \ final_wcs=True,final_rot=0.0,final_scale=pixscale,final_pixfrac=pixfrac,final_kernel='gaussian') elif instrum == 'ACS': astrodrizzle.AstroDrizzle('@imlist.dat',output=filtname.lower(),num_cores=NCORES,final_bits='64,32',in_memory=True,clean=True, combine_type=imedian,preserve=False,\ final_wcs=True,final_rot=0.0,final_scale=pixscale,final_pixfrac=pixfrac,final_kernel='gaussian') else: raise Exception('Instrument '+instrum+' not covered in our case list.') # -- remove unwanted astrodrizzle files tmp=np.concatenate((glob.glob('*single*fits'),glob.glob('*mask.fits'),glob.glob('tmp*.fits'), glob.glob(*blt.fits'))) for ff in tmp: os.remove(ff)
numflts = len(imlist) if numflts < 4: cnhigh = 0 cnlow = 0 combtype = "minmed" elif numflts < 6: cnhigh = 1 cnlow = 0 combtype = "median" else: cnhigh = 2 cnlow = 1 combtype = "median" # -- run AstroDrizzle teal.unlearn("astrodrizzle") if outname == "NA": outname = filtname.lower() + "_" + finalwht.lower() else: outname = outname + "_" + finalwht.lower() astrodrizzle.AstroDrizzle( "@imlist.dat", output=outname, num_cores=NCORES, clean=False, preserve=False, combine_type=combtype, combine_nhigh=cnhigh, combine_nlow=cnlow, skywidth=0.1, skystat=skystat,
def align(self, subtract_background=True, align_method=None, ref_catalog=None, create_diff_source_lists=True, updatehdr=True, updatewcs=True, wcsname='DASH', threshold=50., cw=3.5, searchrad=20., astrodriz=True, cat_file='catalogs/diff_catfile.cat', drz_output=None, move_files=False): ''' Aligns new FLT's to reference catalog. Parameters ---------- self : object DashData object created from an individual IMA file. subtract_background : bool, optional If True, runs subtract_background_reads functions. align_method : string, optional Defines alignment method to be used. Default is None (input files will align to each other). ref_catalog : cat file, optional Defines reference image that will be referenced for CATALOG alignment method. create_diff_source_lists : bool, optional Specifies whether or not to create a segmentation image and source list from the difference files updatehdr : bool, optional Specifies whether to update the headers after aligning during TweakReg. Default is True. updatewcs : bool, optional Specifies whether to update the WCS after aligning during TweakReg. Default is True. wcsname : str, optional (as long as name you choose doesn't already exist) Specifies name of WCS. Default is 'DASH' threshold : float, optional cw : float, optional searchrad : float, optional Radius (in pixels) that TweakReg will search around sources to find matches. Default is 20 pixels. astrodriz : bool, optional Specifies whether to drizzle images together using Astrodrizzle. Default is True. cat_file : str, optional Name of catfile to be used to align sources in TweakReg. Default is the catfile created by setting create_diff_source_lists to True, catalogs/diff_catfile.cat drz_output : str, optional Name of output file after drizzling using AstroDrizzle. Default is the root name of the original IMA. Outputs ------- Shifts file : txt file File containing shifts during TweakReg WCS Shifts file : fits file File containing WCS shifts during TweakReg Residual plot : png Plot showing residuals from TweakReg alignment Vector plot : png Plot showing vector poitnings from TweakReg alignment Aligned FLT's : fits file Same diff files created in split_ima that have been aligned using TweakReg Drizzled Image : fits file Setting astrodriz to True will output a drizzled image form a single exposure (produced only if astrodriz is set to True) ''' #Set name for output drizzled image to the rootname of the original IMA if it is not specified if drz_output is None: drz_output = self.root input_images = sorted(glob('diff/{}_*_diff.fits'.format(self.root))) if not os.path.exists('shifts'): os.mkdir('shifts') outshifts = 'shifts/shifts_{}.txt'.format(self.root) outwcs = 'shifts/shifts_{}_wcs.fits'.format(self.root) if subtract_background: self.subtract_background_reads() #Align images to a catalog if align_method == 'CATALOG': if (ref_catalog is not None): ##Create source list and segmentation maps based on difference files if create_diff_source_lists is True: diffpath = os.path.dirname( os.path.abspath('diff/{}_*_diff.fits'.format( self.root))) cat_images = sorted([ os.path.basename(x) for x in glob('diff/{}_*_diff.fits'.format(self.root)) ]) sc_diff_files = [diffpath + '/' + s for s in cat_images] self.diff_seg_map(cat_images=sc_diff_files) teal.unlearn('tweakreg') teal.unlearn('imagefindpars') tweakreg.TweakReg(input_images, refcat=ref_catalog, catfile=cat_file, xcol=2, ycol=3, updatehdr=updatehdr, updatewcs=updatewcs, wcsname=wcsname, verbose=True, imagefindcfg={ 'threshold': threshold, 'conv_width': cw }, searchrad=searchrad, searchunits='pixels', shiftfile=True, outshifts=outshifts, outwcs=outwcs, interactive=False, fitgeometry='rscale', minobj=5) else: raise Exception( 'Need to specify reference catalog and reference image.') #Align images to the first image else: teal.unlearn('tweakreg') teal.unlearn('imagefindpars') tweakreg.TweakReg(input_images, catfile=cat_file, xcol=2, ycol=3, updatehdr=updatehdr, updatewcs=updatewcs, wcsname=wcsname, verbose=True, imagefindcfg={ 'threshold': threshold, 'conv_width': cw }, searchrad=searchrad, searchunits='pixels', shiftfile=True, outshifts=outshifts, outwcs=outwcs, interactive=False, fitgeometry='rscale', minobj=5) pass #Drizzle the images together if astrodriz is True: #Do not have drizzle take 256 flags into account no_tfs = 2, 4, 8, 16, 32, 64, 128, 512, 2048, 4096, 8192, 16384 astrodrizzle.AstroDrizzle(input_images, output=drz_output, clean=False, final_pixfrac=1.0, context=False, resetbits=0, preserve=False, driz_cr_snr='8.0 5.0', driz_cr_scale='2.5 0.7', driz_sep_bits=no_tfs, final_bits=no_tfs) if move_files is True: self.move_files()