history.py 1.76 KB
Newer Older
sim-baz's avatar
sim-baz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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")