diff --git a/lib/xmet/igra.py b/lib/xmet/igra.py index 0de7dd4..3dbf87b 100644 --- a/lib/xmet/igra.py +++ b/lib/xmet/igra.py @@ -3,6 +3,9 @@ import re import datetime import shapely +from typing import Self + +from xmet.db import DatabaseTable from xmet.coord import COORD_SYSTEM from xmet.sounding import Sounding, SoundingSample @@ -146,3 +149,68 @@ class IGRAReader(): count -= 1 return sounding + +def cols(text: str, start: int, end: int): + a = start - 1 + b = end + + return text[a:b] + +class IGRAStation(DatabaseTable): + __table__ = 'xmet_igra-station' + __key__ = 'code' + + __columns__ = ( + 'code', 'year_start', 'year_end', 'name', 'state', 'elevation', + 'location' + ) + + __columns_read__ = { + 'location': 'ST_AsText(location) as location' + } + + __values_read__ = { + 'location': shapely.from_wkt + } + + __columns_write__ = { + 'location': 'ST_GeomFromText(:location, {crs})'.format(crs=COORD_SYSTEM) + } + + __values_write__ = { + 'location': lambda v: {'location': shapely.to_wkt(v)} + } + + code: str + year_start: int + year_end: int + name: str + state: str + elevation: float + location: shapely.Point + + def __init__(self): + super().__init__() + self.code = None + self.year_start = None + self.year_end = None + self.name = None + self.state = None + self.elevation = None + self.location = None + + @staticmethod + def parse_station(line: str) -> Self: + lat = float(cols(line, 13, 20)) + lon = float(cols(line, 22, 30)) + + station = IGRAStation() + station.code = cols(line, 1, 11) + station.year_start = int(cols(line, 73, 76)) + station.year_end = int(cols(line, 78, 81)) + station.name = cols(line, 42, 71) + station.state = cols(line, 39, 40) + station.elevation = float(cols(line, 32, 37)) + station.location = shapely.Point(lon, lat) + + return station