Source code for pycif.plugins.obsparsers.icos.headers
[docs]
def get_header(
obs_file, maxlen=100,
lon_unit=None, lat_unit=None,
time_interval=None
):
"""Parse the header from the file and fetch relevant information
:param obs_file:
:param maxlen:
:return:
"""
header = {}
with open(obs_file, "r") as f:
find_station_in_domain = False
# lecture d'assez de lignes pour englober l'en-tete...
data = f.readlines()[:maxlen]
header["altitude"] = 0.
for d in data:
if d.find('STATION CODE') >= 0:
header["station"] = d.split(' ')[3].strip()
if d.find('PARAMETER') >= 0:
header["parameter"] = d.split(' ')[2].strip()
if d.find('HEADER LINES') >= 0:
header["nb_header_lines"] = int(d.split(' ')[3])
if d.find('TIME INTERVAL') >= 0 > d.find('hourly'):
if time_interval is None:
raise Exception(f'PROBLEM: {obs_file} is not hourly data')
elif time_interval != "hourly":
raise Exception(
f'PROBLEM: {time_interval} '
f'is not recognized for a time interval')
if d.find('LONGITUDE') >= 0:
if d.split(' ')[-1].strip() == 'E':
header["lon"] = float(d.split(' ')[2])
elif lon_unit is not None:
try:
value = float(d.split(' ')[-1].strip())
except ValueError:
Exception(f"Could not read longitude in line:\n {d}")
if lon_unit == "E":
header["lon"] = value
elif lon_unit == "W":
header["lon"] = - value
else:
raise Exception(
f"{lon_unit} is not recognized as a longitude unit")
else:
raise Exception(f"Could not read longitude in line:\n {d}")
if d.find('LATITUDE') >= 0:
if d.split(' ')[-1].strip() == 'N':
header["lat"] = float(d.split(' ')[2])
elif lat_unit is not None:
try:
value = float(d.split(' ')[-1].strip())
except ValueError:
Exception(f"Could not read latitude in line:\n {d}")
if lat_unit == "N":
header["lat"] = value
elif lat_unit == "S":
header["lat"] = - value
else:
raise Exception(
f"{lat_unit} is not recognized as a latitude unit")
else:
raise Exception(f"Could not read latitude in line:\n {d}")
if d.find('ALTITUDE') >= 0:
header["altitude"] += float(d.split(' ')[2])
if d.find('SAMPLING HEIGHT') >= 0 and d.find('agl') >= 0:
header["altitude"] += float(d.split(' ')[3])
if d.find('MEASUREMENT UNIT') >= 0:
header["unit"] = d.split(' ')[3].splitlines()[0]
if "station" not in header:
raise Exception(f"Did not find station information in {obs_file}")
return header