import logging


def get_ugrid_mesh_description(nc_file):
    """
    Collect UGRID mesh description from mesh file and return a list of UGRID variables;
    only one mesh description is allowed
    """

    # Look for UGRID mesh description dummy variable
    mesh_names = []
    for var_name, var in nc_file.variables.items():
        if 'cf_role' in dir(var):
            if var.cf_role == 'mesh_topology':
                mesh_names.append(var_name)

    if len(mesh_names) != 1:
        logging.critical('Expected 1 UGRID mesh description but found %i', len(mesh_names))
        raise RuntimeError()

    logging.info('Found UGRID mesh "%s"', mesh_names[0])

    # Extract topology dummy var from the first (and only) list element
    ugrid_topology_var = nc_file.variables[mesh_names[0]]

    # Collect expected UGRID location and connectivity variable names from the dummy var
    # attributes
    ugrid_varnames = set()
    ugrid_varnames.update(ugrid_topology_var.node_coordinates.split(" "))
    ugrid_varnames.update(ugrid_topology_var.face_coordinates.split(" "))
    ugrid_varnames.add(ugrid_topology_var.face_node_connectivity)
    ugrid_varnames.add(ugrid_topology_var.edge_node_connectivity)
    ugrid_varnames.add(ugrid_topology_var.face_edge_connectivity)
    ugrid_varnames.add(ugrid_topology_var.face_face_connectivity)

    # Assemble list of UGRID variables, including dummy var
    ugrid_vars = [ugrid_topology_var]
    for varname in ugrid_varnames:
        ugrid_vars.append(nc_file.variables[varname])

    return ugrid_vars