diff --git a/main.py b/main.py index 2851b0d7aa470685a6fe415644710063a35751df..cca093748d6d46aaa8e387770b80abcf6a5a3b3c 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,22 @@ """File to execute to show results""" +#Data +from parameters import SESSION + +#Basics +import re +from datetime import datetime, timedelta +import matplotlib.dates as mdates import numpy as np import matplotlib.pyplot as plt -from datetime import datetime, timedelta -from parameters import SESSION +from pandas.plotting import register_matplotlib_converters +register_matplotlib_converters() +#Stats +import statsmodels.graphics as stm_graphs +import pandas as pd +import statsmodels.api as stm +#Graph map +from mpl_toolkits.basemaps import Basemap + def execute_query(query): @@ -16,6 +30,28 @@ def ask_q(possibilities, text=">>> "): answer = input(text) return answer +def ask_d(text=">>> "): + t = input(text) + done = False + + date_parser = re.compile( + r"(?P\d+)-(?P\d+)-(?P\d+) (?P\d+):(?P\d+)" + ) + match = False + result = None + + + + while(match == False): + match = date_parser.match(t) + if not match: + print("Veuillez suivre le format demandé") + continue + m = match.groupdict() + result = (int(m['year']), int(m['month']), int(m['day']), int(m['hour']), int(m['minute'])) + + + return result class Manager: table = None # table name use by the function @@ -61,80 +97,83 @@ class Manager: print("\t-", text, ":", code) attr = ask_q(decision.keys()) - x = [] - y = [] - saisons = { - 'spring': { - 'n': 0, - 'somme': 0, - 'month': [3, 4, 5], # March - May - 'end': lambda dt: datetime(dt.year, 6, 1) - timedelta(days=1), - 'legend': False, - 'color': 'r' - }, - 'summer': { - 'n': 0, - 'somme': 0, - 'month': [6, 7, 8], # June - August - 'end': lambda dt: datetime(dt.year, 9, 1) - timedelta(days=1), - 'legend': False, - 'color': 'y' - }, - 'automne': { - 'n': 0, - 'somme': 0, - 'month': [9, 10, 11], # September - November - 'end': lambda dt: datetime(dt.year, 12, 1) - timedelta(days=1), - 'legend': False, - 'color': 'm' - }, - 'winter': { - 'n': 0, - 'somme': 0, - 'month': [12, 1, 2], # December - Febrarie - 'end': lambda dt: datetime(dt.year + 1, 3, 1) - timedelta(days=1), - 'legend': False, - 'color': 'k' - } - } - query = "SELECT * FROM {} WHERE station={}".format(self.table, station.__repr__()) + ts = pd.Series() + query = "SELECT time,{} FROM {} WHERE station={}".format(attr,self.table, station.__repr__()) for row in execute_query(query): value = getattr(row, attr) if value is None: continue - x.append(datetime(*list(row.time))) - y.append(value) - # For saison - for saison, d in saisons.items(): - if row.time[1] in d['month']: - d['n'] += 1 - d['somme'] += value - break - - date, last_date = x[0], x[-1] # first date and last date, initialisation - x = np.array(x) - y = np.array(y) - plt.plot(x, y, label=attr) - - # Mark saison - while date < last_date: - for saison, d in saisons.items(): - if date.month in d['month']: - next_date = min(last_date, d['end'](date)) - moyenne = d['somme'] / d['n'] - if d['legend']: - plt.plot([date, next_date], [moyenne, moyenne], d['color'], linewidth=4) - else: - plt.plot([date, next_date], [moyenne, moyenne], d['color'], label=saison, linewidth=4) - d['legend'] = True - date = next_date + timedelta(days=1) - break - plt.title(station) + ts.loc[datetime(*list(row.time))] = value + + figure = plt.figure(figsize=(25,16)) + axes = plt.subplot() + axes.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M')) + _=plt.xticks(rotation=90) + + + plt.plot(ts, label=attr) + plt.title("Donnees de " + str(attr) + " pour la station : " + station ) plt.legend() plt.show() + plt.savefig('./out/graph_'+station+'_'+ str(attr) +'.png') + + res = stm.tsa.seasonal_decompose(ts, freq=15, extrapolate_trend= 'freq') + res.plot() + plt.show() + plt.savefig('./out/decompose_'+station+'_'+ str(attr) +'.png') + + + ACF = stm_graphs.tsaplots.plot_acf(ts,lags=30) + plt.show() + plt.savefig('./out/acf_'+station+'_'+ str(attr) +'.png') def map(self): - print("Cette fonction n'est pas implémentée") + self.table = "TABLE_TIME" + + #Ask Date + print("Entrez une date sous la forme YYYY-MM-DD HH:mm") + date = ask_d() + + # Search element + decision = { + "tmpf": "La témparature", + "relh": "L'humidité" + } + print("Choisissez un élément parmis les suivant :") + for code, text in decision.items(): + print("\t-", text, ":", code) + attr = ask_q(decision.keys()) + + + df = pd.DataFrame() + query = "SELECT station,lon,lat,{} FROM {} WHERE time={}".format(attr,self.table, date) + print(query) + for row in execute_query(query): + if getattr(row, "station") is None: + continue + print(getattr(row,"station")) + df = df.append({"station": getattr(row, "station"), + "lon":getattr(row,"lon"), + "lat":getattr(row,"lat"), + "val":getattr(row,attr)}, + ignore_index=True) + + + map = Basemap(projection='mill',lat_0=26.281898,lon_0=-16.42,lat_1 = 43.08,lon_1 =7.35, resolution='l') + # draw coastlines, country boundaries, fill continents. + map.drawcoastlines(linewidth=0.25) + map.drawcountries(linewidth=0.25) + map.fillcontinents(color='coral',lake_color='aqua') + # draw the edge of the map projection region (the projection limb) + map.drawmapboundary(fill_color='aqua') + # draw lat/lon grid lines every 30 degrees. + map.drawmeridians(np.arange(0,360,30)) + map.drawparallels(np.arange(-90,90,30)) + + plt.title('Map') + plt.show() + plt.savefig('./out/map.png') + def cluster(self): print("Cette fonction n'est pas implémentée") diff --git a/parameters.py b/parameters.py index 64e301d7c6374c0329796bfca535fb85cfde08f7..71a9905e46d7e1ccb8be133f0d253f9d1d8eeb4a 100644 --- a/parameters.py +++ b/parameters.py @@ -7,7 +7,7 @@ from collections import OrderedDict import os import datetime # Can be specified -KEY_SPACE = "nf26" +KEY_SPACE = "lhamadac_projet" TABLE = "Spain" # Don't change @@ -22,12 +22,17 @@ COLUMNS = OrderedDict([ ('lon', float), ('lat', float), ('tmpf', float), + ('tmpc',float), ('dwpf', float), + ('dwpc',float), ('relh', float), ('drct', float), ('sknt', float), + ('sped',float), ('alti', float), ('mslp', float), + # ('p01m',float), all null + # ('p01i',float), all null ('vsby', float), ('gust', float), ('skyc1', str), @@ -45,8 +50,7 @@ COLUMNS = OrderedDict([ # ('peak_wind_gust', ), all null # ('peak_wind_drct', ), all null # ('peak_wind_time', ), all null - ('feel', float), - ('metar', str) + ('feel', float) ]) # Name of table -> Primary key TABLES = {