From e2b89f5b20319c942cdfbbeea95d487be637cfc4 Mon Sep 17 00:00:00 2001 From: Bjarne Riis Date: Sat, 27 Apr 2024 15:55:21 +0200 Subject: [PATCH] Added daily sunrise and sunset values to forecast --- php/forecast_visualcrossing.php | 8 ++++--- pymeteobridgesql/data.py | 38 +++++++++++++++++++++++++++++++++ sql/create_database.sql | 2 ++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/php/forecast_visualcrossing.php b/php/forecast_visualcrossing.php index bfe9cc6..6fd1ccb 100644 --- a/php/forecast_visualcrossing.php +++ b/php/forecast_visualcrossing.php @@ -71,13 +71,15 @@ $jobj_fcst->native_precipitation = $value["precip"]; $jobj_fcst->precipitation_probability = $value["precipprob"]; $jobj_fcst->native_pressure = $value["pressure"]; + $jobj_fcst->sunriseepoch = $value["sunriseEpoch"]; + $jobj_fcst->sunsetepoch = $value["sunsetEpoch"]; $jobj_fcst->wind_bearing = $value["winddir"]; $jobj_fcst->native_wind_speed = kmh2ms($value["windspeed"]); $jobj_fcst->native_wind_gust = kmh2ms($value["windgust"]); - $sql = "INSERT INTO `forecast_daily` (`day_num`, `datetime`, `temperature`, `temp_low`, `description`, `icon`, `precipitation_probability`, `precipitation`, `pressure`, `wind_bearing`, `wind_speed`, `wind_gust`) "; - $sql = $sql . "VALUES (".$day_num.",'".$jobj_fcst->datetime."',".$jobj_fcst->native_temperature.",".$jobj_fcst->native_templow.",'".$jobj_fcst->description."','".$jobj_fcst->icon."',".$jobj_fcst->precipitation_probability."," .$jobj_fcst->native_precipitation."," .$jobj_fcst->native_pressure.",".$jobj_fcst->wind_bearing.",".$jobj_fcst->native_wind_speed.",".$jobj_fcst->native_wind_gust.") "; - $sql = $sql . "ON DUPLICATE KEY UPDATE `datetime`='".$jobj_fcst->datetime."', `temperature`=".$jobj_fcst->native_temperature.", `temp_low`=".$jobj_fcst->native_templow.", `description`='".$jobj_fcst->description."', `icon`='".$jobj_fcst->icon."', `precipitation_probability`=".$jobj_fcst->precipitation_probability.", `precipitation`=".$jobj_fcst->native_precipitation.", `pressure`=".$jobj_fcst->native_pressure.", `wind_bearing`=".$jobj_fcst->wind_bearing.", `wind_speed`=".$jobj_fcst->native_wind_speed.", `wind_gust`=".$jobj_fcst->native_wind_gust; + $sql = "INSERT INTO `forecast_daily` (`day_num`, `datetime`, `temperature`, `temp_low`, `description`, `icon`, `precipitation_probability`, `precipitation`, `pressure`, `sunriseepoch`, `sunsetepoch`, `wind_bearing`, `wind_speed`, `wind_gust`) "; + $sql = $sql . "VALUES (".$day_num.",'".$jobj_fcst->datetime."',".$jobj_fcst->native_temperature.",".$jobj_fcst->native_templow.",'".$jobj_fcst->description."','".$jobj_fcst->icon."',".$jobj_fcst->precipitation_probability."," .$jobj_fcst->native_precipitation."," .$jobj_fcst->native_pressure.",".$jobj_fcst->sunriseepoch.",".$jobj_fcst->sunsetepoch.",".$jobj_fcst->wind_bearing.",".$jobj_fcst->native_wind_speed.",".$jobj_fcst->native_wind_gust.") "; + $sql = $sql . "ON DUPLICATE KEY UPDATE `datetime`='".$jobj_fcst->datetime."', `temperature`=".$jobj_fcst->native_temperature.", `temp_low`=".$jobj_fcst->native_templow.", `description`='".$jobj_fcst->description."', `icon`='".$jobj_fcst->icon."', `precipitation_probability`=".$jobj_fcst->precipitation_probability.", `precipitation`=".$jobj_fcst->native_precipitation.", `pressure`=".$jobj_fcst->native_pressure.", `sunriseepoch`=".$jobj_fcst->sunriseepoch.", `sunsetepoch`=".$jobj_fcst->sunsetepoch.", `wind_bearing`=".$jobj_fcst->wind_bearing.", `wind_speed`=".$jobj_fcst->native_wind_speed.", `wind_gust`=".$jobj_fcst->native_wind_gust; ++$day_num; if (!mysqli_query($conn, $sql)) { diff --git a/pymeteobridgesql/data.py b/pymeteobridgesql/data.py index 5413071..61af2cf 100644 --- a/pymeteobridgesql/data.py +++ b/pymeteobridgesql/data.py @@ -243,6 +243,24 @@ class ForecastHourly: wind_gust: float uv_index: float + def to_dict(self): + return { + "hour_num": self.hour_num, + "datetime": self.datetime, + "temperature": self.temperature, + "apparent_temperature": self.apparent_temperature, + "humidity": self.humidity, + "description": self.description, + "icon": self.icon, + "precipitation_probability": self.precipitation_probability, + "precipitation": self.precipitation, + "pressure": self.pressure, + "wind_bearing": self.wind_bearing, + "wind_speed": self.wind_speed, + "wind_gust": self.wind_gust, + "uv_index": self.uv_index, + } + @dataclass(frozen=True) class ForecastDaily: day_num: int @@ -254,10 +272,30 @@ class ForecastDaily: precipitation_probability: int precipitation: float pressure: float + sunriseepoch: int + sunsetepoch: int wind_bearing: int wind_speed: float wind_gust: float + def to_dict(self): + return { + "day_num": self.day_num, + "datetime": self.datetime, + "temperature": self.temperature, + "temp_low": self.temp_low, + "description": self.description, + "icon": self.icon, + "precipitation_probability": self.precipitation_probability, + "precipitation": self.precipitation, + "pressure": self.pressure, + "sunriseepoch": self.sunriseepoch, + "sunsetepoch": self.sunsetepoch, + "wind_bearing": self.wind_bearing, + "wind_speed": self.wind_speed, + "wind_gust": self.wind_gust, + } + @dataclass(frozen=True) class StationData: ID: str diff --git a/sql/create_database.sql b/sql/create_database.sql index 54d6ff2..ec764ec 100644 --- a/sql/create_database.sql +++ b/sql/create_database.sql @@ -48,6 +48,8 @@ CREATE TABLE `forecast_daily` ( `precipitation_probability` INT NULL DEFAULT NULL , `precipitation` FLOAT NULL DEFAULT NULL , `pressure` FLOAT NULL DEFAULT NULL , + `sunriseepoch` INT NULL DEFAULT NULL , + `sunsetepoch` INT NULL DEFAULT NULL , `wind_bearing` INT NULL DEFAULT NULL , `wind_speed` FLOAT NULL DEFAULT NULL , `wind_gust` FLOAT NULL DEFAULT NULL ,