|
|
@ -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:
|
|
|
@ -70,31 +78,23 @@ for i in station_data['position']:
|
|
|
|
lon = i['longitude']
|
|
|
|
lon = i['longitude']
|
|
|
|
TZ_lon = lon
|
|
|
|
TZ_lon = lon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
|
|
|
|
mydb = mysql.connector.connect(
|
|
|
|
|
|
|
|
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
|
|
|
@ -142,9 +142,14 @@ for i in dates:
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|