def find_dids(qm, rows, dids, project, varlist):
    """ Retrieve missing dataset ids from dictionary representing queue table
    :input: qm - query results
    :input: rows - a dictionary representing each record stored in the file
    :input: dids - a set of unique dataset_ids stored in the file
    :input: project - CMIP5 or CMIP6 currently
    :input: varlist - optional list of requested variables for CMIP5
    :return: queued - a dictionary with (did+var,status) for CMIP5 and (did,status) for CMIP6
             filtered based on query results
    """
    queued={}
    for q in qm:
        did = q[0].replace('output.','output1.')
        if did in dids:
        # when CMIP5 you need to match also the variable
            if project == "CMIP5":
                if varlist != []:
                    queued.update({k[0]+" "+k[1]: v for k,v in rows.items() if k[0]==did and k[1] in varlist})
                else:
                    queued.update({k[0]+" "+k[1]: v for k,v in rows.items() if k[0]==did})
            elif project == "CMIP6":
                queued.update({k: v for k,v in rows.items() if k==did})
            else:
                 pass
    return queued