def n_extensions(fitsfile): '''returns number of extension in a fits file Usage: n = n_extensions(filename)''' iraf.mscred(_doprint=0) result=iraf.mscextensions(fitsfile, output='none') return iraf.mscextensions.nimages
def reproject_ota(img, ota, rad, decd): from pyraf import iraf image = odi.illcorpath+'illcor_'+ota+'.'+str(img[16:]) imout = odi.reprojpath+'reproj_'+ota+'.'+str(img[16:]) iraf.mscred(_doprint=0) iraf.clobber='no' iraf.unlearn(iraf.mscred.mscimage) iraf.mscred.mscimage.format='image' iraf.mscred.mscimage.pixmask='yes' iraf.mscred.mscimage.verbose='yes' iraf.mscred.mscimage.wcssour='parameters' iraf.mscred.mscimage.ref='' iraf.mscred.mscimage.ra=rad iraf.mscred.mscimage.dec=decd iraf.mscred.mscimage.scale=0.11 iraf.mscred.mscimage.rotation=0.0 iraf.mscred.mscimage.blank=-999 iraf.mscred.mscimage.interpo='poly5' iraf.mscred.mscimage.minterp='poly5' iraf.mscred.mscimage.nxbl=4096 iraf.mscred.mscimage.nybl=4096 iraf.mscred.mscimage.fluxcon='yes' iraf.mscred.mscimage(image,imout) return
def aperture_photometry(image,photfilesuffix,threshold=4.,wcs='logical',mode='small', telescope='Pairitel'): '''perform aperture photometry on a given image This procedure perfrom IRAF/DAOPHOT aperture phtometry on (all extensions of) a fits image. It sets some global daophot parameters, then perfroms daophot.daofind and daophot.phot The output .mag file is filtered for valid magnitudes and a mag.sex file computed, where all coordinates are transformed to sexagesimal coordinates. input: image: filename keywords: threshold=4 :daofind detection threshold in sigma_sky wcs='logical' :working wcs system, can be 'logical','physical' or 'tv' in any case an additional sexagesimal output file will be computed mode='small' : 'standard' = 'large' or 'psf' = 'small' choses a pre-defined aperture size for aperture photometry of standards or just as starting point for psf photometry ''' print 'Performing aperture photometry on '+image if telescope == 'Pairitel': set_Pairitel_params() elif telescope == 'FLWO': set_FLWO_params() else: print 'No parameters found for this telescope!' iraf.mscred(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.daophot.verify = False iraf.daophot.wcsin = wcs iraf.daophot.wcsout = wcs iraf.daophot.wcspsf = wcs iraf.centerpars.cbox=2.*iraf.datapars.fwhmpsf iraf.fitskypars.annulus=20 # usually one would use 5.*iraf.datapars.fwhmpsf #annulus must be wide enough to have good sky statistics iraf.fitskypars.dannulus=10. # this is standard if 'standard'.find(mode.lower()) != -1 or 'large'.find(mode.lower()) != -1: iraf.photpars.aperture = 4.*iraf.datapars.fwhmpsf iraf.centerpars.calgorithm = 'centroid' elif 'small'.find(mode.lower()) != -1 or 'psf'.find(mode.lower()) != -1: iraf.photpars.aperture = 4. # close in for crowded regions iraf.centerpars.calgorithm = 'none' else: print '''use: aperture_photometry(image,threshold=4.,wcs="logical",mode="small") The following mode keywords are implemented: standard, large, psf, small''' raise NotImplementedError #for imagebi in fits_ext(image): sky,skydev=get_sky(image) iraf.datapars.datamin=sky-5*skydev iraf.datapars.sigma=skydev #this could be changed to use Stdin and Stdout so that fewer files are written (saves space and time), but for debug purposes better keep all that iraf.daofind(image, output='default', verbose=False, verify=False, threshold=threshold) iraf.daophot.phot(image, coords='default', output=image+photfilesuffix, verbose=False, verify=False, interactive=False, wcsout=wcs)
def fix_wcs_full(img, coords='radec.coo', iters=1): print coords iraf.mscred(_doprint=0) iraf.unlearn(iraf.mscred.msccmatch) # otaext = {'33':'[1]','34':'[2]','44':'[3]','43':'[4]','42':'[5]','32':'[6]','22':'[7]','23':'[8]','24':'[9]'} for i in range(iters): fix = iraf.msccmatch(input=img,coords=coords,usebpm='no',verbose='yes',nsearch=1000,search=10,rsearch=5.5,cfrac=.9,csig=1.0,nfit=3,rms=15,maxshif=50,fitgeom="general",update='yes',interac='no',fit='no',accept='yes', Stdout=1) print 'fixing WCS for',img+', iter ='+repr(i) print fix[-6] print fix[-5] print fix[-4] print fix[-3] print fix[-2]
def shift_wcs(image, shifttab, number): '''Shift wcs in image according to value in shifttab image: filename shifttab: structured record array with cols "from to rashift decshift" rashift and decshift in arcsec number: ID of image to chose record in shifttab This method applies the rashift and decshift specified in the record of shifttab with from <= number <= to from to rashift decshift 90 105 46.7 16.0 106 111 48.0 21.0 112 127 -29.6 18.8 ''' iraf.mscred(_doprint=0) index=(shifttab['from']<=number)*(shifttab['to']>=number) iraf.mscwcs(image,ra_shift=shifttab['rashift'].compress(index)[0],dec_shift=shifttab['decshift'].compress(index)[0])
def iqlfc(inpat,clobber=globclob,verbose=globver): """ intelligent processing of LFC imaging data """ # Necessary packages if not iraf.mscred._loaded: iraf.mscred() # Defaults twait=30 reduced={} filtkey="FILTER" trimsec="[1:2048,1:2048]" biasname="Bias.fits" biaspfx="b" flatpre="Flat-" flatpfx="f" statsec="" sigma=2.0 satval=25000.0 masksfx="mask" pix=0.378 # Perform the LFC assembly allfiles=glob.glob(inpat) for file in allfiles: if not file.endswith('.fits'): os.rename(file,file+".fits") assembled={} allnames=[] for file in allfiles: if file.endswith('.fits'): (root,num,chip,ext)=file.split('.') else: (root,num,chip)=file.split('.') if not assembled.has_key(num): name="%s.%s" % (root,num) iraf.lfcassemble(name) assembled[num]=yes allnames.append(name)
def iqlfc(inpat, clobber=globclob, verbose=globver): """ intelligent processing of LFC imaging data """ # Necessary packages if not iraf.mscred._loaded: iraf.mscred() # Defaults twait = 30 reduced = {} filtkey = "FILTER" trimsec = "[1:2048,1:2048]" biasname = "Bias.fits" biaspfx = "b" flatpre = "Flat-" flatpfx = "f" statsec = "" sigma = 2.0 satval = 25000.0 masksfx = "mask" pix = 0.378 # Perform the LFC assembly allfiles = glob.glob(inpat) for file in allfiles: if not file.endswith('.fits'): os.rename(file, file + ".fits") assembled = {} allnames = [] for file in allfiles: if file.endswith('.fits'): (root, num, chip, ext) = file.split('.') else: (root, num, chip) = file.split('.') if not assembled.has_key(num): name = "%s.%s" % (root, num) iraf.lfcassemble(name) assembled[num] = yes allnames.append(name)
def correct_wcs(image,clobber=False,minmag=13, radius=20.): '''correct wcs of fitsfile to 2MASS coordinates This method loads the 2MASS catalog in the region covered by the fits file and shifts,stretches, and rotates the wcs in the fits file to match the 2MASS coordinates The initial wcs needs to be approximately correct (within a few arcsec), otherwise the matching will fail. image = filename clobber = overwrite existing 2MASS catalog file? minmag = mag of faintest object retrieved from 2MASS''' object = iraf.hselect(image+'[0]','OBJECT', 'yes', Stdout=1)[0] filter = iraf.hselect(image+'[0]','FILTER', 'yes', Stdout=1)[0] catalogue=os.path.join(os.path.dirname(image),"2mass_"+str(object)+'_'+str(filter)+'_'+str(minmag)+".cat") #Gererate catalogue, if it does not exist in this directory if clobber or not os.path.isfile(catalogue): get_catalogue(catalogue,iraf.hselect(image+'[0]','RA','yes', Stdout=1)[0],iraf.hselect(image+'[0]','DEC','yes', Stdout=1)[0], radius=radius, minmag=minmag) #cfrac: maximum fraction of targets which do not center correctly # a large number is no problem here, since often there are a few 100 sources in the catalog to match. iraf.mscred(_doprint=0) #matchout=iraf.msccmatch(image,catalogue,interactive=False, Stdout=1,cfrac=0.95,rms=3) iraf.cd(os.path.dirname(image)) matchout=iraf.msccmatch(os.path.basename(image),catalogue,interactive=False, Stdout=1,cfrac=0.9,rms=10,maxshif=100., nsearch=100, nfit=5, update='yes') try: print matchout[-5] except IndexError: print matchout
def reproject_ota(img, ota, rad, decd, wcsref): """ Use the IRAF task ``mscimage`` in the ``mscred`` package to reproject an OTA to a reference tangent plane with constant pixel scale. Parameters ---------- img : str Name of image being processed ota : str Name of current ``ota`` being processed in ``img`` rad : float Reference Ra position for reprojection decd : float Reference Ra position for reprojection wcfreg : str Name of image and ota to be used as the reference image for ``mscimage`` Note ---- Nothing is returned by this function but the reprojected ota is saved to the ``repreopath``. The pipeline is setup to use OTA33 of the first image in the images list as the reference image for this function. Here is how the ``mscimage`` IRAF parameters are set: - iraf.mscred.mscimage.format='image' - iraf.mscred.mscimage.pixmask='yes' - iraf.mscred.mscimage.verbose='yes' - iraf.mscred.mscimage.wcssour='image' - iraf.mscred.mscimage.ref=wcsref - iraf.mscred.mscimage.ra=rad - iraf.mscred.mscimage.dec=decd - iraf.mscred.mscimage.scale=0.11 - iraf.mscred.mscimage.rotation=0.0 - iraf.mscred.mscimage.blank=-999 - iraf.mscred.mscimage.interpo='poly5' - iraf.mscred.mscimage.minterp='poly5' - iraf.mscred.mscimage.nxbl=4096 - iraf.mscred.mscimage.nybl=4096 - iraf.mscred.mscimage.fluxcon='yes' - iraf.mscred.mscimage(image,imout) """ from pyraf import iraf image = odi.illcorpath+'illcor_'+ota+'.'+img.stem() imout = odi.reprojpath+'reproj_'+ota+'.'+img.stem() iraf.mscred(_doprint=0) iraf.clobber='no' iraf.unlearn(iraf.mscred.mscimage) iraf.mscred.mscimage.format='image' iraf.mscred.mscimage.pixmask='yes' iraf.mscred.mscimage.verbose='yes' iraf.mscred.mscimage.wcssour='image' iraf.mscred.mscimage.ref=wcsref iraf.mscred.mscimage.ra=rad iraf.mscred.mscimage.dec=decd iraf.mscred.mscimage.scale=0.11 iraf.mscred.mscimage.rotation=0.0 iraf.mscred.mscimage.blank=-999 iraf.mscred.mscimage.interpo='poly5' iraf.mscred.mscimage.minterp='poly5' iraf.mscred.mscimage.nxbl=4096 iraf.mscred.mscimage.nybl=4096 iraf.mscred.mscimage.fluxcon='yes' iraf.mscred.mscimage(image,imout) return
def fix_wcs_full(img, coords='radec.coo', iters=1): """ Try to improve the WCS solution of a final stacked image. Parameters ---------- img : str Name of image coords : str Name of coordinate file iter : int Number of desired iterations of ``msccmatch``. It is still being tested, but one might be all that is necessary, especially if using the Gaia catalog. Note ---- This function is set up to use the files in the ``illcor`` directory. The following are the parameters used by ``msccmatch``. - verbose='yes' - usebpm='no' - nsearch=250 - search=30 - rsearch=0.2 - cfrac=.5 - csig=0.1 - nfit=5 - rms=1.0 - maxshif=5.0 - fitgeom="general" - update='yes' - interac='yes' - fit='no', - accept='yes' - Stdout=1 """ print coords iraf.mscred(_doprint=0) iraf.unlearn(iraf.mscred.msccmatch) # otaext = {'33':'[1]','34':'[2]','44':'[3]','43':'[4]','42':'[5]','32':'[6]','22':'[7]','23':'[8]','24':'[9]'} for i in range(iters): fix = iraf.msccmatch(input=img, coords=coords, usebpm='no', verbose='yes', nsearch=250, search=30, rsearch=0.2, cfrac=.5, csig=0.1, nfit=5, rms=1.0, maxshif=5.0, fitgeom="general", update='yes', interac='yes', fit='no', accept='yes', Stdout=1) print 'fixing WCS for',img.f+', iter ='+repr(i) print fix[-6] print fix[-5] print fix[-4] print fix[-3] print fix[-2]
def fix_wcs(img, ota, coords='radec.coo', iters=3): """ Try to improve the WCS solution of an OTA using the IRAF task ``msccmatch``. This function will use the ``img.nofits()+'.'+ota+'.radec.coo'`` file produced by :py:func:`list_wcs_coords`. Parameters ---------- img : str Name of image ota : str Name of OTA coords : str Name of coordinate file iter : int Number of desired iterations of ``msccmatch``. It is still being tested, but one might be all that is necessary, especially if using the Gaia catalog. Note ---- This function is set up to use the files in the ``illcor`` directory. The following are the parameters used by ``msccmatch``. - verbose='yes' - usebpm='no' - nsearch=250 - search=30 - rsearch=0.2 - cfrac=.5 - csig=0.1 - nfit=5 - rms=1.0 - maxshif=5.0 - fitgeom="general" - update='yes' - interac='yes' - fit='no', - accept='yes' - Stdout=1 """ image = odi.illcorpath+'illcor_'+ota+'.'+img.stem() iraf.mscred(_doprint=0) iraf.unlearn(iraf.mscred.msccmatch) for i in range(iters): fix = iraf.msccmatch(input=image, coords=odi.coordspath+coords, usebpm='no', verbose='yes', nsearch=250, search=30, rsearch=0.2, cfrac=.5, csig=0.1, nfit=5, rms=1.0, maxshif=5.0, fitgeom="general", update='yes', interac='yes', fit='no', accept='yes', Stdout=1) print 'fixing WCS for',img.f+'['+ota+'], iter ='+repr(i) print fix[-6] print fix[-5] print fix[-4] print fix[-3] print fix[-2]
# On command line, use 'x' for multiplying instead of '*' if operator == "x" : operator="*" # Check for existing output file if access(outfile,F_OK): print " Output file ", outfile, " already exists. Exiting ... \n" exit(1) if access(outfile+'.fits',F_OK): print " Output file ", outfile+'.fits', " already exists. Exiting ... \n" exit(1) # Start Pyraf currentdir = getcwd() chdir(logindir) from pyraf import iraf chdir(currentdir) # Set some IRAF settings iraf.set(imtype="fits") # Load MSCRED iraf.mscred(_doprint=0,Stdout="/dev/null") # Do imarith while keeping FITS extensions iraf.mscred.mscarith(file1,operator,file2,outfile,verbose='yes')
if operator == "x" : operator="*" # Check for existing output file if access(outfile,F_OK): print " Output file ", outfile, " already exists. Exiting ... \n" exit(1) if access(outfile+'.fits',F_OK): print " Output file ", outfile+'.fits', " already exists. Exiting ... \n" exit(1) # Start Pyraf currentdir = getcwd() chdir(logindir) from pyraf import iraf chdir(currentdir) # Set some IRAF settings iraf.set(imtype="fits") # Load MSCRED iraf.mscred(_doprint=0,Stdout="/dev/null") # Do imarith while keeping FITS extensions iraf.mscred.mscarith(file1,operator,file2,outfile,verbose='yes')