
붉은것이 기상청 AWS 위치이고 파란색점이 공장 위치이다.
붉은 원 반경은 2km로 AWS의 측정된 바람의 신뢰성 범위이다.
파란색 점 공장 위치에서의 풍향을 예측해야한다.
논문을 찬찬히 살펴봐야겠다.
내 미천한 알고리즘 실력에 또 좌절이다.
import pandas as pd
import requests
import matplotlib.pyplot as plt
from geopy.distance import geodesic
import apikey
# 컬럼명 설정
col_name = ["KST", "STN", "WD1", "WS1", "WDS", "WSS", "WD10", "WS10", "TA", "RE", "RN-15m", "RN-60m", "RN-12H", "RN-DAY", "HM", "PA", "PS", "TD"]
def wind_current(stn):
col_name = ["KST", "STN", "WD1", "WS1", "WDS", "WSS", "WD10", "WS10", "TA", "RE", "RN-15m", "RN-60m", "RN-12H", "RN-DAY", "HM", "PA", "PS", "TD"]
# 헤더 설정
headers = {
'Content-Type': 'application/json'
}
# 요청할 URL 설정
domain = "https://apihub.kma.go.kr/api/typ01/cgi-bin/url/nph-aws2_min?"
tm = ""
stn_id = f"stn={stn}&"
option = "help=0&authKey="
auth = apikey.wind_key # 부여받은 API Key 입력
url = domain + tm + stn_id + option + auth
response = requests.get(url, headers=headers) # GET 요청
text = response.text
data_lines = text.split("\n")[3:-2] # 데이터 부분만 추출
# 데이터프레임으로 변환
df = pd.DataFrame(data_lines)[0].str.split(expand=True)
df.columns = col_name
# 필요한 필드만 추출
result = df[[ "STN", "WS1", "WS10","WD1"]]
# 필요한 필드를 새로운 이름으로 변경
# JSON 형식으로 변환
result_json = result.to_json(orient="records")
return result_json
def test_accuracy_wind_data():
# 기상청의 전국 AWS 지점 정보
ulsan_aws_points = {
924: (35.35866, 129.36028),
900: (35.62027, 129.14356),
943: (35.66706, 129.37502),
854: (35.49529, 129.13565),
954: (35.43103, 129.36011),
901: (35.48678, 129.43531),
898: (35.50482, 129.386),
949: (35.6375, 129.4412)
}
factory_points = ["생략"]
# 날씨 데이터를 로드하고 AWS 지점의 풍향 데이터를 병합합니다.
df_aws_list = []
for point in ulsan_aws_points:
print("point:",wind_current(point))
df_aws_list.append(wind_current(point))
df_aws = pd.concat([pd.read_json(data) for data in df_aws_list], ignore_index=True)
# -99.9 데이터를 필터링
df_aws = df_aws[df_aws["WS1"] != -99.9]
("df_aws_list:",df_aws_list)
# 1. 울산 지도에 시각화합니다.
# 1.1 공장의 위치를 시각화
# 1.2 AWS 지점의 위치를 시각화
plt.figure(figsize=(10, 8))
print("ulsan_aws_points",ulsan_aws_points)
# 공장 위치 시각화
for factory in factory_points:
plt.scatter(float(factory['x_coord']), float(factory['y_coord']), c='blue', label='Factory')
plt.text(float(factory['x_coord']), float(factory['y_coord']), '', fontsize=9) # 공장 명 생략
# AWS 지점 위치 시각화
for stn, coord in ulsan_aws_points.items():
plt.scatter(coord[1], coord[0], c='red', label='AWS Point')
plt.text(coord[1], coord[0], str(stn), fontsize=9)
## AWS 지점 주위에 붉은 테두리의 내부는 비어있는 원을 그린다. 둘레는 2km 이다.
circle = plt.Circle((coord[1], coord[0]), 0.018, color='red', fill=False)
plt.gca().add_artist(circle)
# AWS 지점 위치 시각화
for index, row in df_aws.iterrows():
stn = int(row["STN"])
coord = ulsan_aws_points[stn]
wd1 = float(row["WD1"])
ws1 = float(row["WS1"])
# AWS 지점 주위에 붉은 테두리의 내부는 비어있는 원을 그린다. 둘레는 2km 이다.
circle = plt.Circle((coord[1], coord[0]), 0.018, color='red', fill=False)
plt.gca().add_artist(circle)
# 풍향 화살표로 표시
plt.arrow(coord[1], coord[0], 0.001 * ws1 * np.sin(np.deg2rad(wd1)), 0.001 * ws1 * np.cos(np.deg2rad(wd1)),
head_width=0.005, head_length=0.005, fc='k', ec='k')
# 가장 가까운 AWS 지점과의 거리 계산 및 시각화
for factory in factory_points:
factory_coord = (float(factory['y_coord']), float(factory['x_coord']))
distances = {stn: geodesic(factory_coord, aws_coord).meters for stn, aws_coord in ulsan_aws_points.items()}
closest_aws = min(distances, key=distances.get)
closest_aws_coord = ulsan_aws_points[closest_aws]
plt.plot([float(factory['x_coord']), closest_aws_coord[1]], [float(factory['y_coord']), closest_aws_coord[0]], 'k--')
# plt.text((float(factory['x_coord']) + closest_aws_coord[1]) / 2, (float(factory['y_coord']) + closest_aws_coord[0]) / 2,
# f"{distances[closest_aws]:.1f}m", fontsize=8)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.title("Ulsan Factories and AWS Points")
plt.show()
test_accuracy_wind_data()
'Dev' 카테고리의 다른 글
Obsidian과 chatgpt 함께 활용 방안 (0) | 2024.07.20 |
---|