예제 #1
0
def makeTelluricModel(lsf, alpha, flux_offset, wave_offset0, data=data):
	"""
	Make a telluric model as a function of LSF, alpha, and flux offset.
	"""
	data2               = copy.deepcopy(data)
	data2.wave          = data2.wave + wave_offset0
	#data2.wave          = data2.wave * (1 + wave_offset1) + wave_offset0
	telluric_model      = nsp.convolveTelluric(lsf, data2, alpha=alpha)

	if data.order == 35:
		from scipy.optimize import curve_fit
		data_flux_avg = np.average(data2.flux)
		popt, pcov = curve_fit(nsp.voigt_profile,data2.wave[0:-10], data2.flux[0:-10], 
			p0=[21660,data_flux_avg,0.1,0.1,0.01,0.1,10000,1000], maxfev=10000)
		#model               = nsp.continuum(data=data2, mdl=telluric_model, deg=2)
		model = telluric_model
		max_model_flux      = np.max(nsp.voigt_profile(data2.wave, *popt))
		model.flux         *= nsp.voigt_profile(data2.wave, *popt)/max_model_flux
		model               = nsp.continuum(data=data2, mdl=model, deg=2)
	else:
		model               = nsp.continuum(data=data2, mdl=telluric_model, deg=2)
	
	model.flux             += flux_offset

	return model
예제 #2
0
	def bestParams2(theta, data):

		i, alpha, c2, c0, c1 = theta 
		data2                = copy.deepcopy(data)
		data2.wave           = data2.wave*c1 + c0
		telluric_model       = nsp.convolveTelluric(i, data2, alpha=alpha)
		model                = nsp.continuum(data=data2, mdl=telluric_model)
		return np.sum(data.flux - (model.flux + c2))**2
예제 #3
0
def makeTelluricModel(lsf, airmass, pwv, flux_offset, wave_offset, data):
	"""
	Make a telluric model as a function of LSF, alpha, and flux offset.
	"""
	data2               = copy.deepcopy(data)
	data2.wave          = data2.wave + wave_offset
	telluric_model      = convolveTelluric(lsf, airmass, pwv, data2)
	
	model               = nsp.continuum(data=data2, mdl=telluric_model, deg=2)
	
	model.flux         += flux_offset

	return model
예제 #4
0
	def bestParams(data, i, alpha, c2, c0):

		data2          = copy.deepcopy(data)
		data2.wave     = data2.wave + c0
		telluric_model = nsp.convolveTelluric(i, data2, alpha=alpha)
		model          = nsp.continuum(data=data2, mdl=telluric_model)
		#plt.figure(2)
		#plt.plot(model.wave, model.flux+c2, 'r-', alpha=0.5)
		#plt.plot(data.wave*c1+c0, data.flux, 'b-', alpha=0.5)
		#plt.close()
		#plt.show()
		#sys.exit()
		return model.flux + c2
예제 #5
0
def getLSF2(telluric_data, continuum=True, test=False, save_path=None):
	"""
	Return a best LSF value from a telluric data.
	"""
	
	data = copy.deepcopy(telluric_data)

	def bestParams(data, i, alpha, c2, c0):

		data2          = copy.deepcopy(data)
		data2.wave     = data2.wave + c0
		telluric_model = nsp.convolveTelluric(i, data2, alpha=alpha)
		model          = nsp.continuum(data=data2, mdl=telluric_model)
		#plt.figure(2)
		#plt.plot(model.wave, model.flux+c2, 'r-', alpha=0.5)
		#plt.plot(data.wave*c1+c0, data.flux, 'b-', alpha=0.5)
		#plt.close()
		#plt.show()
		#sys.exit()
		return model.flux + c2

	def bestParams2(theta, data):

		i, alpha, c2, c0, c1 = theta 
		data2                = copy.deepcopy(data)
		data2.wave           = data2.wave*c1 + c0
		telluric_model       = nsp.convolveTelluric(i, data2, alpha=alpha)
		model                = nsp.continuum(data=data2, mdl=telluric_model)
		return np.sum(data.flux - (model.flux + c2))**2

	from scipy.optimize import curve_fit, minimize

	popt, pcov = curve_fit(bestParams, data, data.flux, p0=[4.01, 1.01, 0.01, 1.01], maxfev=1000000, epsfcn=0.1)

	#nll = lambda *args: bestParams2(*args)
	#results = minimize(nll, [3., 1., 0.1, -10., 1.], args=(data))
	#popt = results['x']

	data.wave      = data.wave+popt[3]

	telluric_model = nsp.convolveTelluric(popt[0], data, alpha=popt[1])
	model          = nsp.continuum(data=data, mdl=telluric_model)

	#model.flux * np.e**(-popt[2]) + popt[3]
	model.flux + popt[2]

	return popt[0]
def makeModel(teff, logg, z, vsini, rv, alpha, wave_offset, flux_offset,
              **kwargs):
    """
	Return a forward model.

	Parameters
	----------
	params : a dictionary that specifies the parameters such as teff, logg, z.
	data   : an input science data used for continuum correction

	Returns
	-------
	model: a synthesized model
	"""

    # read in the parameters
    order = kwargs.get('order', 33)
    modelset = kwargs.get('modelset', 'btsettl08')
    lsf = kwargs.get('lsf', 6.0)  # instrumental LSF
    tell = kwargs.get('tell', True)  # apply telluric
    data = kwargs.get('data', None)  # for continuum correction and resampling

    if data is not None:
        order = data.order
    # read in a model
    #model    = nsp.Model(teff=teff, logg=logg, feh=z, order=order, modelset=modelset)
    model = nsp.Model()
    model.wave, model.flux = InterpModel(teff,
                                         logg,
                                         modelset=modelset,
                                         order=order)
    model.wave *= 10000
    # wavelength offset
    #model.wave += wave_offset

    # apply vsini
    model.flux = nsp.broaden(wave=model.wave,
                             flux=model.flux,
                             vbroad=vsini,
                             rotate=True,
                             gaussian=False)

    # apply rv (including the barycentric correction)
    model.wave = nsp.rvShift(model.wave, rv=rv)

    # apply telluric
    if tell is True:
        model = nsp.applyTelluric(model=model, alpha=alpha, airmass='1.5')
    # NIRSPEC LSF
    model.flux = nsp.broaden(wave=model.wave,
                             flux=model.flux,
                             vbroad=lsf,
                             rotate=False,
                             gaussian=True)

    # add a fringe pattern to the model
    #model.flux *= (1+amp*np.sin(freq*(model.wave-phase)))

    # wavelength offset
    model.wave += wave_offset

    # integral resampling
    if data is not None:
        model.flux = np.array(
            nsp.integralResample(xh=model.wave, yh=model.flux, xl=data.wave))
        model.wave = data.wave
        # contunuum correction
        model = nsp.continuum(data=data, mdl=model)

    # flux offset
    model.flux += flux_offset
    #model.flux **= (1 + flux_exponent_offset)

    return model
# apply telluric
model = nsp.applyTelluric(model=model, alpha=alpha)
# NIRSPEC LSF
model.flux = nsp.broaden(wave=model.wave,
                         flux=model.flux,
                         vbroad=lsf,
                         rotate=False,
                         gaussian=True)

# integral resampling
model.flux = np.array(
    nsp.integralResample(xh=model.wave, yh=model.flux, xl=data.wave))
model.wave = data.wave

# contunuum correction
model, cont_factor = nsp.continuum(data=data, mdl=model, prop=True)

# NIRSPEC LSF
model_notell.flux = nsp.broaden(wave=model_notell.wave,
                                flux=model_notell.flux,
                                vbroad=lsf,
                                rotate=False,
                                gaussian=True)

# integral resampling
model_notell.flux = np.array(
    nsp.integralResample(xh=model_notell.wave,
                         yh=model_notell.flux,
                         xl=data.wave))
model_notell.wave = data.wave
model_notell.flux *= cont_factor
예제 #8
0
def makeModel(teff, logg, z, vsini, rv, wave_offset, flux_offset, **kwargs):
    """
	Return a forward model.

	Parameters
	----------
	teff   : effective temperature
	
	data   : an input science data used for continuum correction

	Optional Parameters
	-------------------
	

	Returns
	-------
	model: a synthesized model
	"""

    # read in the parameters
    order = kwargs.get('order', 33)
    modelset = kwargs.get('modelset', 'btsettl08')
    instrument = kwargs.get('instrument', 'nirspec')
    lsf = kwargs.get('lsf', 6.0)  # instrumental LSF
    tell = kwargs.get('tell', True)  # apply telluric
    data = kwargs.get('data', None)  # for continuum correction and resampling

    if data is not None and instrument == 'nirspec':
        order = data.order
        # read in a model
        #print('teff ',teff,'logg ',logg, 'z', z, 'order', order, 'modelset', modelset)
        #print('teff ',type(teff),'logg ',type(logg), 'z', type(z), 'order', type(order), 'modelset', type(modelset))
        model = nsp.Model(teff=teff,
                          logg=logg,
                          feh=z,
                          order=order,
                          modelset=modelset,
                          instrument=instrument)

    elif data is not None and instrument == 'apogee':
        model = nsp.Model(teff=teff,
                          logg=logg,
                          feh=z,
                          modelset=modelset,
                          instrument=instrument)

    # wavelength offset
    #model.wave += wave_offset

    # apply vsini
    model.flux = nsp.broaden(wave=model.wave,
                             flux=model.flux,
                             vbroad=vsini,
                             rotate=True,
                             gaussian=False)

    # apply rv (including the barycentric correction)
    model.wave = nsp.rvShift(model.wave, rv=rv)

    # instrumental LSF
    model.flux = nsp.broaden(wave=model.wave,
                             flux=model.flux,
                             vbroad=lsf,
                             rotate=False,
                             gaussian=True)

    # add a fringe pattern to the model
    #model.flux *= (1+amp*np.sin(freq*(model.wave-phase)))

    # wavelength offset
    model.wave += wave_offset

    # integral resampling
    if data is not None:
        model.flux = np.array(
            nsp.integralResample(xh=model.wave, yh=model.flux, xl=data.wave))
        model.wave = data.wave
        # contunuum correction
        model = nsp.continuum(data=data, mdl=model)

    # flux offset
    model.flux += flux_offset
    #model.flux **= (1 + flux_exponent_offset)

    return model
예제 #9
0
	labels=ylabels,
	truths=[lsf_mcmc[0], 
	alpha_mcmc[0],
	A_mcmc[0],
	B_mcmc[0]],
	quantiles=[0.16, 0.84],
	label_kwargs={"fontsize": 20})
plt.minorticks_on()
fig.savefig(save_to_path+'/triangle.png', dpi=300, bbox_inches='tight')
#plt.show()
plt.close()

data2               = copy.deepcopy(data)
data2.wave          = data2.wave + B_mcmc[0]
telluric_model      = nsp.convolveTelluric(lsf_mcmc[0], data, alpha=alpha_mcmc[0])
model, pcont        = nsp.continuum(data=data, mdl=telluric_model, deg=2, tell=True)
model.flux         += A_mcmc[0]

plt.tick_params(labelsize=20)
fig = plt.figure(figsize=(20,8))
ax1 = fig.add_subplot(111)
ax1.plot(model.wave, model.flux, c='C3', ls='-', alpha=0.5)
ax1.plot(model.wave, np.polyval(pcont, model.wave) + A_mcmc[0], c='C1', ls='-', alpha=0.5)
ax1.plot(data.wave, data.flux, 'k-', alpha=0.5)
ax1.plot(data.wave, data.flux-(model.flux+A_mcmc[0]),'k-', alpha=0.5)
ax1.minorticks_on()
plt.figtext(0.89,0.86,"{} O{}".format(tell_data_name, order),
	color='k',
	horizontalalignment='right',
	verticalalignment='center',
	fontsize=12)
예제 #10
0
def makeModel(teff,logg,z,vsini,rv,alpha,wave_offset,flux_offset,**kwargs):
	"""
	Return a forward model.

	Parameters
	----------
	teff   : effective temperature
	
	data   : an input science data used for continuum correction

	Optional Parameters
	-------------------
	

	Returns
	-------
	model: a synthesized model
	"""

	# read in the parameters
	order      = kwargs.get('order', 33)
	modelset   = kwargs.get('modelset', 'btsettl08')
	instrument = kwargs.get('instrument', 'nirspec')
	lsf        = kwargs.get('lsf', 6.0)   # instrumental LSF
	tell       = kwargs.get('tell', True) # apply telluric
	data       = kwargs.get('data', None) # for continuum correction and resampling
	output_stellar_model = kwargs.get('output_stellar_model', False)
	
	if data is not None and instrument == 'nirspec':
		order = data.order
		# read in a model
		#print('teff ',teff,'logg ',logg, 'z', z, 'order', order, 'modelset', modelset)
		#print('teff ',type(teff),'logg ',type(logg), 'z', type(z), 'order', type(order), 'modelset', type(modelset))
		model    = nsp.Model(teff=teff, logg=logg, feh=z, order=order, modelset=modelset, instrument=instrument)

	#elif data is not None and instrument == 'apogee':
	elif instrument == 'apogee':
		model    = nsp.Model(teff=teff, logg=logg, feh=z, modelset=modelset, instrument=instrument)
	
	elif data is None and instrument == 'nirspec':
		model    = nsp.Model(teff=teff, logg=logg, feh=z, order=order, modelset=modelset, instrument=instrument)
	
	# wavelength offset
	#model.wave += wave_offset

	# apply vsini
	model.flux = nsp.broaden(wave=model.wave, 
		flux=model.flux, vbroad=vsini, rotate=True, gaussian=False)
	
	# apply rv (including the barycentric correction)
	model.wave = rvShift(model.wave, rv=rv)
	
	if output_stellar_model:
		stellar_model = copy.deepcopy(model)
	# apply telluric
	if tell is True:
		model = nsp.applyTelluric(model=model, alpha=alpha, airmass='1.5')
	# instrumental LSF
	model.flux = nsp.broaden(wave=model.wave, 
		flux=model.flux, vbroad=lsf, rotate=False, gaussian=True)

	if output_stellar_model:
		stellar_model.flux = nsp.broaden(wave=stellar_model.wave, 
			flux=stellar_model.flux, vbroad=lsf, rotate=False, gaussian=True)

	# add a fringe pattern to the model
	#model.flux *= (1+amp*np.sin(freq*(model.wave-phase)))

	# wavelength offset
	model.wave += wave_offset

	if output_stellar_model: stellar_model.wave += wave_offset

	# integral resampling
	if data is not None:
		model.flux = np.array(nsp.integralResample(xh=model.wave, 
			yh=model.flux, xl=data.wave))
		model.wave = data.wave

		if output_stellar_model:
			stellar_model.flux = np.array(nsp.integralResample(xh=stellar_model.wave, 
				yh=stellar_model.flux, xl=data.wave))
			stellar_model.wave = data.wave
		# contunuum correction
		if data.instrument == 'nirspec':
			if output_stellar_model:
				model, cont_factor = nsp.continuum(data=data, mdl=model, prop=True)
				stellar_model.flux *= cont_factor
			else:
				model = nsp.continuum(data=data, mdl=model)
		elif data.instrument == 'apogee' and data.datatype =='apvisit':
			## set the order in the continuum fit
			deg         = 5

			## because of the APOGEE bands, continuum is corrected from three pieces of the spectra
			data0       = copy.deepcopy(data)
			model0      = copy.deepcopy(model)

			range0      = np.where((data0.wave >= data.oriWave0[0][-1]) & (data0.wave <= data.oriWave0[0][0]))
			data0.wave  = data0.wave[range0]
			data0.flux  = data0.flux[range0]
			data0.noise = data0.noise[range0]
			model0.wave = model0.wave[range0]
			model0.flux = model0.flux[range0]
			model0      = nsp.continuum(data=data0, mdl=model0, deg=deg)

			data1       = copy.deepcopy(data)
			model1      = copy.deepcopy(model)
			range1      = np.where((data1.wave >= data.oriWave0[1][-1]) & (data1.wave <= data.oriWave0[1][0]))
			data1.wave  = data1.wave[range1]
			data1.flux  = data1.flux[range1]
			data1.noise = data1.noise[range1]
			model1.wave = model1.wave[range1]
			model1.flux = model1.flux[range1]
			model1      = nsp.continuum(data=data1, mdl=model1, deg=deg)

			data2       = copy.deepcopy(data)
			model2      = copy.deepcopy(model)
			range2      = np.where((data2.wave >= data.oriWave0[2][-1]) & (data2.wave <= data.oriWave0[2][0]))
			data2.wave  = data2.wave[range2]
			data2.flux  = data2.flux[range2]
			data2.noise = data2.noise[range2]
			model2.wave = model2.wave[range2]
			model2.flux = model2.flux[range2]
			model2      = nsp.continuum(data=data2, mdl=model2, deg=deg)

			model.flux  = np.array( list(model0.flux) + list(model1.flux) + list(model2.flux) )
			model.wave  = np.array( list(model0.wave) + list(model1.wave) + list(model2.wave) )
		elif data.instrument == 'apogee' and data.datatype =='apstar':
			model = nsp.continuum(data=data, mdl=model)

	# flux offset
	model.flux += flux_offset
	if output_stellar_model: stellar_model.flux += flux_offset
	#model.flux **= (1 + flux_exponent_offset)

	if output_stellar_model:
		return model, stellar_model
	else:
		return model