From bf216721dc3078febace243b120605cde12e35c8 Mon Sep 17 00:00:00 2001 From: sim-baz Date: Mon, 17 Jun 2019 20:01:42 +0200 Subject: [PATCH] Add history for station --- history.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ loading.py | 8 +++----- 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 history.py diff --git a/history.py b/history.py new file mode 100644 index 0000000..1a7920f --- /dev/null +++ b/history.py @@ -0,0 +1,48 @@ +import matplotlib.pyplot as plt +from cassandra.cluster import Cluster +from datetime import datetime + +table_name = "data" +numeric_columns = ["lon","lat","tmpf","dwpf","relh","drct","sknt","p01i","alti","mslp","vsby","gust","skyl1","skyl2","skyl3","skyl4","feel","ice_accretion_1hr","ice_accretion_3hr","ice_accretion_6hr","peak_wind_gust","peak_wind_drct","peak_wind_time"] + +cluster = Cluster() +session = cluster.connect() +session.set_keyspace("bazinsim_roisinos_metar") + +def getHistory(station, indicator): + datas = session.execute(f"SELECT year, month, day, hour, minute, {indicator} FROM {table_name} where station = '{station}'") + # for t in datas: + # print(t[0]) + return datas + +def plotHistory(station, indicator): + if indicator in numeric_columns: + table = getHistory(station, indicator) + + if not table: + print(f"Aucune donnée pour la station {station} et pour l'indicateur {indicator}") + return + + table_date = {} + for r in table: + if (r[len(r) - 1] != None): + date = datetime.strptime(str(r[0]) + "-" + str(r[1]) + "-" + str(r[2]), '%Y-%m-%d').date() + if date not in table_date.keys(): + table_date[date] = 0,0 + table_date[date] = (table_date[date][0] + r[len(r) - 1], table_date[date][1] + 1) + + for d in table_date.keys(): + table_date[d] = table_date[d][0] / table_date[d][1] + + currentDateTime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + file_name = str(currentDateTime) + "_" + station + "_" + indicator + ".png" + + plt.plot_date(table_date.keys(), table_date.values(), '-', xdate = True) + plt.title(f"Evolution de {indicator} pour la station {station}") + plt.xlabel('Date') + plt.ylabel(indicator) + plt.savefig(file_name) + else: + print("Les données pour cet indicateur ne sont pas numériques, impossible de tracer un graphique") + +plotHistory("EFKI", "tmpf") \ No newline at end of file diff --git a/loading.py b/loading.py index 58dfa61..2fbf961 100644 --- a/loading.py +++ b/loading.py @@ -3,6 +3,7 @@ import csv import re FILE_NAME = "asos.txt" +table_name = "data" # Country: Finland # Dates : 2001 to 2010 @@ -256,7 +257,7 @@ def createTableQuery(table): peak_wind_drct decimal, peak_wind_time decimal, metar varchar, - PRIMARY KEY(station) + PRIMARY KEY((station), year, month, day, hour, minute) )""" return query @@ -319,7 +320,6 @@ dict = loadata(FILE_NAME) # # -------------------------------------------------------- # # A faire seulement 1 fois pour charger les données -# table_name = "data" # session.execute(dropTableQuery(table_name)) # print(f"Table {table_name} dropped") # session.execute(createTableQuery(table_name)) @@ -329,6 +329,4 @@ dict = loadata(FILE_NAME) # for d in dict: # session.execute(insertQueryData(d, table_name)) # print(f"Datas inserted into {table_name}") -# # -------------------------------------------------------- - - +# # -------------------------------------------------------- \ No newline at end of file -- GitLab