카테고리 없음

[35-2 파이썬] 날씨 코드 의미 weather id, API 실습 예제, 세계 날씨 맵, 날씨 예측하기

Olivia-BlackCherry 2022. 9. 27. 19:48

지난 시간에 이어서 API 실습 예제를 깊이 살펴보자. 

 

import requests
URL = "http://api.openweathermap.org/data/2.5/forecast"
API_KEY = "MY_API"

parameters = {
    "lat":37.663998,
    "lon":127.978462,
    "cnt": 12,
    "lang": "kr",
    "appid":API_KEY
}
response = requests.get(URL, params= parameters)
response.raise_for_status()
data = response.json()
print(data)

 

내용을 해석하기 위해서는

API 설명서를 읽어봐야 한다.

날씨는 id 칸에 있다. 

list - weather - id 

 

 

weather condition codes

날씨 코드에 대한 해석은 아래와 같다.

200대는 천둥 번개를 동반한 상태이다.

300대는 보슬보슬 내리는 이슬비, 보슬비를 뜻한다.(drizzle)

 

 

500대는 비와 관련되있다.

600대는 눈과 관련되어 있다.

 

700대는 분위기나 상태(먼지, 안개, 스모그 등)를 말한다.

 

800대는 맑음을, 80x대는 구름이 있는 날씨를 의미한다.

 

내일 아침 일기 예보를 확인했을 때, 

비가 온다면 우산을 챙기라는 문구를 출력하려고 한다. 

 

<최종코드>

import requests
URL = "http://api.openweathermap.org/data/2.5/forecast"
API_KEY = "MY_API"

parameters = {
    "lat":37.663998,
    "lon":127.978462,
    "cnt": 12,
    "lang": "kr",
    "appid":API_KEY
}
response = requests.get(URL, params= parameters)
response.raise_for_status()
data = response.json()

data_list=data["list"]
weather_condition = [day_weather['weather'][0]["id"] for day_weather in data_list[6:11]]
print(weather_condition)
# 출력: [804, 804, 804, 802, 802]

for time in weather_condition:
    if time <800:
        print("우산을 챙기세요")
    else:
        print("오늘 날씨 맑음!")

 

data

>>{'cod': '200', 'message': 0, 'cnt': 12, 'list': [{'dt': 1664280000, 'main': {'temp': 287.89, 'feels_like': 287.2, 'temp_min': 287.8, 'temp_max': 287.89, 'pressure': 1015, 'sea_level': 1015, 'grnd_level': 980, 'humidity': 68, 'temp_kf': 0.09}, 'weather': [{'id': 800, 'main': 'Clear', 'description': '맑음', 'icon': '01n'}], 'clouds': {'all': 0}, 'wind': {'speed': 2.07, 'deg': 71, 'gust': 1.79}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-27 12:00:00'}, {'dt': 1664290800, 'main': {'temp': 286.64, 'feels_like': 285.87, 'temp_min': 285.99, 'temp_max': 286.64, 'pressure': 1016, 'sea_level': 1016, 'grnd_level': 980, 'humidity': 70, 'temp_kf': 0.65}, 'weather': [{'id': 800, 'main': 'Clear', 'description': '맑음', 'icon': '01n'}], 'clouds': {'all': 5}, 'wind': {'speed': 1.43, 'deg': 75, 'gust': 1.26}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-27 15:00:00'}, {'dt': 1664301600, 'main': {'temp': 285.23, 'feels_like': 284.14, 'temp_min': 285.23, 'temp_max': 285.23, 'pressure': 1016, 'sea_level': 1016, 'grnd_level': 979, 'humidity': 63, 'temp_kf': 0}, 'weather': [{'id': 802, 'main': 'Clouds', 'description': '구름조금', 'icon': '03n'}], 'clouds': {'all': 37}, 'wind': {'speed': 1.42, 'deg': 76, 'gust': 1.25}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-27 18:00:00'}, {'dt': 1664312400, 'main': {'temp': 285, 'feels_like': 283.78, 'temp_min': 285, 'temp_max': 285, 'pressure': 1016, 'sea_level': 1016, 'grnd_level': 979, 'humidity': 59, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04n'}], 'clouds': {'all': 100}, 'wind': {'speed': 0.97, 'deg': 95, 'gust': 0.88}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-27 21:00:00'}, {'dt': 1664323200, 'main': {'temp': 290.45, 'feels_like': 289.65, 'temp_min': 290.45, 'temp_max': 290.45, 'pressure': 1016, 'sea_level': 1016, 'grnd_level': 980, 'humidity': 54, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04d'}], 'clouds': {'all': 100}, 'wind': {'speed': 0.3, 'deg': 74, 'gust': 0.54}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'd'}, 'dt_txt': '2022-09-28 00:00:00'}, {'dt': 1664334000, 'main': {'temp': 295.32, 'feels_like': 294.85, 'temp_min': 295.32, 'temp_max': 295.32, 'pressure': 1015, 'sea_level': 1015, 'grnd_level': 979, 'humidity': 48, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04d'}], 'clouds': {'all': 100}, 'wind': {'speed': 1.11, 'deg': 263, 'gust': 1.48}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'd'}, 'dt_txt': '2022-09-28 03:00:00'}, {'dt': 1664344800, 'main': {'temp': 295.42, 'feels_like': 295.06, 'temp_min': 295.42, 'temp_max': 295.42, 'pressure': 1013, 'sea_level': 1013, 'grnd_level': 978, 'humidity': 52, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04d'}], 'clouds': {'all': 100}, 'wind': {'speed': 1.11, 'deg': 265, 'gust': 1.61}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'd'}, 'dt_txt': '2022-09-28 06:00:00'}, {'dt': 1664355600, 'main': {'temp': 291, 'feels_like': 290.8, 'temp_min': 291, 'temp_max': 291, 'pressure': 1014, 'sea_level': 1014, 'grnd_level': 978, 'humidity': 75, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04d'}], 'clouds': {'all': 100}, 'wind': {'speed': 0.52, 'deg': 203, 'gust': 0.57}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'd'}, 'dt_txt': '2022-09-28 09:00:00'}, {'dt': 1664366400, 'main': {'temp': 287.87, 'feels_like': 287.62, 'temp_min': 287.87, 'temp_max': 287.87, 'pressure': 1015, 'sea_level': 1015, 'grnd_level': 978, 'humidity': 85, 'temp_kf': 0}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': '온흐림', 'icon': '04n'}], 'clouds': {'all': 96}, 'wind': {'speed': 1.04, 'deg': 98, 'gust': 0.97}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-28 12:00:00'}, {'dt': 1664377200, 'main': {'temp': 286.3, 'feels_like': 286.05, 'temp_min': 286.3, 'temp_max': 286.3, 'pressure': 1015, 'sea_level': 1015, 'grnd_level': 979, 'humidity': 91, 'temp_kf': 0}, 'weather': [{'id': 802, 'main': 'Clouds', 'description': '구름조금', 'icon': '03n'}], 'clouds': {'all': 50}, 'wind': {'speed': 0.84, 'deg': 93, 'gust': 0.75}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-28 15:00:00'}, {'dt': 1664388000, 'main': {'temp': 285.52, 'feels_like': 285.27, 'temp_min': 285.52, 'temp_max': 285.52, 'pressure': 1015, 'sea_level': 1015, 'grnd_level': 979, 'humidity': 94, 'temp_kf': 0}, 'weather': [{'id': 802, 'main': 'Clouds', 'description': '구름조금', 'icon': '03n'}], 'clouds': {'all': 38}, 'wind': {'speed': 0.99, 'deg': 89, 'gust': 0.89}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-28 18:00:00'}, {'dt': 1664398800, 'main': {'temp': 285.01, 'feels_like': 284.71, 'temp_min': 285.01, 'temp_max': 285.01, 'pressure': 1016, 'sea_level': 1016, 'grnd_level': 979, 'humidity': 94, 'temp_kf': 0}, 'weather': [{'id': 802, 'main': 'Clouds', 'description': '구름조금', 'icon': '03n'}], 'clouds': {'all': 46}, 'wind': {'speed': 0.75, 'deg': 93, 'gust': 0.73}, 'visibility': 10000, 'pop': 0, 'sys': {'pod': 'n'}, 'dt_txt': '2022-09-28 21:00:00'}], 'city': {'id': 1844191, 'name': 'Hongch’ŏn', 'coord': {'lat': 37.664, 'lon': 127.9785}, 'country': 'KR', 'population': 75251, 'timezone': 32400, 'sunrise': 1664227174, 'sunset': 1664270333}}

 

>>날씨 코드

내일 아침 6시부터~ 저녁 9시까지의 날씨(3시간 간격)

[804, 804, 804, 802, 802]

 

>>출력화면

오늘 날씨 맑음!
오늘 날씨 맑음!
오늘 날씨 맑음!
오늘 날씨 맑음!
오늘 날씨 맑음!

 

 

<현재 날씨 상황 보여주는 지도>

현재 비가 오는 곳의 위치를 찾아보자!

위도와 경도를 찾아 파라미터를 바꿔준다. 

https://www.ventusky.com/ko/54.978;-136.143

 

위치 - Weather Forecast Maps | Ventusky

Air quality index (marked as AQI) is developed by Environmental Protection Agency. Values over 300 represents hazardous air quality, between 200-300 very unhealthy, 150-200 unhealthy, 100-150 unhealthy for sensitive groups and below 100 or rather below 50

www.ventusky.com

import requests
URL = "http://api.openweathermap.org/data/2.5/forecast"
API_KEY = "MY_API"

parameters = {
    "lat":54.58,
    "lon":136.8,
    "cnt": 12,
    "lang": "kr",
    "appid":API_KEY
}
response = requests.get(URL, params= parameters)
response.raise_for_status()
data = response.json()
print(data)

data_list=data["list"]
weather_condition = [day_weather['weather'][0]["id"] for day_weather in data_list[6:11]]
print(weather_condition)
# 출력: [804, 804, 804, 802, 802]

for time in weather_condition:
    if time <800:
        print("우산을 챙기세요")
    else:
        print("오늘 날씨 맑음!")

>>출력화면

[500, 500, 802, 800, 800]
우산을 챙기세요
우산을 챙기세요
오늘 날씨 맑음!
오늘 날씨 맑음!
오늘 날씨 맑음!

 

 

코드를 조금 더 세련되게 수정해보자!

결국 비가 오므로, 우산을 챙기라는 말 한 마디면 충분하다.

True, False를 이용해보자.

will_rain = False
for time in weather_condition:
    if time <800:
        will_rain = True

if will_rain:
    print("우산 챙기세요!")