HelperFunctions.py 文件源码

python
阅读 88 收藏 0 点赞 0 评论 0

项目:gullikson-scripts 作者: kgullikson88 项目源码 文件源码
def astropy_smooth(data, vel, linearize=False, kernel=convolution.Gaussian1DKernel, **kern_args):
    """
    Smooth using a gaussian filter, using astropy.

    Parameters:
    ===========
    - data:          kglib.utils.DataStructures.xypoint instance
                     The data to smooth.

    - vel:           float
                     The velocity scale to smooth out.
                     Can either by an astropy quantity or a float in km/s

    - linearize:     boolean
                     If True, we will put the data in a constant
                     log-wavelength spacing grid before smoothing.
                     The output has the same spacing as the input
                     regardless of this variable.

    - kernel:        astropy.convolution kernel
                     The astropy kernel to use. The default is the
                     Gaussian1DKernel.

    - kern_args:     Additional kernel arguments beyond width

    Returns:
    ========
    A smoothed version of the data, on the same wavelength grid as the data
    """

    if linearize:
        original_data = data.copy()
        datafcn = spline(data.x, data.y, k=3)
        linear = DataStructures.xypoint(data.x.size)
        linear.x = np.logspace(np.log10(data.x[0]), np.log10(data.x[-1]), linear.size())
        linear.y = datafcn(linear.x)
        data = linear

    # Figure out feature size in pixels
    if not isinstance(vel, u.quantity.Quantity):
        vel *= u.km / u.second

    featuresize = (vel / constants.c).decompose().value
    dlam = np.log(data.x[1] / data.x[0])
    Npix = featuresize / dlam

    # Make kernel and smooth
    kern = kernel(Npix, **kern_args)
    smoothed = convolution.convolve(data.y, kern, boundary='extend')

    if linearize:
        fcn = spline(data.x, smoothed)
        return fcn(original_data.x)
    return smoothed
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号