def _cast_mesh(mesh, Nmesh):
    """
    Cast an object to a MeshSource. Nmesh is used only on FKPCatalog

    """
    from .catalog import FKPCatalog
    from .catalogmesh import FKPCatalogMesh
    if not isinstance(mesh, (FKPCatalogMesh, FKPCatalog)):
        raise TypeError(
            'input sources should be a FKPCatalog or FKPCatalogMesh')
    if isinstance(mesh, FKPCatalog):
        mesh = mesh.to_mesh(Nmesh=Nmesh, dtype='f8', compensated=False)
    if Nmesh is not None and any(mesh.attrs['Nmesh'] != Nmesh):
        raise ValueError(
            'Mismatched Nmesh between __init__ and mesh.attrs; if trying to re-sample with a different mesh, specify `Nmesh` as keyword of to_mesh()'
            )
    return mesh
