def calc_M20(peaks, HKL, ifX20=True):
    """needs a doc string"""
    diff = 0
    X20 = 0
    for Nobs20, peak in enumerate(peaks):
        if peak[3]:
            Qobs = 1.0 / peak[7] ** 2
            Qcalc = 1.0 / peak[8] ** 2
            diff += abs(Qobs - Qcalc)
        elif peak[2]:
            X20 += 1
        if Nobs20 == 19:
            d20 = peak[7]
            break
    else:
        d20 = peak[7]
        Nobs20 = len(peaks)
    for N20, hkl in enumerate(HKL):
        if hkl[3] < d20:
            break
    Q20 = 1.0 / d20 ** 2
    if diff:
        M20 = Q20 / (2.0 * diff)
    else:
        M20 = 0
    if ifX20:
        M20 /= 1.0 + X20
    return M20, X20
