import re


def timestamp_to_terraref(timestamp):
    """Converts a timestamp to TERRA REF format
    Args:
        timestamp(str): the ISO timestamp to convert
    Returns:
        The ISO string reformatted to match the TERRA REF time & date stamp. Returns the original
        string if time units aren't specified
    Note:
        Does a simple replacement of the 'T' to '__' and stripping the fraction of seconds and
        timezone information from the string.
    """
    return_ts = timestamp

    if 'T' in timestamp:
        regex_mask = r'(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})'
        res = re.search(regex_mask, timestamp)
        if res:
            return_ts = res.group(0)
            return_ts = return_ts.replace('T', '__').replace(':', '-')

    return return_ts