def fullGetSpacingAndPhase(data, f=None, firstPeak = None, cutoff = -.2, smooth = False, debugging = False):
    data = np.array(data-min(data))
    if f is None:
        f = fitInterference(data, cutoff=cutoff, smooth = smooth, debugging = debugging)[0]/14e3

    if f<0:
        return -1, -1
    if debugging:
        print "freq:",f
    if firstPeak is None:
        firstPeak = qdPeaks(data, cutoff=cutoff, smooth = smooth, debugging = False)[1]

    f = fitSpacingAndPhase(data, f, firstPeak = firstPeak, debugging = debugging)[0]/14e3

    return fitSpacingAndPhase(data, f, firstPeak = firstPeak, debugging = debugging)
def getWavelength(data, debugging=None):
    if debugging is not None:
        if not isinstance(debugging, list):
            debugging = None

    alph = 50.8325 * np.pi/180  # 596.335
    eps = 0.0242377 * np.pi/180 # 596.335
    S0 = 1.8909e8
    e0 = 0.978584e6
    leftSideOfCCD = 14e3 * 512
    a = leftSideOfCCD

    spacing, phase = fitInterference(data)

    if spacing<=0:
        print "Error, bad spacing"
        return
    if debugging is not None:
        debugging.append([spacing, phase])
        pks = getPeaks(data)
        firstPeak = qdPeaks(data)[1]
        debugging.append(np.arange(0, len(pks))+int(firstPeak/spacing))
        debugging.append(pks)
    numIters = 3

    lOld = spacing * dpda(750, alph, eps)
    lHistory = [lOld]
    pHistory = [0]
    # lHistory.append(lOld)
    for ii in range(numIters):
        lNew = spacing * dpda(lOld, alph, eps)
        p0 = p(lNew, alph, eps, a, S0, e0)
        o = (p0/lNew)
        pHistory.append(o)
        o = np.round(o)
        lNew = p0/(o + phase - 0.5)
        lHistory.append(lNew)
        if debugging:
            print "\t iter: {}, o={}, lNew={}".format(ii, pHistory[-1], lNew)
        lOld = lNew

    pHistory[0] = pHistory[1]

    return lNew