def fit_spectra(data, t0, sig, N=10000): """Fit spectra runs global mcmc on one data set Parameters: ----------- data: list List containing a spectral object, SN template flux at the same wavelengths as the spectral object, and a galaxy template flux at the same wavelengths of the spectral object:w Output ------ n1: float Best normalization for the SN template n2: float Best normalization for the galaxy template likelihood: float Likelihood for best fit combination of SN and galaxy template """ global_mcmc.mcmc(likelihood, prior, t0, N, sig, data, "sn_spectral_fitting.txt") # read in results from mcmc p1, p2, l = np.loadtxt("sn_spectral_fitting.txt", unpack=True) i = np.array(l).argmax() n1 = p1[i] n2 = p2[i] return n1, n2, l[i]
fout=open('final_params.dat', 'w') sig=np.array([0.1,0.1])*7 t0=np.array([0.5,0.5]) spec=loadtext(sys.argv[1]) spec.wavelength = spec.wavelength / (1+float(sys.argv[2])) N=int(sys.argv[3]) for snt in snlist: for galt in gallist: sntmpl=loadtext2(snt) galtmpl=loadsdss(pyfits.open(galt)) isnflux=interpo(spec.wavelength, sntmpl.wavelength, sntmpl.flux) igalflux=interpo(spec.wavelength, galtmpl.wavelength, galtmpl.flux ) data=[spec, isnflux, igalflux] global_mcmc.mcmc(likelihood,prior,t0,N,sig,data, snt + '_' + galt +'.txt') #read in results from mcmc p1, p2, l = np.loadtxt(snt + '_' + galt + '.txt', unpack=True) i = np.array(l).argmax() n1 = p1[i] n2 = p2[i] fout.write('%s %s %f %f %f\n' % (snt,galt, n1, n2, l[i])) fout.close() fin=open('final_params.dat', 'r') fin_lines=file.readlines(fin) norm1, norm2,like=np.loadtxt('final_params.dat',usecols=(2, 3, 4), unpack=True) k=like.argmax() print fin_lines[k] norm1=float(fin_lines[k].split()[2])