Skip to content
Snippets Groups Projects
Commit cc8a6173 authored by Clemens Berteld's avatar Clemens Berteld
Browse files

Minor changes

parent 1ad33abf
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ cfg = configparser.ConfigParser() ...@@ -20,6 +20,7 @@ cfg = configparser.ConfigParser()
cfg.read('config.ini') cfg.read('config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini" assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"] param_postgres = cfg["POSTGRES"]
param_interpol = cfg["INTERPOLATION"]
stationGPD = None stationGPD = None
...@@ -65,7 +66,7 @@ def dbexists(db_name): ...@@ -65,7 +66,7 @@ def dbexists(db_name):
def check_for_db_existence(station_list, db_name): def check_for_db_existence(station_list, db_name):
print("Checking for database existence") print("Checking for database existence")
if dbexists(db_name): if dbexists(db_name):
print('DB existing exists') print('DB existing')
else: else:
create_db(db_name) create_db(db_name)
create_table(station_list, db_name) create_table(station_list, db_name)
...@@ -89,9 +90,11 @@ def create_table(station_list, db_name): ...@@ -89,9 +90,11 @@ def create_table(station_list, db_name):
def insert_empty_matrix_into_db(): def insert_empty_matrix_into_db():
print('Inserting empty matrix into database')
matrix_density = param_interpol['matrix_density']
with psycopg2.connect(database=param_postgres['dbName'], user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection: with psycopg2.connect(database=param_postgres['dbName'], user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection:
with connection.cursor() as cursor: with connection.cursor() as cursor:
with open('clipped_matrix_25x25.csv', 'r') as matrix: with open('clipped_matrix_{}x{}.csv'.format(matrix_density, matrix_density), 'r') as matrix:
matrix_data = matrix.readlines() matrix_data = matrix.readlines()
for line in matrix_data[1:]: for line in matrix_data[1:]:
values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection) values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection)
...@@ -107,10 +110,11 @@ def insert_empty_matrix_into_db(): ...@@ -107,10 +110,11 @@ def insert_empty_matrix_into_db():
# print(query.as_string(cursor)) # print(query.as_string(cursor))
# print(values) # print(values)
cursor.execute(query, values) cursor.execute(query, values)
print('Inserted empty matrix into database')
def create_matrix_data(): def create_matrix_data():
print('Calculating interpolation data for matrix')
# start_time = time.time()
with psycopg2.connect(database=param_postgres['dbName'], user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"], with psycopg2.connect(database=param_postgres['dbName'], user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"],
keepalives=1, keepalives_idle=30, keepalives_interval=10, keepalives_count=5) as connection: keepalives=1, keepalives_idle=30, keepalives_interval=10, keepalives_count=5) as connection:
with connection.cursor() as cursor: with connection.cursor() as cursor:
...@@ -127,10 +131,12 @@ def create_matrix_data(): ...@@ -127,10 +131,12 @@ def create_matrix_data():
update_data.append({'year': year, 'value': value, 'id': id}) update_data.append({'year': year, 'value': value, 'id': id})
query = sql.SQL("""UPDATE stations SET "%(year)s" = %(value)s WHERE id = %(id)s; """) query = sql.SQL("""UPDATE stations SET "%(year)s" = %(value)s WHERE id = %(id)s; """)
psycopg2.extras.execute_batch(cursor, query, update_data) # 2 times faster than using execute() in a for loop, ~20 mins instead of 40 psycopg2.extras.execute_batch(cursor, query, update_data) # Multiple times faster than using execute() in a for loop
# print((time.time() - start_time), 'seconds')
def insert_data(station_list, db_name): def insert_data(station_list, db_name):
print('Inserting data into database')
with psycopg2.connect(database=db_name, user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection: with psycopg2.connect(database=db_name, user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection:
with connection.cursor() as cursor: with connection.cursor() as cursor:
...@@ -160,8 +166,5 @@ def export(station_list): ...@@ -160,8 +166,5 @@ def export(station_list):
check_for_db_existence(station_list, param_postgres['dbName']) check_for_db_existence(station_list, param_postgres['dbName'])
insert_data(station_list, param_postgres['dbName']) insert_data(station_list, param_postgres['dbName'])
insert_empty_matrix_into_db() insert_empty_matrix_into_db()
create_matrix_data()
print('Installation successful. You can run the api.py now.')
start_time = time.time()
create_matrix_data()
print((time.time() - start_time), 'seconds')
...@@ -85,7 +85,7 @@ def get_interpolation_data_for_point(lat, lon, columns): ...@@ -85,7 +85,7 @@ def get_interpolation_data_for_point(lat, lon, columns):
year_columns = (str(columns).replace("""SQL('""", "").replace('"', '').replace("')", "")).split(',') year_columns = (str(columns).replace("""SQL('""", "").replace('"', '').replace("')", "")).split(',')
neighbours = get_neighbours(cursor, lat, lon, columns) neighbours = get_neighbours(cursor, lat, lon, columns)
avg_data = calc_idw(neighbours, year_columns) avg_data = calc_idw(neighbours, year_columns)
print(avg_data) # print(avg_data)
return avg_data return avg_data
......
...@@ -6,4 +6,7 @@ password = postgres ...@@ -6,4 +6,7 @@ password = postgres
dbName = temperatures_berteld_morstein dbName = temperatures_berteld_morstein
[INTERPOLATION] [INTERPOLATION]
amount_neighbours = 5 ; neighbouring measurements used for interpolation
\ No newline at end of file amount_neighbours = 5
; in km. 25km or 10km possible
matrix_density = 25
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment