import sys
import os
import asyncio
from dotenv import load_dotenv
import json
# Ensure import works
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
try:
from server import convert_latlon_to_grid, get_village_forecast
except ImportError:
# Fallback if running from root
sys.path.append(os.path.join(current_dir, "src"))
from src.server import convert_latlon_to_grid, get_village_forecast
load_dotenv()
async def main():
# Gyeongbuk Yecheon Coordinates
lat = 36.6577
lon = 128.4528
nx, ny = convert_latlon_to_grid(lat, lon)
print(f"π Yecheon ({lat}, {lon}) -> Grid ({nx}, {ny})")
print("\n1οΈβ£ Fetching 3-Day Forecast (Vilage Fcst)...")
try:
# Fetch data
result_str = await get_village_forecast(lat, lon)
data = json.loads(result_str)
items = data['response']['body']['items']['item']
# Group by Date
forecast = {}
for item in items:
fcstDate = item['fcstDate']
fcstTime = item['fcstTime']
category = item['category']
value = item['fcstValue']
if fcstDate not in forecast:
forecast[fcstDate] = {}
if fcstTime not in forecast[fcstDate]:
forecast[fcstDate][fcstTime] = {}
forecast[fcstDate][fcstTime][category] = value
# Print Summary for next 3 days
print(f"β
Retrieved {len(forecast)} days of data.")
sorted_dates = sorted(forecast.keys())
for date in sorted_dates[:3]: # Show up to 3 days
print(f"\nπ
Date: {date}")
# Pick noon duration (1200) or nearest for temp sample
times = forecast[date].keys()
sample_time = "1200" if "1200" in times else sorted(times)[0]
data_at_time = forecast[date].get(sample_time, {})
temp = data_at_time.get('TMP', 'N/A')
sky = data_at_time.get('SKY', 'N/A')
pop = data_at_time.get('POP', 'N/A')
sky_map = {"1": "λ§μ", "3": "ꡬλ¦λ§μ", "4": "νλ¦Ό"}
sky_str = sky_map.get(sky, sky)
print(f" π {sample_time} κΈ°μ€: κΈ°μ¨ {temp}β, νλ {sky_str}, κ°μνλ₯ {pop}%")
# Find Min/Max for the day (TMN/TMX)
# TMN/TMX are usually at 0600 and 1500 in data, need to scan all items
# Simplified: just scan all values in this date for TMX/TMN
tmn = -999
tmx = -999
for t_time in forecast[date]:
cats = forecast[date][t_time]
if 'TMN' in cats: tmn = cats['TMN']
if 'TMX' in cats: tmx = cats['TMX']
if tmn != -999 or tmx != -999:
print(f" π‘οΈ μ΅μ /μ΅κ³ : {tmn}/{tmx} β")
except Exception as e:
print(f"β Error: {e}")
if __name__ == "__main__":
asyncio.run(main())