def construct_header(metadata):
    """

    Construct a text file header in string format.
    For more informaiton, see format description at the top.

    Parameters
    ----------
    - metadata: dictionary-like object witih the metadata

    Returns
    -------
    A string with the metadata, ready to be used as header of a text file.

    """

    header_str = ""

    for name, value in metadata.items():

        if name == "filename":
            continue

        header_str += "# {}:".format(name)

        if type(value) == dict:
            for (k, v) in value.items():
                k = k.replace(" ", "_")
                if type(v) == str:
                    v = v.replace(" ", "_")
            value_str = ["{}={}".format(k, v) for k, v in value.items()]
            value_str = ("," + " ").join(value_str)
            header_str += " {}\n".format(value_str)

        else:
            header_str += " {}\n".format(value)

    return header_str.strip("\n")