Adding moon and sun phases

This commit is contained in:
2025-07-10 23:53:16 +00:00
parent 3d7354481b
commit 6747c731c6
7 changed files with 161 additions and 22 deletions

View File

@@ -1,8 +1,29 @@
import pytz
import ephem
import requests
import datetime
from geopy.geocoders import Nominatim
def get_sun_and_moon_phase(lat, long, date_str):
date_str = f"{date_str[:4]}/{date_str[4:6]}/{date_str[6:8]}"
observer = ephem.Observer()
observer.lat = str(lat)
observer.lon = str(long)
observer.date = date_str
sun = ephem.Sun()
sun.compute(observer)
sunrise_time = int(observer.next_rising(sun).datetime().strftime('%Y%m%d'))
sunset_time = int(observer.next_setting(sun).datetime().strftime('%Y%m%d'))
date = ephem.Date(date_str)
moon = ephem.Moon()
moon.compute(date)
return (sunrise_time, sunset_time, moon.phase)
def get_timezone(latitude: float, longitude: float):
geolocator = Nominatim()
location = geolocator.reverse(f"{latitude}, {longitude}")

View File

@@ -143,7 +143,6 @@ CREATE TABLE IF NOT EXISTS weather (
dew_point FLOAT,
apparent_temperature FLOAT,
air_pressure FLOAT,
wind_speed FLOAT,
precipitation FLOAT,
rain FLOAT,
snowfall FLOAT,

View File

@@ -19,12 +19,13 @@ INSERT OR IGNORE INTO parks
("MIN04", "Target Field", 339, 8, 404, 8, 328, 23, 0, 44.981667, -93.278333, 827),
("NYC20", "Citi Field", 335, 8, 408, 8, 330, 8, 0, 40.756944, -73.845833, 13),
("NYC21", "Yankee Stadium II", 318, 8, 408, 8, 314, 8, 0, 40.829167, -73.926389, 23),
("OAK01", "Oakland-Alameda County Coliseum", 330, 8, 400, 8, 330, 8, 0, 37.751667, -122.200556, -16),
("PHI13", "Citizens Bank Park", 329, 10, 401, 6, 330, 13, 0, 39.905833, -75.166389, 0),
("PHO01", "Chase Field", 330, 7.6, 407, 25, 335, 7.6, 1, 33.445526, -112.066664, 1060.36),
("PIT08", "PNC Park", 325, 6, 399, 15, 320, 21, 0, 40.446944, -80.005833, 725),
("OAK01", "Oakland-Alameda County Coliseum", 330, 8, 400, 8, 330, 8, 0, 37.751667, -122.200556, -16),
("SAN02", "Petco Park", 334, 4, 369, 4, 322, 5, 0, 32.7073, -117.1566, 16),
("SEA03", "T-Mobile Park", 331, 15, 401, 7, 326, 7, 1, 47.591, -122.333, 16),
("SEO01", "Gocheok Sky Dome", 325, 12, 400, 12, 325, 12, 1, 37.498222, 126.86725, 40),
("SFO03", "Oracle Park", 339, 8, 399, 8, 309, 25, 0, 37.778611, -122.389167, 13),
("STL10", "Busch Stadium III", 336, 8, 400, 8, 335, 8, 0, 38.6225, -90.193056, 440),
("STP01", "Tropicana Field", 315, 11, 404, 9, 322, 11, 1, 27.768333, -82.653333, 39),

View File

@@ -2,7 +2,7 @@ import os
import csv
import shutil
from data.db_connect import Database
from data.build_weather import get_weather
from data.build_weather import get_weather, get_sun_and_moon_phase
class Importer:
def __init__(self, database: Database):
@@ -191,8 +191,11 @@ class Importer:
self.database.insert(insert_team_game, visiting_team_data)
self.database.insert(insert_team_game, home_team_data)
parkid = game_stats["park-id"]
print(f"SELECT latitude, longitude FROM parks WHERE park_id = {parkid}")
park_data = self.database.select("SELECT latitude, longitude FROM parks WHERE park_id = ?", (game_stats["park-id"],))
print(f"latlon {park_data}")
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"]
@@ -202,10 +205,10 @@ class Importer:
(
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
precipitation, rain, snowfall,
cloud_cover, wind_speed, wind_direction,
wind_gusts, sun_rise, sun_set,
moon_phase
)
VALUES
(
@@ -214,11 +217,17 @@ class Importer:
?, ?, ?,
?, ?, ?,
?, ?, ?,
?, ?
?,
)
"""
(sunrise_time, sunset_time, moonphase) = get_sun_and_moon_phase(park_data[0], park_data[1], game_stats["date"])
weather_data = [
game_id, historic_weather["temperature_2m"][hour], historic_weather["relative_humidity_2m"][hour],
historic_weather["dew_point_2m"][hour], historic_weather["apparent_temperature"][hour], historic_weather["pressure_msl"][hour],
historic_weather["precipitation"][hour], historic_weather["rain"][hour], historic_weather["snowfall"][hour],
historic_weather["cloud_cover"][hour], historic_weather["wind_speed_10m"][hour], historic_weather["wind_direction_10m"][hour],
historic_weather["wind_gusts_10m"][hour], sunrise_time, sunset_time,
moonphase,
]