Example #1
0
def initModelFit(sci_data, lsf, modelset='btsettl08'):
	"""
	Conduct simple chisquare fit to obtain the initial parameters
	for the forward modeling MCMC.

	The function would calculate the chisquare for teff, logg, vini, rv, and alpha.

	Parameters
	----------
	data 				:	spectrum object
							input science data

	lsf 				:	float
							line spread function for the NIRSPEC

	Returns
	-------
	best_params_dic 	:	dic
							a dictionary that stores the best parameters for 
							teff, logg, vsini, rv, and alpha

	chisquare 			:	int
							minimum chisquare

	"""
	data            = copy.deepcopy(sci_data)

	## set up the parameter grid for chisquare computation
	teff_array      = np.arange(1200,3001,100)
	logg_array      = np.arange(3.5,5.51,0.5)
	vsini_array     = np.arange(10,101,10)
	rv_array        = np.arange(-200,201,50)
	alpha_array     = np.arange(0.5,2.01,0.5)
	chisquare_array = np.empty(len(teff_array)*len(logg_array)*len(vsini_array)*len(rv_array)*len(alpha_array))\
	.reshape(len(teff_array),len(logg_array),len(vsini_array),len(rv_array),len(alpha_array))

	time1 = time.time()
	for i, teff in enumerate(teff_array):
		for j, logg in enumerate(logg_array):
			for k, vsini in enumerate(vsini_array):
				for l, rv in enumerate(rv_array):
					for m, alpha in enumerate(alpha_array):
						model = nsp.makeModel(teff, logg, 0.0, vsini, rv, alpha, 0, 0,
							lsf=lsf, order=data.order, data=data, modelset=modelset)
						chisquare_array[i,j,k,l,m] = nsp.chisquare(data, model)
	time2 = time.time()
	print("total time:",time2-time1)

	ind = np.unravel_index(np.argmin(chisquare_array, axis=None), chisquare_array.shape)
	print("ind ",ind)
	chisquare       = chisquare_array[ind]

	best_params_dic = {'teff':teff_array[ind[0]], 'logg':logg_array[ind[1]], 
	'vsini':vsini_array[ind[2]], 'rv':rv_array[ind[3]], 'alpha':alpha_array[ind[4]]}

	print(best_params_dic, chisquare)

	return best_params_dic , chisquare
def lnlike(theta, data=data):
	"""
	Log-likelihood, computed from chi-squared.

	Parameters
	----------
	theta
	data

	Returns
	-------
	-0.5 * chi-square + sum of the log of the noise
	"""
	## Parameters MCMC
	lsf, alpha, A, B = theta

	model = makeTelluricModel(lsf, alpha, A, B, data=data)

	chisquare = nsp.chisquare(data, model)

	return -0.5 * (chisquare + np.sum(np.log(2*np.pi*data.noise**2)))
def lnlike(theta, data, lsf):
    """
	Log-likelihood, computed from chi-squared.

	Parameters
	----------
	theta
	lsf
	data

	Returns
	-------
	-0.5 * chi-square + sum of the log of the noise

	"""

    ## Parameters MCMC
    teff, logg, vsini, rv, alpha, A, N = theta  #A: flux offset; N: noise prefactor

    ## wavelength offset is set to 0
    model = makeModel(teff,
                      logg,
                      0.0,
                      vsini,
                      rv,
                      alpha,
                      0.0,
                      A,
                      lsf=lsf,
                      data=data,
                      modelset=modelset,
                      instrument=instrument)
    print(len(data.wave), len(model.wave), len(data.noise), teff, logg,
          modelset, instrument)
    chisquare = nsp.chisquare(data, model) / N**2
    print(chisquare)
    return -0.5 * (chisquare + np.sum(np.log(2 * np.pi * (data.noise * N)**2)))
def lnlike(theta, data, lsf):
    """
	Log-likelihood, computed from chi-squared.

	Parameters
	----------
	theta
	lsf
	data

	Returns
	-------
	-0.5 * chi-square + sum of the log of the noise

	"""

    ## Parameters MCMC
    teff, logg, vsini, rv, alpha, A, B, N = theta  #N noise prefactor
    #teff, logg, vsini, rv, alpha, A, B, freq, amp, phase = theta

    model = nsp.makeModel(teff,
                          logg,
                          0.0,
                          vsini,
                          rv,
                          alpha,
                          B,
                          A,
                          lsf=lsf,
                          order=data.order,
                          data=data,
                          modelset=modelset)

    chisquare = nsp.chisquare(data, model) / N**2

    return -0.5 * (chisquare + np.sum(np.log(2 * np.pi * (data.noise * N)**2)))
round(teff_mcmc[2]),
round(logg_mcmc[0],1),
round(logg_mcmc[1],3),
round(logg_mcmc[2],3),
round(vsini_mcmc[0],2),
round(vsini_mcmc[1],2),
round(vsini_mcmc[2],2),
round(rv_mcmc[0]+barycorr,2),
round(rv_mcmc[1],2),
round(rv_mcmc[2],2)),
            color='C0',
            horizontalalignment='right',
            verticalalignment='center',
            fontsize=12)
plt.figtext(0.89,0.79,r"$\chi^2$ = {}, DOF = {}".format(\
 round(nsp.chisquare(data,model)), round(len(data.wave-ndim)/3)),
            color='k',
            horizontalalignment='right',
            verticalalignment='center',
            fontsize=12)
plt.minorticks_on()

ax2 = ax1.twiny()
ax2.plot(pixel, data.flux, color='w', alpha=0)
ax2.set_xlabel('Pixel', fontsize=15)
ax2.tick_params(labelsize=15)
ax2.set_xlim(pixel[0], pixel[-1])
ax2.minorticks_on()

#plt.legend()
plt.savefig(save_to_path + '/spectrum.png', dpi=300, bbox_inches='tight')
Example #6
0
def getAlpha(telluric_data,lsf,continuum=True,test=False,save_path=None):
	"""
	Return a best alpha value from a telluric data.
	"""
	alpha_list = []
	test_alpha = np.arange(0.1,7,0.1)

	data = copy.deepcopy(telluric_data)
	if continuum is True:
		data = nsp.continuumTelluric(data=data, order=data.order)

	for i in test_alpha:
		telluric_model = nsp.convolveTelluric(lsf,data,
			alpha=i)
		#telluric_model.flux **= i 
		if data.order == 59:
			# mask hydrogen absorption feature
			data2          = copy.deepcopy(data)
			tell_mdl       = copy.deepcopy(telluric_model)
			mask_pixel     = 450
			data2.wave     = data2.wave[mask_pixel:]
			data2.flux     = data2.flux[mask_pixel:]
			data2.noise    = data2.noise[mask_pixel:]
			tell_mdl.wave  = tell_mdl.wave[mask_pixel:]
			tell_mdl.flux  = tell_mdl.flux[mask_pixel:]

			chisquare = nsp.chisquare(data2,tell_mdl)

		else:
			chisquare = nsp.chisquare(data,telluric_model)
		alpha_list.append([chisquare,i])

		if test is True:
			plt.plot(telluric_model.wave,telluric_model.flux+i*10,
				'k-',alpha=0.5)

	if test is True:
		plt.plot(telluric_data.wave,telluric_data.flux,
			'r-',alpha=0.5)
		plt.rc('font', family='sans-serif')
		plt.title("Test Alpha",fontsize=15)
		plt.xlabel("Wavelength ($\AA$)",fontsize=12)
		plt.ylabel("Transmission + Offset",fontsize=12)
		plt.minorticks_on()
		if save_path is not None:
			plt.savefig(save_path+\
				"/{}_O{}_alpha_data_mdl.png"\
				.format(telluric_data.name,
					telluric_data.order))
		plt.show()
		plt.close()

		fig, ax = plt.subplots()
		plt.rc('font', family='sans-serif')
		for i in range(len(alpha_list)):
			ax.plot(alpha_list[i][1],alpha_list[i][0],'k.',alpha=0.5)
		ax.plot(min(alpha_list)[1],min(alpha_list)[0],'r.',
			label="best alpha {}".format(min(alpha_list)[1]))
		ax.set_xlabel(r"$\alpha$",fontsize=12)
		ax.set_ylabel("$\chi^2$",fontsize=12)
		plt.minorticks_on()
		plt.legend(fontsize=10)
		if save_path is not None:
			plt.savefig(save_path+\
				"/{}_O{}_alpha_chi2.png"\
				.format(telluric_data.name,
					telluric_data.order))
		plt.show()
		plt.close()

	alpha = min(alpha_list)[1]

	return alpha
Example #7
0
def getLSF(telluric_data, alpha=1.0, continuum=True,test=False,save_path=None):
	"""
	Return a best LSF value from a telluric data.
	"""
	lsf_list = []
	test_lsf = np.arange(3.0,13.0,0.1)
	
	data = copy.deepcopy(telluric_data)
	if continuum is True:
		data = nsp.continuumTelluric(data=data)

	data.flux **= alpha
	for i in test_lsf:
		telluric_model = nsp.convolveTelluric(i,data)
		if telluric_data.order == 59:
			telluric_model.flux **= 3
			# mask hydrogen absorption feature
			data2          = copy.deepcopy(data)
			tell_mdl       = copy.deepcopy(telluric_model)
			mask_pixel     = 450
			data2.wave     = data2.wave[mask_pixel:]
			data2.flux     = data2.flux[mask_pixel:]
			data2.noise    = data2.noise[mask_pixel:]
			tell_mdl.wave  = tell_mdl.wave[mask_pixel:]
			tell_mdl.flux  = tell_mdl.flux[mask_pixel:]

			chisquare = nsp.chisquare(data2,tell_mdl)

		else:
			chisquare = nsp.chisquare(data,telluric_model)
		lsf_list.append([chisquare,i])

		if test is True:
			plt.plot(telluric_model.wave,telluric_model.flux+(i-3)*10+1,
				'r-',alpha=0.5)

	if test is True:
		plt.plot(data.wave,data.flux,
			'k-',label='telluric data',alpha=0.5)
		plt.title("Test LSF",fontsize=15)
		plt.xlabel("Wavelength ($\AA$)",fontsize=12)
		plt.ylabel("Transmission + Offset",fontsize=12)
		plt.minorticks_on()
		if save_path is not None:
			plt.savefig(save_path+\
				"/{}_O{}_lsf_data_mdl.png"\
				.format(data.name, data.order))
		#plt.show()
		plt.close()

		fig, ax = plt.subplots()
		for i in range(len(lsf_list)):
			ax.plot(lsf_list[i][1],lsf_list[i][0],'k.',alpha=0.5)
		ax.plot(min(lsf_list)[1],min(lsf_list)[0],'r.',
			label="best LSF {} km/s".format(min(lsf_list)[1]))
		ax.set_xlabel("LSF (km/s)",fontsize=12)
		ax.set_ylabel("$\chi^2$",fontsize=11)
		plt.minorticks_on()
		plt.legend(fontsize=10)
		if save_path is not None:
			plt.savefig(save_path+\
				"/{}_O{}_lsf_chi2.png"\
				.format(data.name, data.order))
		#plt.show()
		plt.close()

	lsf = min(lsf_list)[1]

	if telluric_data.order == 61 or telluric_data.order == 62 \
	or telluric_data.order == 63: #or telluric_data.order == 64:
		lsf = 5.5
		print("The LSF is obtained from orders 60 and 65 (5.5 km/s).")

	return lsf