Add human readable station name

master
Micke Nordin 5 years ago
parent 4c822f44f5
commit a2077857e7

@ -87,7 +87,7 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
cursor = mydb.cursor() cursor = mydb.cursor()
date_select = ("SELECT DISTINCT `date` " "FROM weather ") date_select = ("SELECT DISTINCT `date` " "FROM weather ")
s_name = ""
cursor.execute(date_select) cursor.execute(date_select)
dates = cursor.fetchall() dates = cursor.fetchall()
csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n" 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 | | #| temp | float | YES | | NULL | |
#| windspeed | float | YES | | NULL | | #| windspeed | float | YES | | NULL | |
#| station | int | YES | | NULL | | #| station | int | YES | | NULL | |
#| station_name | varchar(255) | YES | | NULL | |
#| winddir | int | YES | | NULL | | #| winddir | int | YES | | NULL | |
#+----------------+---------------+------+-----+---------+----------------+ #+----------------+---------------+------+-----+---------+----------------+
@ -129,10 +130,12 @@ for i in dates:
temp = 5 temp = 5
#windspeed = 6 #windspeed = 6
#station = 7 #station = 7
#winddir = 8 station_name = 8
#winddir = 9
sum_rain += j[rainfall] sum_rain += j[rainfall]
sum_temp += j[temp] sum_temp += j[temp]
sum_rel_hum += j[rel_hum] sum_rel_hum += j[rel_hum]
s_name = j[station_name]
if T_max < j[temp]: if T_max < j[temp]:
T_max = j[temp] T_max = j[temp]
if T_min > j[temp]: if T_min > j[temp]:
@ -158,8 +161,8 @@ eto1 = et1.eto_hargreaves()
upsert = ( upsert = (
"REPLACE INTO aggregated_weather " "REPLACE INTO aggregated_weather "
"(Date, T_max, T_min, T_mean, RH_max, RH_min, RH_mean, Rainfall, ETo, station) " "(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)") "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
for key, value in eto1.items(): for key, value in eto1.items():
aggdate = key.strftime('%Y-%m-%d') aggdate = key.strftime('%Y-%m-%d')
@ -169,7 +172,7 @@ for key, value in eto1.items():
float(tsdata.loc[aggdate, float(tsdata.loc[aggdate,
'RH_max']), float(tsdata.loc[aggdate, 'RH_min']), 'RH_max']), float(tsdata.loc[aggdate, 'RH_min']),
float(tsdata.loc[aggdate, 'RH_mean']), 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) cursor.execute(upsert, data)
mydb.commit() mydb.commit()

@ -73,8 +73,8 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
cursor = mydb.cursor() cursor = mydb.cursor()
upsert = ("REPLACE INTO weather " upsert = ("REPLACE INTO weather "
"(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) " "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station, station_name) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)") "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
# Metrics mapping # Metrics mapping
rainfall = "7" rainfall = "7"
rel_hum = "6" 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' 'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json'
.format(metric, station, period)) .format(metric, station, period))
station_name = results[rainfall].json()['station']['name']
rainfall_arr = sorted(results[rainfall].json()['value'], rainfall_arr = sorted(results[rainfall].json()['value'],
key=lambda i: i['date']) key=lambda i: i['date'])
rel_hum_arr = sorted(results[rel_hum].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'] widi = winddir_arr[i]['value']
wisp = windspeed_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) cursor.execute(upsert, data)
i = i + 1 i = i + 1

@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS `weather` (
`temp` float DEFAULT NULL, `temp` float DEFAULT NULL,
`windspeed` float DEFAULT NULL, `windspeed` float DEFAULT NULL,
`station` int DEFAULT NULL, `station` int DEFAULT NULL,
`station_name` varchar(255) DEFAULT NULL,
`winddir` int DEFAULT NULL, `winddir` int DEFAULT NULL,
PRIMARY KEY (`observation_id`), PRIMARY KEY (`observation_id`),
UNIQUE KEY `date_time_idx` (`date`,`time`,`station`) UNIQUE KEY `date_time_idx` (`date`,`time`,`station`)
@ -24,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `aggregated_weather` (
`Rainfall` float DEFAULT NULL, `Rainfall` float DEFAULT NULL,
`ETo` float DEFAULT NULL, `ETo` float DEFAULT NULL,
`station` int DEFAULT NULL, `station` int DEFAULT NULL,
`station_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Date_id`), PRIMARY KEY (`Date_id`),
UNIQUE KEY `date_idx` (`Date`,`station`) UNIQUE KEY `date_idx` (`Date`,`station`)
); );

Loading…
Cancel
Save