def lax(fs=None): iraf.cd('work') if fs is None: fs = glob('bkg/*bkg*.fits') if len(fs) == 0: print "WARNING: No background-subtracted files for Lacosmicx." iraf.cd('..') return if not os.path.exists('lax'): os.mkdir('lax') for f in fs: outname = f.replace('bkg','lax') hdu = pyfits.open(f) # Add a CRM extension hdu.append(pyfits.ImageHDU(data=hdu['BPM'].data.copy(), header=hdu['BPM'].header.copy(), name='CRM')) # Set all of the pixels in the CRM mask to zero hdu['CRM'].data[:, :] = 0 # less aggressive lacosmic on standard star observations if not isstdstar(f): objl = 1.0 sigc = 4.0 else: objl = 3.0 sigc = 10.0 chipgaps = get_chipgaps(hdu) chipedges = [[0, chipgaps[0][0]], [chipgaps[0][1] + 1, chipgaps[1][0]], [chipgaps[1][1] + 1, chipgaps[2][0]]] # Run each chip separately for chip in range(3): # Use previously subtracted sky level = 0 as we have already added # a constant sky value in the background task # Gain = 1, readnoise should be small so it shouldn't matter much. # Default value seems to work. chipinds = slice(chipedges[chip][0], chipedges[chip][1]) crmask, _cleanarr = lacosmicx.lacosmicx(hdu[1].data[:, chipinds].copy(), inmask=np.asarray(hdu[2].data[:, chipinds].copy(), dtype = np.uint8), sigclip=sigc, objlim=objl, sigfrac=0.1, gain=1.0, pssl=0.0) # Update the image hdu['CRM'].data[:, chipinds][:, :] = crmask[:,:] # Flag the cosmic ray pixels with a large negative number hdu['SCI'].data[:, chipinds][crmask == 1] = -1000000 # Save the file hdu.writeto(outname, clobber=True) hdu.close() iraf.cd('..')
def cosmic_ray_remove(fnlist): """Runs the LACosmicx routine on a series of images in order to correct cosmic rays. Creates the header keyword 'fpcosmic' and sets its value to 'True' Inputs: fnlist -> A list containing the paths to fits images. """ for i in range(len(fnlist)): print "Cosmic-ray correcting image "+str(i+1)+" of "+str(len(fnlist))+": "+fnlist[i] image = openfits(fnlist[i],mode="update") image[0].header["fpcosmic"] = "True" _mask, image[0].data = lacosmicx(image[0].data,verbose=False,cleantype='idw') image[0].data[np.isnan(image[0].data)]=0 image.close() return
def prep_direct_grism_pair(direct_asn='goodss-34-F140W_asn.fits', grism_asn='goodss-34-G141_asn.fits', radec=None, raw_path='../RAW/', mask_grow=18, scattered_light=False, final_scale=None, skip_direct=False, ACS=False, jump=False, order=2, get_shift=True, align_threshold=20, column_average=True, sky_iter=3, run_acs_lacosmic=False): """ Process both the direct and grism observations of a given visit """ import threedhst.prep_flt_astrodrizzle as prep import drizzlepac from stwcs import updatewcs import time t0 = time.time() #direct_asn='goodss-34-F140W_asn.fits'; grism_asn='goodss-34-G141_asn.fits'; radec=None; raw_path='../RAW/' #radec = os.getenv('THREEDHST') + '/ASTRODRIZZLE_FLT/Catalog/goodss_radec.dat' ################################ #### Direct image processing ################################ #### xx add astroquery 2MASS/SDSS workaround for radec=None if not skip_direct: #### Get fresh FLTS from ../RAW/ asn = threedhst.utils.ASNFile(direct_asn) if ACS: for exp in asn.exposures: print 'cp %s/%s_flc.fits.gz .' %(raw_path, exp) os.system('cp %s/%s_flc.fits.gz .' %(raw_path, exp)) os.system('gunzip -f %s_flc.fits.gz' %(exp)) if run_acs_lacosmic: try: import lacosmicx status = True except: print 'import lacosmicx failed!' status = False if status: im = pyfits.open('%s_flc.fits' %(exp), mode='update') for ext in [1,2]: indata = im['SCI',ext].data #inmask = im['DQ',ext].data > 0 if im['SCI',ext].header['BUNIT'] == 'ELECTRONS': gain = 1 else: gain = 1./im[0].header['EXPTIME'] if 'MDRIZSK0' in im['SCI',ext].header: pssl = im['SCI',ext].header['MDRIZSK0'] else: pssl = 0. if 'FLASHLVL' in im[0].header: pssl += im[0].header['FLASHLVL'] sig_scale = 1.8 else: sig_scale = 1. out = lacosmicx.lacosmicx(indata, inmask=None, sigclip=3.5*sig_scale, sigfrac=0.2, objlim=7.0, gain=gain, readnoise=im[0].header['READNSEA'], satlevel=np.inf, pssl=pssl, niter=5, sepmed=True, cleantype='meanmask', fsmode='median', psfmodel='gauss', psffwhm=2.5,psfsize=7, psfk=None, psfbeta=4.765, verbose=True) crmask, cleanarr = out im['DQ',ext].data |= 16*crmask ### Low pixels if im[0].header['INSTRUME'] == 'WFC3': bad = im['SCI',ext].data < -4*im['ERR',ext].data im['DQ',ext].data |= 16*bad im.flush() else: threedhst.process_grism.fresh_flt_files(direct_asn, from_path=raw_path) if (not ACS): #### Subtract WFC3/IR direct backgrounds prep.subtract_flt_background(root=direct_asn.split('_asn')[0], scattered_light=scattered_light, order=order) #### Flag IR CRs again within runTweakReg #### Run TweakReg if (radec is None) & (not ACS): print len(asn.exposures) if len(asn.exposures) > 1: drizzlepac.astrodrizzle.AstroDrizzle(direct_asn, clean=True, final_scale=None, final_pixfrac=0.8, context=False, final_bits=576, preserve=False, driz_cr_snr='5.0 4.0', driz_cr_scale = '2.5 0.7') else: drizzlepac.astrodrizzle.AstroDrizzle(direct_asn, clean=True, final_scale=None, final_pixfrac=1, context=False, final_bits=576, preserve=False, driz_separate=False, driz_sep_wcs=False, median=False, blot=False, driz_cr=False, driz_cr_corr=False, driz_combine=True) else: if get_shift: prep.runTweakReg(asn_file=direct_asn, master_catalog=radec, final_scale=None, ACS=ACS, threshold=align_threshold) #### Subtract background of direct ACS images if ACS: for exp in asn.exposures: flc = pyfits.open('%s_flc.fits' %(exp), mode='update') if 'SUB' in flc[0].header['APERTURE']: extensions = [1] else: extensions = [1,4] for ext in extensions: threedhst.showMessage('Subtract background from %s_flc.fits[%d] : %.4f' %(exp, ext, flc[ext].header['MDRIZSKY'])) flc[ext].data -= flc[ext].header['MDRIZSKY'] flc[ext].header['MDRIZSK0'] = flc[ext].header['MDRIZSKY'] flc[ext].header['MDRIZSKY'] = 0. # flc.flush() else: pass #### Do this later, gives segfaults here??? #prep.subtract_flt_background(root=direct_asn.split('_asn')[0], scattered_light=scattered_light) #### Flag CRs again on BG-subtracted image #drizzlepac.astrodrizzle.AstroDrizzle(direct_asn, clean=True, final_scale=None, final_pixfrac=0.8, context=False, final_bits=576, preserve=False, driz_cr_snr='5.0 4.0', driz_cr_scale = '2.5 0.7') # , ################################ #### Grism image processing ################################ if grism_asn: asn = threedhst.utils.ASNFile(grism_asn) if ACS: for exp in asn.exposures: print 'cp %s/%s_flc.fits.gz .' %(raw_path, exp) os.system('cp %s/%s_flc.fits.gz .' %(raw_path, exp)) os.system('gunzip -f %s_flc.fits.gz' %(exp)) updatewcs.updatewcs('%s_flc.fits' %(exp)) prep.copy_adriz_headerlets(direct_asn=direct_asn, grism_asn=grism_asn, ACS=True) prep.subtract_acs_grism_background(asn_file=grism_asn, final_scale=None) else: #### Remove the sky and flag CRs ## with mask from rough zodi-only subtraction prep.subtract_grism_background(asn_file=grism_asn, PATH_TO_RAW='../RAW/', final_scale=None, visit_sky=True, column_average=False, mask_grow=mask_grow, first_run=True) ## Redo making mask from better combined image prep.subtract_grism_background(asn_file=grism_asn, PATH_TO_RAW='../RAW/', final_scale=final_scale, visit_sky=True, column_average=column_average, mask_grow=mask_grow, first_run=False, sky_iter=sky_iter) #### Copy headers from direct images if radec is not None: prep.copy_adriz_headerlets(direct_asn=direct_asn, grism_asn=grism_asn, ACS=False) #### Run CR rejection with final shifts drizzlepac.astrodrizzle.AstroDrizzle(grism_asn, clean=True, skysub=False, final_wcs=True, final_scale=final_scale, final_pixfrac=0.8, context=False, final_bits=576, driz_sep_bits=576, preserve=False, driz_cr_snr='8.0 5.0', driz_cr_scale='2.5 0.7') # driz_cr_snr='5.0 4.0', driz_cr_scale = '2.5 0.7') if not grism_asn: t1 = time.time() threedhst.showMessage('direct: %s\n\nDone (%d s).' %(direct_asn, int(t1-t0))) else: t1 = time.time() threedhst.showMessage('direct: %s\ngrism: %s\n\nDone (%d s).' %(direct_asn, grism_asn, int(t1-t0)))
def fix_cosmic_rays(self, rm_custom=False, flag=None, **lacosmic_param): ''' Resets cosmic rays within the seg maps of objects and uses L.A.Cosmic to find them again. Parameters ---------- self : object DashData object created from an individual IMA file. rm_custom : bool Specifies whether or not the user would like to remove custom flags within the boundaries of sources, as defined by the segmentation map created from the original FLT. flag : int Specifies flag the user would like the remove within the boundaries of sources. lacosmic_param : dic Dictionary of the L.A.Cosmic parameters that users may want to specify. If not set, then presets are used. Output ------ Fixed for cosmic rays diff files : fits Same diff files created in split_ima that have now been corrected for cosmic ray errors. ''' asn_exposures = sorted(glob('diff/' + self.root + '_*_diff.fits')) seg = fits.open('segmentation_maps/{}_seg.fits'.format(self.root)) seg_data = np.cast[np.float32](seg[0].data) flt_full = fits.open(self.flt_file_name) flt_full_wcs = stwcs.wcsutil.HSTWCS(flt_full, ext=1) EXPTIME = flt_full[0].header['EXPTIME'] if lacosmic_param: gain = lacosmic_param['gain'] readnoise = lacosmic_param['readnoise'] objlim = lacosmic_param['objlim'] pssl = lacosmic_param['pssl'] verbose = lacosmic_param['verbose'] else: gain = 1.0 readnoise = 20. objlim = 15.0 pssl = 0. verbose = True #Have lacosmicx locate cosmic rays crmask, clean = lacosmicx.lacosmicx(flt_full[1].data, gain=gain, readnoise=readnoise, objlim=objlim, pssl=pssl, verbose=verbose) yi, xi = np.indices((1014, 1014)) #Remove all 4096 flags within the boundaries of objects for exp in asn_exposures: flt = fits.open(exp, mode='update') flagged_stars = ((flt['DQ'].data & 4096) > 0) & (seg_data > 0) flt['DQ'].data[flagged_stars] -= 4096 new_cr = (crmask == 1) & ((flt['DQ'].data & 4096) == 0) & ( (seg_data == 0) | ((seg_data > 0) & (flt['SCI'].data < 1.))) & (xi > 915) & (yi < 295) flt['DQ'].data[new_cr] += 4096 flt.flush() #Remove custom flags if rm_custom is True: if flag is not None: for exp in asn_exposures: flt = fits.open(exp, mode='update') flagged_stars = ( (flt['DQ'].data & flag) > 0) & (seg_data > 0) flt['DQ'].data[flagged_stars] -= flag new_cr = (crmask == 1) & ((flt['DQ'].data & flag) == 0) & ( (seg_data == 0) | ((seg_data > 0) & (flt['SCI'].data < 1.))) & (xi > 915) & (yi < 295) flt['DQ'].data[new_cr] += flag flt.flush() else: raise Exception('Must specify which flags to remove.')