def kepfield(infile,plotfile,rownum,imscale,colmap,lcolor,srctab,verbose,logfile,status,cmdLine=False): # input arguments status = 0 seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFIELD -- ' call += 'infile='+infile+' ' call += 'plotfile='+plotfile+' ' call += 'rownum='+str(rownum)+' ' call += 'imscale='+imscale+' ' call += 'colmap='+colmap+' ' call += 'lcolor='+lcolor+' ' srct = 'n' if (srctab): srct = 'y' call += 'srctab='+srct+' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose='+chatter+' ' call += 'logfile='+logfile kepmsg.log(logfile,call+'\n',verbose) # start time kepmsg.clock('KEPFIELD started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # reference color map if colmap == 'browse': status = cmap_plot(cmdLine) # open TPF FITS file if status == 0: try: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) except: message = 'ERROR -- KEPFIELD: is %s a Target Pixel File? ' % infile status = kepmsg.err(logfile,message,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition(infile,logfile,verbose) # observed or simulated data? if status == 0: coa = False instr = pyfits.open(infile,mode='readonly',memmap=True) filever, status = kepkey.get(infile,instr[0],'FILEVER',logfile,verbose) if filever == 'COA': coa = True # print target data if status == 0 and verbose: print('') print(' KepID: %s' % kepid) print(' BJD: %.2f' % (barytime[rownum-1] + 2454833.0)) print(' RA (J2000): %s' % ra) print('Dec (J2000): %s' % dec) print(' KepMag: %s' % kepmag) print(' SkyGroup: %2s' % skygroup) print(' Season: %2s' % str(season)) print(' Channel: %2s' % channel) print(' Module: %2s' % module) print(' Output: %1s' % output) print('') # is this a good row with finite timestamp and pixels? if status == 0: if not numpy.isfinite(barytime[rownum-1]) or not numpy.nansum(fluxpixels[rownum-1,:]): message = 'ERROR -- KEPFIELD: Row ' + str(rownum) + ' is a bad quality timestamp' status = kepmsg.err(logfile,message,verbose) # construct input pixel image if status == 0: flux = fluxpixels[rownum-1,:] # image scale and intensity limits of pixel data if status == 0: flux_pl, zminfl, zmaxfl = kepplot.intScale1D(flux,imscale) n = 0 imgflux_pl = empty((ydim+2,xdim+2)) for i in range(ydim+2): for j in range(xdim+2): imgflux_pl[i,j] = numpy.nan for i in range(ydim): for j in range(xdim): imgflux_pl[i+1,j+1] = flux_pl[n] n += 1 # cone search around target coordinates using the MAST target search form if status == 0: dr = max([ydim+2,xdim+2]) * 4.0 kepid,ra,dec,kepmag = MASTRADec(float(ra),float(dec),dr,srctab) # convert celestial coordinates to detector coordinates if status == 0: sx = numpy.array([]) sy = numpy.array([]) inf, status = kepio.openfits(infile,'readonly',logfile,verbose) try: crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status = \ kepkey.getWCSs(infile,inf['APERTURE'],logfile,verbose) crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status = \ kepkey.getWCSp(infile,inf['APERTURE'],logfile,verbose) for i in range(len(kepid)): dra = (ra[i] - crval1) * math.cos(math.radians(dec[i])) / cdelt1 ddec = (dec[i] - crval2) / cdelt2 if coa: sx = numpy.append(sx,-(pc[0,0] * dra + pc[0,1] * ddec) + crpix1 + crval1p - 1.0) else: sx = numpy.append(sx,pc[0,0] * dra + pc[0,1] * ddec + crpix1 + crval1p - 1.0) sy = numpy.append(sy,pc[1,0] * dra + pc[1,1] * ddec + crpix2 + crval2p - 1.0) except: message = 'ERROR -- KEPFIELD: Non-compliant WCS information within file %s' % infile status = kepmsg.err(logfile,message,verbose) # plot style if status == 0: try: params = {'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': 48, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': 20, 'ytick.labelsize': 20} pylab.rcParams.update(params) except: pass pylab.figure(figsize=[10,10]) pylab.clf() # pixel limits of the subimage if status == 0: ymin = copy(float(row)) ymax = ymin + ydim xmin = copy(float(column)) xmax = xmin + xdim # plot limits for flux image if status == 0: ymin = float(ymin) - 1.5 ymax = float(ymax) + 0.5 xmin = float(xmin) - 1.5 xmax = float(xmax) + 0.5 # plot the image window if status == 0: ax = pylab.axes([0.1,0.11,0.88,0.88]) pylab.imshow(imgflux_pl,aspect='auto',interpolation='nearest',origin='lower', vmin=zminfl,vmax=zmaxfl,extent=(xmin,xmax,ymin,ymax),cmap=colmap) pylab.gca().set_autoscale_on(False) labels = ax.get_yticklabels() setp(labels, 'rotation', 90) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.xlabel('Pixel Column Number', {'color' : 'k'}) pylab.ylabel('Pixel Row Number', {'color' : 'k'}) # plot mask borders if status == 0: kepplot.borders(maskimg,xdim,ydim,pixcoord1,pixcoord2,1,lcolor,'--',0.5) # plot aperture borders if status == 0: kepplot.borders(maskimg,xdim,ydim,pixcoord1,pixcoord2,2,lcolor,'-',4.0) # list sources if status == 0: print('Column Row RA J2000 Dec J2000 Kp Kepler ID') print('----------------------------------------------------') for i in range(len(sx)-1,-1,-1): if sx[i] >= xmin and sx[i] < xmax and sy[i] >= ymin and sy[i] < ymax: if kepid[i] != 0 and kepmag[i] != 0.0: print('%6.1f %6.1f %9.5f %8.5f %5.2f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),float(kepmag[i]),int(kepid[i]))) elif kepid[i] != 0 and kepmag[i] == 0.0: print('%6.1f %6.1f %9.5f %8.5f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),int(kepid[i]))) else: print('%6.1f %6.1f %9.5f %8.5f' % (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]))) # plot sources if status == 0: for i in range(len(sx)-1,-1,-1): if kepid[i] != 0 and kepmag[i] != 0.0: size = max(array([80.0,80.0 + (2.5**(18.0 - max(12.0,float(kepmag[i])))) * 250.0])) pylab.scatter(sx[i],sy[i],s=size,facecolors='g',edgecolors='k',alpha=0.4) else: pylab.scatter(sx[i],sy[i],s=80,facecolors='r',edgecolors='k',alpha=0.4) # render plot if status == 0 and len(plotfile) > 0 and plotfile.lower() != 'none': pylab.savefig(plotfile) if status == 0: if cmdLine: pylab.show(block=True) else: pylab.ion() pylab.plot([]) pylab.ioff() # stop time kepmsg.clock('\nKEPFIELD ended at',logfile,verbose) return
def __init__(self, infile, rownum=0, imscale='linear', cmap='YlOrBr', lcolor='k', acolor='b', query=True, logfile='kepcrowd.log', **kwargs): self.colrow = [] self.fluxes = [] self._text = [] # hide warnings np.seterr(all="ignore") # test log file logfile = kepmsg.test(logfile) # info hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, False) call = 'KEPFIELD -- ' call += 'infile=' + infile + ' ' call += 'rownum=' + str(rownum) kepmsg.log(logfile, call + '\n', False) try: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile, 'TIME', logfile, False) except: message = 'ERROR -- KEPFIELD: is %s a Target Pixel File? ' % infile kepmsg.err(logfile, message, False) return "", "", "", None kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,False) kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'rownumNO',logfile,False) kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,False) kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,False) kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,False) # read mask defintion data from TPF file maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition( infile, logfile, False) # observed or simulated data? coa = False instr = pyfits.open(infile, mode='readonly', memmap=True) filever, status = kepkey.get(infile, instr[0], 'FILEVER', logfile, False) if filever == 'COA': coa = True # is this a good row with finite timestamp and pixels? if not np.isfinite(barytime[rownum - 1]) or not np.nansum( fluxpixels[rownum - 1, :]): message = 'ERROR -- KEPFIELD: Row ' + str( rownum) + ' is a bad quality timestamp' kepmsg.err(logfile, message, True) return "", "", "", None # construct input pixel image flux = fluxpixels[rownum - 1, :] # image scale and intensity limits of pixel data flux_pl, zminfl, zmaxfl = kepplot.intScale1D(flux, imscale) n = 0 imgflux_pl = np.empty((ydim + 2, xdim + 2)) for i in range(ydim + 2): for j in range(xdim + 2): imgflux_pl[i, j] = np.nan for i in range(ydim): for j in range(xdim): imgflux_pl[i + 1, j + 1] = flux_pl[n] n += 1 # cone search around target coordinates using the MAST target search form dr = max([ydim + 2, xdim + 2]) * 4.0 kepid, ra, dec, kepmag = MASTRADec(float(ra), float(dec), dr, query, logfile) # convert celestial coordinates to detector coordinates sx = np.array([]) sy = np.array([]) inf, status = kepio.openfits(infile, 'readonly', logfile, False) try: crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status = \ kepkey.getWCSs(infile,inf['APERTURE'],logfile,False) crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status = \ kepkey.getWCSp(infile,inf['APERTURE'],logfile,False) for i in range(len(kepid)): dra = (ra[i] - crval1) * np.cos(np.radians(dec[i])) / cdelt1 ddec = (dec[i] - crval2) / cdelt2 if coa: sx = np.append( sx, -(pc[0, 0] * dra + pc[0, 1] * ddec) + crpix1 + crval1p - 1.0) else: sx = np.append( sx, pc[0, 0] * dra + pc[0, 1] * ddec + crpix1 + crval1p - 1.0) sy = np.append( sy, pc[1, 0] * dra + pc[1, 1] * ddec + crpix2 + crval2p - 1.0) except: message = 'ERROR -- KEPFIELD: Non-compliant WCS information within file %s' % infile kepmsg.err(logfile, message, True) return "", "", "", None # plot self.fig = pl.figure(figsize=[10, 10]) pl.clf() # pixel limits of the subimage ymin = np.copy(float(row)) ymax = ymin + ydim xmin = np.copy(float(column)) xmax = xmin + xdim # plot limits for flux image ymin = float(ymin) - 1.5 ymax = float(ymax) + 0.5 xmin = float(xmin) - 1.5 xmax = float(xmax) + 0.5 # plot the image window ax = pl.axes([0.1, 0.11, 0.88, 0.82]) pl.title('Select sources for fitting (KOI first)', fontsize=24) pl.imshow(imgflux_pl, aspect='auto', interpolation='nearest', origin='lower', vmin=zminfl, vmax=zmaxfl, extent=(xmin, xmax, ymin, ymax), cmap=cmap) pl.gca().set_autoscale_on(False) labels = ax.get_yticklabels() pl.setp(labels, 'rotation', 90) pl.gca().xaxis.set_major_formatter(pl.ScalarFormatter(useOffset=False)) pl.gca().yaxis.set_major_formatter(pl.ScalarFormatter(useOffset=False)) pl.xlabel('Pixel Column Number', {'color': 'k'}, fontsize=24) pl.ylabel('Pixel Row Number', {'color': 'k'}, fontsize=24) # plot mask borders kepplot.borders(maskimg, xdim, ydim, pixcoord1, pixcoord2, 1, lcolor, '--', 0.5) # plot aperture borders kepplot.borders(maskimg, xdim, ydim, pixcoord1, pixcoord2, 2, lcolor, '-', 4.0) # list sources with open(logfile, 'a') as lf: print('Column Row RA J2000 Dec J2000 Kp Kepler ID', file=lf) print('----------------------------------------------------', file=lf) for i in range(len(sx) - 1, -1, -1): if sx[i] >= xmin and sx[i] < xmax and sy[i] >= ymin and sy[ i] < ymax: if kepid[i] != 0 and kepmag[i] != 0.0: print('%6.1f %6.1f %9.5f %8.5f %5.2f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),float(kepmag[i]),int(kepid[i])), file = lf) elif kepid[i] != 0 and kepmag[i] == 0.0: print('%6.1f %6.1f %9.5f %8.5f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),int(kepid[i])), file = lf) else: print('%6.1f %6.1f %9.5f %8.5f' % (float( sx[i]), float(sy[i]), float(ra[i]), float(dec[i])), file=lf) # plot sources for i in range(len(sx) - 1, -1, -1): if kepid[i] != 0 and kepmag[i] != 0.0: size = max( np.array([ 80.0, 80.0 + (2.5**(18.0 - max(12.0, float(kepmag[i])))) * 250.0 ])) pl.scatter(sx[i], sy[i], s=size, facecolors='g', edgecolors='k', alpha=0.4) else: pl.scatter(sx[i], sy[i], s=80, facecolors='r', edgecolors='k', alpha=0.4) # Sizes for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(16) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(16) # render plot and activate source selection self.srcinfo = [kepid, sx, sy, kepmag] pl.connect('button_release_event', self.on_mouse_release) pl.show(block=True) pl.close()
def kepfield(infile,plotfile,rownum,imscale='linear',colmap='YlOrBr',lcolor='gray',verbose=0, logfile='kepfield.log',status=0,kic=0,cmdLine=False): # input arguments seterr(all="ignore") # log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPFIELD -- ' call += 'infile='+infile+' ' call += 'plotfile='+plotfile+' ' call += 'rownum='+str(rownum)+' ' call += 'imscale='+imscale+' ' call += 'colmap='+colmap+' ' call += 'lcolor='+lcolor+' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose='+chatter+' ' call += 'logfile='+logfile kepmsg.log(logfile,call+'\n',verbose) # start time kepmsg.clock('KEPFIELD started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # reference color map if colmap == 'browse': status = cmap_plot(cmdLine) # open TPF FITS file if status == 0: try: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, barytime, status = \ kepio.readTPF(infile,'TIME',logfile,verbose) except: message = 'ERROR -- KEPFIELD: is %s a Target Pixel File? ' % infile status = kepmsg.err(logfile,message,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = \ kepio.readTPF(infile,'TIMECORR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, cadno, status = \ kepio.readTPF(infile,'CADENCENO',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = \ kepio.readTPF(infile,'FLUX',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = \ kepio.readTPF(infile,'FLUX_ERR',logfile,verbose) if status == 0: kepid, channel, skygroup, module, output, quarter, season, \ ra, dec, column, row, kepmag, xdim, ydim, qual, status = \ kepio.readTPF(infile,'QUALITY',logfile,verbose) # read mask defintion data from TPF file if status == 0: maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition(infile,logfile,verbose) # observed or simulated data? if status == 0: coa = False instr = pyfits.open(infile,mode='readonly',memmap=True) filever, status = kepkey.get(infile,instr[0],'FILEVER',logfile,verbose) if filever == 'COA': coa = True # print target data if status == 0 and verbose: print '' print ' KepID: %s' % kepid print ' RA (J2000): %s' % ra print 'Dec (J2000): %s' % dec print ' KepMag: %s' % kepmag print ' SkyGroup: %2s' % skygroup print ' Season: %2s' % str(season) print ' Channel: %2s' % channel print ' Module: %2s' % module print ' Output: %1s' % output print '' # is this a good row with finite timestamp and pixels? if status == 0: if not numpy.isfinite(barytime[rownum-1]) or not numpy.nansum(fluxpixels[rownum-1,:]): message = 'ERROR -- KEPFIELD: Row ' + str(rownum) + ' is a bad quality timestamp' status = kepmsg.err(logfile,message,verbose) # construct input pixel image if status == 0: flux = fluxpixels[rownum-1,:] # image scale and intensity limits of pixel data if status == 0: flux_pl, zminfl, zmaxfl = kepplot.intScale1D(flux,imscale) n = 0 imgflux_pl = empty((ydim+2,xdim+2)) for i in range(ydim+2): for j in range(xdim+2): imgflux_pl[i,j] = numpy.nan for i in range(ydim): for j in range(xdim): imgflux_pl[i+1,j+1] = flux_pl[n] n += 1 # cone search around target coordinates using the MAST target search form if status == 0: dr = max([ydim+2,xdim+2]) * 4.0 kepid,ra,dec,kepmag = MASTRADec(float(ra),float(dec),dr) # convert celestial coordinates to detector coordinates if status == 0: sx = numpy.array([]) sy = numpy.array([]) inf, status = kepio.openfits(infile,'readonly',logfile,verbose) crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status = \ kepkey.getWCSs(infile,inf['APERTURE'],logfile,verbose) crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status = \ kepkey.getWCSp(infile,inf['APERTURE'],logfile,verbose) for i in range(len(kepid)): dra = (ra[i] - crval1) * math.sin(math.radians(dec[i])) / cdelt1 ddec = (dec[i] - crval2) / cdelt2 if coa: sx = numpy.append(sx,-(pc[0,0] * dra + pc[0,1] * ddec) + crpix1 + crval1p - 1.0) else: sx = numpy.append(sx,pc[0,0] * dra + pc[0,1] * ddec + crpix1 + crval1p - 1.0) sy = numpy.append(sy,pc[1,0] * dra + pc[1,1] * ddec + crpix2 + crval2p - 1.0) # plot style if status == 0: try: params = {'backend': 'png', 'axes.linewidth': 2.0, 'axes.labelsize': 24, 'axes.font': 'sans-serif', 'axes.fontweight' : 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': 18, 'ytick.labelsize': 18} pylab.rcParams.update(params) except: pass pylab.figure(figsize=[10,10]) pylab.clf() # pixel limits of the subimage if status == 0: ymin = copy(float(row)) ymax = ymin + ydim xmin = copy(float(column)) xmax = xmin + xdim # plot limits for flux image if status == 0: ymin = float(ymin) - 1.5 ymax = float(ymax) + 0.5 xmin = float(xmin) - 1.5 xmax = float(xmax) + 0.5 # plot the image window if status == 0: ax = pylab.axes([0.1,0.11,0.88,0.88]) pylab.imshow(imgflux_pl,aspect='auto',interpolation='nearest',origin='lower', vmin=zminfl,vmax=zmaxfl,extent=(xmin,xmax,ymin,ymax),cmap=colmap) pylab.gca().set_autoscale_on(False) labels = ax.get_yticklabels() setp(labels, 'rotation', 90) pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False)) pylab.xlabel('Pixel Column Number', {'color' : 'k'}) pylab.ylabel('Pixel Row Number', {'color' : 'k'}) # plot mask borders if status == 0: kepplot.borders(maskimg,xdim,ydim,pixcoord1,pixcoord2,1,lcolor,'-',1) # plot aperture borders if status == 0: kepplot.borders(maskimg,xdim,ydim,pixcoord1,pixcoord2,2,lcolor,'-',4.0) # list sources if status == 0 and verbose: print 'Column Row RA J2000 Dec J2000 Kp Kepler ID' print '----------------------------------------------------' for i in range(len(sx)-1,-1,-1): if sx[i] >= xmin and sx[i] < xmax and sy[i] >= ymin and sy[i] < ymax: if kepid[i] != 0 and kepmag[i] != 0.0: print '%6.1f %6.1f %9.5f %8.5f %5.2f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),float(kepmag[i]),int(kepid[i])) elif kepid[i] != 0 and kepmag[i] == 0.0: print '%6.1f %6.1f %9.5f %8.5f KIC %d' % \ (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i]),int(kepid[i])) else: print '%6.1f %6.1f %9.5f %8.5f' % (float(sx[i]),float(sy[i]),float(ra[i]),float(dec[i])) # plot sources if status == 0: for i in range(len(sx)-1,-1,-1): if kepid[i] != 0 and kepmag[i] != 0.0: size = max(array([80.0,80.0 + (2.5**(18.0 - max(12.0,float(kepmag[i])))) * 250.0])) pylab.scatter(sx[i],sy[i],s=size,facecolors='g',edgecolors='k',alpha=0.4) else: pylab.scatter(sx[i],sy[i],s=80,facecolors='r',edgecolors='k',alpha=0.4) # render plot if status == 0 and len(plotfile) > 0 and plotfile.lower() != 'none': pylab.savefig(plotfile) if status == 0: if cmdLine: pylab.show(block=True) else: pylab.ion() pylab.plot([]) pylab.ioff() pylab.clf() # stop time kepmsg.clock('\nKEPFIELD ended at',logfile,verbose) # pdb.set_trace() if kic > 0: ind = np.where(kepid == kic) colret = sx[ind] rowret = sy[ind] raret = ra[ind] decret = dec[ind] kepmagret = kepmag[ind] kepidret = kepid[ind] else: inds = np.where(kepmag != 0.0) colret = sx[inds] rowret = sy[inds] raret = ra[inds] decret = dec[inds] kepmagret = kepmag[inds] kepidret = kepid[inds] return colret,rowret,raret,decret,kepmagret,kepidret
def __init__( self, infile, rownum=0, imscale="linear", cmap="YlOrBr", lcolor="k", acolor="b", query=True, logfile="kepcrowd.log", **kwargs ): self.colrow = [] self.fluxes = [] self._text = [] # hide warnings np.seterr(all="ignore") # test log file logfile = kepmsg.test(logfile) # info hashline = "----------------------------------------------------------------------------" kepmsg.log(logfile, hashline, False) call = "KEPFIELD -- " call += "infile=" + infile + " " call += "rownum=" + str(rownum) kepmsg.log(logfile, call + "\n", False) try: kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, barytime, status = kepio.readTPF( infile, "TIME", logfile, False ) except: message = "ERROR -- KEPFIELD: is %s a Target Pixel File? " % infile kepmsg.err(logfile, message, False) return "", "", "", None kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, tcorr, status = kepio.readTPF( infile, "TIMECORR", logfile, False ) kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, cadno, status = kepio.readTPF( infile, "rownumNO", logfile, False ) kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, fluxpixels, status = kepio.readTPF( infile, "FLUX", logfile, False ) kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, errpixels, status = kepio.readTPF( infile, "FLUX_ERR", logfile, False ) kepid, channel, skygroup, module, output, quarter, season, ra, dec, column, row, kepmag, xdim, ydim, qual, status = kepio.readTPF( infile, "QUALITY", logfile, False ) # read mask defintion data from TPF file maskimg, pixcoord1, pixcoord2, status = kepio.readMaskDefinition(infile, logfile, False) # observed or simulated data? coa = False instr = pyfits.open(infile, mode="readonly", memmap=True) filever, status = kepkey.get(infile, instr[0], "FILEVER", logfile, False) if filever == "COA": coa = True # is this a good row with finite timestamp and pixels? if not np.isfinite(barytime[rownum - 1]) or not np.nansum(fluxpixels[rownum - 1, :]): message = "ERROR -- KEPFIELD: Row " + str(rownum) + " is a bad quality timestamp" kepmsg.err(logfile, message, True) return "", "", "", None # construct input pixel image flux = fluxpixels[rownum - 1, :] # image scale and intensity limits of pixel data flux_pl, zminfl, zmaxfl = kepplot.intScale1D(flux, imscale) n = 0 imgflux_pl = np.empty((ydim + 2, xdim + 2)) for i in range(ydim + 2): for j in range(xdim + 2): imgflux_pl[i, j] = np.nan for i in range(ydim): for j in range(xdim): imgflux_pl[i + 1, j + 1] = flux_pl[n] n += 1 # cone search around target coordinates using the MAST target search form dr = max([ydim + 2, xdim + 2]) * 4.0 kepid, ra, dec, kepmag = MASTRADec(float(ra), float(dec), dr, query, logfile) # convert celestial coordinates to detector coordinates sx = np.array([]) sy = np.array([]) inf, status = kepio.openfits(infile, "readonly", logfile, False) try: crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status = kepkey.getWCSs( infile, inf["APERTURE"], logfile, False ) crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status = kepkey.getWCSp( infile, inf["APERTURE"], logfile, False ) for i in range(len(kepid)): dra = (ra[i] - crval1) * np.cos(np.radians(dec[i])) / cdelt1 ddec = (dec[i] - crval2) / cdelt2 if coa: sx = np.append(sx, -(pc[0, 0] * dra + pc[0, 1] * ddec) + crpix1 + crval1p - 1.0) else: sx = np.append(sx, pc[0, 0] * dra + pc[0, 1] * ddec + crpix1 + crval1p - 1.0) sy = np.append(sy, pc[1, 0] * dra + pc[1, 1] * ddec + crpix2 + crval2p - 1.0) except: message = "ERROR -- KEPFIELD: Non-compliant WCS information within file %s" % infile kepmsg.err(logfile, message, True) return "", "", "", None # plot self.fig = pl.figure(figsize=[10, 10]) pl.clf() # pixel limits of the subimage ymin = np.copy(float(row)) ymax = ymin + ydim xmin = np.copy(float(column)) xmax = xmin + xdim # plot limits for flux image ymin = float(ymin) - 1.5 ymax = float(ymax) + 0.5 xmin = float(xmin) - 1.5 xmax = float(xmax) + 0.5 # plot the image window ax = pl.axes([0.1, 0.11, 0.88, 0.82]) pl.title("Select sources for fitting (KOI first)", fontsize=24) pl.imshow( imgflux_pl, aspect="auto", interpolation="nearest", origin="lower", vmin=zminfl, vmax=zmaxfl, extent=(xmin, xmax, ymin, ymax), cmap=cmap, ) pl.gca().set_autoscale_on(False) labels = ax.get_yticklabels() pl.setp(labels, "rotation", 90) pl.gca().xaxis.set_major_formatter(pl.ScalarFormatter(useOffset=False)) pl.gca().yaxis.set_major_formatter(pl.ScalarFormatter(useOffset=False)) pl.xlabel("Pixel Column Number", {"color": "k"}, fontsize=24) pl.ylabel("Pixel Row Number", {"color": "k"}, fontsize=24) # plot mask borders kepplot.borders(maskimg, xdim, ydim, pixcoord1, pixcoord2, 1, lcolor, "--", 0.5) # plot aperture borders kepplot.borders(maskimg, xdim, ydim, pixcoord1, pixcoord2, 2, lcolor, "-", 4.0) # list sources with open(logfile, "a") as lf: print("Column Row RA J2000 Dec J2000 Kp Kepler ID", file=lf) print("----------------------------------------------------", file=lf) for i in range(len(sx) - 1, -1, -1): if sx[i] >= xmin and sx[i] < xmax and sy[i] >= ymin and sy[i] < ymax: if kepid[i] != 0 and kepmag[i] != 0.0: print( "%6.1f %6.1f %9.5f %8.5f %5.2f KIC %d" % ( float(sx[i]), float(sy[i]), float(ra[i]), float(dec[i]), float(kepmag[i]), int(kepid[i]), ), file=lf, ) elif kepid[i] != 0 and kepmag[i] == 0.0: print( "%6.1f %6.1f %9.5f %8.5f KIC %d" % (float(sx[i]), float(sy[i]), float(ra[i]), float(dec[i]), int(kepid[i])), file=lf, ) else: print( "%6.1f %6.1f %9.5f %8.5f" % (float(sx[i]), float(sy[i]), float(ra[i]), float(dec[i])), file=lf, ) # plot sources for i in range(len(sx) - 1, -1, -1): if kepid[i] != 0 and kepmag[i] != 0.0: size = max(np.array([80.0, 80.0 + (2.5 ** (18.0 - max(12.0, float(kepmag[i])))) * 250.0])) pl.scatter(sx[i], sy[i], s=size, facecolors="g", edgecolors="k", alpha=0.4) else: pl.scatter(sx[i], sy[i], s=80, facecolors="r", edgecolors="k", alpha=0.4) # Sizes for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(16) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(16) # render plot and activate source selection self.srcinfo = [kepid, sx, sy, kepmag] pl.connect("button_release_event", self.on_mouse_release) pl.show(block=True) pl.close()