Commit 563b636a authored by sim-baz's avatar sim-baz

Back to date by year, month, day

parent 274af57f
......@@ -9,27 +9,22 @@ table_name_space = loading.table_name_space
table_name_date = loading.table_name_date
numeric_columns = loading.numeric_columns
MIN_DATE = 2001
MAX_DATE = 2010
cluster = Cluster()
session = cluster.connect()
session.set_keyspace("bazinsim_roisinos_metar")
MIN_DATE = loading.MIN_DATE
MAX_DATE = loading.MAX_DATE
def getHistory(station, indicator):
datas = session.execute(f"SELECT datetime, {indicator} FROM {table_name_space} where station = '{station}'")
datas = session.execute(f"SELECT year, month, day, {indicator} FROM {table_name_space} where station = '{station}'")
return datas
def getMeanByDay(table, dateMin, dateMax):
# Dictionary to store sum of measures and number of measures by day
table_date = {}
for r in table:
# convert timestamp into year as int
year = int(str(r[0])[0:4])
year = r[0]
# Verify the measures is for the period chosen by user and have a value
if year >= dateMin and year < dateMax and r[len(r) - 1] != None:
# convert timestamp into date as string
date = str(r[0])[0:10]
# convert attributes to date format as string
date = str(r[0]) + "-" + str(r[1]) + "-" + str(r[2])
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)
......@@ -46,8 +41,7 @@ def getMeanByMonth(table):
for r in table:
# Verify the value of measure (needs to have a value)
if r[len(r) - 1] != None:
# convert timestamp into month as string
month = str(r[0])[5:7]
month = r[1]
if month not in table_month.keys():
table_month[month] = 0,0
table_month[month] = (table_month[month][0] + r[len(r) - 1], table_month[month][1] + 1)
......@@ -141,6 +135,10 @@ def plotHistory(station, indicator, dateMin, dateMax):
print("Les données pour cet indicateur ne sont pas numériques, impossible de tracer un graphique")
if __name__ == '__main__':
cluster = Cluster()
session = cluster.connect()
session.set_keyspace("bazinsim_roisinos_metar")
print()
plotHistory("EFKI", "tmpf", 2001, 2005)
print()
\ No newline at end of file
......@@ -8,6 +8,9 @@ table_name_date = "date"
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"]
MIN_DATE = 2001
MAX_DATE = 2010
# Country: Finland
# Dates : 2001 to 2010
......@@ -54,7 +57,11 @@ def loadata(filename):
data["station"] = r["station"]
valid = dateparser.match(r["valid"]).groupdict()
data["date_and_time"] = valid["year"] + "-" + valid["month"] + "-" + valid["day"] + " " + valid["hour"] + ":" + valid["minute"] + ":00"
data["year"] = int(valid["year"])
data["month"] = int(valid["month"])
data["day"] = int(valid["day"])
data["hour"] = int(valid["hour"])
data["minute"] = int(valid["minute"])
data["lon"] = float(r["lon"])
data["lat"] = float(r["lat"])
......@@ -177,7 +184,11 @@ def loadata(filename):
NAME_COLUMNS = """
station ,
datetime ,
year ,
month ,
day ,
hour ,
minute ,
lon ,
lat ,
tmpf ,
......@@ -218,7 +229,11 @@ def dropTableQuery(table):
def createTableQuery(table):
query = f"""CREATE TABLE {table}(
station varchar,
datetime timestamp,
year int,
month int,
day int,
hour int,
minute int,
lon decimal,
lat decimal,
tmpf decimal,
......@@ -248,14 +263,18 @@ def createTableQuery(table):
peak_wind_drct decimal,
peak_wind_time decimal,
metar varchar,
PRIMARY KEY((station), datetime)
PRIMARY KEY((station), year, month, day, hour, minute)
)"""
return query
def createTableQueryPartitionningByDate(table):
query = f"""CREATE TABLE {table}(
station varchar,
datetime timestamp,
year int,
month int,
day int,
hour int,
minute int,
lon decimal,
lat decimal,
tmpf decimal,
......@@ -285,7 +304,7 @@ def createTableQueryPartitionningByDate(table):
peak_wind_drct decimal,
peak_wind_time decimal,
metar varchar,
PRIMARY KEY((datetime), station)
PRIMARY KEY((year), month, day, hour, minute, station)
)"""
return query
......@@ -297,7 +316,11 @@ def insertQueryData(row, table):
)
VALUES(
'{row["station"]}',
'{row["date_and_time"]}',
{row["year"]},
{row["month"]},
{row["day"]},
{row["hour"]},
{row["minute"]},
{row["lon"]},
{row["lat"]},
{row["tmpf"]},
......@@ -342,18 +365,18 @@ if __name__ == '__main__':
dict = loadata(FILE_NAME)
# --------------------------------------------------------
# A faire seulement 1 fois pour charger les données
session.execute(dropTableQuery(table_name_space))
print(f"Table {table_name_space} dropped")
session.execute(createTableQuery(table_name_space))
print(f"Table {table_name_space} created")
print(f"Starting inserting datas into table {table_name_space}")
for d in dict:
session.execute(insertQueryData(d, table_name_space))
print(f"Datas inserted into {table_name_space}")
# --------------------------------------------------------
# # --------------------------------------------------------
# # A faire seulement 1 fois pour charger les données
# session.execute(dropTableQuery(table_name_space))
# print(f"Table {table_name_space} dropped")
# session.execute(createTableQuery(table_name_space))
# print(f"Table {table_name_space} created")
# print(f"Starting inserting datas into table {table_name_space}")
# for d in dict:
# session.execute(insertQueryData(d, table_name_space))
# print(f"Datas inserted into {table_name_space}")
# # --------------------------------------------------------
# --------------------------------------------------------
# A faire seulement 1 fois pour charger les données
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment