Started adding weather code
This commit is contained in:
@@ -1,75 +1,85 @@
|
||||
import pytz
|
||||
import requests
|
||||
import datetime
|
||||
from geopy.geocoders import Nominatim
|
||||
|
||||
class Weather:
|
||||
# Example usage (requires OpenWeatherMap API key)
|
||||
# Replace 'YOUR_API_KEY' with your actual key from https://openweathermap.org/
|
||||
def get_weather(self, latitude: float, longitude: float, date_str: str, hour: int) -> dict:
|
||||
"""
|
||||
Fetches weather data for the specified location and time.
|
||||
def get_timezone(latitude: float, longitude: float):
|
||||
geolocator = Nominatim()
|
||||
location = geolocator.reverse(f"{latitude}, {longitude}")
|
||||
|
||||
if location:
|
||||
for tag in location.raw['address']:
|
||||
if 'timezone' in tag:
|
||||
tz_name = tag.split('=')[1]
|
||||
return pytz.timezone(tz_name)
|
||||
|
||||
def get_weather(latitude: float, longitude: float, date_str: str, hour: int) -> dict:
|
||||
"""
|
||||
Fetches weather data for the specified location and time.
|
||||
|
||||
Args:
|
||||
latitude (float): Latitude of the location in degrees.
|
||||
longitude (float): Longitude of the location in degrees.
|
||||
date_str (str): Date in YYYYMMDD format.
|
||||
hour (int): Hour (0-23) when you want to know the weather.
|
||||
|
||||
Returns:
|
||||
dict: Dictionary containing temperature, condition, and sunrise/sunset times.
|
||||
"""
|
||||
# Convert date string components for API request
|
||||
date = f"{date_str[:4]}-{date_str[4:6]}-{date_str[6:8]}"
|
||||
|
||||
stats_to_get = [
|
||||
"temperature_2m",
|
||||
"relative_humidity_2m",
|
||||
"dew_point_2m",
|
||||
"apparent_temperature",
|
||||
"pressure_msl",
|
||||
"surface_pressure",
|
||||
"precipitation",
|
||||
"rain",
|
||||
"snowfall",
|
||||
"cloud_cover",
|
||||
"cloud_cover_low",
|
||||
"cloud_cover_mid",
|
||||
"cloud_cover_high",
|
||||
"shortwave_radiation",
|
||||
"direct_radiation",
|
||||
"direct_normal_irradiance",
|
||||
"diffuse_radiation",
|
||||
"global_tilted_irradiance",
|
||||
"sunshine_duration",
|
||||
"wind_speed_10m",
|
||||
"wind_speed_100m",
|
||||
"wind_direction_10m",
|
||||
"wind_direction_100m",
|
||||
"wind_gusts_10m",
|
||||
"et0_fao_evapotranspiration",
|
||||
"weather_code",
|
||||
"snow_depth",
|
||||
"vapour_pressure_deficit",
|
||||
"soil_temperature_0_to_7cm",
|
||||
"soil_temperature_7_to_28cm",
|
||||
"soil_temperature_28_to_100cm",
|
||||
"soil_temperature_100_to_255cm",
|
||||
"soil_moisture_0_to_7cm",
|
||||
"soil_moisture_7_to_28cm",
|
||||
"soil_moisture_28_to_100cm",
|
||||
"soil_moisture_100_to_255cm",
|
||||
]
|
||||
|
||||
stats_to_get = ','.join(stats_to_get)
|
||||
timezone = get_timezone(latitude, longitude)
|
||||
|
||||
try:
|
||||
api_url = f"https://archive-api.open-meteo.com/v1/archive?latitude={latitude}&longitude={longitude}&timezone={timezone}&start_date={date}&end_date={date}&hourly={stats_to_get}"
|
||||
response = requests.get(api_url)
|
||||
data = response.json()
|
||||
|
||||
Args:
|
||||
latitude (float): Latitude of the location in degrees.
|
||||
longitude (float): Longitude of the location in degrees.
|
||||
date_str (str): Date in YYYYMMDD format.
|
||||
hour (int): Hour (0-23) when you want to know the weather.
|
||||
|
||||
Returns:
|
||||
dict: Dictionary containing temperature, condition, and sunrise/sunset times.
|
||||
"""
|
||||
# Convert date string components for API request
|
||||
date = f"{date_str[:4]}-{date_str[4:6]}-{date_str[6:8]}"
|
||||
if response.status_code != 200:
|
||||
return {"error": "Failed to fetch weather", "details": data}
|
||||
|
||||
stats_to_get = [
|
||||
"temperature_2m",
|
||||
"relative_humidity_2m",
|
||||
"dew_point_2m",
|
||||
"apparent_temperature",
|
||||
"pressure_msl",
|
||||
"surface_pressure",
|
||||
"precipitation",
|
||||
"rain",
|
||||
"snowfall",
|
||||
"cloud_cover",
|
||||
"cloud_cover_low",
|
||||
"cloud_cover_mid",
|
||||
"cloud_cover_high",
|
||||
"shortwave_radiation",
|
||||
"direct_radiation",
|
||||
"direct_normal_irradiance",
|
||||
"diffuse_radiation",
|
||||
"global_tilted_irradiance",
|
||||
"sunshine_duration",
|
||||
"wind_speed_10m",
|
||||
"wind_speed_100m",
|
||||
"wind_direction_10m",
|
||||
"wind_direction_100m",
|
||||
"wind_gusts_10m",
|
||||
"et0_fao_evapotranspiration",
|
||||
"weather_code",
|
||||
"snow_depth",
|
||||
"vapour_pressure_deficit",
|
||||
"soil_temperature_0_to_7cm",
|
||||
"soil_temperature_7_to_28cm",
|
||||
"soil_temperature_28_to_100cm",
|
||||
"soil_temperature_100_to_255cm",
|
||||
"soil_moisture_0_to_7cm",
|
||||
"soil_moisture_7_to_28cm",
|
||||
"soil_moisture_28_to_100cm",
|
||||
"soil_moisture_100_to_255cm",
|
||||
]
|
||||
|
||||
stats_to_get = ','.join(stats_to_get)
|
||||
|
||||
try:
|
||||
api_url = f"https://archive-api.open-meteo.com/v1/archive?latitude={latitude}&longitude={longitude}&start_date={date}&end_date={date}&hourly={stats_to_get}"
|
||||
response = requests.get(api_url)
|
||||
data = response.json()
|
||||
|
||||
if response.status_code != 200:
|
||||
return {"error": "Failed to fetch weather", "details": data}
|
||||
|
||||
return data
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e), "details": f"No weather data available for {latitude}, {longitude} on {date_str}"}
|
||||
return data
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e), "details": f"No weather data available for {latitude}, {longitude} on {date_str}"}
|
||||
|
||||
@@ -13,17 +13,26 @@ class Database:
|
||||
cursor.executescript(sql_script_string)
|
||||
self.db.commit()
|
||||
|
||||
def select(self, index):
|
||||
def select(self, query, values):
|
||||
# Query the database for the specified index
|
||||
cursor = self.db.cursor()
|
||||
query = "SELECT name, address FROM people WHERE id = ?"
|
||||
cursor.execute(query, (index,))
|
||||
cursor.execute(query, values)
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
return result
|
||||
else:
|
||||
return None
|
||||
|
||||
def selectall(self, query, values):
|
||||
# Query the database for the specified index
|
||||
cursor = self.db.cursor()
|
||||
cursor.execute(query, values)
|
||||
result = cursor.fetchall()
|
||||
if result:
|
||||
return result
|
||||
else:
|
||||
return None
|
||||
|
||||
def insert(self, query, values):
|
||||
# Insert new entry into the database
|
||||
cursor = self.db.cursor()
|
||||
|
||||
@@ -138,15 +138,20 @@ CREATE TABLE IF NOT EXISTS team_game (
|
||||
|
||||
CREATE TABLE IF NOT EXISTS weather (
|
||||
game_id INTEGER NOT NULL,
|
||||
temperature SMALLINT,
|
||||
wind_speed FLOAT,
|
||||
air_pressure FLOAT,
|
||||
temperature FLOAT,
|
||||
humidity SMALLINT UNSIGNED,
|
||||
uv_index FLOAT,
|
||||
air_quality TINYINT UNSIGNED,
|
||||
percipitation_type CHAR(10),
|
||||
percipitation_amount FLOAT,
|
||||
sky_condition CHAR(20),
|
||||
dew_point FLOAT,
|
||||
apparent_temperature FLOAT,
|
||||
air_pressure FLOAT,
|
||||
wind_speed FLOAT,
|
||||
precipitation FLOAT,
|
||||
rain FLOAT,
|
||||
snowfall FLOAT,
|
||||
cloud_cover SMALLINT UNSIGNED,
|
||||
wind_speed FLOAT,
|
||||
wind_direction SMALLINT UNSIGNED,
|
||||
wind_gusts SMALLINT UNSIGNED,
|
||||
|
||||
sun_rise TIME,
|
||||
sun_set TIME,
|
||||
moon_phase TINYINT UNSIGNED,
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import csv
|
||||
import shutil
|
||||
from data.db_connect import Database
|
||||
from data.build_weather import get_weather
|
||||
|
||||
class Importer:
|
||||
def __init__(self, database: Database):
|
||||
@@ -71,6 +72,19 @@ class Importer:
|
||||
)
|
||||
"""
|
||||
|
||||
game_data = [
|
||||
game_stats["date"], game_stats["num-of-game"], game_stats["day-of-week"],
|
||||
game_stats["length-in-outs"], game_stats["day-night"], game_stats["completion-info"],
|
||||
game_stats["forfeit"], game_stats["protest"], game_stats["park-id"],
|
||||
game_stats["attendance"], game_stats["length-in-min"], game_stats["home-plate-ump-id"],
|
||||
game_stats["home-plate-ump-name"], game_stats["1b-plate-ump-id"], game_stats["1b-plate-ump-name"],
|
||||
game_stats["2b-plate-ump-id"], game_stats["2b-plate-ump-name"], game_stats["3b-plate-ump-id"],
|
||||
game_stats["3b-plate-ump-name"], game_stats["lf-plate-ump-id"], game_stats["lf-plate-ump-name"],
|
||||
game_stats["rf-plate-ump-id"], game_stats["rf-plate-ump-name"],
|
||||
]
|
||||
|
||||
game_id = self.database.insert(insert_game, game_data)
|
||||
|
||||
insert_team_game = """
|
||||
INSERT INTO team_game
|
||||
(
|
||||
@@ -124,19 +138,6 @@ class Importer:
|
||||
)
|
||||
"""
|
||||
|
||||
game_data = [
|
||||
game_stats["date"], game_stats["num-of-game"], game_stats["day-of-week"],
|
||||
game_stats["length-in-outs"], game_stats["day-night"], game_stats["completion-info"],
|
||||
game_stats["forfeit"], game_stats["protest"], game_stats["park-id"],
|
||||
game_stats["attendance"], game_stats["length-in-min"], game_stats["home-plate-ump-id"],
|
||||
game_stats["home-plate-ump-name"], game_stats["1b-plate-ump-id"], game_stats["1b-plate-ump-name"],
|
||||
game_stats["2b-plate-ump-id"], game_stats["2b-plate-ump-name"], game_stats["3b-plate-ump-id"],
|
||||
game_stats["3b-plate-ump-name"], game_stats["lf-plate-ump-id"], game_stats["lf-plate-ump-name"],
|
||||
game_stats["rf-plate-ump-id"], game_stats["rf-plate-ump-name"],
|
||||
]
|
||||
|
||||
game_id = self.database.insert(insert_game, game_data)
|
||||
|
||||
visiting_team_data = [
|
||||
game_id, game_stats["visiting-team"], game_stats["visiting-game-num"],
|
||||
game_stats["visiting-score"], game_stats["visiting-line-scores"], game_stats["visiting-at-bats"],
|
||||
@@ -189,3 +190,35 @@ class Importer:
|
||||
|
||||
self.database.insert(insert_team_game, visiting_team_data)
|
||||
self.database.insert(insert_team_game, home_team_data)
|
||||
|
||||
park_data = self.database.select("SELECT latitude, longitude FROM parks WHERE park_id = ?", (game_stats["park-id"],))
|
||||
|
||||
hour = 15 if game_stats["day-night"] == "D" else 19
|
||||
historic_weather = get_weather(park_data[0], park_data[1], game_stats["date"], hour)
|
||||
historic_weather = historic_weather["hourly"]
|
||||
|
||||
insert_into_weather = """
|
||||
INSERT INTO weather
|
||||
(
|
||||
game_id, temperature, humidity,
|
||||
dew_point, apparent_temperature, air_pressure,
|
||||
wind_speed, precipitation, rain,
|
||||
snowfall, cloud_cover, wind_speed,
|
||||
wind_direction, wind_gusts, sun_rise,
|
||||
sin_set, moon_phase
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?
|
||||
)
|
||||
"""
|
||||
|
||||
weather_data = [
|
||||
game_id, historic_weather["temperature_2m"][hour], historic_weather["relative_humidity_2m"][hour],
|
||||
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user