利用ispec包中的modeling文件中的ew.py文件计算等效宽度
def ew(x, y, errors=None, continuum_func=None, vspan=1000., mask_lines=True):
"""Estimate the equivalent width of a spectral line.
Parameters:
x (array): Wavelengths.
y (array): Fluxes.
errors (array): Uncertainties in fluxes. If not provided it is assumed that all points have an uncertainty of 1%.
continuum_func (function or callable object): Continuum function to fit the line region and measure the equivalent width. Default is a straight line in log-space with 2 degrees of freedom (1 slope + 1 offset). It can be any other callable object that accepts x and y as arguments and returns a set of parameters describing the fitted model.
Returns:
Equivalent width value and fitting parameters used to calculate it if continuum_func was provided as input parameter, None otherwise.
"""
# Set default values for optional parameters 不定长参数设置默认值,如果有传入参数则以传入参数为准,没有则使用默认值
if errors is None: # 错误率为1%
errors = np.full(len(y), 0.01)
# Default linear fit in log space 拟合方法为log空间直线拟合法,默认2度自由度
if continuum_func is None:
def _continuum_model(x, m, c): # 该函数定义的是直线的斜率m和截距c来拟合一条直线;返回y = m*x + c中的y。即把x带入函数中得到对应的y。
return m * x + c
def _continuum_func(*params): # 该函数将上述定义好的log space直线方法带入参数中得到对应的参量。即根据已定义好的log space直线方法,通过前面定义好的方法进行fit得到最佳参量。
return lambda xx: _continuum_model(xx, *params)
# Least squares fitting with two degrees of freedom 向量形式表明正是2个独立性变量m、c # Least squares fitting with two degrees of freedom 通过了leastsq()函数来最小二乘权重拟合funk()所定义好的fitting model()
initial_guess = [0., 0.] # 初始化m、c都为0 // Least-squares minimization
bestfitpars = leastsq(_funk , initial_guess , args=(x , np . log10(y),errors))[0] // 带入initial guess 然后再根据funk()和args=(x , np . log10(y),errors) 求出bestfitpars[] // Calculate equivalent width
ew = -np . trapz((np . power(10 , _continuum_model((lmin + lmax)/2., bestfitpars[0],bestfitpars[1]))- (np . power(10 , _continuum_model((lmin+lmax)/2., bestfitpars[0], bestfitpars[1]))) / // Calculate error in equivalent width ((np . power(10 , _continuum_model((lmin+lmax)/2.,bestfitpars[0], bestfitpars[1])))) return ew
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!