import gzip
import re


def readHeader(fname):
    """Read QA4ECV NO2 ASCII data header.

    PARAMETERS
    ----------
    fname : str
        Path to the filename

    RETURNS
    -------
    header : a list of useful variables: year, month, nlat, nlon, lat_start, lat_end, lon_start, lon_end 
    """
    f=gzip.open(fname,'rb')
    line0 = f.readline()
    line1 = str(f.readline())
    date = re.findall(r"[-+]?\d*\.\d+|\d+", line1)
    year = int(date[0])
    month = int(date[1])
    line2 = str(f.readline())
    lon_array = re.findall(r"[-+]?\d*\.\d+|\d+", line2)
    nlon = int(lon_array[0])
    lon_start = float(lon_array[1])*-1.
    lon_end = float(lon_array[2])
    line3 = str(f.readline())
    lat_array = re.findall(r"[-+]?\d*\.\d+|\d+", line3)
    nlat = int(lat_array[0])
    lat_start = float(lat_array[1])*-1.
    lat_end = float(lat_array[2])
    header = [year, month, nlat, nlon, lat_start, lat_end, lon_start, lon_end]
    f.close()
    return header