def compile_stats_pointing(): ra = 0 dec = 0 out = open("/tmp/pointing", "w") out.write("#f, imtype, obj, jd, filter, radeg, decdeg, dra, ddec\n") myfiles = glob.glob("/scr2/sedm/phot/20160616/a_*[0-9].fits") myfiles.sort() for f in myfiles: #try: imtype = fitsutils.get_par(f, "IMGTYPE") newra = fitsutils.get_par(f, "OBJRA") newdec = fitsutils.get_par(f, "OBJDEC") newra, newdec = cc.hour2deg(newra, newdec) myfilter = fitsutils.get_par(f, "FILTER") if (imtype == "ACQUISITION" or imtype == "SCIENCE"):#: and np.round(ra, 2) != np.round(newra, 2) and np.round(dec, 2) != np.round(newdec, 2): obj = fitsutils.get_par(f, "OBJECT") jd = fitsutils.get_par(f, "JD") status, dra, ddec = recenter_ifu.get_offset_center(f, plot=False, interactive=False) print f, ra, newra, imtype, jd, dra, ddec out.write("%s,%s,%s,%.2f,%s,%.5f,%.5f,%.2f,%.2f\n"%(f, imtype, obj, jd, myfilter, newra, newdec, dra, ddec)) ra = newra dec = newdec #except: # pass out.close()
def compile_stats_pointing(): ra = 0 dec = 0 out = open("/tmp/pointing", "w") out.write("#f, imtype, obj, jd, filter, radeg, decdeg, dra, ddec\n") myfiles = glob.glob(_photpath + "/20160616/a_*[0-9].fits") myfiles.sort() for f in myfiles: #try: imtype = fitsutils.get_par(f, "IMGTYPE") imtype = imtype.upper() newra = fitsutils.get_par(f, "OBJRA") newdec = fitsutils.get_par(f, "OBJDEC") newra, newdec = cc.hour2deg(newra, newdec) myfilter = fitsutils.get_par(f, "FILTER") if ( "ACQU" in imtype or imtype == "SCIENCE" ): #: and np.round(ra, 2) != np.round(newra, 2) and np.round(dec, 2) != np.round(newdec, 2): obj = fitsutils.get_par(f, "OBJECT") jd = fitsutils.get_par(f, "JD") status, dra, ddec = recenter_ifu.get_offset_center( f, plot=False, interactive=False) print f, ra, newra, imtype, jd, dra, ddec out.write("%s,%s,%s,%.2f,%s,%.5f,%.5f,%.2f,%.2f\n" % (f, imtype, obj, jd, myfilter, newra, newdec, dra, ddec)) ra = newra dec = newdec #except: # pass out.close()
def __fill_ifu_dic(ifu_img, ifu_dic={}): ''' Fills some of the parameters for the IFU image that will be used to gather its guider images later. ''' imgtype = fitsutils.get_par(ifu_img, "IMGTYPE") if not imgtype is None: imgtype = imgtype.upper() #In Richard's pipeline, the JD is the beginning of the exposure, #in Nick's one is the end. pipeline_jd_end = fitsutils.get_par(ifu_img, "TELESCOP") == '60' if imgtype == "SCIENCE" or imgtype == "STANDARD": if pipeline_jd_end: jd_ini = fitsutils.get_par(ifu_img, "JD") - fitsutils.get_par( ifu_img, "EXPTIME") / (24 * 3600.) jd_end = fitsutils.get_par(ifu_img, "JD") else: jd_ini = fitsutils.get_par(ifu_img, "JD") jd_end = fitsutils.get_par(ifu_img, "JD") + fitsutils.get_par( ifu_img, "EXPTIME") / (24 * 3600.) #We only fill the dictionary if the exposure is a valid science or standard image. name = fitsutils.get_par(ifu_img, "OBJECT") ra = fitsutils.get_par(ifu_img, "RA") dec = fitsutils.get_par(ifu_img, "DEC") rad, decd = cc.hour2deg(ra, dec) exptime = fitsutils.get_par(ifu_img, "EXPTIME") ifu_dic[ifu_img] = (name, jd_ini, jd_end, rad, decd, exptime) else: print("Image %s is not SCIENCE or STANDARD." % ifu_img)
def is_on_target(image): ''' Add as a parameter whether the image is on target or not. ''' import coordinates_conversor as cc ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) impf = fits.open(image) w = wcs.WCS(impf[0].header) filt = fitsutils.get_par(image, "FILTER") #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] pra, pdec = get_xy_coords(image, ra,dec) shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): fitsutils.update_par(image, "ONTARGET", 1) return True else: fitsutils.update_par(image, "ONTARGET", 0) return False
def is_on_target(image): ''' Add as a parameter whether the image is on target or not. ''' import coordinates_conversor as cc ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) impf = fits.open(image) w = wcs.WCS(impf[0].header) filt = fitsutils.get_par(image, "FILTER") #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] pra, pdec = get_xy_coords(image, ra, dec) shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): fitsutils.update_par(image, "ONTARGET", 1) return True else: fitsutils.update_par(image, "ONTARGET", 0) return False
def simple_finder_astro(myfile, findername, searchrad=28. / 3600): hdulist = pf.open(myfile)[0] img = hdulist.data * 1. name = fitsutils.get_par(myfile, "NAME") filter = fitsutils.get_par(myfile, "FILTER") ra, dec = coordinates_conversor.hour2deg( fitsutils.get_par(myfile, "OBJRA"), fitsutils.get_par(myfile, "OBJDEC")) wcs = WCS(hdulist.header) target_pix = wcs.wcs_world2pix([(np.array([ra, dec], np.float_))], 1)[0] corner_pix = wcs.wcs_world2pix( [(np.array([ra + searchrad, dec + searchrad], np.float_))], 1)[0] X = int(target_pix[0]) Y = int(target_pix[1]) #Size of the finder in pixels dx = int(np.abs(np.ceil(corner_pix[0] - target_pix[0]))) dy = int(np.abs(np.ceil(corner_pix[1] - target_pix[1]))) #size = int( (searchrad/0.394)/2) #zmin, zmax = zscale.zscale() newimg = img[X - dx:X + dx, Y - dy:Y + dy] zmin = np.percentile(newimg.flatten(), 5) zmax = np.percentile(newimg.flatten(), 98.5) print("X %d Y %d Size %d, %d zmin=%.2f zmax=%.2f. Size = %s" % (X, Y, dx, dy, zmin, zmax, newimg.shape)) from astropy.visualization.wcsaxes import SphericalCircle plt.figure(figsize=(10, 9)) ax = plt.subplot(projection=wcs) ax.imshow(np.flip(newimg, axis=0), \ origin="lower", cmap=plt.get_cmap('gray'), vmin=zmin, vmax=zmax) r = SphericalCircle((ra * u.deg, dec * u.deg), 5. / 3600 * u.degree, edgecolor='red', facecolor='none', transform=ax.get_transform('fk5')) ax.add_patch(r) #ax = plt.gca() #ax.scatter(ra, dec, transform=ax.get_transform('fk5'), s=20, # edgecolor='red', facecolor='none') #plt.plot(dy, dx, "+", color="r", ms=20, mfc=None, mew=2) #plt.plot(Y, X, "+", color="r", ms=20, mfc=None, mew=2) #plt.xlim(Y-dy, Y+dy, X-dx, X+dx) plt.savefig(findername) print("Created ", findername)
def finder(myfile,searchrad=0.2/60.): ra, dec = coordinates_conversor.hour2deg(fitsutils.get_par(myfile, "OBJRA"), fitsutils.get_par(myfile, "OBJDEC")) hdulist = pf.open(myfile)[0] img = hdulist.data * 1. img = img.T wcs = pywcs.WCS(hdulist.header) target_pix = wcs.wcs_sky2pix([(np.array([ra,dec], np.float_))], 1)[0] corner_pix = wcs.wcs_sky2pix([(np.array([ra,dec+searchrad], np.float_))], 1)[0] dx = int(np.abs(np.ceil(corner_pix[1] - target_pix[1]))) imgslice = img[int(target_pix[0])-2*dx:int(target_pix[0])+2*dx, int(target_pix[1])-2*dx:int(target_pix[1])+2*dx] #zmin, zmax = zscale.zscale() zmin = np.percentile(imgslice.flatten(), 5) zmax = np.percentile(imgslice.flatten(), 98) print "Min: %.1f, max: %.1f"%(zmin, zmax) gc = aplpy.FITSFigure(myfile, figsize=(10,9), north=True) gc.show_grayscale(vmin=zmin, vmax=zmax, smooth=1, kernel="gauss") gc.show_scalebar(0.1/60.) gc.scalebar.set_label('10 arcsec') gc.scalebar.set_color('white') gc.recenter(ra, dec, searchrad) #gc.show_markers(ra,dec+searchrad/20.,edgecolor='red',facecolor='none',marker="|",s=250, lw=10) #gc.show_markers(ra-(searchrad/20.)/np.cos(np.deg2rad(dec)),dec,edgecolor='red',facecolor='none',marker="_",s=250, lw=10) ras = np.array([ra , ra]) decs = np.array([dec, dec]) dxs = np.array([0, searchrad/10 / np.cos(np.deg2rad(dec))]) dys = np.array([searchrad/10, 0]) gc.show_arrows(ras, decs, dxs, dys, edgecolor="red", facecolor="red", head_width=0) ras = np.array([ra+searchrad*0.7/ np.cos(np.deg2rad(dec)), ra+searchrad*0.7/ np.cos(np.deg2rad(dec))]) decs = np.array([dec-searchrad*0.9, dec-searchrad*0.9]) dxs = np.array([0, searchrad/5 / np.cos(np.deg2rad(dec))]) dys = np.array([searchrad/5, 0]) gc.show_arrows(ras, decs, dxs, dys, edgecolor="k", facecolor="k") gc.add_label(ras[0]+dxs[0]*1.1, decs[0]+dys[0]*1.1, 'N', relative=False, color="k", horizontalalignment="center") gc.add_label(ras[1]+dxs[1]*1.1, decs[1]+dys[1]*1.1, 'E', relative=False, color="k", horizontalalignment="center") name = fitsutils.get_par(myfile, "NAME") filter = fitsutils.get_par(myfile, "FILTER") gc.add_label(0.05, 0.95, 'Object: %s'%(name), relative=True, color="white", horizontalalignment="left") gc.add_label(0.05, 0.9, 'Coordinates: RA=%s DEC=%s'%(coordinates_conversor.deg2hour(ra, dec)), relative=True, color="white", horizontalalignment="left") gc.add_label(0.05, 0.84, 'Filter: SDSS %s'%filter, relative=True, color="white", horizontalalignment="left") findername = 'finders/finder_%s_%s.jpg'%(name, filter) gc.save(findername) return findername
def update_phot_blagorodnova(image): ''' Updates the DB with the aperture photometry extrated from the fits and the interpolated zeropoint. ''' racen, deccen = cc.hour2deg(fitsutils.get_par(image, "OBJRA"), fitsutils.get_par(image, "OBJRA")) issub = 'f' refsys = 'SDSSinterpolated' filt = fitsutils.get_par(image, "FILTER") exptime = fitsutils.get_par(image, "EXPTIME") utdate = time_utils.jd2utc(fitsutils.get_par(image, "JD"), string=True) mag = np.round(fitsutils.get_par(image, "APPMAG"), 3) magerr = np.round(fitsutils.get_par(image, "APPMAGER"), 3) limmag = np.round(fitsutils.get_par(image, "ZEROPT"), 3) name = fitsutils.get_par(image, "NAME").replace("PTF", "") observer = 'SEDMachine' reducedby = 'Blagorodnova Automated Pipeline SEDM' if mag == 0 or mag > limmag or mag > 22: mag = 99 magerr = 99 db = pg.DB(dbname='ptftransient', user='******', passwd='followup', host='yupana.caltech.edu') getsrcidquery = "SELECT id from sources where name='%s'" % name result = db.query(getsrcidquery) for row in result.dictresult(): srcid = int(row['id']) query3 = "SELECT id from phot WHERE sourceid=%d and instrumentid=64 and obsdate='%s' and filter='%s' and reducedby='Blagorodnova Automated Pipeline SEDM';" % ( srcid, utdate, filter) result = db.query(query3) print query3 if len(result.dictresult()) == 0: print srcid, racen, deccen, utdate, exptime, filt, mag, magerr, limmag, issub, refsys, observer, reducedby query2 = "INSERT INTO phot (sourceid,programid,instrumentid,ra,dec,obsdate,exptime,filter,mag,emag,limmag,issub,refsys,observer,reducedby) VALUES " query2 += "(%d,1,64,%f,%f,'%s',%f,'%s',%f,%f,%f,'%s','%s','%s','%s');" % ( srcid, racen, deccen, utdate, exptime, filt, mag, magerr, limmag, issub, refsys, observer, reducedby) print query2 db.query(query2) else: for row in result.dictresult(): photid = int(row['id']) query2 = "DELETE from phot WHERE id=%d;" % (photid) print query2 db.query(query2) query2 = "INSERT INTO phot (sourceid,programid,instrumentid,ra,dec,obsdate,exptime,filter,mag,emag,limmag,issub,refsys,observer,reducedby) VALUES " query2 += "(%d,1,64,%f,%f,'%s',%f,'%s',%f,%f,%f,'%s','%s','%s','%s');" % ( srcid, racen, deccen, utdate, exptime, filt, mag, magerr, limmag, issub, refsys, observer, reducedby) print query2 db.query(query2)
def finder(myfile,searchrad=0.2/60.): ra, dec = coordinates_conversor.hour2deg(fitsutils.get_par(myfile, "RA"), fitsutils.get_par(myfile, "DEC")) hdulist = pf.open(myfile)[0] img = hdulist.data * 1. wcs = pywcs.WCS(hdulist.header) target_pix = wcs.wcs_sky2pix([(np.array([ra,dec], np.float_))], 1)[0] corner_pix = wcs.wcs_sky2pix([(np.array([ra,dec+searchrad], np.float_))], 1)[0] dx = int(np.abs(np.ceil(corner_pix[1] - target_pix[1]))) imgslice = img[int(target_pix[0])-2*dx:int(target_pix[0])+2*dx, int(target_pix[1])-2*dx:int(target_pix[1])+2*dx] #zmin, zmax = zscale.zscale() zmin = np.percentile(imgslice.flatten(), 5) zmax = np.percentile(imgslice.flatten(), 99) gc = aplpy.FITSFigure(myfile, figsize=(10,9), north=True) gc.show_grayscale(vmin=zmin, vmax=zmax) gc.show_scalebar(0.1/60.) gc.scalebar.set_label('10 arcsec') gc.scalebar.set_color('white') gc.recenter(ra, dec, searchrad) gc.show_markers(ra,dec+searchrad/20.,edgecolor='red',facecolor='none',marker="|",s=250, lw=10) gc.show_markers(ra-(searchrad/20.)/np.cos(np.deg2rad(dec)),dec,edgecolor='red',facecolor='none',marker="_",s=250, lw=10) ras = np.array([ra , ra]) decs = np.array([dec, dec]) dxs = np.array([0, searchrad/10 / np.cos(np.deg2rad(dec))]) dys = np.array([searchrad/10, 0]) gc.show_arrows(ras, decs, dxs, dys, edgecolor="red", facecolor="red", head_width=0) ras = np.array([ra+searchrad*0.7/ np.cos(np.deg2rad(dec)), ra+searchrad*0.7/ np.cos(np.deg2rad(dec))]) decs = np.array([dec-searchrad*0.9, dec-searchrad*0.9]) dxs = np.array([0, searchrad/5 / np.cos(np.deg2rad(dec))]) dys = np.array([searchrad/5, 0]) gc.show_arrows(ras, decs, dxs, dys, edgecolor="k", facecolor="k") gc.add_label(ras[0]+dxs[0]*1.1, decs[0]+dys[0]*1.1, 'N', relative=False, color="k", horizontalalignment="center") gc.add_label(ras[1]+dxs[1]*1.1, decs[1]+dys[1]*1.1, 'E', relative=False, color="k", horizontalalignment="center") name = fitsutils.get_par(myfile, "OBJECT") gc.add_label(0.05, 0.95, 'Object: %s'%(name), relative=True, color="white", horizontalalignment="left") gc.add_label(0.05, 0.9, 'Coordinates: RA=%s DEC=%s'%(coordinates_conversor.deg2hour(ra, dec)), relative=True, color="white", horizontalalignment="left") gc.add_label(0.05, 0.84, 'Filter: SDSS r', relative=True, color="white", horizontalalignment="left") findername = '%s_finder.jpg'%(name) gc.save(findername) return findername
def get_offset_center(f, plot=False, interactive=False): ''' Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go from the current pointing position, to the coodinates of the object specified in the fits file. ''' if(not os.path.isfile(f)): print "File %s does not exist! Returning Zero offsets..."%f return -1, 0,0 else: image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['OBJRA'],image[0].header['OBJDEC'] ) x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 0)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) xl, yu = np.round(wcs.wcs_sky2pix(rra+90./3600, rdec-90./3600, 0), 0) xu, yl = np.round(wcs.wcs_sky2pix(rra-90./3600, rdec+90./3600, 0), 0) imageloc = image[0].data.T[xl:xu,yl:yu] if imageloc.shape[0]==0 or imageloc.shape[1]==0: logger.warn( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution") logger.error("Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution") print "Pixels are", xl, xu, yl, yu try: code, dra, ddec = get_offset_center_failed_astro(f, plot=plot, interactive=interactive) return 2, dra, ddec except: return -1,0,0 if(plot): plt.figure(figsize=(8,8)) zmin, zmax = zscale.zscale(imageloc) #print zmin, zmax, imageloc, (xl,xu,yl,yu) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) plt.imshow(imageloc.T, extent=(xl[0],xu[0],yl[0],yu[0]), aspect="equal", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(1293., 1280., "ws", ms=7, label="Current pointing") plt.plot(x, y, "b*", ms=10, label="Target pointing") plt.gca().invert_xaxis() plt.legend() if (interactive): plt.show() else: plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_a.png"))) plt.clf() return 0, dra, ddec
def get_offset_ref_pixel(f): if(not os.path.isfile(f)): print "File %s does not exist! Returning Zero offsets..."%f return 0,0 else: image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['OBRA'],image[0].header['OBDEC'] ) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 1)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) return dra, ddec
def plot_offset_shift(dirname): dras = [] ddecs = [] d = [] r = [] m = [] for f in glob.glob(os.path.join(dirname, "*new")): image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['RA'], image[0].header['DEC']) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]], np.float_), 1)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) if np.abs(dra) > 100 or np.abs(ddec) > 100: continue print f, image[ 0].data.shape, "(", rra, rdec, ") vs. (", pra, pdec, ")", dra, ddec dras.append(dra) ddecs.append(ddec) d.append(rdec) r.append(rra) m.append(image[0].header['JD']) dras = np.array(dras) ddecs = np.array(ddecs) plt.scatter(dras, ddecs, c=np.array(r), cmap=matplotlib.cm.jet, s=130) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='RA [deg]') f = plt.figure() plt.scatter(dras, ddecs, c=np.array(d), cmap=matplotlib.cm.jet, s=120) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='DEC [deg]') f = plt.figure() plt.scatter(dras, ddecs, c=(np.array(m) - np.min(m)) * 24, cmap=matplotlib.cm.jet, s=130) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='JD [hours since first image]') plt.show()
def update_phot_blagorodnova(image): ''' Updates the DB with the aperture photometry extrated from the fits and the interpolated zeropoint. ''' racen, deccen = cc.hour2deg(fitsutils.get_par(image, "OBJRA"), fitsutils.get_par(image, "OBJRA")) issub='f' refsys='SDSSinterpolated' filt=fitsutils.get_par(image, "FILTER") exptime=fitsutils.get_par(image, "EXPTIME") utdate=time_utils.jd2utc(fitsutils.get_par(image, "JD"), string=True) mag = np.round(fitsutils.get_par(image, "APPMAG"), 3) magerr = np.round(fitsutils.get_par(image, "APPMAGER"), 3) limmag = np.round(fitsutils.get_par(image, "ZEROPT"), 3) name = fitsutils.get_par(image, "NAME").replace("PTF", "") observer = 'SEDMachine' reducedby = 'Blagorodnova Automated Pipeline SEDM' if mag==0 or mag > limmag or mag>22: mag = 99 magerr = 99 db = pg.DB(dbname='ptftransient',user='******',passwd='followup',host='yupana.caltech.edu') getsrcidquery = "SELECT id from sources where name='%s'"%name result = db.query(getsrcidquery) for row in result.dictresult(): srcid = int(row['id']) query3 = "SELECT id from phot WHERE sourceid=%d and instrumentid=64 and obsdate='%s' and filter='%s' and reducedby='Blagorodnova Automated Pipeline SEDM';" % (srcid,utdate,filter) result=db.query(query3) print query3 if len(result.dictresult()) == 0: print srcid,racen,deccen,utdate,exptime,filt,mag,magerr,limmag,issub,refsys,observer,reducedby query2 = "INSERT INTO phot (sourceid,programid,instrumentid,ra,dec,obsdate,exptime,filter,mag,emag,limmag,issub,refsys,observer,reducedby) VALUES " query2 += "(%d,1,64,%f,%f,'%s',%f,'%s',%f,%f,%f,'%s','%s','%s','%s');" % (srcid,racen,deccen,utdate,exptime,filt,mag,magerr,limmag,issub,refsys,observer,reducedby) print query2 db.query(query2) else: for row in result.dictresult(): photid = int(row['id']) query2 = "DELETE from phot WHERE id=%d;" % (photid) print query2 db.query(query2) query2 = "INSERT INTO phot (sourceid,programid,instrumentid,ra,dec,obsdate,exptime,filter,mag,emag,limmag,issub,refsys,observer,reducedby) VALUES " query2 += "(%d,1,64,%f,%f,'%s',%f,'%s',%f,%f,%f,'%s','%s','%s','%s');" % (srcid,racen,deccen,utdate,exptime,filt,mag,magerr,limmag,issub,refsys,observer,reducedby) print query2 db.query(query2)
def plot_offset_shift(dirname): dras = [] ddecs = [] d = [] r = [] m = [] for f in glob.glob(os.path.join(dirname,"*new")): image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['RA'],image[0].header['DEC'] ) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]] , np.float_), 1)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) if np.abs(dra) > 100 or np.abs(ddec)>100: continue print f, image[0].data.shape , "(",rra, rdec, ") vs. (", pra, pdec, ")", dra, ddec dras.append(dra) ddecs.append(ddec) d.append(rdec) r.append(rra) m.append(image[0].header['JD']) dras = np.array(dras) ddecs = np.array(ddecs) plt.scatter(dras, ddecs, c=np.array(r), cmap=matplotlib.cm.jet, s=130) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='RA [deg]') f = plt.figure() plt.scatter(dras, ddecs, c=np.array(d), cmap=matplotlib.cm.jet, s=120) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='DEC [deg]') f = plt.figure() plt.scatter(dras, ddecs, c=(np.array(m)-np.min(m))*24, cmap=matplotlib.cm.jet, s=130) plt.xlabel('dRA [arcsec]') plt.ylabel('dDEC [arcsec]') cb = plt.colorbar(label='JD [hours since first image]') plt.show()
def get_sao(radius=2000): ''' Uses the current time and the latitude of Palomar to find the best SAO stars at zenith. Palomar.lon, Palomar.lat = '243.1361', '33.3558' ''' #Log into a file FORMAT = '%(asctime)-15s %(levelname)s [%(name)s] %(message)s' root_dir = _logpath now = datetime.datetime.utcnow() timestamp=datetime.datetime.isoformat(now) timestamp=timestamp.split("T")[0] logging.basicConfig(format=FORMAT, filename=os.path.join(root_dir, "listener_{0}.log".format(timestamp)), level=logging.INFO) logger = logging.getLogger('sao') d = datetime.datetime.now() utc = d.utcnow() #Get reasonably high target ra = 15*((utc.hour+10)%24) + 15*(utc.minute/60.) dec = 40 hra, hdec = coordinates_conversor.deg2hour(ra, dec) logger.info( "Coordinates to search %s %s"%(hra,hdec)) sao = get_sao_rec(hra, hdec, radius) while (len(sao) == 0): radius = radius+1000 sao = get_sao_rec(hra, hdec, radius) logger.info( "Found %d"%len(sao)) if np.ndim(sao) > 1: np.random.shuffle(sao) logger.info( "Returning %s %s"%(sao[0][1], sao[0][2])) saora, saodec = coordinates_conversor.hour2deg(sao[0][1], sao[0][2]) return "SAO%s"%(sao[0][0]), saora, saodec
def get_app_phot_target(image, plot=False, store=True, wcsin="logical", fwhm=2, box=4, ra=None, dec=None): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = pywcs.WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) pra, pdec = get_xy_coords(image, ra, dec) else: if(wcsin == "logical"): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as pywcs does not seem to be working well. pra, pdec = get_xy_coords(image, ra, dec) #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") fwhm_value = fwhm nsrc, fwhm_value, ellip = sextractor.get_image_pars(image) if np.isnan(fwhm_value): fwhm_value=99 fitsutils.update_par(image, 'FWHM', fwhm_value) if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*3) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad*4) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if (plot): zmin, zmax = zscale.zscale(impf[0].data) im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") plt.scatter(pra, pdec, marker="o", s=100, facecolor="none") plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 15. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): if (ma.size==1): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
def get_offsets_A_B(f, plot=False, interactive=False): ''' Returns the offsets for A and B, so that when offseting, the images do not overlap. Example fits:/scr2/nblago/Projects/SEDM/data/finders/f_b_a_rPTF15fks_r.fits ''' from scipy import stats image = pf.open(f) data = image[0].data.T wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) obj = fitsutils.get_par(f, "OBJECT") pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image xl, yl = np.round(wcs.wcs_sky2pix(ra + 30. / 3600, dec - 30. / 3600, 0), 0) xu, yu = np.round(wcs.wcs_sky2pix(ra - 30. / 3600, dec + 30. / 3600, 0), 0) imageloc = image[0].data[xl:xu, yu:yl] bkg = np.median(imageloc) perc10 = np.percentile(imageloc, 15) perc90 = np.percentile(imageloc, 85) mask = (image[0].data > perc10) * (image[0].data < perc90) bkg_std = np.std(image[0].data[mask]) # mad(image[0].data) linestyles = ['solid', 'dashed', 'dashdot', 'dotted', 'solid'] offsets = np.array([[+3, -4], [+3, +4], [+2, +4], [+2, +2]]) Noff = len(offsets) pvalues = np.zeros((Noff, 2)) if (plot): fig, axarr = plt.subplots(2, Noff, sharey='row', figsize=(6 * len(offsets), 12)) axarr = np.array(axarr, ndmin=1) for i, off in enumerate(offsets): ra_off = ra - off[0] / 3600. dec_off = dec - off[1] / 3600. ra_off_op = ra + off[0] / 3600. dec_off_op = dec + off[1] / 3600. prao, pdeco = wcs.wcs_sky2pix(ra + off[0] / 3600., dec + off[1] / 3600., 0) praosym, pdecosym = wcs.wcs_sky2pix(ra - 2 * off[0] / 3600., dec - 2 * off[1] / 3600., 0) #Extract sample window to check wether the background matches well. #number on samples on each side ns = 7 sample = data[prao - ns:prao + ns, pdeco - ns:pdeco + ns] sm = np.median(sample) sstd = np.std(sample[(sample < 6000) * (sample > 0)]) bkg_prob = np.minimum(1 - stats.norm.cdf(sm, bkg, bkg_std), stats.norm.cdf(sm, bkg, bkg_std)) #Extract sample window to check wether the background matches well. sample = data[praosym - 7:praosym + 7, pdecosym - 7:pdecosym + 7] smo = np.median(sample) sstdo = np.std(sample) bkg_probo = np.minimum(1 - stats.norm.cdf(smo, bkg, bkg_std), stats.norm.cdf(smo, bkg, bkg_std)) pvalues[i] = np.array([bkg_prob, bkg_probo]) if (plot): #Retrieve the image of the object xl, yl = wcs.wcs_sky2pix(ra + 20. / 3600, dec - 20. / 3600, 0) xu, yu = wcs.wcs_sky2pix(ra - 20. / 3600, dec + 20. / 3600, 0) ifuwin = data[xl:xu, yu:yl] #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off + 20. / 3600, dec_off - 20. / 3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off - 20. / 3600, dec_off + 20. / 3600, 0) ifuwin1 = data[x0:x1, y1:y0] nx, ny = ifuwin1.shape #print nx,ny, ifuwin1.shape #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off_op + 20. / 3600, dec_off_op - 20. / 3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off_op - 20. / 3600, dec_off_op + 20. / 3600, 0) ifuwin2 = data[x0:x0 + nx, y1:y1 + ny] #Plot the A and B zmin, zmax = zscale.zscale(ifuwin) zmin1, zmax1 = zscale.zscale(ifuwin1 - ifuwin2) axarr[0, i].imshow(ifuwin.T, aspect="auto", vmin=zmin, vmax=zmax, extent=(20, -20, -20, 20), alpha=0.5) axarr[1, i].imshow(ifuwin1.T - ifuwin2.T, aspect="auto", vmin=zmin1, vmax=zmax1, extent=(20, -20, -20, 20), alpha=0.5) #, cmap=matplotlib.cm.RdBu_r) #axarr[1, i].imshow(-1 * ifuwin2.T, aspect="auto", vmin=zmin2, vmax=zmax2, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) axarr[0, i].scatter(0, 0, marker="x", s=20) axarr[1, i].scatter(0, 0, marker="x", s=20) axarr[0, i].set_xlabel("OBJRA") axarr[0, i].set_ylabel("OBJDEC") axarr[1, i].set_xlabel("OBJRA") axarr[1, i].set_ylabel("OBJDEC") axarr[0, i].text( 19, 17, "Red stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f" % (sm, sstd, bkg_prob), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text( 19, 14, "Blue stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f" % (smo, sstdo, bkg_probo), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 11, "Background stats: $\mu=$%.2f, $\sigma=$%.2f" % (bkg, bkg_std), bbox=dict(facecolor='white', alpha=0.5)) #r = plt.Circle((prao-pra, pdeco-pdec), 5, facecolor="none", edgecolor="red", lw=2) #b = plt.Circle((praosym-pra, pdecosym-pdec), 5, facecolor="none", edgecolor="blue", lw=2) r = plt.Circle((2 * off[0], +2 * off[1]), 2, facecolor="none", edgecolor="red", lw=3, ls=linestyles[i]) b = plt.Circle((-2 * off[0], -2 * off[1]), 2, facecolor="none", edgecolor="blue", lw=3, ls=linestyles[i]) #Plot the true location of the IFU patches = [] Path = mpath.Path path_data = [(Path.MOVETO, [20, -12]), (Path.LINETO, [20, 20]), (Path.LINETO, [-13, 13]), (Path.LINETO, [-20, -18]), (Path.CLOSEPOLY, [20, 10])] codes, verts = zip(*path_data) path = mpath.Path(verts, codes) patch = mpatches.PathPatch(path) patches.append(patch) collection = PatchCollection(patches, cmap=plt.cm.YlGn, alpha=0.2) colors = np.linspace(0, 1, len(patches)) collection.set_array(np.array(colors)) axarr[0, i].add_artist(r) axarr[0, i].add_artist(b) axarr[1, i].add_collection(collection) plt.suptitle(obj, fontsize=20) prod = np.prod(pvalues, axis=1) if (plot and interactive): axarr[0, np.argmax(prod)].text(0, 0, "WINNER") plt.show() elif (plot): axarr[0, np.argmax(prod)].text(0, 0, "WINNER") plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_ab.png"))) plt.clf() return 0, offsets[np.argmax(prod)], -2 * offsets[np.argmax(prod)]
def get_offset_center(f, plot=False, interactive=False): ''' Given a fits image, returns the offset in Ra, DEC, that needs to be applied for the telescope tp go from the current pointing position, to the coodinates of the object specified in the fits file. ''' if (not os.path.isfile(f)): print "File %s does not exist! Returning Zero offsets..." % f return -1, 0, 0 else: image = pf.open(f) wcs = pywcs.WCS(image[0].header) rra, rdec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) x, y = np.round(wcs.wcs_sky2pix(rra, rdec, 0), 0) pra, pdec = wcs.wcs_pix2sky(np.array([[1293., 1280.]], np.float_), 0)[0] dra, ddec = cc.get_offset(pra, pdec, rra, rdec) xl, yu = np.round( wcs.wcs_sky2pix(rra + 90. / 3600, rdec - 90. / 3600, 0), 0) xu, yl = np.round( wcs.wcs_sky2pix(rra - 90. / 3600, rdec + 90. / 3600, 0), 0) imageloc = image[0].data.T[xl:xu, yl:yu] if imageloc.shape[0] == 0 or imageloc.shape[1] == 0: logger.warn( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution" ) logger.error( "Astrometry has FAILED on this! The object is outside the frame! Resending to the numb astrometric solution" ) print "Pixels are", xl, xu, yl, yu try: code, dra, ddec = get_offset_center_failed_astro( f, plot=plot, interactive=interactive) return 2, dra, ddec except: return -1, 0, 0 if (plot): plt.figure(figsize=(8, 8)) zmin, zmax = zscale.zscale(imageloc) #print zmin, zmax, imageloc, (xl,xu,yl,yu) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) plt.imshow(imageloc.T, extent=(xl[0], xu[0], yl[0], yu[0]), aspect="equal", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(1293., 1280., "ws", ms=7, label="Current pointing") plt.plot(x, y, "b*", ms=10, label="Target pointing") plt.gca().invert_xaxis() plt.legend() if (interactive): plt.show() else: plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_a.png"))) plt.clf() return 0, dra, ddec
def get_offset_center_failed_astro(f, plot=False, interactive=True): ''' For fields where astrometry is challenging, there is a simple solution. Find the brightest peak within the pointing error of the telescope. As this fields will usually be centered in Standard stars and very short exposure time, the fit intends to ''' image = pf.open(f) data = image[0].data wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC']) pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int) #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int) imageloc = image[0].data.T[1293 - 150:1293 + 150, 1280 - 150:1280 + 150] nx = 300 ny = 300 def_x = np.argmax(np.sum(imageloc, axis=0)) def_y = np.argmax(np.sum(imageloc, axis=1)) newx = pra - nx / 2. + def_x newy = pdec - ny / 2. + def_y pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]], np.float_), 0)[0] dra, ddec = cc.get_offset(ra, dec, pra, pdec) print "Offset", dra, ddec, "Position RA,DEC", pra, pdec x, y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc) if (plot): plt.figure(figsize=(8, 8)) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) zmin, zmax = zscale.zscale(imageloc) plt.imshow(imageloc, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax, extent=(0, +300, 0, +300)) plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.") plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.") plt.plot(150, 150, "wo", ms=20, label="Initial pointing") plt.legend() '''zmin, zmax = zscale.zscale(data) plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(newx, newy, "go") plt.show()''' if (interactive): plt.show() else: plt.savefig( os.path.join( os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_std.png"))) plt.clf() return 1, ddec, dra
def get_app_phot_target(image, ra=None, dec=None, plot=True, store=True, wcsin="logical", fwhm=None, box=15, arcsecpix=0.394, app=2): ''' coords: files: wcsin: can be "world", "logic" fwhm: in arcsec ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) print "Assuming ra=%.5f, dec=%.5f"%(ra, dec) pra, pdec = get_xy_coords(image, ra, dec) else: if("logic" in wcsin): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as wcs module does not seem to be working well. try: #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #print "Retrieved the pixel number" pra, pdec = get_xy_coords(image, ra, dec) except IndexError: print "Error with astrometry.net. trying the rudimentary method." pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") if (not fwhm is None): fwhm_value = fwhm elif (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM') else: #Put some default value for Palomar fwhm_value=1.5 if (wcsin == 'logical'): fwhm_value = fwhm_value / arcsecpix if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 if (not fitsutils.has_par(image, "EXPTIME")): if (fitsutils.has_par(image, "ITIME") and fitsutils.has_par(image, "COADDS")): exptime = fitsutils.get_par(image, "ITIME")*fitsutils.get_par(image, "COADDS") fitsutils.update_par(image, "EXPTIME", exptime) exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*app) # Set aperture radius to two times the PSF radius sky_rad= math.ceil(aperture_rad*app*2) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 20. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), \ ("stdev","<f4"), ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT") and fitsutils.has_par(image, "ZEROPTU")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) else: mag = 0 magerr = 0 if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): #zmin, zmax = zscale.zscale(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50]) #zmin, zmax = zscale.zscale(impf[0].data) #im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) impf[0].data[np.isnan(impf[0].data)] = np.nanmedian(impf[0].data) print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) im = plt.imshow(impf[0].data, vmin=np.percentile(impf[0].data, 5), vmax=np.percentile(impf[0].data, 95), origin="bottom") X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png"), dpi=200) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
def get_offsets_A_B(f, plot=False, interactive=False): ''' Returns the offsets for A and B, so that when offseting, the images do not overlap. Example fits:/scr2/nblago/Projects/SEDM/data/finders/f_b_a_rPTF15fks_r.fits ''' from scipy import stats image = pf.open(f) data = image[0].data.T wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'] ) obj = fitsutils.get_par(f, "OBJECT") pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image xl, yl = np.round(wcs.wcs_sky2pix(ra+30./3600, dec-30./3600, 0), 0) xu, yu = np.round(wcs.wcs_sky2pix(ra-30./3600, dec+30./3600, 0), 0) imageloc = image[0].data[xl:xu,yu:yl] bkg = np.median(imageloc) perc10 = np.percentile(imageloc, 15) perc90 = np.percentile(imageloc, 85) mask = (image[0].data > perc10) * (image[0].data < perc90) bkg_std = np.std(image[0].data[mask])# mad(image[0].data) linestyles = ['solid' , 'dashed' , 'dashdot' , 'dotted', 'solid'] offsets = np.array([[+3, -4], [+3, +4], [+2, +4], [+2, +2]]) Noff = len(offsets) pvalues = np.zeros((Noff, 2)) if (plot): fig, axarr = plt.subplots(2, Noff, sharey='row', figsize=(6*len(offsets), 12)) axarr = np.array(axarr, ndmin=1) for i, off in enumerate(offsets): ra_off = ra - off[0]/3600. dec_off = dec - off[1]/3600. ra_off_op = ra + off[0]/3600. dec_off_op = dec + off[1]/3600. prao, pdeco = wcs.wcs_sky2pix(ra+ off[0]/3600., dec + off[1]/3600., 0) praosym, pdecosym = wcs.wcs_sky2pix(ra- 2*off[0]/3600., dec - 2*off[1]/3600., 0) #Extract sample window to check wether the background matches well. #number on samples on each side ns = 7 sample = data[prao-ns:prao+ns, pdeco-ns:pdeco+ns] sm = np.median(sample) sstd = np.std(sample[(sample<6000)*(sample>0)]) bkg_prob = np.minimum(1 - stats.norm.cdf(sm, bkg, bkg_std), stats.norm.cdf(sm, bkg, bkg_std)) #Extract sample window to check wether the background matches well. sample = data[praosym-7:praosym+7, pdecosym-7:pdecosym+7] smo = np.median(sample) sstdo = np.std(sample) bkg_probo = np.minimum(1 - stats.norm.cdf(smo, bkg, bkg_std), stats.norm.cdf(smo, bkg, bkg_std)) pvalues[i] = np.array([bkg_prob, bkg_probo]) if(plot): #Retrieve the image of the object xl, yl = wcs.wcs_sky2pix(ra+20./3600, dec-20./3600, 0) xu, yu = wcs.wcs_sky2pix(ra-20./3600, dec+20./3600, 0) ifuwin = data[xl:xu,yu:yl] #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off+20./3600, dec_off-20./3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off-20./3600, dec_off+20./3600, 0) ifuwin1 = data[x0:x1,y1:y0] nx, ny = ifuwin1.shape #print nx,ny, ifuwin1.shape #Retrieve the offset A image of the object x0, y0 = wcs.wcs_sky2pix(ra_off_op+20./3600, dec_off_op-20./3600, 0) x1, y1 = wcs.wcs_sky2pix(ra_off_op-20./3600, dec_off_op+20./3600, 0) ifuwin2 = data[x0:x0+nx,y1:y1+ny] #Plot the A and B zmin, zmax = zscale.zscale(ifuwin) zmin1, zmax1 = zscale.zscale(ifuwin1-ifuwin2) axarr[0, i].imshow(ifuwin.T, aspect="auto", vmin=zmin, vmax=zmax, extent=(20,-20,-20,20), alpha=0.5) axarr[1, i].imshow(ifuwin1.T-ifuwin2.T, aspect="auto", vmin=zmin1, vmax=zmax1, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) #axarr[1, i].imshow(-1 * ifuwin2.T, aspect="auto", vmin=zmin2, vmax=zmax2, extent=(20,-20,-20,20), alpha=0.5)#, cmap=matplotlib.cm.RdBu_r) axarr[0, i].scatter(0, 0, marker="x", s=20) axarr[1, i].scatter(0, 0, marker="x", s=20) axarr[0, i].set_xlabel("OBJRA") axarr[0, i].set_ylabel("OBJDEC") axarr[1, i].set_xlabel("OBJRA") axarr[1, i].set_ylabel("OBJDEC") axarr[0, i].text(19, 17, "Red stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f"%(sm, sstd, bkg_prob), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 14, "Blue stats: $\mu=$%.2f, $\sigma=$%.2f, p-value=%.5f"%(smo, sstdo, bkg_probo), bbox=dict(facecolor='white', alpha=0.5)) axarr[0, i].text(19, 11, "Background stats: $\mu=$%.2f, $\sigma=$%.2f"%( bkg, bkg_std), bbox=dict(facecolor='white', alpha=0.5)) #r = plt.Circle((prao-pra, pdeco-pdec), 5, facecolor="none", edgecolor="red", lw=2) #b = plt.Circle((praosym-pra, pdecosym-pdec), 5, facecolor="none", edgecolor="blue", lw=2) r = plt.Circle((2*off[0], + 2*off[1]), 2, facecolor="none", edgecolor="red", lw=3, ls=linestyles[i]) b = plt.Circle((-2*off[0], -2*off[1]), 2, facecolor="none", edgecolor="blue", lw=3, ls=linestyles[i]) #Plot the true location of the IFU patches = [] Path = mpath.Path path_data = [ (Path.MOVETO, [20, -12]), (Path.LINETO, [20, 20]), (Path.LINETO, [-13, 13]), (Path.LINETO, [-20 , -18]), (Path.CLOSEPOLY, [20, 10]) ] codes, verts = zip(*path_data) path = mpath.Path(verts, codes) patch = mpatches.PathPatch(path) patches.append(patch) collection = PatchCollection(patches, cmap=plt.cm.YlGn, alpha=0.2) colors = np.linspace(0, 1, len(patches)) collection.set_array(np.array(colors)) axarr[0, i].add_artist(r) axarr[0, i].add_artist(b) axarr[1, i].add_collection(collection) plt.suptitle(obj, fontsize=20) prod = np.prod(pvalues, axis=1) if (plot and interactive): axarr[0, np.argmax(prod)].text(0,0,"WINNER") plt.show() elif(plot): axarr[0, np.argmax(prod)].text(0,0,"WINNER") plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_ab.png"))) plt.clf() return 0, offsets[np.argmax(prod)], -2*offsets[np.argmax(prod)]
def get_offset_center_failed_astro(f, plot=False, interactive=True): ''' For fields where astrometry is challenging, there is a simple solution. Find the brightest peak within the pointing error of the telescope. As this fields will usually be centered in Standard stars and very short exposure time, the fit intends to ''' image = pf.open(f) data = image[0].data wcs = pywcs.WCS(image[0].header) ra, dec = cc.hour2deg(image[0].header['OBJRA'], image[0].header['OBJDEC'] ) pra, pdec = wcs.wcs_sky2pix(ra, dec, 0) #Get a local image #xl, yl = np.array(wcs.wcs_sky2pix(ra+(60./3600)*np.cos(np.deg2rad(dec)), dec-60./3600, 0), dtype=np.int) #xu, yu = np.array(wcs.wcs_sky2pix(ra-(60./3600)*np.cos(np.deg2rad(dec)), dec+60./3600, 0), dtype=np.int) imageloc = image[0].data.T[1293-150:1293+150,1280-150:1280+150] nx = 300 ny=300 def_x = np.argmax(np.sum(imageloc, axis=0)) def_y = np.argmax(np.sum(imageloc, axis=1)) newx = pra-nx/2.+def_x newy = pdec-ny/2.+def_y pra, pdec = wcs.wcs_pix2sky(np.array([[newx[0], newy[0]]] , np.float_), 0)[0] dra, ddec = cc.get_offset(ra, dec, pra, pdec) print "Offset", dra, ddec, "Position RA,DEC", pra, pdec x,y, fwhmx, fwhmy, bkg, amp = fit_utils.fit_gauss(imageloc) if (plot): plt.figure(figsize=(8,8)) obj = fitsutils.get_par(f, "OBJECT") plt.suptitle(obj, fontsize=20) zmin, zmax = zscale.zscale(imageloc) plt.imshow(imageloc, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax, extent=(0,+300,0,+300)) plt.plot(x, y, "go", ms=20, label="Centroid using gaussiuan fit.") plt.plot(def_x, def_y, "b*", ms=20, label="Centroid using max/min.") plt.plot(150,150,"wo", ms=20, label="Initial pointing") plt.legend() '''zmin, zmax = zscale.zscale(data) plt.imshow(data, aspect="auto", interpolation="none", origin="lower", vmin=zmin, vmax=zmax) plt.plot(newx, newy, "go") plt.show()''' if (interactive): plt.show() else: plt.savefig(os.path.join(os.path.dirname(f).replace("raw", "phot"), os.path.basename(f).replace(".fits", "_std.png"))) plt.clf() return 1, ddec, dra