diff options
-rw-r--r-- | waterspout_radar/_radar.py | 30 | ||||
-rw-r--r-- | waterspout_radar/_storage.py | 11 | ||||
-rw-r--r-- | waterspout_radar/cli.py | 5 |
3 files changed, 39 insertions, 7 deletions
diff --git a/waterspout_radar/_radar.py b/waterspout_radar/_radar.py index 345bbd6..c8cca1f 100644 --- a/waterspout_radar/_radar.py +++ b/waterspout_radar/_radar.py @@ -23,6 +23,31 @@ class Prediction: low_clouds: float swi: float + def json(self): + return { + "time": self.time.isoformat(), + "latitude": self.latitude, + "longitude": self.longitude, + "dt": self.temperature_difference.m_as("kelvin"), + "ccd": self.convective_cloud_depth.m_as("foot"), + "wind": self.wind.m_as("knot"), + "clouds": self.low_clouds, + "swi": self.swi, + } + + @classmethod + def from_json(cls, json): + return cls( + datetime.datetime.fromisoformat(json["time"]), + json["latitude"], + json["longitude"], + json["dt"] * metunits.units.kelvin, + json["ccd"] * metunits.units.foot, + json["wind"] * metunits.units.knot, + json["clouds"], + json["swi"]) + + def calculate(config) -> typing.List[Prediction]: units = metunits.units @@ -45,12 +70,13 @@ def calculate(config) -> typing.List[Prediction]: pressure, _ = calc.el(cast["pressure"], cast["temp"], cast["dewpoint"]) el = calc.pressure_to_height_std(pressure) ccd = (el - lcl).to(units.ft) - clouds = cast["lclouds"].magnitude / 100 + clouds = cast["lclouds"][0].magnitude / 100 try: swi = szilagyi.calculate_swi(dt, ccd) except ValueError: swi = -10 - yield Prediction(cast.timestamp, latitude, longitude, dt, ccd, 0, clouds, swi) + wind = 0.0 * units.kts + yield Prediction(cast.timestamp, latitude, longitude, dt, ccd, wind, clouds, swi) predictions = [] locator = geocoders.Nominatim(user_agent="waterspout-radar") diff --git a/waterspout_radar/_storage.py b/waterspout_radar/_storage.py index 6b9b968..aa9f0b1 100644 --- a/waterspout_radar/_storage.py +++ b/waterspout_radar/_storage.py @@ -1,6 +1,8 @@ import datetime import typing +import tinydb + from . import _radar Period = typing.Tuple[datetime.datetime, datetime.datetime] @@ -8,11 +10,14 @@ Period = typing.Tuple[datetime.datetime, datetime.datetime] class Storage: def __init__(self, pathname): - pass + self.db = tinydb.TinyDB(pathname, create_dirs=True) def show(self, period: Period=None): - return [] + return map(_radar.Prediction.from_json, self.db.all()) def extend(self, predictions: typing.Iterable[_radar.Prediction]): for prediction in predictions: - print(prediction.time, prediction.swi) + self.db.insert(prediction.json()) + + def __iter__(self): + return self.show() diff --git a/waterspout_radar/cli.py b/waterspout_radar/cli.py index ecf79d8..24f9e53 100644 --- a/waterspout_radar/cli.py +++ b/waterspout_radar/cli.py @@ -11,8 +11,9 @@ def main(): args = parser.parse_args() config = _config.load(args.config) storage = _storage.Storage(config.db) - predictions = _radar.calculate(config) - storage.extend(predictions) + storage.extend(_radar.calculate(config)) + for prediction in sorted(storage, key=lambda x: (x.time, x.swi)): + print(prediction.time, prediction.low_clouds, prediction.swi) if __name__ == "__main__": |