from datetime import datetime
import re


def parse_nowcast(text):
    """
    Parses the immediate aurora forecast.
    http://services.swpc.noaa.gov/text/aurora-nowcast-map.txt
    """

    time = datetime.max  # forecast time remains far in the future if time not found in the file
    data = []
    for line in map(lambda l: l.decode('utf_8').strip(), text):
        if line:
            if not line.startswith('#'):
                for p in line.split():
                    data.append(int(p) if p != 'n/a' else 0)
            else:
                m = re.search('^# Product Valid At: (.*)', line)
                if m:
                    time = datetime.strptime(m.group(1), '%Y-%m-%d %H:%M')
    return data, time