From a2077857e73d09558129941dd9d62ce601947ddf Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 23 May 2020 17:14:16 +0200 Subject: [PATCH] Add human readable station name --- aggregateweather.py | 13 ++++++++----- getweather.py | 7 ++++--- weather.sql | 2 ++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/aggregateweather.py b/aggregateweather.py index d77edf7..d4b6881 100755 --- a/aggregateweather.py +++ b/aggregateweather.py @@ -87,7 +87,7 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password', cursor = mydb.cursor() date_select = ("SELECT DISTINCT `date` " "FROM weather ") - +s_name = "" cursor.execute(date_select) dates = cursor.fetchall() csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n" @@ -118,6 +118,7 @@ for i in dates: #| temp | float | YES | | NULL | | #| windspeed | float | YES | | NULL | | #| station | int | YES | | NULL | | + #| station_name | varchar(255) | YES | | NULL | | #| winddir | int | YES | | NULL | | #+----------------+---------------+------+-----+---------+----------------+ @@ -129,10 +130,12 @@ for i in dates: temp = 5 #windspeed = 6 #station = 7 - #winddir = 8 + station_name = 8 + #winddir = 9 sum_rain += j[rainfall] sum_temp += j[temp] sum_rel_hum += j[rel_hum] + s_name = j[station_name] if T_max < j[temp]: T_max = j[temp] if T_min > j[temp]: @@ -158,8 +161,8 @@ eto1 = et1.eto_hargreaves() upsert = ( "REPLACE INTO aggregated_weather " - "(Date, T_max, T_min, T_mean, RH_max, RH_min, RH_mean, Rainfall, ETo, station) " - "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") + "(Date, T_max, T_min, T_mean, RH_max, RH_min, RH_mean, Rainfall, ETo, station, station_name) " + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") for key, value in eto1.items(): aggdate = key.strftime('%Y-%m-%d') @@ -169,7 +172,7 @@ for key, value in eto1.items(): float(tsdata.loc[aggdate, 'RH_max']), float(tsdata.loc[aggdate, 'RH_min']), float(tsdata.loc[aggdate, 'RH_mean']), - float(tsdata.loc[aggdate, 'Rainfall']), float(value), int(station)) + float(tsdata.loc[aggdate, 'Rainfall']), float(value), int(station), s_name) cursor.execute(upsert, data) mydb.commit() diff --git a/getweather.py b/getweather.py index fb70e6e..27fa247 100755 --- a/getweather.py +++ b/getweather.py @@ -73,8 +73,8 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password', cursor = mydb.cursor() upsert = ("REPLACE INTO weather " - "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) " - "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)") + "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station, station_name) " + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)") # Metrics mapping rainfall = "7" rel_hum = "6" @@ -88,6 +88,7 @@ for metric in [rainfall, rel_hum, temp, winddir, windspeed]: 'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json' .format(metric, station, period)) +station_name = results[rainfall].json()['station']['name'] rainfall_arr = sorted(results[rainfall].json()['value'], key=lambda i: i['date']) rel_hum_arr = sorted(results[rel_hum].json()['value'], key=lambda i: i['date']) @@ -106,7 +107,7 @@ for k in rainfall_arr: widi = winddir_arr[i]['value'] wisp = windspeed_arr[i]['value'] - data = (date, time, rain, hum, te, widi, wisp, station) + data = (date, time, rain, hum, te, widi, wisp, station, station_name) cursor.execute(upsert, data) i = i + 1 diff --git a/weather.sql b/weather.sql index 8f3fefb..f5fd355 100644 --- a/weather.sql +++ b/weather.sql @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS `weather` ( `temp` float DEFAULT NULL, `windspeed` float DEFAULT NULL, `station` int DEFAULT NULL, + `station_name` varchar(255) DEFAULT NULL, `winddir` int DEFAULT NULL, PRIMARY KEY (`observation_id`), UNIQUE KEY `date_time_idx` (`date`,`time`,`station`) @@ -24,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `aggregated_weather` ( `Rainfall` float DEFAULT NULL, `ETo` float DEFAULT NULL, `station` int DEFAULT NULL, + `station_name` varchar(255) DEFAULT NULL, PRIMARY KEY (`Date_id`), UNIQUE KEY `date_idx` (`Date`,`station`) );