47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import xarray as xr
|
|
import numpy as np
|
|
import os
|
|
|
|
# 1. 파일 열기
|
|
file_path = 'sfc_grid_latlon.nc'
|
|
if not os.path.exists(file_path):
|
|
print(f"Error: {file_path} not found.")
|
|
exit(1)
|
|
|
|
ds = xr.open_dataset(file_path)
|
|
|
|
# 2. 대상 좌표 (34°55'28.93"N 127°42'43.46"E)
|
|
target_lat = 34 + (55/60) + (28.93/3600)
|
|
target_lon = 127 + (42/60) + (43.46/3600)
|
|
|
|
print(f"Target: Lat {target_lat:.6f}, Lon {target_lon:.6f}")
|
|
|
|
# 3. 가장 가까운 격자점 인덱스 찾기
|
|
# 격자 파일의 lat, lon 변수 확인 (보통 2차원 배열)
|
|
lat_arr = ds.lat.values
|
|
lon_arr = ds.lon.values
|
|
|
|
# 유클리드 거리 계산 (간단한 근사)
|
|
dist = (lat_arr - target_lat)**2 + (lon_arr - target_lon)**2
|
|
ny, nx = np.unravel_index(dist.argmin(), dist.shape)
|
|
|
|
print(f"Found nearest grid index: ny={ny}, nx={nx}")
|
|
print(f"Grid Lat: {lat_arr[ny, nx]:.6f}, Grid Lon: {lon_arr[ny, nx]:.6f}")
|
|
|
|
# 4. 주변 3x3 격자점 좌표 추출
|
|
print("\n--- 3x3 Grid Points from NetCDF ---")
|
|
grid_points = []
|
|
for j in range(ny-1, ny+2):
|
|
for i in range(nx-1, nx+2):
|
|
l_lat = lat_arr[j, i]
|
|
l_lon = lon_arr[j, i]
|
|
pos = ["NW", "N", "NE", "W", "C", "E", "SW", "S", "SE"][len(grid_points)]
|
|
grid_points.append((l_lat, l_lon))
|
|
print(f"{pos:3} (ny={j}, nx={i}): Lat {l_lat:.6f}, Lon {l_lon:.6f}")
|
|
|
|
# 5. 결과 저장 (시각화 등에 활용)
|
|
with open('exact_grid_points.txt', 'w') as f:
|
|
for lt, ln in grid_points:
|
|
f.write(f"{lt:.6f},{ln:.6f}\n")
|
|
|
|
ds.close()
|