Add human readable station name

master
Micke Nordin 4 år sedan
förälder 4c822f44f5
incheckning a2077857e7

@ -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()

@ -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

@ -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`)
);

Laddar…
Avbryt
Spara