def find_lines(data, thresh=3): nside = 8 nleft = 8 nright = 8 index = 8 B = 1.0 / 1200 while index < data.wave.size - 8: lines = array([data.wave[index]]) results,chisq = line_flux(data,lines,nleft=nleft,nright=nright, \ quiet=True) results_fw,chisq_fw = line_flux(data,lines,nleft=nleft,nright=nright,\ quiet=True,fixed_width=True) chisq_test = (chisq <= thresh) & (chisq_fw <= thresh) SN_test = (results[0,1]/results[0,2] >= 5) & \ (results_fw[0,1]/results_fw[0,2] >= 5) width_test = (results[0,3] >= lines[0]*B/2) & (results[0,3] <= \ lines[0]*3*B) flux_test = absv(results[0, 1] - results_fw[0, 1]) / results[0, 1] <= 0.2 #if (chisq_test & flux_test) & (SN_test & width_test): if (chisq_test & SN_test): print(" {0:f} {1:e} {2:e} {3:f} {4:f}".format( \ results[0,0],results[0,1],results[0,2],results[0,3],chisq)) results,chisq = line_flux(data,lines,nleft=nleft,nright=nright, \ plotout="{0:6.3f}.pdf".format(results[0,0]),quiet=True) index = where(absv(data.wave-data.wave[index]-0.05) == \ absv(data.wave-data.wave[index]-0.05).min())[0][0] nside = 6 else: index = where(absv(data.wave-data.wave[index]-0.05) == \ absv(data.wave-data.wave[index]-0.05).min())[0][0]
def find_lines(data,thresh=3): nside = 8 nleft = 8 nright = 8 index = 8 B = 1.0/1200 while index < data.wave.size-8: lines = array([data.wave[index]]) results,chisq = line_flux(data,lines,nleft=nleft,nright=nright, \ quiet=True) results_fw,chisq_fw = line_flux(data,lines,nleft=nleft,nright=nright,\ quiet=True,fixed_width=True) chisq_test = (chisq <= thresh) & (chisq_fw <= thresh) SN_test = (results[0,1]/results[0,2] >= 5) & \ (results_fw[0,1]/results_fw[0,2] >= 5) width_test = (results[0,3] >= lines[0]*B/2) & (results[0,3] <= \ lines[0]*3*B) flux_test = absv(results[0,1]-results_fw[0,1])/results[0,1] <= 0.2 #if (chisq_test & flux_test) & (SN_test & width_test): if (chisq_test & SN_test): print(" {0:f} {1:e} {2:e} {3:f} {4:f}".format( \ results[0,0],results[0,1],results[0,2],results[0,3],chisq)) results,chisq = line_flux(data,lines,nleft=nleft,nright=nright, \ plotout="{0:6.3f}.pdf".format(results[0,0]),quiet=True) index = where(absv(data.wave-data.wave[index]-0.05) == \ absv(data.wave-data.wave[index]-0.05).min())[0][0] nside = 6 else: index = where(absv(data.wave-data.wave[index]-0.05) == \ absv(data.wave-data.wave[index]-0.05).min())[0][0]
def line_flux(data,lines,nleft=8,nright=8,plotout=None,quiet=False, \ fixed_width=False,fringing=False): B = 1.0 / 1500 lines = array(lines) wave = data.wave flux = data.flux unc = data.unc nlines = lines.size ind = arange(nlines) left = where(absv(wave - lines[0]) == absv(wave - lines[0]).min()) right = where(absv(wave-lines[lines.size-1]) == absv(wave- \ lines[lines.size-1]).min()) prange = arange(right[0] - left[0] + nleft + nright + 1) - nleft + left[0] wave_fit = wave[prange] flux_fit = flux[prange] unc_fit = unc[prange] flux_in = ones(lines.size) #sigma_in = lines*B fwhm_in = lines / (888.488 - 9.553 * lines) sigma_in = fwhm_in / (2 * sqrt(2 * log(2))) omega_in = array([2 * pi / (4 * sigma_in[0])]) amp_in = array([0.1]) phi_in = array([pi / 2]) slope = array([(flux_fit[flux_fit.size-1]-flux_fit[0])/(wave_fit.max()- \ wave_fit.min())]) yint = array([flux_fit[0] - slope[0] * wave_fit.min()]) if fringing: A = concatenate((flux_in,lines,sigma_in,yint,slope, \ amp_in,omega_in,phi_in)) else: A = concatenate((flux_in, lines, sigma_in, yint, slope)) parinfo = [] for i in arange(A.size): parinfo.append({"limited":[False,False], "limits":[0.0,0.0], \ "mpside":2, "fixed":False}) for i in arange(lines.size): parinfo[i]["limited"] = [True, False] parinfo[i]["limits"] = [0.001, 0.0] parinfo[lines.size + i]["limited"] = [True, True] parinfo[lines.size + i]["limits"] = [lines[i] - 0.025, lines[i] + 0.025] parinfo[2 * lines.size + i]["limited"] = [True, False] parinfo[2 * lines.size + i]["limits"] = [1.0e-3, 0.0] if fixed_width: parinfo[2 * lines.size + i]["fixed"] = True if fringing: parinfo[-1]["limited"] = [True, True] parinfo[-1]["limits"] = [0.0, 2 * pi] fa = {"x": wave_fit, "y": flux_fit, "err": unc_fit, "fringing": fringing} mfit = mpfit(gauss, xall=A, functkw=fa, parinfo=parinfo, quiet=1) A = mfit.params fit = gauss(A,x=wave_fit,y=flux_fit,err=unc_fit,fringing=fringing)[1]* \ unc_fit*(-1)+flux_fit chisq = ((fit - flux_fit)**2 / unc_fit**2).sum() / (wave_fit.size - A.size) # Calculate the flux and uncertainty. F = sqrt(2*pi)*A[ind]*Jy*(c*A[ind+2*nlines]*1.0e-4)/(A[ind+nlines]* \ 1.0e-4)**2/1.0e7 deltaF = ones(nlines) for i in arange(nlines): deltaF[i] = sqrt(((unc_fit*Jy*c*B/(wave_fit*1.0e-4)*exp(-1.0*( \ wave_fit-A[i+nlines])**2/(2*A[i+2*nlines]**2)))**2).sum())/1.0e7 # Output the results. Results = concatenate((array(mat(A[ind+nlines]).T),array(mat(F).T), \ array(mat(deltaF).T),array(mat(A[ind+2*nlines]*2*sqrt(2*log(2))).T)), \ axis=1) if quiet == False: print("") for i in arange(lines.size): print(" {0:>6.3f} {1:>9.3e} {2:>9.3e} {3:>6.4f}".format( \ Results[i,0],Results[i,1],Results[i,2],Results[i,3])) print("") print("Reduced chi-squared of the fit: ", chisq) print("") # Plot the results. if (plotout != None) or (quiet == False): plt.errorbar(wave_fit, flux_fit, fmt="b", yerr=unc_fit) plt.plot(wave_fit, fit, "r") plt.xlabel("$\lambda$ [$\mu$" + "m]") plt.ylabel(r"F$_{\nu}$ [Jy]") if plotout != None: plt.savefig(plotout) elif quiet == False: plt.show() plt.clf() return Results, chisq
def line_flux(data,lines,nleft=8,nright=8,plotout=None,quiet=False, \ fixed_width=False,fringing=False): B = 1.0/1500 lines = array(lines) wave = data.wave flux = data.flux unc = data.unc nlines = lines.size ind = arange(nlines) left = where(absv(wave-lines[0]) == absv(wave-lines[0]).min()) right = where(absv(wave-lines[lines.size-1]) == absv(wave- \ lines[lines.size-1]).min()) prange = arange(right[0]-left[0]+nleft+nright+1)-nleft+left[0] wave_fit = wave[prange] flux_fit = flux[prange] unc_fit = unc[prange] flux_in = ones(lines.size) #sigma_in = lines*B fwhm_in = lines/(888.488-9.553*lines) sigma_in = fwhm_in/(2*sqrt(2*log(2))) omega_in = array([2*pi/(4*sigma_in[0])]) amp_in = array([0.1]) phi_in = array([pi/2]) slope = array([(flux_fit[flux_fit.size-1]-flux_fit[0])/(wave_fit.max()- \ wave_fit.min())]) yint = array([flux_fit[0]-slope[0]*wave_fit.min()]) if fringing: A = concatenate((flux_in,lines,sigma_in,yint,slope, \ amp_in,omega_in,phi_in)) else: A = concatenate((flux_in,lines,sigma_in,yint,slope)) parinfo = [] for i in arange(A.size): parinfo.append({"limited":[False,False], "limits":[0.0,0.0], \ "mpside":2, "fixed":False}) for i in arange(lines.size): parinfo[i]["limited"] = [True,False] parinfo[i]["limits"] = [0.001,0.0] parinfo[lines.size+i]["limited"] = [True,True] parinfo[lines.size+i]["limits"] = [lines[i]-0.025,lines[i]+0.025] parinfo[2*lines.size+i]["limited"] = [True,False] parinfo[2*lines.size+i]["limits"] = [1.0e-3,0.0] if fixed_width: parinfo[2*lines.size+i]["fixed"] = True if fringing: parinfo[-1]["limited"] = [True,True] parinfo[-1]["limits"] = [0.0,2*pi] fa = {"x":wave_fit, "y":flux_fit, "err":unc_fit, "fringing":fringing} mfit = mpfit(gauss,xall=A,functkw=fa,parinfo=parinfo,quiet=1) A=mfit.params fit = gauss(A,x=wave_fit,y=flux_fit,err=unc_fit,fringing=fringing)[1]* \ unc_fit*(-1)+flux_fit chisq = ((fit - flux_fit)**2/unc_fit**2).sum()/(wave_fit.size-A.size) # Calculate the flux and uncertainty. F = sqrt(2*pi)*A[ind]*Jy*(c*A[ind+2*nlines]*1.0e-4)/(A[ind+nlines]* \ 1.0e-4)**2/1.0e7 deltaF = ones(nlines) for i in arange(nlines): deltaF[i] = sqrt(((unc_fit*Jy*c*B/(wave_fit*1.0e-4)*exp(-1.0*( \ wave_fit-A[i+nlines])**2/(2*A[i+2*nlines]**2)))**2).sum())/1.0e7 # Output the results. Results = concatenate((array(mat(A[ind+nlines]).T),array(mat(F).T), \ array(mat(deltaF).T),array(mat(A[ind+2*nlines]*2*sqrt(2*log(2))).T)), \ axis=1) if quiet == False: print("") for i in arange(lines.size): print(" {0:>6.3f} {1:>9.3e} {2:>9.3e} {3:>6.4f}".format( \ Results[i,0],Results[i,1],Results[i,2],Results[i,3])) print("") print("Reduced chi-squared of the fit: ",chisq) print("") # Plot the results. if (plotout != None) or (quiet == False): plt.errorbar(wave_fit,flux_fit,fmt="b",yerr=unc_fit) plt.plot(wave_fit,fit,"r") plt.xlabel("$\lambda$ [$\mu$"+"m]") plt.ylabel(r"F$_{\nu}$ [Jy]") if plotout != None: plt.savefig(plotout) elif quiet == False: plt.show() plt.clf() return Results, chisq