Browse Source

Add human readable station name

master
Micke Nordin 1 year ago
parent
commit
a2077857e7
3 changed files with 14 additions and 8 deletions
  1. 8
    5
      aggregateweather.py
  2. 4
    3
      getweather.py
  3. 2
    0
      weather.sql

+ 8
- 5
aggregateweather.py View File

@@ -87,7 +87,7 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
87 87
 cursor = mydb.cursor()
88 88
 
89 89
 date_select = ("SELECT DISTINCT `date` " "FROM weather ")
90
-
90
+s_name = ""
91 91
 cursor.execute(date_select)
92 92
 dates = cursor.fetchall()
93 93
 csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n"
@@ -118,6 +118,7 @@ for i in dates:
118 118
         #| temp           | float         | YES  |     | NULL    |                |
119 119
         #| windspeed      | float         | YES  |     | NULL    |                |
120 120
         #| station        | int           | YES  |     | NULL    |                |
121
+        #| station_name   | varchar(255)  | YES  |     | NULL    |                |
121 122
         #| winddir        | int           | YES  |     | NULL    |                |
122 123
         #+----------------+---------------+------+-----+---------+----------------+
123 124
 
@@ -129,10 +130,12 @@ for i in dates:
129 130
         temp = 5
130 131
         #windspeed = 6
131 132
         #station = 7
132
-        #winddir = 8
133
+        station_name = 8
134
+        #winddir = 9
133 135
         sum_rain += j[rainfall]
134 136
         sum_temp += j[temp]
135 137
         sum_rel_hum += j[rel_hum]
138
+        s_name = j[station_name]
136 139
         if T_max < j[temp]:
137 140
             T_max = j[temp]
138 141
         if T_min > j[temp]:
@@ -158,8 +161,8 @@ eto1 = et1.eto_hargreaves()
158 161
 
159 162
 upsert = (
160 163
     "REPLACE INTO aggregated_weather "
161
-    "(Date, T_max, T_min, T_mean, RH_max, RH_min, RH_mean, Rainfall, ETo, station) "
162
-    "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
164
+    "(Date, T_max, T_min, T_mean, RH_max, RH_min, RH_mean, Rainfall, ETo, station, station_name) "
165
+    "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
163 166
 
164 167
 for key, value in eto1.items():
165 168
     aggdate = key.strftime('%Y-%m-%d')
@@ -169,7 +172,7 @@ for key, value in eto1.items():
169 172
             float(tsdata.loc[aggdate,
170 173
                              'RH_max']), float(tsdata.loc[aggdate, 'RH_min']),
171 174
             float(tsdata.loc[aggdate, 'RH_mean']),
172
-            float(tsdata.loc[aggdate, 'Rainfall']), float(value), int(station))
175
+            float(tsdata.loc[aggdate, 'Rainfall']), float(value), int(station), s_name)
173 176
     cursor.execute(upsert, data)
174 177
 
175 178
 mydb.commit()

+ 4
- 3
getweather.py View File

@@ -73,8 +73,8 @@ mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
73 73
 cursor = mydb.cursor()
74 74
 
75 75
 upsert = ("REPLACE INTO weather "
76
-          "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) "
77
-          "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
76
+          "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station, station_name) "
77
+          "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
78 78
 # Metrics mapping
79 79
 rainfall = "7"
80 80
 rel_hum = "6"
@@ -88,6 +88,7 @@ for metric in [rainfall, rel_hum, temp, winddir, windspeed]:
88 88
         'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json'
89 89
         .format(metric, station, period))
90 90
 
91
+station_name = results[rainfall].json()['station']['name']
91 92
 rainfall_arr = sorted(results[rainfall].json()['value'],
92 93
                       key=lambda i: i['date'])
93 94
 rel_hum_arr = sorted(results[rel_hum].json()['value'], key=lambda i: i['date'])
@@ -106,7 +107,7 @@ for k in rainfall_arr:
106 107
     widi = winddir_arr[i]['value']
107 108
     wisp = windspeed_arr[i]['value']
108 109
 
109
-    data = (date, time, rain, hum, te, widi, wisp, station)
110
+    data = (date, time, rain, hum, te, widi, wisp, station, station_name)
110 111
     cursor.execute(upsert, data)
111 112
     i = i + 1
112 113
 

+ 2
- 0
weather.sql View File

@@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS `weather` (
7 7
   `temp` float DEFAULT NULL,
8 8
   `windspeed` float DEFAULT NULL,
9 9
   `station` int DEFAULT NULL,
10
+  `station_name` varchar(255) DEFAULT NULL,
10 11
   `winddir` int DEFAULT NULL,
11 12
   PRIMARY KEY (`observation_id`),
12 13
   UNIQUE KEY `date_time_idx` (`date`,`time`,`station`)
@@ -24,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `aggregated_weather` (
24 25
   `Rainfall` float DEFAULT NULL,
25 26
   `ETo` float DEFAULT NULL,
26 27
   `station` int DEFAULT NULL,
28
+  `station_name` varchar(255) DEFAULT NULL,
27 29
   PRIMARY KEY (`Date_id`),
28 30
   UNIQUE KEY `date_idx` (`Date`,`station`)
29 31
 );

Loading…
Cancel
Save