def display_image(img,frame,_z1,_z2,scale,_xcen=0.5,_ycen=0.5,_xsize=1,_ysize=1,_erase='yes'): goon='True' import glob, subprocess, os, time ds9 = subprocess.Popen("ps -U {:d} u | grep -v grep | grep ds9".format(os.getuid()),shell=True,stdout=subprocess.PIPE).stdout.readlines() if len(ds9)== 0 : subproc = subprocess.Popen('ds9',shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string,os if _z2: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon='False' else: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon=False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0='y' elif answ0=='no' or answ0=='NO': answ0='n' while answ0=='n': _z11=float(string.split(string.split(sss[0])[0],'=')[1]) _z22=float(string.split(string.split(sss[0])[1],'=')[1]) z11 = raw_input('>>> z1 = ? ['+str(_z11)+'] ? ') z22 = raw_input('>>> z2 = ? ['+str(_z22)+'] ? ') if not z11: z11=_z11 else: z11=float(z11) if not z22: z22=_z22 else: z22=float(z22) print z11,z22 sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0='y' elif answ0=='no' or answ0=='NO': answ0='n' if goon: _z1,_z2=string.split(string.split(sss[0])[0],'=')[1],string.split(string.split(sss[0])[1],'=')[1] else: print 'Warning: image '+str(img)+' not found in the directory ' return _z1,_z2,goon
def getFWHMfromFITScoo(fitsFile,cooFileFull): cr=['\n'] # Only use the first coordinate cooFile=cooFileFull+".short" os.system(" head -n 1 "+cooFileFull+" > "+cooFile) iraf.tv() iraf.imexam.setParam("imagecur",cooFile) iraf.imexam.setParam("use_display","no") results=iraf.imexam(fitsFile,frame=1,Stdout=1,Stdin=cr) # Line 0: headers for coords; 1: coords; 2: header for FWHM etc; we want 3 # Gaussian is -3rd element, Moffat -2, direct -1 moffatFWHM = results[3].split()[-2] return moffatFWHM
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars, iraf.fitskypars) iraf.apphot.phot.setParam('interactive', "no") iraf.apphot.phot.setParam('verify', "no") iraf.datapars.setParam('datamax', 50000.) iraf.datapars.setParam('gain', "gain") iraf.datapars.setParam('ccdread', "rdnoise") iraf.datapars.setParam('exposure', "exptime") iraf.datapars.setParam('airmass', "airmass") iraf.datapars.setParam('filter', "filter") iraf.datapars.setParam('obstime', "time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma', "INDEF") iraf.photpars.setParam('zmag', 0.) iraf.centerpars.setParam('cbox', 9.) iraf.centerpars.setParam('maxshift', 3.) iraf.fitskypars.setParam('salgorithm', "median") iraf.fitskypars.setParam('dannulus', 10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm == 'INDEF') good = np.where(fwhm != 'INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak == 'INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5] + '.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median( fwhm_good ) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0 * ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf', ap1x) iraf.photpars.setParam('apertures', repr(ap1x) + ', ' + repr(ap2x)) iraf.fitskypars.setParam('annulus', 4. * ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5] + '.phot') with open(image[0:-5] + '.txdump', 'w+') as txdump_out: iraf.ptools.txdump( textfiles=image[0:-5] + '.phot', fields= "id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr= 'MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5] + '.txdump', usecols=(1, 2), unpack=True) iraf_id = np.loadtxt(image[0:-5] + '.txdump', usecols=(0, ), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0, 1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[ keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos #[good] yCut = ypos #[good] idCut = iraf_id fwhmCut = fwhm #_good peakCut = peak print(peakCut.size, magCut.size, diffCut.size) print(diffCut.size, 0, np.median(diffCut), diffCut.std()) nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = list( zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist())) dtype = [('mag', float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print(peakVal) plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where( abs(peakVal - diffCut) < 2.0 * diffCut.std()) #[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print(diffCut.size, nRemoved, np.median(diffCut), diffCut.std()) if 0.05 < diffCut.std() < 0.06: nRemoved = 0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(-12, 0)) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(-12, 0)) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width / 2 # print bin_meds, bin_stds bin_hw = np.zeros_like(bin_stds) for i, bin_std in enumerate(bin_stds): if bin_std > 0.025: bin_hw[i] = 3.0 * bin_std else: bin_hw[i] = 0.075 # print len(binnumber) # for i,bin_hwi in enumerate(bin_hw): left_edge = np.array(list(zip(peakVal - bin_hw, bin_centers))) right_edge = np.flipud(np.array(list(zip(peakVal + bin_hw, bin_centers)))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # DON'T USE A PATH BECAUSE APPARENTLY IT CAN SELECT THE INVERSE SET!! WTF # print verts esRegion = Path(verts) sources = esRegion.contains_points(list(zip(diff, mag2x))) # print sources with open('escutREG_i.pos', 'w+') as f: for i, blah in enumerate(xpos[sources]): print((xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i], file=f) magCut2 = mag2x[sources] magCut1 = mag1x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut2 < -4) & (fwhmCut < 90.0)) print(np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2])) fwchk = np.where( np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std( fwhmCut[fwhmchk2]) keep = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) <= 10.0 * np.std( fwhmCut[fwhmchk2]) with open('escutVBAD_i.pos', 'w+') as f: for i, blah in enumerate(xCut[fwchk]): print((xCut[fwchk])[i], (yCut[fwchk])[i], file=f) with open('escut_i.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) with open('escut_g.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) plt.fill_betweenx(bin_centers, peakVal + bin_hw, peakVal - bin_hw, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut2[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0, -12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2, 1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut2, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut2[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([ np.median(fwhmCut) + fwhmCut.std(), np.median(fwhmCut) - fwhmCut.std() ], -12, 0, colors='red', linestyle='dotted') plt.ylim(0, 20) plt.xlim(-12, 0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return fwhmCut[keep]
def display_image(img, frame, _z1, _z2, scale, _xcen=0.5, _ycen=0.5, _xsize=1, _ysize=1, _erase='yes'): # print "LOGX:: Entering `display_image` method/function in %(__file__)s" # % globals() goon = 'True' import glob import subprocess import time import os ds9 = subprocess.Popen("ps -U" + str(os.getuid()) + "|grep -v grep | grep ds9", shell=True, stdout=subprocess.PIPE).stdout.readlines() if len(ds9) == 0: subproc = subprocess.Popen('ds9', shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string import os if _z2: try: sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2, Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = 'False' else: try: sss = iraf.display(img, frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' while answ0 == 'n': _z11 = float(string.split(string.split(sss[0])[0], '=')[1]) _z22 = float(string.split(string.split(sss[0])[1], '=')[1]) z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ') z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ') if not z11: z11 = _z11 else: z11 = float(z11) if not z22: z22 = _z22 else: z22 = float(z22) print z11, z22 sss = iraf.display(img, frame, fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' if goon: _z1, _z2 = string.split(string.split(sss[0])[0], '=')[ 1], string.split(string.split(sss[0])[1], '=')[1] else: print 'Warning: image ' + str(img) + ' not found in the directory ' return _z1, _z2, goon
def main(argv): home_root = os.environ['HOME'] iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) for file_ in os.listdir("./"): if file_.endswith("_i.fits"): fits_file_i = file_ title_string = file_[0:9] if file_.endswith("_g.fits"): fits_file_g = file_ fits_h_i = fits.open(fits_file_i) fits_h_g = fits.open(fits_file_g) fwhm_i = fits_h_i[0].header['FWHMPSF'] fwhm_g = fits_h_g[0].header['FWHMPSF'] print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i) # if not os.path.isdir('psf'): # make a psf subfolder if it doesn't exist # os.mkdir('psf') # # and then copy in the data files if they don't already exist as files # if not os.path.isfile('psf/'+fits_file_i): # iraf.images.imcopy(fits_file_i,'psf/'+fits_file_i,verbose="yes") # # if not os.path.isfile('psf/'+fits_file_g) : # iraf.images.imcopy(fits_file_g,'psf/'+fits_file_g,verbose="yes") # # # also copy in the apcor star lists # if not (os.path.isfile('psf/apcor_stars_i.txt') or os.path.islink('psf/apcor_stars_i.txt')) : # shutil.copyfile('apcor_stars_i.txt','psf/apcor_stars_i.txt') # # if not (os.path.isfile('psf/apcor_stars_g.txt') or os.path.islink('psf/apcor_stars_g.txt')) : # shutil.copyfile('apcor_stars_g.txt','psf/apcor_stars_g.txt') # # # and change to the psf folder # os.chdir('psf') iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) iraf.datapars.setParam('fwhmpsf',fwhm_g) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_g)) iraf.fitskypars.setParam('annulus',(6.*fwhm_g)) iraf.apphot.phot(image=fits_file_g, coords='apcor_stars_g.txt', output="g_psfstars.mag.1") iraf.datapars.setParam('fwhmpsf',fwhm_i) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_i)) iraf.fitskypars.setParam('annulus',(6.*fwhm_i)) iraf.apphot.phot(image=fits_file_i, coords='apcor_stars_i.txt', output="i_psfstars.mag.1") with open("apcor_stars_i.txt") as foo: lines = len(foo.readlines()) iraf.daophot(_doprint=0) iraf.pstselect.setParam('image',fits_file_i) iraf.pstselect.setParam('photfile',"i_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect(verify='no', mode='h') with open("apcor_stars_g.txt") as foo: lines = len(foo.readlines()) iraf.pstselect.setParam('image',fits_file_g) iraf.pstselect.setParam('photfile',"g_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect() iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.daopars.setParam('psfrad',iraf.nint(3.*fwhm_i)) iraf.daopars.setParam('fitrad',fwhm_i) iraf.psf.setParam('image',fits_file_i) iraf.psf.setParam('photfile',"i_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_i+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf() iraf.psf.setParam('image',fits_file_g) iraf.psf.setParam('photfile',"g_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_g+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf()
def display_image(img, frame, _z1, _z2, scale, _xcen=0.5, _ycen=0.5, _xsize=1, _ysize=1, _erase='yes'): goon = 'True' import glob, subprocess, os, time # removing u : this option does not work on mac ds9 = subprocess.Popen("ps -U {:d} | grep -v grep | grep ds9".format( os.getuid()), shell=True, stdout=subprocess.PIPE).stdout.readlines() if len(ds9) == 0: subproc = subprocess.Popen('ds9', shell=True) time.sleep(3) if glob.glob(img): from pyraf import iraf iraf.images(_doprint=0) iraf.tv(_doprint=0) import string, os if _z2: try: sss=iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ fill='yes', zscale='no', zrange='no', z1=_z1, z2=_z2,Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = 'False' else: try: sss = iraf.display(img + '[0]', frame, xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase, fill='yes', Stdout=1) except: print '' print '### ERROR: PROBLEM OPENING DS9' print '' goon = False if scale and goon: answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' while answ0 == 'n': _z11 = float(string.split(string.split(sss[0])[0], '=')[1]) _z22 = float(string.split(string.split(sss[0])[1], '=')[1]) z11 = raw_input('>>> z1 = ? [' + str(_z11) + '] ? ') z22 = raw_input('>>> z2 = ? [' + str(_z22) + '] ? ') if not z11: z11 = _z11 else: z11 = float(z11) if not z22: z22 = _z22 else: z22 = float(z22) print z11, z22 sss=iraf.display(img + '[0]',frame,fill='yes', xcen=_xcen, ycen=_ycen, xsize=_xsize, ysize=_ysize, erase=_erase,\ zrange='no', zscale='no', z1=z11, z2=z22, Stdout=1) answ0 = raw_input('>>> Cuts OK ? [y/n] ? [y] ') if not answ0: answ0 = 'y' elif answ0 == 'no' or answ0 == 'NO': answ0 = 'n' if goon: _z1, _z2 = string.split(string.split(sss[0])[0], '=')[1], string.split( string.split(sss[0])[1], '=')[1] else: print 'Warning: image ' + str(img) + ' not found in the directory ' return _z1, _z2, goon
import matplotlib.pyplot as plt import matplotlib.image as mpimg #from matplotlib.pyplot import plot, draw, show plt.ion() plt.plot(datos[200]) plt.show() print(type(datos), len(datos)) import numpy as np a = np.asarray(datos) print(a.shape) a.shape = (512, 512) print(type(a)) print(a.shape) from astropy.io import fits hdu = fits.PrimaryHDU(a) hdu.writeto('mask4.fits') from pyraf import iraf iraf.tv() iraf.tv.display('mask4.fits', 1) plt.imshow(a, cmap='gray') plt.colorbar()
def main(): fwhm = float(sys.argv[1]) filter_string = 'psmap' fwhm_string = '{:3.1f}'.format(fwhm) iraf.tv(_doprint=0) home_root = os.environ['HOME'] imexam_flag = False # print "Getting fits files..." # Load the FITS header using astropy.io.fits for file_ in os.listdir("./"): if file_.endswith("i.fits"): fits_file_i = file_ for file_ in os.listdir("./"): if file_.endswith("g.fits"): fits_file_g = file_ # downloadSDSSgal(fits_file_g, fits_file_i) fits_i = fits.open(fits_file_i) fits_g = fits.open(fits_file_g) # print "Opened fits files:",fits_file_g,"&",fits_file_i # objid = fits_i[0].header['OBJECT'] title_string = fits_file_i[0:9] # get the first 9 characters of the filename. # set up some filenames out_file = filter_string + '_' + fwhm_string + '_' + title_string + '.pdf' mark_file = 'f_list_' + filter_string + '_' + fwhm_string + '_' + title_string + '.reg' circ_file = 'c_list_' + filter_string + '_' + fwhm_string + '_' + title_string + '.reg' mag_file = "AGC249525_daophot.dat.cut" idr,rar,decr,ixr,iyr,am_g,g_ir,g_ierrr,am_i,i_ir,i_ierrr,g_magr,i_magr,gmir,chi,sharp,ebv,gfwhmr,fwhm_sr = np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18),unpack=True) gxr, gyr = ixr, iyr # read in magnitudes, colors, and positions(x,y) # mag_file = 'calibrated_mags.dat' # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True) print(len(gxr), "total stars") # filter out the things with crappy color errors mag_error_cut = 0.99 color_error_cut = np.sqrt(2.0)*mag_error_cut gmi_errr = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr))] gx = [gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] gy = [gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] g_mag = [g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] g_ierr = [g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] ix = [ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] iy = [iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] i_mag = [i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] i_ierr = [i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] gmi = [gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] gmi_err = [np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut))] # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True) pixcrd = list(zip(ix,iy)) # print "Reading WCS info from image header..." # Parse the WCS keywords in the primary HDU warnings.filterwarnings('ignore', category=UserWarning, append=True) w = wcs.WCS(fits_i[0].header) # Print out the "name" of the WCS, as defined in the FITS header # print w.wcs.name # Print out all of the settings that were parsed from the header # w.wcs.print_contents() # Convert pixel coordinates to world coordinates # The second argument is "origin" -- in this case we're declaring we # have 1-based (Fortran-like) coordinates. world = w.all_pix2world(pixcrd, 1) ra_corner, dec_corner = w.all_pix2world(0,0,1) ra_c_d,dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True) print('Corner RA:',ra_c_d,':: Corner Dec:',dec_c_d) # fwhm_i = fits_i[0].header['FWHMPSF'] # fwhm_g = fits_g[0].header['FWHMPSF'] # # print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i) fits_i.close() fits_g.close() # split the ra and dec out into individual arrays and transform to arcmin from the corner i_ra = [abs((world[i,0]-ra_corner)*60) for i in range(len(world[:,0]))] i_dec = [abs((world[i,1]-dec_corner)*60) for i in range(len(world[:,1]))] # also preserve the decimal degrees for reference i_rad = [world[i,0] for i in range(len(world[:,0]))] i_decd = [world[i,1] for i in range(len(world[:,1]))] # bin the filtered stars into a grid with pixel size XXX # print "Binning for m-M =",dm bins = 165 width = 22 # galaxyMap(fits_file_i, fwhm, -1.0, filter_file) grid, xedges, yedges = np.histogram2d(i_dec, i_ra, bins=[bins,bins], range=[[0,width],[0,width]]) hist_points = list(zip(xedges,yedges)) sig = ((bins/width)*fwhm)/2.355 pltsig = fwhm/2.0 # convolve the grid with a gaussian # print "Convolving for m-M =",dm grid_gaus = ndimage.filters.gaussian_filter(grid, sig, mode='constant', cval=0) S = np.array(grid_gaus*0) S_th = 3.0 # print grid_gaus[0:44][0:44] grid_mean = np.mean(grid_gaus) grid_sigma = np.std(grid_gaus) S = (grid_gaus-grid_mean)/grid_sigma above_th = [(int(i),int(j)) for i in range(len(S)) for j in range(len(S[i])) if (S[i][j] >= S_th)] # for i in range(143) : # for j in range(143) : # ie = i + 22 # je = j + 22 # grid_mean = np.mean(grid_gaus[i:ie,j:je]) # grid_sigma = np.std(grid_gaus[i:ie,j:je]) # S[(11+i),(11+j)] = (grid_gaus[(11+i),(11+j)]-grid_mean)/grid_sigma # # print i,j,grid_mean,grid_sigma,S[(22+i),(22+j)] # find the maximum point in the grid and center the circle there x_cent, y_cent = np.unravel_index(grid_gaus.argmax(),grid_gaus.shape) print('Max of gaussian convolved grid located at:','('+'{0:6.3f}'.format(yedges[y_cent])+','+'{0:6.3f}'.format(xedges[x_cent])+')') # print grid_gaus[x_cent][y_cent] print('Value of S at above:','{0:6.3f}'.format(S[x_cent][y_cent])) x_cent_S, y_cent_S = np.unravel_index(S.argmax(),S.shape) print('Max of S located at:','('+'{0:6.3f}'.format(yedges[y_cent_S])+','+'{0:6.3f}'.format(xedges[x_cent_S])+')') # print grid_gaus[x_cent][y_cent] print('Value of S at above:','{0:6.3f}'.format(S[x_cent_S][y_cent_S])) print('Number of bins above S_th: {0:4d}'.format(len(above_th))) # make a circle to highlight a certain region cosd = lambda x : np.cos(np.deg2rad(x)) sind = lambda x : np.sin(np.deg2rad(x)) x_circ = [yedges[y_cent] + 3.0*cosd(t) for t in range(0,359,1)] y_circ = [xedges[x_cent] + 3.0*sind(t) for t in range(0,359,1)] xy_points = list(zip(i_ra,i_dec)) verts_circ = list(zip(x_circ,y_circ)) circ_filter = Path(verts_circ) circ_c_x = ra_corner-(yedges[y_cent]/60.) circ_c_y = (xedges[x_cent]/60.)+dec_corner circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1) ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y,1) # ds9_file = 'circles_psmap_' + title_string + '.reg' # with open(ds9_file,'w+') as ds9: # print >> ds9, "fk5;circle({:f},{:f},2') # color=cyan width=2 label=psmap".format(ra_c, dec_c) stars_circ = circ_filter.contains_points(xy_points) i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])] gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])] i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])] i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])] i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])] i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])] i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])] i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])] # f2 = open(circ_file, 'w+') # for i in range(len(i_x_c)) : # print >> f2, '{0:8.2f} {1:8.2f} {2:12.8f} {3:12.8f} {4:8.2f} '.format(i_x_c[i],i_y_c[i],i_rad_c[i],i_decd_c[i],i_mag_c[i],gmi_c[i]) # f2.close() # print "{0:3d} stars in filter, {1:3d} stars in circle, {2:3d} stars in both.".format(len(i_mag_f),len(i_mag_c),len(i_mag_fc)) # for i in range(len(i_x_fc)) : # print (i_x_fc[i],i_y_fc[i]) # circ_c_x = (yedges[y_cent]/60.)+ra_c # circ_c_y = (xedges[x_cent]/60.)+dec_c # circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1) # # hi_c_ra, hi_c_dec = 142.5104167, 16.6355556 # hi_c_x, hi_c_y = abs((hi_c_ra-ra_c)*60), abs((hi_c_dec-dec_c)*60) # hi_x_circ = [hi_c_x + pltsig*cosd(t) for t in range(0,359,1)] # hi_y_circ = [hi_c_y + pltsig*sind(t) for t in range(0,359,1)] # # hi_pix_x,hi_pix_y = w.wcs_world2pix(hi_c_ra,hi_c_dec,1) # center_file = 'im_cens_'+fwhm_string+'.dat' # im_cens = open(center_file,'w+') # print >> im_cens, -circ_pix_x, circ_pix_y, 'circle_center' # print >> im_cens, hi_pix_x, hi_pix_y, 'HI_centroid' # im_cens.close() # # mark_radius = int(pltsig*60/0.11) # mark_radii = str(repr(mark_radius-2)+','+repr(mark_radius-1)+','+repr(mark_radius)+','+repr(mark_radius+1)+','+repr(mark_radius+2)) # if disp_flag : # iraf.unlearn(iraf.tv.tvmark) # iraf.tv.tvmark.setParam('label',"no") # iraf.tv.tvmark.setParam('pointsize',7) # iraf.tv.tvmark.setParam('mark',"circle") # iraf.tv.display(image=fits_file_g, frame=1) # iraf.tv.display(image=fits_file_i, frame=2) # iraf.tv.tvmark(frame=1, coords=mark_file, radii="14,15,16", color=207) # iraf.tv.tvmark(frame=1, coords=circ_file, radii="20,21,22", color=209) # iraf.tv.tvmark(frame=1, coords=center_file, txsize=4, mark="plus", color=208, label="yes") # iraf.tv.tvmark(frame=1, coords=center_file, radii=mark_radii, color=208) # iraf.tv.tvmark(frame=2, coords=mark_file, radii="14,15,16", color=207, label="yes") # iraf.tv.tvmark(frame=2, coords=circ_file, radii="20,21,22", color=209) # iraf.tv.tvmark(frame=2, coords=center_file, txsize=4, mark="plus", color=208, label="yes") # iraf.tv.tvmark(frame=2, coords=center_file, radii=mark_radii, color=208) # # setup the pdf output pp = PdfPages('psmap_'+ out_file) # plot # print "Plotting for m-M = ",dm ax0 = plt.subplot(2,2,1) plt.scatter(i_ra, i_dec, c=gmi, marker='o', s=(27-np.array(i_mag)), edgecolors='none', cmap=cm.rainbow) plt.plot(x_circ,y_circ,linestyle='-', color='magenta') plt.clim(-1,4) # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') plt.colorbar() # plt.scatter(i_ra_c, i_dec_c, color='red', marker='o', s=3, edgecolors='none') plt.ylabel('Dec (arcmin)') plt.xlim(0,max(i_ra)) plt.ylim(0,max(i_dec)) plt.title('sky positions') ax0.set_aspect('equal') ax1 = plt.subplot(2,2,2) plt.scatter(gmi, i_mag, c='black', marker='o', s=1, edgecolors='none') plt.scatter(gmi_c, i_mag_c, c='red', marker='o', s=3, edgecolors='none') plt.tick_params(axis='y',left='on',right='off',labelleft='on',labelright='off') ax1.yaxis.set_label_position('left') plt.ylabel('$i$') plt.ylim(25,15) plt.xlim(-1,4) ax1.set_aspect(0.5) ax2 = plt.subplot(2,2,3) extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] plt.imshow(S, extent=extent, interpolation='nearest', cmap=cm.viridis) cbar_S = plt.colorbar() # cbar_S.tick_params(labelsize=10) plt.plot(x_circ,y_circ,linestyle='-', color='magenta') # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') # X, Y = np.meshgrid(xedges,yedges) # ax3.pcolormesh(X,Y,grid_gaus) plt.xlabel('RA (arcmin)') plt.ylabel('Dec (arcmin)') # plt.ylabel('Dec (arcmin)') plt.xlim(0,max(i_ra)) plt.ylim(0,max(i_dec)) ax2.set_aspect('equal') ax3 = plt.subplot(2,2,4) # plt.imshow(imagedata) # extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] # plt.imshow(grid, extent=extent, interpolation='nearest') # cbar_gaus = plt.colorbar() # # cbar_gaus.tick_params(labelsize=10) # plt.plot(x_circ,y_circ,linestyle='-', color='magenta') # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') # plt.xlabel('RA (arcmin)') # plt.ylabel('Dec (arcmin)') # plt.xlim(0,max(i_ra)) # plt.ylim(0,max(i_dec)) plt.scatter(gmi_c, i_mag_c, color='red', marker='o', s=3, edgecolors='none') plt.tick_params(axis='y',left='on',right='off',labelleft='on',labelright='off') ax0.yaxis.set_label_position('left') plt.xlabel('$(g-i)$') plt.ylabel('$i$') plt.ylim(25,15) plt.xlim(-1,4) ax3.set_aspect(0.5) plt.suptitle(title_string+ ' -- ' + fwhm_string + ' arcmin smoothing') plt.show pp.savefig() pp.close()
coordFile = open(objname + '_' + filter_ + '.fits' + '.coo.1') except: coordFile = open(objname + '_' + filter_ + '_sh.fits' + '.coo.1') coord = coordFile.read() coordLines = coord.splitlines() fwhm = float(coordLines[9].split()[3]) sigma = float(coordLines[19].split()[3]) threshold = float(coordLines[27].split()[3]) coordFile.close() print(objname, filter_, fwhm, sigma, threshold) print('creating psf...') iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.daophot(_doprint=0) iraf.tables(_doprint=0) iraf.unlearn(iraf.ptools.pselect, iraf.tables.tcreate, iraf.tables.tmatch, iraf.tables.tinfo, iraf.tables.tdump) # first measure the psf for the image iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars, iraf.fitskypars)
#! /usr/local/bin/python import os, sys import numpy as np from pyraf import iraf from odi_calibrate import download_sdss, js_calibrate from sdss_fit import getVabs from collections import OrderedDict iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) def getHImass(object, dm): # print object, mpc uchvcdb = os.path.dirname(os.path.abspath(__file__))+'/predblist.sort.csv' name, mass = np.loadtxt(uchvcdb, usecols=(1,6), dtype=str, delimiter=',', unpack=True) # find the right row coord = [i for i,this in enumerate(mass) if object.upper() in name[i]][0] # print 'the HI mass of', name[coord], 'is', mass[coord], 'at 1 Mpc' # mpc = mpc/1000. mpc = pow(10,((dm + 5.)/5.))/1000000. logm = float(mass[coord]) mass = mpc*mpc*10**logm # make sure to scale by the distance in Mpc^2 print '{:3.1f}'.format(np.log10(mass))
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',50000.) iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm=='INDEF') good = np.where(fwhm!='INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak=='INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5]+'.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median(fwhm_good) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0*ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf',ap1x) iraf.photpars.setParam('apertures',repr(ap1x)+', '+repr(ap2x)) iraf.fitskypars.setParam('annulus',4.*ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5]+'.phot') with open(image[0:-5]+'.txdump','w+') as txdump_out : iraf.ptools.txdump(textfiles=image[0:-5]+'.phot', fields="id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr='MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5]+'.txdump', usecols=(1,2), unpack=True) iraf_id = np.loadtxt(image[0:-5]+'.txdump', usecols=(0,), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0,1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos#[good] yCut = ypos#[good] idCut = iraf_id fwhmCut = fwhm#_good peakCut = peak print peakCut.size, magCut.size, diffCut.size print diffCut.size, 0, np.median(diffCut), diffCut.std() nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist()) dtype = [('mag',float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print peakVal plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where(abs(peakVal-diffCut) < 2.5*diffCut.std())#[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print diffCut.size, nRemoved, np.median(diffCut), diffCut.std() if 0.05 < diffCut.std() <0.06: nRemoved=0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(magCut.min(),magCut.max())) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(magCut.min(),magCut.max())) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width/2 # print bin_meds, bin_stds left_edge = np.array(zip(peakVal-2.5*bin_stds, bin_centers)) right_edge = np.flipud(np.array(zip(peakVal+2.5*bin_stds, bin_centers))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # print verts esRegion = Path(verts) sources = esRegion.contains_points(zip(diff,mag2x)) # print sources with open('escutREG_i.pos','w+') as f: for i,blah in enumerate(xpos[sources]): print >> f, (xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i] magCut = mag2x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut<-4) & (fwhmCut<90.0)) print np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2]) fwchk = np.where(np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2]) # fwmag = mag2x[sources] with open('escutVBAD_i.pos','w+') as f: for i,blah in enumerate(xCut[fwchk]): print >> f, (xCut[fwchk])[i], (yCut[fwchk])[i] with open('escut_r.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] with open('escut_g.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] plt.fill_betweenx(bin_centers, peakVal+2.5*bin_stds, peakVal-2.5*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0,-12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2,1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([np.median(fwhmCut)+fwhmCut.std(), np.median(fwhmCut)-fwhmCut.std()], -12, 0, colors='red', linestyle='dotted') plt.ylim(0,20) plt.xlim(-12,0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return True
A comparative login.cl to pyraf """ from pyraf import iraf #load usefull packages for exoplanet reduction iraf.stsdas() iraf.toolbox() iraf.noao() iraf.astutil() iraf.proto() iraf.imred() iraf.ccdred() iraf.digiphot() iraf.apphot() iraf.images() iraf.tv() iraf.imutil() iraf.headers() iraf.immatch() iraf.crutil() #****************************************************************************** #******************** BEGIN CONFIGURATION ************************************* #****************************************************************************** #*********************************************************************** IMEXAM #give the default conditions for imexamine iraf.imexamine.unlearn() iraf.imexamine.frame = 1 iraf.imexamine.nloutput = 101 iraf.imexamine.ncoutput = 101
def main(): fwhm = float(sys.argv[1]) filter_string = 'psmap' fwhm_string = '{:3.1f}'.format(fwhm) iraf.tv(_doprint=0) home_root = os.environ['HOME'] imexam_flag = False # print "Getting fits files..." # Load the FITS header using astropy.io.fits for file_ in os.listdir("./"): if file_.endswith("i.fits"): fits_file_i = file_ for file_ in os.listdir("./"): if file_.endswith("g.fits"): fits_file_g = file_ # downloadSDSSgal(fits_file_g, fits_file_i) fits_i = fits.open(fits_file_i) fits_g = fits.open(fits_file_g) # print "Opened fits files:",fits_file_g,"&",fits_file_i # objid = fits_i[0].header['OBJECT'] title_string = fits_file_i[ 0:9] # get the first 9 characters of the filename. # set up some filenames out_file = filter_string + '_' + fwhm_string + '_' + title_string + '.pdf' mark_file = 'f_list_' + filter_string + '_' + fwhm_string + '_' + title_string + '.reg' circ_file = 'c_list_' + filter_string + '_' + fwhm_string + '_' + title_string + '.reg' mag_file = "AGC249525_daophot.dat.cut" idr, rar, decr, ixr, iyr, am_g, g_ir, g_ierrr, am_i, i_ir, i_ierrr, g_magr, i_magr, gmir, chi, sharp, ebv, gfwhmr, fwhm_sr = np.loadtxt( mag_file, usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18), unpack=True) gxr, gyr = ixr, iyr # read in magnitudes, colors, and positions(x,y) # mag_file = 'calibrated_mags.dat' # gxr,gyr,g_magr,g_ierrr,ixr,iyr,i_magr,i_ierrr,gmir= np.loadtxt(mag_file,usecols=(0,1,2,3,4,5,6,7,8),unpack=True) print((len(gxr), "total stars")) # filter out the things with crappy color errors mag_error_cut = 0.99 color_error_cut = np.sqrt(2.0) * mag_error_cut gmi_errr = [ np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) ] gx = [ gxr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] gy = [ gyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] g_mag = [ g_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] g_ierr = [ g_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] ix = [ ixr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] iy = [ iyr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] i_mag = [ i_magr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] i_ierr = [ i_ierrr[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] gmi = [ gmir[i] for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] gmi_err = [ np.sqrt(g_ierrr[i]**2 + i_ierrr[i]**2) for i in range(len(gxr)) if (abs(gmi_errr[i] < color_error_cut and i_ierrr[i] < mag_error_cut)) ] # nid = np.loadtxt(mag_file,usecols=(0,),dtype=int,unpack=True) pixcrd = list(zip(ix, iy)) # print "Reading WCS info from image header..." # Parse the WCS keywords in the primary HDU warnings.filterwarnings('ignore', category=UserWarning, append=True) w = wcs.WCS(fits_i[0].header) # Print out the "name" of the WCS, as defined in the FITS header # print w.wcs.name # Print out all of the settings that were parsed from the header # w.wcs.print_contents() # Convert pixel coordinates to world coordinates # The second argument is "origin" -- in this case we're declaring we # have 1-based (Fortran-like) coordinates. world = w.all_pix2world(pixcrd, 1) ra_corner, dec_corner = w.all_pix2world(0, 0, 1) ra_c_d, dec_c_d = deg2HMS(ra=ra_corner, dec=dec_corner, round=True) print(('Corner RA:', ra_c_d, ':: Corner Dec:', dec_c_d)) # fwhm_i = fits_i[0].header['FWHMPSF'] # fwhm_g = fits_g[0].header['FWHMPSF'] # # print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i) fits_i.close() fits_g.close() # split the ra and dec out into individual arrays and transform to arcmin from the corner i_ra = [ abs((world[i, 0] - ra_corner) * 60) for i in range(len(world[:, 0])) ] i_dec = [ abs((world[i, 1] - dec_corner) * 60) for i in range(len(world[:, 1])) ] # also preserve the decimal degrees for reference i_rad = [world[i, 0] for i in range(len(world[:, 0]))] i_decd = [world[i, 1] for i in range(len(world[:, 1]))] # bin the filtered stars into a grid with pixel size XXX # print "Binning for m-M =",dm bins = 165 width = 22 # galaxyMap(fits_file_i, fwhm, -1.0, filter_file) grid, xedges, yedges = np.histogram2d(i_dec, i_ra, bins=[bins, bins], range=[[0, width], [0, width]]) hist_points = list(zip(xedges, yedges)) sig = ((bins / width) * fwhm) / 2.355 pltsig = fwhm / 2.0 # convolve the grid with a gaussian # print "Convolving for m-M =",dm grid_gaus = ndimage.filters.gaussian_filter(grid, sig, mode='constant', cval=0) S = np.array(grid_gaus * 0) S_th = 3.0 # print grid_gaus[0:44][0:44] grid_mean = np.mean(grid_gaus) grid_sigma = np.std(grid_gaus) S = (grid_gaus - grid_mean) / grid_sigma above_th = [(int(i), int(j)) for i in range(len(S)) for j in range(len(S[i])) if (S[i][j] >= S_th)] # for i in range(143) : # for j in range(143) : # ie = i + 22 # je = j + 22 # grid_mean = np.mean(grid_gaus[i:ie,j:je]) # grid_sigma = np.std(grid_gaus[i:ie,j:je]) # S[(11+i),(11+j)] = (grid_gaus[(11+i),(11+j)]-grid_mean)/grid_sigma # # print i,j,grid_mean,grid_sigma,S[(22+i),(22+j)] # find the maximum point in the grid and center the circle there x_cent, y_cent = np.unravel_index(grid_gaus.argmax(), grid_gaus.shape) print(('Max of gaussian convolved grid located at:', '(' + '{0:6.3f}'.format(yedges[y_cent]) + ',' + '{0:6.3f}'.format(xedges[x_cent]) + ')')) # print grid_gaus[x_cent][y_cent] print(('Value of S at above:', '{0:6.3f}'.format(S[x_cent][y_cent]))) x_cent_S, y_cent_S = np.unravel_index(S.argmax(), S.shape) print(('Max of S located at:', '(' + '{0:6.3f}'.format(yedges[y_cent_S]) + ',' + '{0:6.3f}'.format(xedges[x_cent_S]) + ')')) # print grid_gaus[x_cent][y_cent] print(('Value of S at above:', '{0:6.3f}'.format(S[x_cent_S][y_cent_S]))) print(('Number of bins above S_th: {0:4d}'.format(len(above_th)))) # make a circle to highlight a certain region cosd = lambda x: np.cos(np.deg2rad(x)) sind = lambda x: np.sin(np.deg2rad(x)) x_circ = [yedges[y_cent] + 3.0 * cosd(t) for t in range(0, 359, 1)] y_circ = [xedges[x_cent] + 3.0 * sind(t) for t in range(0, 359, 1)] xy_points = list(zip(i_ra, i_dec)) verts_circ = list(zip(x_circ, y_circ)) circ_filter = Path(verts_circ) circ_c_x = ra_corner - (yedges[y_cent] / 60.) circ_c_y = (xedges[x_cent] / 60.) + dec_corner circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x, circ_c_y, 1) ra_c, dec_c = w.all_pix2world(circ_pix_x, circ_pix_y, 1) # ds9_file = 'circles_psmap_' + title_string + '.reg' # with open(ds9_file,'w+') as ds9: # print >> ds9, "fk5;circle({:f},{:f},2') # color=cyan width=2 label=psmap".format(ra_c, dec_c) stars_circ = circ_filter.contains_points(xy_points) i_mag_c = [i_mag[i] for i in range(len(i_mag)) if (stars_circ[i])] gmi_c = [gmi[i] for i in range(len(i_mag)) if (stars_circ[i])] i_ra_c = [i_ra[i] for i in range(len(i_mag)) if (stars_circ[i])] i_dec_c = [i_dec[i] for i in range(len(i_mag)) if (stars_circ[i])] i_rad_c = [i_rad[i] for i in range(len(i_mag)) if (stars_circ[i])] i_decd_c = [i_decd[i] for i in range(len(i_mag)) if (stars_circ[i])] i_x_c = [ix[i] for i in range(len(i_mag)) if (stars_circ[i])] i_y_c = [iy[i] for i in range(len(i_mag)) if (stars_circ[i])] # f2 = open(circ_file, 'w+') # for i in range(len(i_x_c)) : # print >> f2, '{0:8.2f} {1:8.2f} {2:12.8f} {3:12.8f} {4:8.2f} '.format(i_x_c[i],i_y_c[i],i_rad_c[i],i_decd_c[i],i_mag_c[i],gmi_c[i]) # f2.close() # print "{0:3d} stars in filter, {1:3d} stars in circle, {2:3d} stars in both.".format(len(i_mag_f),len(i_mag_c),len(i_mag_fc)) # for i in range(len(i_x_fc)) : # print (i_x_fc[i],i_y_fc[i]) # circ_c_x = (yedges[y_cent]/60.)+ra_c # circ_c_y = (xedges[x_cent]/60.)+dec_c # circ_pix_x, circ_pix_y = w.wcs_world2pix(circ_c_x,circ_c_y,1) # # hi_c_ra, hi_c_dec = 142.5104167, 16.6355556 # hi_c_x, hi_c_y = abs((hi_c_ra-ra_c)*60), abs((hi_c_dec-dec_c)*60) # hi_x_circ = [hi_c_x + pltsig*cosd(t) for t in range(0,359,1)] # hi_y_circ = [hi_c_y + pltsig*sind(t) for t in range(0,359,1)] # # hi_pix_x,hi_pix_y = w.wcs_world2pix(hi_c_ra,hi_c_dec,1) # center_file = 'im_cens_'+fwhm_string+'.dat' # im_cens = open(center_file,'w+') # print >> im_cens, -circ_pix_x, circ_pix_y, 'circle_center' # print >> im_cens, hi_pix_x, hi_pix_y, 'HI_centroid' # im_cens.close() # # mark_radius = int(pltsig*60/0.11) # mark_radii = str(repr(mark_radius-2)+','+repr(mark_radius-1)+','+repr(mark_radius)+','+repr(mark_radius+1)+','+repr(mark_radius+2)) # if disp_flag : # iraf.unlearn(iraf.tv.tvmark) # iraf.tv.tvmark.setParam('label',"no") # iraf.tv.tvmark.setParam('pointsize',7) # iraf.tv.tvmark.setParam('mark',"circle") # iraf.tv.display(image=fits_file_g, frame=1) # iraf.tv.display(image=fits_file_i, frame=2) # iraf.tv.tvmark(frame=1, coords=mark_file, radii="14,15,16", color=207) # iraf.tv.tvmark(frame=1, coords=circ_file, radii="20,21,22", color=209) # iraf.tv.tvmark(frame=1, coords=center_file, txsize=4, mark="plus", color=208, label="yes") # iraf.tv.tvmark(frame=1, coords=center_file, radii=mark_radii, color=208) # iraf.tv.tvmark(frame=2, coords=mark_file, radii="14,15,16", color=207, label="yes") # iraf.tv.tvmark(frame=2, coords=circ_file, radii="20,21,22", color=209) # iraf.tv.tvmark(frame=2, coords=center_file, txsize=4, mark="plus", color=208, label="yes") # iraf.tv.tvmark(frame=2, coords=center_file, radii=mark_radii, color=208) # # setup the pdf output pp = PdfPages('psmap_' + out_file) # plot # print "Plotting for m-M = ",dm ax0 = plt.subplot(2, 2, 1) plt.scatter(i_ra, i_dec, c=gmi, marker='o', s=(27 - np.array(i_mag)), edgecolors='none', cmap=cm.rainbow) plt.plot(x_circ, y_circ, linestyle='-', color='magenta') plt.clim(-1, 4) # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') plt.colorbar() # plt.scatter(i_ra_c, i_dec_c, color='red', marker='o', s=3, edgecolors='none') plt.ylabel('Dec (arcmin)') plt.xlim(0, max(i_ra)) plt.ylim(0, max(i_dec)) plt.title('sky positions') ax0.set_aspect('equal') ax1 = plt.subplot(2, 2, 2) plt.scatter(gmi, i_mag, c='black', marker='o', s=1, edgecolors='none') plt.scatter(gmi_c, i_mag_c, c='red', marker='o', s=3, edgecolors='none') plt.tick_params(axis='y', left='on', right='off', labelleft='on', labelright='off') ax1.yaxis.set_label_position('left') plt.ylabel('$i$') plt.ylim(25, 15) plt.xlim(-1, 4) ax1.set_aspect(0.5) ax2 = plt.subplot(2, 2, 3) extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] plt.imshow(S, extent=extent, interpolation='nearest', cmap=cm.viridis) cbar_S = plt.colorbar() # cbar_S.tick_params(labelsize=10) plt.plot(x_circ, y_circ, linestyle='-', color='magenta') # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') # X, Y = np.meshgrid(xedges,yedges) # ax3.pcolormesh(X,Y,grid_gaus) plt.xlabel('RA (arcmin)') plt.ylabel('Dec (arcmin)') # plt.ylabel('Dec (arcmin)') plt.xlim(0, max(i_ra)) plt.ylim(0, max(i_dec)) ax2.set_aspect('equal') ax3 = plt.subplot(2, 2, 4) # plt.imshow(imagedata) # extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] # plt.imshow(grid, extent=extent, interpolation='nearest') # cbar_gaus = plt.colorbar() # # cbar_gaus.tick_params(labelsize=10) # plt.plot(x_circ,y_circ,linestyle='-', color='magenta') # plt.plot(hi_x_circ,hi_y_circ,linestyle='-', color='black') # plt.xlabel('RA (arcmin)') # plt.ylabel('Dec (arcmin)') # plt.xlim(0,max(i_ra)) # plt.ylim(0,max(i_dec)) plt.scatter(gmi_c, i_mag_c, color='red', marker='o', s=3, edgecolors='none') plt.tick_params(axis='y', left='on', right='off', labelleft='on', labelright='off') ax0.yaxis.set_label_position('left') plt.xlabel('$(g-i)$') plt.ylabel('$i$') plt.ylim(25, 15) plt.xlim(-1, 4) ax3.set_aspect(0.5) plt.suptitle(title_string + ' -- ' + fwhm_string + ' arcmin smoothing') plt.show pp.savefig() pp.close()