def CombineXYpoints(xypts, snr=None, xspacing=None, numpoints=None, interp_order=3):
    """
    Function to combine a list of xypoints into a single
      xypoint. Useful for combining several orders/chips
      or for coadding spectra

    Warning! This function is basically un-tested! It is NOT used
    in TelFit!

      ***Optional keywords***
      snr: the spectra will be weighted by the signal-to-noise ratio
           before adding
      xspacing: the x-spacing in the final array
      numpoints: the number of points in the final array. If neither
                 numpoints nor xspacing is given, the x-spacing in the
                 final array will be determined by averaging the spacing
                 in each of the xypoints.
      interp_order: the interpolation order. Default is cubic
    """
    from scipy.interpolate import InterpolatedUnivariateSpline

    if snr is None or type(snr) != list:
        snr = [1.0] * len(xypts)

    # Find the maximum range of the x data:
    first = np.min([o.x[0] for o in xypts])
    last = np.max([o.x[-1] for o in xypts])
    avg_spacing = np.mean([(o.x[-1] - o.x[0]) / float(o.size() - 1) for o in xypts])

    if xspacing is None and numpoints is None:
        xspacing = avg_spacing
    if numpoints is None:
        if xspacing is None:
            xspacing = avg_spacing
        numpoints = (last - first) / xspacing
    x = np.linspace(first, last, numpoints)

    full_array = xypoint(x=x, y=np.zeros(x.size))
    numvals = np.zeros(x.size, dtype=np.float)  #The number of arrays each x point is in
    normalization = 0.0
    for xypt in xypts:
        interpolator = InterpolatedUnivariateSpline(xypt.x, xypt.y / xypt.cont, k=interp_order)
        left = np.searchsorted(full_array.x, xypt.x[0])
        right = np.searchsorted(full_array.x, xypt.x[-1], side='right')
        if right < xypt.size():
            right += 1
        numvals[left:right] += 1.0
        full_array.y[left:right] += interpolator(full_array.x[left:right])
    full_array.y[numvals > 0] /= numvals[numvals > 0]
    return full_array
dataset/ETHPy150Open kgullikson88/Telluric-Fitter/src/DataStructures.py/CombineXYpoints/original