import linecache


def get_ts_header(TSfile):
    """
    Returns a dictionary with information contined in the header of the TSfile
    produced by WRF.

    Input:
        TSfile - The time series output produced by WRF in the form XXX.d0Y.TS
                 where XXX is the station ID and Y is the domain.
    Output:
        header_dict - A dictionary that contains the following:
            stn_name     = string of the station's full name
            grid_id      = string of the station's ID
            stn_id       = tuple of the station's grid ID
            stn_latlon   = list of the station's real latitude and longitude
            grid_indices = tuple of the grid indices. Location on domain.
            grid_latlon  = list of the grid latitude and longitude
            grid_elev    = float of the grid elevation
            elev_units   = units of the elevation
    """
    line = linecache.getline(TSfile,1)

    name = line[0:25]
    gridID1 = line[26:29]
    gridID2 = line[29:32]
    stnID = line[33:38]
    stnlat = line[39:46]
    stnlon = line[47:55]
    gridindx1 = line[58:62]
    gridindx2 = line[63:67]
    gridlat = line[70:77]
    gridlon = line[78:86]
    grid_elev = line[88:94]
    elev_units = line[95:]

    header_dict = {'stn_name' : name.strip(),
                   'grid_id' : (int(gridID1), int(gridID2)),
                   'stn_id' : stnID.strip(),
                   'stn_latlon' : [float(stnlat), float(stnlon)],
                   'grid_indices' : (int(gridindx1), int(gridindx2)),
                   'grid_latlon' : [float(gridlat), float(gridlon)],
                   'grid_elev' : float(grid_elev),
                   'elev_units' : elev_units.strip()}

    return header_dict