kma-data-api/visualize_grid.py

58 lines
1.9 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
import os
# 1. 좌표 설정 (34°55'28.93"N 127°42'43.46"E)
def dms_to_decimal(degrees, minutes, seconds):
return degrees + (minutes / 60.0) + (seconds / 3600.0)
TARGET_LAT = dms_to_decimal(34, 55, 28.93)
TARGET_LON = dms_to_decimal(127, 42, 43.46)
# 500m 격자 간격 (근사치)
# 위도 1도 ~ 111km -> 500m ~ 0.0045도
# 경도 1도 (35도 부근) ~ 91km -> 500m ~ 0.0055도
D_LAT = 500 / 111000
D_LON = 500 / (111000 * np.cos(np.radians(TARGET_LAT)))
# 3x3 격자점 계산
lats = [TARGET_LAT + D_LAT, TARGET_LAT, TARGET_LAT - D_LAT]
lons = [TARGET_LON - D_LON, TARGET_LON, TARGET_LON + D_LON]
grid_points = []
for lt in lats:
for ln in lons:
grid_points.append((lt, ln))
# 시각화 (Matplotlib)
plt.figure(figsize=(8, 8))
for i, (lt, ln) in enumerate(grid_points):
color = 'red' if i == 4 else 'blue' # 중앙점은 빨간색
label = 'Target Point' if i == 4 else None
plt.scatter(ln, lt, c=color, s=100, label=label)
plt.text(ln, lt, f' ({ln:.4f}, {lt:.4f})', fontsize=9, verticalalignment='bottom')
plt.title("KMA 500m Grid Points (3x3)")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()
# 상세 정보 출력
print(f"중앙 지점: 위도 {TARGET_LAT:.6f}, 경도 {TARGET_LON:.6f}")
print("-" * 30)
print("인접 9개 격자점 좌표:")
for i, (lt, ln) in enumerate(grid_points):
pos = ["북서", "", "북동", "", "중앙", "", "남서", "", "남동"][i]
print(f"{pos:4}: {lt:.6f}, {ln:.6f}")
# 결과 저장
plt.savefig("grid_points_visualization.png")
print("\n시각화 결과가 'grid_points_visualization.png'로 저장되었습니다.")
# sfc_grid_latlon.nc 파일이 있다면 실제 인덱스 확인 가능 (xarray/netCDF4 필요)
"""
import xarray as xr
ds = xr.open_dataset('sfc_grid_latlon.nc')
# 가장 가까운 인덱스 찾기 로직...
"""