Commit 57136175 authored by Tianyang's avatar Tianyang

change database: date

parent cbaad544
No preview for this file type
...@@ -12,7 +12,8 @@ def connection(): ...@@ -12,7 +12,8 @@ def connection():
def databaseCreate_Q2(session): def databaseCreate_Q2(session):
query = """ query = """
CREATE TABLE database_time ( CREATE TABLE database_time (
date timestamp, date varchar,
time varchar,
lon float, lon float,
lat float, lat float,
station varchar, station varchar,
...@@ -43,7 +44,7 @@ def databaseCreate_Q2(session): ...@@ -43,7 +44,7 @@ def databaseCreate_Q2(session):
peak_wind_time varchar, peak_wind_time varchar,
feel float, feel float,
metar varchar, metar varchar,
PRIMARY KEY ((date),lon,lat,station) PRIMARY KEY ((date),time,lon,lat,station)
)""" )"""
session.execute(query) session.execute(query)
print("DATA BASE database_time created!") print("DATA BASE database_time created!")
...@@ -51,17 +52,20 @@ def databaseCreate_Q2(session): ...@@ -51,17 +52,20 @@ def databaseCreate_Q2(session):
def load_data(filename): def load_data(filename):
with open(filename) as f: with open(filename) as f:
dateparser = re.compile("(?P<date>\d+-\d+-\d+) (?P<time>\d+:\d+)")
for r in csv.DictReader(f): for r in csv.DictReader(f):
match_time = dateparser.match(r["valid"])
if not r["valid"]: if not r["valid"]:
continue continue
time = match_time.groupdict()
for colonne in r: for colonne in r:
if r[colonne] == "M": if r[colonne] == "M":
r[colonne]= "nan" r[colonne]= "nan"
data = {} data = {}
data["date"] = r["valid"] data["date"] = time["date"]
data["station"] = r["station"] data["station"] = r["station"]
data["time"] = time["time"]
data["lon"] = float(r["lon"]) data["lon"] = float(r["lon"])
data["lat"] = float(r["lat"]) data["lat"] = float(r["lat"])
data["tmpf"] = float(r["tmpf"]) data["tmpf"] = float(r["tmpf"])
......
...@@ -16,6 +16,7 @@ def databaseCreate_Q3(session): ...@@ -16,6 +16,7 @@ def databaseCreate_Q3(session):
lon float, lon float,
lat float, lat float,
station varchar, station varchar,
time varchar,
tmpf float, tmpf float,
dwpf float, dwpf float,
relh float, relh float,
...@@ -43,27 +44,30 @@ def databaseCreate_Q3(session): ...@@ -43,27 +44,30 @@ def databaseCreate_Q3(session):
peak_wind_time varchar, peak_wind_time varchar,
feel float, feel float,
metar varchar, metar varchar,
PRIMARY KEY ((date),lon,lat,station) PRIMARY KEY ((date),lon,lat,station,time)
)""" )"""
session.execute(query) session.execute(query)
print("DATA BASE database_kmeans created!") print("DATA BASE database_kmeans created!")
def load_data(filename): def load_data(filename):
dateparser = re.compile("(?P<date>\d+-\d+-\d+) (?P<time>\d+:\d+)")
with open(filename) as f: with open(filename) as f:
for r in csv.DictReader(f): for r in csv.DictReader(f):
if not r["valid"]: match_time = dateparser.match(r["valid"])
if not match_time:
continue continue
time = match_time.groupdict()
for collonne in r: for collonne in r:
if r[collonne] == "M": if r[collonne] == "M":
r[collonne]= "nan" r[collonne]= "nan"
data = {} data = {}
data["date"] = r["valid"] data["date"] = time["date"]
data["station"] = r["station"] data["station"] = r["station"]
data["lon"] = float(r["lon"]) data["lon"] = float(r["lon"])
data["lat"] = float(r["lat"]) data["lat"] = float(r["lat"])
data["time"] = time["time"]
data["tmpf"] = float(r["tmpf"]) data["tmpf"] = float(r["tmpf"])
data["dwpf"] = float(r["dwpf"]) data["dwpf"] = float(r["dwpf"])
data["relh"] = float(r["relh"]) data["relh"] = float(r["relh"])
......
...@@ -12,8 +12,8 @@ def createMap(data): ...@@ -12,8 +12,8 @@ def createMap(data):
count = 0 count = 0
for each in data.result(): for each in data.result():
#print(each) #print(each)
mean_lat += each[1] mean_lat += each[2]
mean_lon += each[2] mean_lon += each[3]
count += 1 count += 1
if count == 0: if count == 0:
print('No data available at this timestamp !') print('No data available at this timestamp !')
...@@ -28,12 +28,14 @@ def createMap(data): ...@@ -28,12 +28,14 @@ def createMap(data):
"metar", "mslp", "p01i", "peak_wind_drct", "peak_wind_gust", "peak_wind_time", "relh", "sknt", "metar", "mslp", "p01i", "peak_wind_drct", "peak_wind_gust", "peak_wind_time", "relh", "sknt",
"skyc1", "skyc2", "skyc3", "skyc4", "skyl1", "skyl2", "skyl3", "skyl4", "tmpf", "vsby", "wxcodes"] "skyc1", "skyc2", "skyc3", "skyc4", "skyl1", "skyl2", "skyl3", "skyl4", "tmpf", "vsby", "wxcodes"]
for each in data.result(): for each in data.result():
# print(each) print(each)
# Here we choose not to display the None values and the METAR ID # Here we choose not to display the None values and the METAR ID
l = [attributes[i] + ":" + str(each[i + 4]) for i in range(len(attributes)) if each[i + 4] != None ls = ["station:"+each[4]+"\n"]
l = [attributes[i] + ":" + str(each[i + 5]) for i in range(len(attributes)) if each[i + 5] != None
and attributes[i] != "metar"] and attributes[i] != "metar"]
l = ls + l
string='\n'.join(l) string='\n'.join(l)
folium.Marker([each[2],each[1]], folium.Marker([each[3],each[2]],
popup=string, popup=string,
icon=folium.Icon(color='red')).add_to(m) icon=folium.Icon(color='red')).add_to(m)
m.save("Projet-NF26/map2.html") m.save("Projet-NF26/map2.html")
...@@ -44,8 +46,7 @@ if __name__ == "__main__": ...@@ -44,8 +46,7 @@ if __name__ == "__main__":
session = connection() session = connection()
# Timestamp user wants to search # Timestamp user wants to search
date = input("Please enter the date you want to search (format 'yyyy-MM-dd'):") date = input("Please enter the date you want to search (format 'yyyy-MM-dd'):")
time = input("Please enter the time you want to search (format 'hh:MM:ss'):") time = input("Please enter the time you want to search (format 'hh:MM'):")
#timestamp = '2017-12-02 00:30:00' #timestamp = '2017-12-02 00:30:00'
timestamp = date + ' ' + time data = session.execute_async("select * from caitiany.database_time where date = '%s' AND time = '%s'"%(date,time))
data = session.execute_async("select * from caitiany.database_time where date = '%s' ALLOW FILTERING"%(timestamp))
createMap(data) createMap(data)
...@@ -6,6 +6,7 @@ import matplotlib.pyplot as plt ...@@ -6,6 +6,7 @@ import matplotlib.pyplot as plt
import re import re
import folium import folium
import random import random
import datetime
table_variable = [ table_variable = [
...@@ -13,6 +14,7 @@ table_variable = [ ...@@ -13,6 +14,7 @@ table_variable = [
'lon', 'lon',
'lat', 'lat',
'station', 'station',
'time',
'alti', 'alti',
'drct', 'drct',
'dwpf', 'dwpf',
......
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