def fluxResPlot(input='OUTPUT/photz.out',outplot='fluxres.pdf'): sed = eazy.getEazySED(idnum, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', scale_flambda=True, verbose=False, individual_templates=False) lambdaz, temp_sed, lci, obs_sed, fobs, efobs = sed
def plotResult(idnum,catalog,save=False): id=str(idnum) obsData=Table.read(catalog).to_pandas() rmag=-2.5*np.log10(obsData['fRauto'][idnum])+23.9 axes = eazy.plotExampleSED(idx=idnum, writePNG=False, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='OUTPUT', CACHE_FILE='Same', lrange=[3800, 3.e4], axes=None, individual_templates=False, fnu=False) sed = eazy.getEazySED(idnum, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', scale_flambda=True, verbose=False, individual_templates=False) lambdaz, temp_sed, lci, obs_sed, fobs, efobs = sed zgrid, pzi, prior = eazy.getEazyPz(idnum, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', binaries=None, get_prior=True) axes[1].plot(zgrid, pzi, color='black') axes[1].plot(zgrid, prior/prior.max()*pzi.max(), color='purple') axes[1].set_xlim([0.,1.]) axes[0].text(5500.,axes[0].get_ylim()[1]*0.1,r'$m_{R}$ = '+str(rmag)[:5]+' mag') if save == True: plt.savefig('OUTPUT/'+id+'.pdf',format='pdf',dpi=6000) plt.clf() else: plt.show() plt.close()
lo, hi = stack.min(), stack.max() stack -= lo stack /= (hi - lo) stack *= 2 hinds = pylab.where(stack > 1) stack[hinds] = 1 rgb.set_data(stack) ### sed subplot sed = eazyPy.getEazySED(i, MAIN_OUTPUT_FILE=version, OUTPUT_DIRECTORY='./EAZY') lambdaz, temp_sed, lci, temp_obs, fobs, efobs = sed lambdaz, lci = lambdaz / 10.**4, lci / 10.**4 sp_sed.loglog(lambdaz, temp_sed, color='r', lw=1, zorder=1) # sp_sed.errorbar(lci, temp_obs, ls='', marker='s', ms=8, mew=2, mec='r', mfc='r', zorder=2) sp_sed.errorbar(lci, fobs, yerr=efobs, ls='', marker='o', ms=12, mew=2, mfc='none', mec='k', ecolor='k', zorder=3) indspos = pylab.find(fobs > 0) fmin = min(fobs[indspos]) fmax = max(fobs[indspos]) sp_sed.set_xlim(2*10**-1, 1.7*10**1) sp_sed.set_ylim(fmin/5, fmax*5) sp_sed = restore_logaxes_labels(sp_sed)
################################################ #### Example object. `idx` is zero-indexed ################################################ axes = eazy.plotExampleSED(idx=17, writePNG=False, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='OUTPUT', CACHE_FILE='Same', lrange=[3800, 3.e4], axes=None, individual_templates=True, fnu=False) plt.savefig('eazy_fit.png'); plt.close() ################################################ #### Pull out data from the BINARY_OUTPUTS files ################################################ ## SED, data & fit sed = eazy.getEazySED(17, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', scale_flambda=True, verbose=False, individual_templates=False) lambdaz, temp_sed, lci, obs_sed, fobs, efobs = sed axes[0].scatter(lci, obs_sed, color='orange', zorder=2) axes[0].scatter(lci, fobs, color='green', marker='s', s=150, zorder=2) ## p(z) zgrid, pzi, prior = eazy.getEazyPz(17, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', binaries=None, get_prior=True) axes[1].plot(zgrid, pzi, color='black') axes[1].plot(zgrid, prior/prior.max()*pzi.max(), color='purple') plt.savefig('eazy_fit_2.png') ################################################
def specphot_acs_and_wfc3(id=69, grism_root='ibhm45030', MAIN_OUTPUT_FILE = 'cosmos-1.v4.6', OUTPUT_DIRECTORY = '/Users/gbrammer/research/drg/PHOTZ/EAZY/NEWFIRM/v4.6/OUTPUT_KATE/', CACHE_FILE = 'Same', Verbose=False, SPC = None, cat=None, grismCat = None, zout = None, fout = None, OUT_PATH='/tmp/', OUT_FILE_FORMAT=True, OUT_FILE='junk.png', GET_SPEC_ONLY=False, GET_WFC3=False, WFC3_DIR='/3DHST/Spectra/Release/v2.0/GOODS-S'): """ specphot_acs_and_wfc3(id) Get photometry/SED fit as well as WFC3 spectrum when available and overplot G141 spectrum. This is different from unicorn.analysis.specphot() which does not get the WFC3 spectrum. """ import threedhst.eazyPy as eazy import threedhst.catIO as catIO import pyfits #### Get G141 spectrum if Verbose: print 'Read SPC' if SPC is None: SPC = threedhst.plotting.SPCFile(grism_root+'_2_opt.SPC.fits', axe_drizzle_dir='DRIZZLE_G141') spec = SPC.getSpec(id) if spec is False: return False xmin = 3000 xmax = 2.4e4 lam = spec.field('LAMBDA') flux = spec.field('FLUX') ffix = flux-spec.field('CONTAM') ferr = spec.field('FERROR') #*0.06/0.128254 if Verbose: print 'Read grism catalog' #### Read the grism catalog and get coords of desired object if grismCat is None: grismCat = threedhst.sex.mySexCat('DATA/'+grism_root+'_drz.cat') #### Source size R = np.sqrt(np.cast[float](grismCat.A_IMAGE)*np.cast[float](grismCat.B_IMAGE)) grism_idx = np.where(grismCat.id == id)[0][0] Rmatch = R[grism_idx]*1. ra0 = grismCat.ra[grismCat.id == id][0] de0 = grismCat.dec[grismCat.id == id][0] #### Read EAZY outputs and get info for desired object if cat is None: cat = catIO.ReadASCIICat(OUTPUT_DIRECTORY+'../'+MAIN_OUTPUT_FILE+'.cat') dr = np.sqrt((cat.ra-ra0)**2*np.cos(de0/360.*2*np.pi)**2+(cat.dec-de0)**2)*3600. photom_idx = np.where(dr == np.min(dr))[0][0] drMatch = dr[photom_idx]*1. if drMatch > 2: return False if Verbose: print 'Read zout' if zout is None: zout = catIO.ReadASCIICat(OUTPUT_DIRECTORY+'/'+MAIN_OUTPUT_FILE+'.zout') if fout is None: fout = catIO.ReadASCIICat(OUTPUT_DIRECTORY+'/../cosmos-1.m05.v4.6.fout') if Verbose: print 'Read binaries' lambdaz, temp_sed, lci, obs_sed, fobs, efobs = \ eazy.getEazySED(photom_idx, MAIN_OUTPUT_FILE=MAIN_OUTPUT_FILE, \ OUTPUT_DIRECTORY=OUTPUT_DIRECTORY, \ CACHE_FILE = CACHE_FILE) try: lambdaz, temp_sed_sm = unicorn.analysis.convolveWithThumb(id, lambdaz, temp_sed, SPC) except: temp_sed_sm = temp_sed*1. wfc3_exist = False if GET_WFC3: wfc3_file_path = WFC3_DIR+"/*/1D/FITS/*%05d.1D.fits" %(cat.id[photom_idx]) wfc3_file = glob.glob(wfc3_file_path) if wfc3_file != []: wfc3_spec = pyfits.open(wfc3_file[0]) wfc3_exist = True else: print 'No WFC3 spectrum.' if Verbose: print 'Normalize spectrum' q = np.where((lam > 0.55e4) & (lam < 1.0e4) & (flux > 0))[0] if len(q) == 0: return False yint = np.interp(lam[q], lambdaz, temp_sed_sm) anorm = np.sum(yint*ffix[q])/np.sum(ffix[q]**2) if np.isnan(anorm): anorm=1. total_err = np.sqrt((ferr)**2+(1.0*spec.field('CONTAM'))**2)*anorm if GET_SPEC_ONLY: if drMatch > 1: return False else: return lam, ffix*anorm, total_err, lci, fobs, efobs, photom_idx if Verbose: print 'Start plot' #### Make the plot threedhst.plotting.defaultPlotParameters() xs=5.8 ys = xs/4.8*3.2 if USE_PLOT_GUI: fig = plt.figure(figsize=[xs,ys],dpi=100) else: fig = Figure(figsize=[xs,ys], dpi=100) fig.subplots_adjust(wspace=0.2,hspace=0.2,left=0.13*4.8/xs, bottom=0.15*4.8/xs,right=1.-0.02*4.8/xs,top=1-0.10*4.8/xs) ax = fig.add_subplot(111) ymax = np.max((ffix[q])*anorm) if Verbose: print 'Make the plot' ax.plot(lambdaz, temp_sed_sm, color='red') ax.plot(lam[q],ffix[q]*anorm, color='blue', alpha=0.2, linewidth=1) #### Show own extraction sp1d = threedhst.spec1d.extract1D(id, root=grism_root, path='./HTML', show=False, out2d=False)#, GRISM_NAME='G800L') lam = sp1d['lam'] flux = sp1d['flux'] ffix = sp1d['flux']-sp1d['contam'] ferr = sp1d['error'] anorm = np.sum(yint*ffix[q])/np.sum(ffix[q]**2) ax.plot(lam[q],ffix[q]*anorm, color='blue', alpha=0.6, linewidth=1) #### Show photometry + eazy template ax.errorbar(lci, fobs, yerr=efobs, color='orange', marker='o', markersize=10, linestyle='None', alpha=0.4) ax.plot(lambdaz, temp_sed_sm, color='red', alpha=0.4) if wfc3_exist: q_wfc3 = np.where((wfc3_spec[1].data.wave > 1.08e4) & (wfc3_spec[1].data.wave < 1.68e4) & (wfc3_spec[1].data.flux > 0))[0] yint_wfc3 = np.interp(wfc3_spec[1].data.wave[q_wfc3], lambdaz, temp_sed_sm) spec_wfc3 = (wfc3_spec[1].data.flux-wfc3_spec[1].data.contam)/wfc3_spec[1].data.sensitivity anorm_wfc3 = np.sum(yint_wfc3*spec_wfc3[q_wfc3])/np.sum(spec_wfc3[q_wfc3]**2) if np.isnan(anorm_wfc3): anorm_wfc3 = 1. print 'Scaling factors: ', anorm, anorm_wfc3 ax.plot(wfc3_spec[1].data.wave[q_wfc3], spec_wfc3[q_wfc3]*anorm_wfc3, color='blue',alpha=0.6, linewidth=1) ax.set_ylabel(r'$f_{\lambda}$') if plt.rcParams['text.usetex']: ax.set_xlabel(r'$\lambda$ [\AA]') ax.set_title('%s: \#%d, z=%4.1f' %(SPC.filename.split('_2_opt')[0].replace('_','\_'),id, zout.z_peak[photom_idx])) else: ax.set_xlabel(r'$\lambda$ [$\AA$]') ax.set_title('%s: #%d, z=%4.1f' %(SPC.filename.split('_2_opt')[0].replace('_','\_'),id, zout.z_peak[photom_idx])) #kmag = 25-2.5*np.log10(cat.ktot[photom_idx]) kmag = cat.kmag[photom_idx] ##### Labels label = 'ID='+r'%s K=%4.1f $\log M$=%4.1f' %(np.int(cat.id[photom_idx]), kmag, fout.field('lmass')[photom_idx]) ax.text(5e3,1.08*ymax, label, horizontalalignment='left', verticalalignment='bottom') label = 'R=%4.1f"' %(drMatch) if drMatch > 1.1: label_color = 'red' else: label_color = 'black' ax.text(2.2e4,1.08*ymax, label, horizontalalignment='right', color=label_color, verticalalignment='bottom') ax.set_xlim(xmin,xmax) ax.set_ylim(-0.1*ymax,1.2*ymax) if Verbose: print 'Save the plot' if OUT_FILE_FORMAT: out_file = '%s_%05d_SED.png' %(grism_root, id) else: out_file = OUT_FILE if USE_PLOT_GUI: fig.savefig(OUT_PATH+'/'+out_file,dpi=100,transparent=False) plt.close() else: canvas = FigureCanvasAgg(fig) canvas.print_figure(OUT_PATH+'/'+out_file, dpi=100, transparent=False) print unicorn.noNewLine+OUT_PATH+'/'+out_file if Verbose: print 'Close the plot window'
def specphot_acs_and_wfc3( id=69, grism_root='ibhm45030', MAIN_OUTPUT_FILE='cosmos-1.v4.6', OUTPUT_DIRECTORY='/Users/gbrammer/research/drg/PHOTZ/EAZY/NEWFIRM/v4.6/OUTPUT_KATE/', CACHE_FILE='Same', Verbose=False, SPC=None, cat=None, grismCat=None, zout=None, fout=None, OUT_PATH='/tmp/', OUT_FILE_FORMAT=True, OUT_FILE='junk.png', GET_SPEC_ONLY=False, GET_WFC3=False, WFC3_DIR='/3DHST/Spectra/Release/v2.0/GOODS-S'): """ specphot_acs_and_wfc3(id) Get photometry/SED fit as well as WFC3 spectrum when available and overplot G141 spectrum. This is different from unicorn.analysis.specphot() which does not get the WFC3 spectrum. """ import threedhst.eazyPy as eazy import threedhst.catIO as catIO import pyfits #### Get G141 spectrum if Verbose: print 'Read SPC' if SPC is None: SPC = threedhst.plotting.SPCFile(grism_root + '_2_opt.SPC.fits', axe_drizzle_dir='DRIZZLE_G141') spec = SPC.getSpec(id) if spec is False: return False xmin = 3000 xmax = 2.4e4 lam = spec.field('LAMBDA') flux = spec.field('FLUX') ffix = flux - spec.field('CONTAM') ferr = spec.field('FERROR') #*0.06/0.128254 if Verbose: print 'Read grism catalog' #### Read the grism catalog and get coords of desired object if grismCat is None: grismCat = threedhst.sex.mySexCat('DATA/' + grism_root + '_drz.cat') #### Source size R = np.sqrt(np.cast[float](grismCat.A_IMAGE) * np.cast[float](grismCat.B_IMAGE)) grism_idx = np.where(grismCat.id == id)[0][0] Rmatch = R[grism_idx] * 1. ra0 = grismCat.ra[grismCat.id == id][0] de0 = grismCat.dec[grismCat.id == id][0] #### Read EAZY outputs and get info for desired object if cat is None: cat = catIO.ReadASCIICat(OUTPUT_DIRECTORY + '../' + MAIN_OUTPUT_FILE + '.cat') dr = np.sqrt((cat.ra - ra0)**2 * np.cos(de0 / 360. * 2 * np.pi)**2 + (cat.dec - de0)**2) * 3600. photom_idx = np.where(dr == np.min(dr))[0][0] drMatch = dr[photom_idx] * 1. if drMatch > 2: return False if Verbose: print 'Read zout' if zout is None: zout = catIO.ReadASCIICat(OUTPUT_DIRECTORY + '/' + MAIN_OUTPUT_FILE + '.zout') if fout is None: fout = catIO.ReadASCIICat(OUTPUT_DIRECTORY + '/../cosmos-1.m05.v4.6.fout') if Verbose: print 'Read binaries' lambdaz, temp_sed, lci, obs_sed, fobs, efobs = \ eazy.getEazySED(photom_idx, MAIN_OUTPUT_FILE=MAIN_OUTPUT_FILE, \ OUTPUT_DIRECTORY=OUTPUT_DIRECTORY, \ CACHE_FILE = CACHE_FILE) try: lambdaz, temp_sed_sm = unicorn.analysis.convolveWithThumb( id, lambdaz, temp_sed, SPC) except: temp_sed_sm = temp_sed * 1. wfc3_exist = False if GET_WFC3: wfc3_file_path = WFC3_DIR + "/*/1D/FITS/*%05d.1D.fits" % ( cat.id[photom_idx]) wfc3_file = glob.glob(wfc3_file_path) if wfc3_file != []: wfc3_spec = pyfits.open(wfc3_file[0]) wfc3_exist = True else: print 'No WFC3 spectrum.' if Verbose: print 'Normalize spectrum' q = np.where((lam > 0.55e4) & (lam < 1.0e4) & (flux > 0))[0] if len(q) == 0: return False yint = np.interp(lam[q], lambdaz, temp_sed_sm) anorm = np.sum(yint * ffix[q]) / np.sum(ffix[q]**2) if np.isnan(anorm): anorm = 1. total_err = np.sqrt((ferr)**2 + (1.0 * spec.field('CONTAM'))**2) * anorm if GET_SPEC_ONLY: if drMatch > 1: return False else: return lam, ffix * anorm, total_err, lci, fobs, efobs, photom_idx if Verbose: print 'Start plot' #### Make the plot threedhst.plotting.defaultPlotParameters() xs = 5.8 ys = xs / 4.8 * 3.2 if USE_PLOT_GUI: fig = plt.figure(figsize=[xs, ys], dpi=100) else: fig = Figure(figsize=[xs, ys], dpi=100) fig.subplots_adjust(wspace=0.2, hspace=0.2, left=0.13 * 4.8 / xs, bottom=0.15 * 4.8 / xs, right=1. - 0.02 * 4.8 / xs, top=1 - 0.10 * 4.8 / xs) ax = fig.add_subplot(111) ymax = np.max((ffix[q]) * anorm) if Verbose: print 'Make the plot' ax.plot(lambdaz, temp_sed_sm, color='red') ax.plot(lam[q], ffix[q] * anorm, color='blue', alpha=0.2, linewidth=1) #### Show own extraction sp1d = threedhst.spec1d.extract1D(id, root=grism_root, path='./HTML', show=False, out2d=False) #, GRISM_NAME='G800L') lam = sp1d['lam'] flux = sp1d['flux'] ffix = sp1d['flux'] - sp1d['contam'] ferr = sp1d['error'] anorm = np.sum(yint * ffix[q]) / np.sum(ffix[q]**2) ax.plot(lam[q], ffix[q] * anorm, color='blue', alpha=0.6, linewidth=1) #### Show photometry + eazy template ax.errorbar(lci, fobs, yerr=efobs, color='orange', marker='o', markersize=10, linestyle='None', alpha=0.4) ax.plot(lambdaz, temp_sed_sm, color='red', alpha=0.4) if wfc3_exist: q_wfc3 = np.where((wfc3_spec[1].data.wave > 1.08e4) & (wfc3_spec[1].data.wave < 1.68e4) & (wfc3_spec[1].data.flux > 0))[0] yint_wfc3 = np.interp(wfc3_spec[1].data.wave[q_wfc3], lambdaz, temp_sed_sm) spec_wfc3 = (wfc3_spec[1].data.flux - wfc3_spec[1].data.contam) / wfc3_spec[1].data.sensitivity anorm_wfc3 = np.sum(yint_wfc3 * spec_wfc3[q_wfc3]) / np.sum( spec_wfc3[q_wfc3]**2) if np.isnan(anorm_wfc3): anorm_wfc3 = 1. print 'Scaling factors: ', anorm, anorm_wfc3 ax.plot(wfc3_spec[1].data.wave[q_wfc3], spec_wfc3[q_wfc3] * anorm_wfc3, color='blue', alpha=0.6, linewidth=1) ax.set_ylabel(r'$f_{\lambda}$') if plt.rcParams['text.usetex']: ax.set_xlabel(r'$\lambda$ [\AA]') ax.set_title( '%s: \#%d, z=%4.1f' % (SPC.filename.split('_2_opt')[0].replace( '_', '\_'), id, zout.z_peak[photom_idx])) else: ax.set_xlabel(r'$\lambda$ [$\AA$]') ax.set_title( '%s: #%d, z=%4.1f' % (SPC.filename.split('_2_opt')[0].replace( '_', '\_'), id, zout.z_peak[photom_idx])) #kmag = 25-2.5*np.log10(cat.ktot[photom_idx]) kmag = cat.kmag[photom_idx] ##### Labels label = 'ID=' + r'%s K=%4.1f $\log M$=%4.1f' % (np.int( cat.id[photom_idx]), kmag, fout.field('lmass')[photom_idx]) ax.text(5e3, 1.08 * ymax, label, horizontalalignment='left', verticalalignment='bottom') label = 'R=%4.1f"' % (drMatch) if drMatch > 1.1: label_color = 'red' else: label_color = 'black' ax.text(2.2e4, 1.08 * ymax, label, horizontalalignment='right', color=label_color, verticalalignment='bottom') ax.set_xlim(xmin, xmax) ax.set_ylim(-0.1 * ymax, 1.2 * ymax) if Verbose: print 'Save the plot' if OUT_FILE_FORMAT: out_file = '%s_%05d_SED.png' % (grism_root, id) else: out_file = OUT_FILE if USE_PLOT_GUI: fig.savefig(OUT_PATH + '/' + out_file, dpi=100, transparent=False) plt.close() else: canvas = FigureCanvasAgg(fig) canvas.print_figure(OUT_PATH + '/' + out_file, dpi=100, transparent=False) print unicorn.noNewLine + OUT_PATH + '/' + out_file if Verbose: print 'Close the plot window'
axes=None, individual_templates=True, fnu=False) plt.savefig('eazy_fit.png') plt.close() ################################################ #### Pull out data from the BINARY_OUTPUTS files ################################################ ## SED, data & fit sed = eazy.getEazySED(17, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', scale_flambda=True, verbose=False, individual_templates=False) lambdaz, temp_sed, lci, obs_sed, fobs, efobs = sed axes[0].scatter(lci, obs_sed, color='orange', zorder=2) axes[0].scatter(lci, fobs, color='green', marker='s', s=150, zorder=2) ## p(z) zgrid, pzi, prior = eazy.getEazyPz(17, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', binaries=None, get_prior=True)