def doScaleSubMask( targname, targfilter, targepoch, tempfilter, tempepoch, tempfilter2=None, tempepoch2=None, scalefactor=1, filtdir='HSTFILTERS', clean=True, verbose=True, clobber=False ): """ Primary function for making pseudo-filter diff images : * make a scaled template image, scaling the template filter to match the target filter * subtract scaled template from target filter+epoch * make a union badpix mask * apply the union badpix mask * make a composite weight mask """ import os import imarith import badpix from astropy.io import fits as pyfits targdir = '%s.e%02i'%(targname, targepoch) targsci = '%s_%s_e%02i_reg_drc_sci.fits'%(targname, targfilter, targepoch) targsci = os.path.join( targdir, targsci ) if not os.path.isfile( targsci ) : targsci = targsci.replace('_drc','_drz') targwht = targsci.replace( '_sci.fits','_wht.fits') targbpx = targsci.replace( '_sci.fits','_bpx.fits') assert os.path.isfile( targsci ) assert os.path.isfile( targwht ) # assert os.path.isfile( targbpx ) hdr = pyfits.getheader( targsci ) if 'CAMERA' in hdr : instrument = hdr['CAMERA'] detector = '' elif 'INSTRUME' in hdr : instrument = hdr['INSTRUME'] if 'DETECTOR' in hdr : detector = hdr['DETECTOR'] targetcamfilter = '-'.join([instrument,detector,targfilter.upper()]).rstrip('-') if targetcamfilter.endswith('L') : targetcamfilter += 'P' tempdir = '%s.e%02i'%(targname, tempepoch) tempfile = '%s_%s_e%02i_reg_drc_sci.fits'%(targname, tempfilter, tempepoch) tempfile = os.path.join( tempdir, tempfile ) if not os.path.isfile( tempfile ) : tempfile = tempfile.replace('_drc','_drz') assert os.path.isfile( tempfile ) if tempfilter2 is None : tempfile2 = None else : if tempepoch2 is None : tempepoch2 = tempepoch tempdir2 = '%s.e%02i'%(targname, tempepoch2) tempfile2 = '%s_%s_e%02i_reg_drc_sci.fits'%(targname, tempfilter2, tempepoch2) tempfile2 = os.path.join( tempdir2, tempfile2 ) if not os.path.isfile( tempfile2 ) : tempfile2 = tempfile2.replace('_drc','_drz') assert os.path.isfile( tempfile2 ) outfile = os.path.join( tempdir, '%s_~%s_e%02i_reg_drc_sci.fits'%(targname, targfilter, tempepoch) ) if os.path.exists( outfile ) and not clobber : print("Template file %s already exists, not clobbering."%outfile) tempsci = outfile tempwht = tempsci.replace('sci.fits','wht.fits') tempbpx = tempsci.replace('sci.fits','bpx.fits') else : tempsci, tempwht, tempbpx = mkscaledtemplate( targetcamfilter, tempfile, imfile2=tempfile2, outfile=outfile, scalefactor=scalefactor, filtdir=filtdir, verbose=verbose, clobber=clobber) subsci = '%s_%s_e%02i-e%02i_sub_sci.fits'%(targname, targfilter, targepoch, tempepoch) subsci = os.path.join( targdir, subsci ) if verbose : print( "Making pseudo diff image %s"%subsci ) diffim = imarith.imsubtract( tempsci, targsci, outfile=subsci, clobber=clobber, verbose=verbose ) diffwht = imarith.combine_ivm_maps( targwht, tempwht, diffim.replace('sci.fits','wht.fits'), clobber=clobber, verbose=verbose ) if os.path.exists(targbpx): diffbpx = badpix.unionmask( tempbpx, targbpx, diffim.replace('sci.fits','bpx.fits'), clobber=clobber, verbose=verbose) diffim_masked = badpix.applymask( diffim, diffbpx, clobber=clobber, verbose=verbose) if clean: # delete the sub_sci.fits, b/c it is superseded os.remove( diffim ) else: diffim_masked = diffim.replace('sub_sci.fits', 'sub_masked.fits') os.rename(diffim, diffim_masked) diffbpx = None if verbose : print("Created diff image %s, wht map %s, and bpx mask %s"%( diffim_masked, diffwht, diffbpx ) ) return( diffim_masked, diffwht, diffbpx )
def mkscaledtemplate( targetfilter, imfile1, imfile2=None, outfile=None, scalefactor=1, filtdir='HSTFILTERS', verbose=True, clobber=False ): """ Combine and/or scale imfile1 (and imfile2) to match the filter transmission function for targetfilter (e.g. for making a template image for F814W from two images in F775W and F850LP). If an outfile is specified and the wht and bpx files associated with the input image files are available, then they will also be combined and written to appropriately named output files The argument targetfilter must have the form 'INSTRUMENT-DETECTOR-FILTER'. For example: 'ACS-WFC-F606W' Returns the names of all files generated (sci, wht, bpx) """ from astropy.io import fits as pyfits import imarith import shutil import badpix sourcefilter1 = camfiltername( imfile1 ) if imfile2 : sourcefilter2 = camfiltername( imfile2 ) if verbose : print('Scaling %s + %s to match %s'%(sourcefilter1,sourcefilter2,targetfilter)) else : sourcefilter2 = None if verbose : print('Scaling %s to match %s'%(sourcefilter1,targetfilter)) scalefactor = scalefactor * computeFilterScaling( targetfilter, sourcefilter1, source2=sourcefilter2, filtdir=filtdir) if imfile2 : imdat = imarith.imsum( imfile1, imfile2 ) else : imdat = pyfits.getdata( imfile1 ) imdatscaled = imdat * scalefactor outhdr = pyfits.getheader( imfile1 ) outhdr['FILTER'] = '~' + targetfilter.split('-')[-1] if 'FILTER1' in outhdr : outhdr.remove('FILTER1') if 'FILTER2' in outhdr : outhdr.remove('FILTER2') if outfile is None : return( imdatscaled ) outdir = os.path.split( outfile )[0] if outdir : if not os.path.isdir(outdir): os.makedirs( outdir ) pyfits.writeto( outfile, imdatscaled, header=outhdr, clobber=clobber ) if not outfile.endswith('sci.fits') or not imfile1.endswith('sci.fits'): return( outfile ) outwht = outfile.replace('sci.fits','wht.fits') outbpx = outfile.replace('sci.fits','bpx.fits') inwht1 = imfile1.replace('sci.fits','wht.fits') inbpx1 = imfile1.replace('sci.fits','bpx.fits') if imfile2 is None : if os.path.isfile( inwht1 ) and os.path.isfile( inbpx1 ) : shutil.copy( inwht1, outwht ) shutil.copy( inbpx1, outbpx ) return( outfile, outwht, outbpx) else : return( outfile ) inwht2 = imfile2.replace('sci.fits','wht.fits') inbpx2 = imfile2.replace('sci.fits','bpx.fits') if os.path.isfile( inwht2 ) : outwht = imarith.combine_ivm_maps( inwht1, inwht2, outwht, clobber=clobber, verbose=verbose ) if os.path.isfile( inbpx2 ) : outbpx = badpix.unionmask( inbpx1, inbpx2, outbpx, clobber=clobber, verbose=verbose ) assert os.path.isfile( outbpx ), "Scaled badpix image %s not created."%outbpx else: outbpx = None assert os.path.isfile( outfile ), "Scaled template %s not created."%outfile assert os.path.isfile( outwht ), "Scaled weight image %s not created."%outwht return( outfile, outwht, outbpx)