def levelFromHtmid(htmid):
    """
    Find the level of a trixel from its htmid.  The level
    indicates how refined the triangular mesh is.

    There are 8*4**(d-1) triangles in a mesh of level=d

    (equation 2.5 of
    Szalay A. et al. (2007)
    "Indexing the Sphere with the Hierarchical Triangular Mesh"
    arXiv:cs/0701164)

    Note: valid htmids have 4+2n bits with a leading bit of 1
    """
    htmid_copy = htmid
    i_level = 1
    while htmid_copy > 15:
        htmid_copy >>= 2
        i_level += 1

    if htmid_copy < 8:
        raise RuntimeError('\n%d is not a valid htmid.\n' % htmid
                           + 'Valid htmids will have 4+2n bits\n'
                           + 'with a leading bit of 1\n')

    return i_level