def calc_TMI(mdf):
    """ Calculates the magnitude of the B-field from its three components """
    mdf_tmi = mdf.deepcopy()
    tmi = (mdf['Bx'] ** 2 + mdf['By'] ** 2 + mdf['Bz'] ** 2) ** 0.5
    mdf_tmi.add_column('TMI', tmi)
    if mdf_tmi.col_sfields:
        stmi = ((mdf['Bx'] / tmi * mdf['sBx']) ** 2 + (mdf['By'] / tmi *
            mdf['sBy']) ** 2 + (mdf['Bz'] / tmi * mdf['sBz']) ** 2) ** 0.5
        mdf_tmi.add_column('sTMI', stmi)
    return mdf_tmi
