From 38a366239626f42a952e44fdf6bc643c7381d2b3 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Fri, 22 May 2020 16:38:33 +0200 Subject: [PATCH] yapf --- aggregateweather.py | 79 +++++++++++++++++++++++---------------------- getweather.py | 59 +++++++++++++++++---------------- 2 files changed, 72 insertions(+), 66 deletions(-) diff --git a/aggregateweather.py b/aggregateweather.py index 18173c8..252a71f 100755 --- a/aggregateweather.py +++ b/aggregateweather.py @@ -10,9 +10,15 @@ import pandas as pd import requests import sys + def usage(): - print( "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") + print( + "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.add_argument('--config') parser.add_argument('--db') @@ -33,7 +39,6 @@ password = config['MySQL']['password'] station = config['SMHI']['station'] - # Defaults z_msl = 48.854 lat = 59.178503 @@ -61,7 +66,10 @@ if not (db and host and user and password): sys.exit(1) 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 for i in station_data['position']: if i['to'] > newest_to: @@ -69,36 +77,28 @@ for i in station_data['position']: lat = i['latitude'] lon = i['longitude'] TZ_lon = lon - -mydb = mysql.connector.connect( - auth_plugin='mysql_native_password', - database=db, - host=host, - passwd=password, - user=user -) +mydb = mysql.connector.connect(auth_plugin='mysql_native_password', + database=db, + host=host, + passwd=password, + user=user) -cursor = mydb.cursor(); +cursor = mydb.cursor() -date_select = ( - "SELECT DISTINCT `date` " - "FROM weather " -) +date_select = ("SELECT DISTINCT `date` " "FROM weather ") cursor.execute(date_select) dates = cursor.fetchall() csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n" for i in dates: working_date = i[0].strftime('%Y-%m-%d') - day_select = ( - 'SELECT * FROM weather ' - 'WHERE date = "{}"'.format(working_date) - ) + day_select = ('SELECT * FROM weather ' + 'WHERE date = "{}"'.format(working_date)) cursor.execute(day_select) day = cursor.fetchall() sum_rain = 0 - T_max = -9999 + T_max = -9999 T_min = 9999 RH_max = -9999 RH_min = 9999 @@ -132,19 +132,24 @@ for i in dates: sum_temp += j[temp] sum_rel_hum += j[rel_hum] sum_windspeed += j[windspeed] - if T_max < j[temp]: + if T_max < j[temp]: T_max = j[temp] - if T_min > j[temp]: + if 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] - if RH_min > j[rel_hum]: + if RH_min > j[rel_hum]: RH_min = j[rel_hum] T_mean = sum_temp / 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) -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.param_est(tsdata, freq, z_msl, lat, lon, TZ_lon) et1.ts_param.head() @@ -153,20 +158,18 @@ 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)" -) - - + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") for key, value in eto1.items(): aggdate = key.strftime('%Y-%m-%d') - data = ( - 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) - ) + data = (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)) cursor.execute(upsert, data) - - mydb.commit() mydb.close() - diff --git a/getweather.py b/getweather.py index 554ed87..925558e 100755 --- a/getweather.py +++ b/getweather.py @@ -3,13 +3,19 @@ from datetime import datetime import argparse import configparser -import mysql.connector +import mysql.connector import requests import sys + 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" - "Default configfile is weather.ini, any parameter can be overwritten on the command line") + 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" + "Default configfile is weather.ini, any parameter can be overwritten on the command line" + ) + + parser = argparse.ArgumentParser() parser.add_argument('--config') parser.add_argument('--station') @@ -58,21 +64,17 @@ if not (db and host and user and password): usage() sys.exit(1) -mydb = mysql.connector.connect( - auth_plugin='mysql_native_password', - database=db, - host=host, - passwd=password, - user=user -) +mydb = mysql.connector.connect(auth_plugin='mysql_native_password', + database=db, + host=host, + passwd=password, + user=user) -cursor = mydb.cursor(); +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)" -) +upsert = ("REPLACE INTO weather " + "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station) " + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)") # Metrics mapping rainfall = "7" rel_hum = "6" @@ -80,14 +82,19 @@ temp = "1" winddir = "3" windspeed = "4" results = {} -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)) +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)) -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']) -temp_arr = sorted(results[temp].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']) +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']) +temp_arr = sorted(results[temp].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 for k in rainfall_arr: ts = int(k['date'] / 1000) @@ -99,13 +106,9 @@ 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) cursor.execute(upsert, data) i = i + 1 mydb.commit() mydb.close() - -