yapf
This commit is contained in:
parent
5df1df43ca
commit
38a3662396
2 changed files with 72 additions and 66 deletions
|
@ -10,9 +10,15 @@ import pandas as pd
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print( "Usage: " + __file__ + "[--config path_to_file.ini] [--station smhi_station_number ] --db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
|
print(
|
||||||
"Default configfile is weather.ini, any parameter can be overwritten on the command line")
|
"Usage: " + __file__ +
|
||||||
|
"[--config path_to_file.ini] [--station smhi_station_number ] --db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
|
||||||
|
"Default configfile is weather.ini, any parameter can be overwritten on the command line"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--config')
|
parser.add_argument('--config')
|
||||||
parser.add_argument('--db')
|
parser.add_argument('--db')
|
||||||
|
@ -33,7 +39,6 @@ password = config['MySQL']['password']
|
||||||
|
|
||||||
station = config['SMHI']['station']
|
station = config['SMHI']['station']
|
||||||
|
|
||||||
|
|
||||||
# Defaults
|
# Defaults
|
||||||
z_msl = 48.854
|
z_msl = 48.854
|
||||||
lat = 59.178503
|
lat = 59.178503
|
||||||
|
@ -61,7 +66,10 @@ if not (db and host and user and password):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
pd.options.mode.chained_assignment = None
|
pd.options.mode.chained_assignment = None
|
||||||
station_data = requests.get(url='https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/1/station/{}.json'.format(station)).json()
|
station_data = requests.get(
|
||||||
|
url=
|
||||||
|
'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/1/station/{}.json'
|
||||||
|
.format(station)).json()
|
||||||
newest_to = 0
|
newest_to = 0
|
||||||
for i in station_data['position']:
|
for i in station_data['position']:
|
||||||
if i['to'] > newest_to:
|
if i['to'] > newest_to:
|
||||||
|
@ -69,36 +77,28 @@ for i in station_data['position']:
|
||||||
lat = i['latitude']
|
lat = i['latitude']
|
||||||
lon = i['longitude']
|
lon = i['longitude']
|
||||||
TZ_lon = lon
|
TZ_lon = lon
|
||||||
|
|
||||||
|
|
||||||
mydb = mysql.connector.connect(
|
mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
|
||||||
auth_plugin='mysql_native_password',
|
database=db,
|
||||||
database=db,
|
host=host,
|
||||||
host=host,
|
passwd=password,
|
||||||
passwd=password,
|
user=user)
|
||||||
user=user
|
|
||||||
)
|
|
||||||
|
|
||||||
cursor = mydb.cursor();
|
cursor = mydb.cursor()
|
||||||
|
|
||||||
date_select = (
|
date_select = ("SELECT DISTINCT `date` " "FROM weather ")
|
||||||
"SELECT DISTINCT `date` "
|
|
||||||
"FROM weather "
|
|
||||||
)
|
|
||||||
|
|
||||||
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"
|
||||||
for i in dates:
|
for i in dates:
|
||||||
working_date = i[0].strftime('%Y-%m-%d')
|
working_date = i[0].strftime('%Y-%m-%d')
|
||||||
day_select = (
|
day_select = ('SELECT * FROM weather '
|
||||||
'SELECT * FROM weather '
|
'WHERE date = "{}"'.format(working_date))
|
||||||
'WHERE date = "{}"'.format(working_date)
|
|
||||||
)
|
|
||||||
cursor.execute(day_select)
|
cursor.execute(day_select)
|
||||||
day = cursor.fetchall()
|
day = cursor.fetchall()
|
||||||
sum_rain = 0
|
sum_rain = 0
|
||||||
T_max = -9999
|
T_max = -9999
|
||||||
T_min = 9999
|
T_min = 9999
|
||||||
RH_max = -9999
|
RH_max = -9999
|
||||||
RH_min = 9999
|
RH_min = 9999
|
||||||
|
@ -132,19 +132,24 @@ for i in dates:
|
||||||
sum_temp += j[temp]
|
sum_temp += j[temp]
|
||||||
sum_rel_hum += j[rel_hum]
|
sum_rel_hum += j[rel_hum]
|
||||||
sum_windspeed += j[windspeed]
|
sum_windspeed += j[windspeed]
|
||||||
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]:
|
||||||
T_min = j[temp]
|
T_min = j[temp]
|
||||||
if RH_max < j[rel_hum]:
|
if RH_max < j[rel_hum]:
|
||||||
RH_max = j[rel_hum]
|
RH_max = j[rel_hum]
|
||||||
if RH_min > j[rel_hum]:
|
if RH_min > j[rel_hum]:
|
||||||
RH_min = j[rel_hum]
|
RH_min = j[rel_hum]
|
||||||
T_mean = sum_temp / counter
|
T_mean = sum_temp / counter
|
||||||
RH_mean = sum_rel_hum / counter
|
RH_mean = sum_rel_hum / counter
|
||||||
csv += working_date + "," + str(T_max) + "," + str(T_min) + "," + str(T_mean) + "," + str(RH_max) + "," + str(RH_min) +"," + str(RH_mean) + "," + str(sum_rain) + "\n"
|
csv += working_date + "," + str(T_max) + "," + str(T_min) + "," + str(
|
||||||
|
T_mean) + "," + str(RH_max) + "," + str(RH_min) + "," + str(
|
||||||
|
RH_mean) + "," + str(sum_rain) + "\n"
|
||||||
DATA = StringIO(csv)
|
DATA = StringIO(csv)
|
||||||
tsdata = pd.read_csv(DATA, parse_dates=True, infer_datetime_format=True, index_col='date')
|
tsdata = pd.read_csv(DATA,
|
||||||
|
parse_dates=True,
|
||||||
|
infer_datetime_format=True,
|
||||||
|
index_col='date')
|
||||||
et1 = ETo()
|
et1 = ETo()
|
||||||
et1.param_est(tsdata, freq, z_msl, lat, lon, TZ_lon)
|
et1.param_est(tsdata, freq, z_msl, lat, lon, TZ_lon)
|
||||||
et1.ts_param.head()
|
et1.ts_param.head()
|
||||||
|
@ -153,20 +158,18 @@ 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) "
|
||||||
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
|
"VALUES (%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')
|
||||||
data = (
|
data = (aggdate, float(tsdata.loc[aggdate, 'T_max']),
|
||||||
aggdate,float(tsdata.loc[aggdate, 'T_max']),float(tsdata.loc[aggdate, 'T_min']),float(tsdata.loc[aggdate, 'T_mean']),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,
|
||||||
)
|
'T_min']), float(tsdata.loc[aggdate, 'T_mean']),
|
||||||
|
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))
|
||||||
cursor.execute(upsert, data)
|
cursor.execute(upsert, data)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,19 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import argparse
|
import argparse
|
||||||
import configparser
|
import configparser
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print( "Usage: " + __file__ + "[--config path_to_file.ini] [--station smhi_station_number] [--period latest-hour|latest-day|latest-months] [--db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
|
print(
|
||||||
"Default configfile is weather.ini, any parameter can be overwritten on the command line")
|
"Usage: " + __file__ +
|
||||||
|
"[--config path_to_file.ini] [--station smhi_station_number] [--period latest-hour|latest-day|latest-months] [--db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
|
||||||
|
"Default configfile is weather.ini, any parameter can be overwritten on the command line"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--config')
|
parser.add_argument('--config')
|
||||||
parser.add_argument('--station')
|
parser.add_argument('--station')
|
||||||
|
@ -58,21 +64,17 @@ if not (db and host and user and password):
|
||||||
usage()
|
usage()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
mydb = mysql.connector.connect(
|
mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
|
||||||
auth_plugin='mysql_native_password',
|
database=db,
|
||||||
database=db,
|
host=host,
|
||||||
host=host,
|
passwd=password,
|
||||||
passwd=password,
|
user=user)
|
||||||
user=user
|
|
||||||
)
|
|
||||||
|
|
||||||
cursor = mydb.cursor();
|
cursor = mydb.cursor()
|
||||||
|
|
||||||
upsert = (
|
upsert = ("REPLACE INTO weather "
|
||||||
"REPLACE INTO weather "
|
"(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) "
|
||||||
"(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) "
|
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
|
||||||
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
|
|
||||||
)
|
|
||||||
# Metrics mapping
|
# Metrics mapping
|
||||||
rainfall = "7"
|
rainfall = "7"
|
||||||
rel_hum = "6"
|
rel_hum = "6"
|
||||||
|
@ -80,14 +82,19 @@ temp = "1"
|
||||||
winddir = "3"
|
winddir = "3"
|
||||||
windspeed = "4"
|
windspeed = "4"
|
||||||
results = {}
|
results = {}
|
||||||
for metric in [rainfall,rel_hum,temp,winddir,windspeed]:
|
for metric in [rainfall, rel_hum, temp, winddir, windspeed]:
|
||||||
results[metric] = requests.get(url='https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json'.format(metric,station,period))
|
results[metric] = requests.get(
|
||||||
|
url=
|
||||||
|
'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json'
|
||||||
|
.format(metric, station, period))
|
||||||
|
|
||||||
rainfall_arr = sorted(results[rainfall].json()['value'], key = lambda i: i['date'])
|
rainfall_arr = sorted(results[rainfall].json()['value'],
|
||||||
rel_hum_arr = sorted(results[rel_hum].json()['value'], key = lambda i: i['date'])
|
key=lambda i: i['date'])
|
||||||
temp_arr = sorted(results[temp].json()['value'], key = lambda i: i['date'])
|
rel_hum_arr = sorted(results[rel_hum].json()['value'], key=lambda i: i['date'])
|
||||||
winddir_arr = sorted(results[winddir].json()['value'], key = lambda i: i['date'])
|
temp_arr = sorted(results[temp].json()['value'], key=lambda i: i['date'])
|
||||||
windspeed_arr = sorted(results[windspeed].json()['value'], key = lambda i: i['date'])
|
winddir_arr = sorted(results[winddir].json()['value'], key=lambda i: i['date'])
|
||||||
|
windspeed_arr = sorted(results[windspeed].json()['value'],
|
||||||
|
key=lambda i: i['date'])
|
||||||
i = 0
|
i = 0
|
||||||
for k in rainfall_arr:
|
for k in rainfall_arr:
|
||||||
ts = int(k['date'] / 1000)
|
ts = int(k['date'] / 1000)
|
||||||
|
@ -99,13 +106,9 @@ 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 = (
|
data = (date, time, rain, hum, te, widi, wisp, station)
|
||||||
date, time, rain, hum, te, widi, wisp, station
|
|
||||||
)
|
|
||||||
cursor.execute(upsert, data)
|
cursor.execute(upsert, data)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue