Skip to content

weather_codes

Weather code conversion utilities for Korean descriptions.

This module provides functions to convert weather codes and values to human-readable Korean descriptions.

deg_to_direction(degree)

Convert degree (0-360) to wind direction code (N, NE, E, etc.).

Parameters:

  • degree (float) –

    Wind direction in degrees (0-360)

Returns:

  • str

    Direction code (e.g., 'N', 'NE', 'E')

Example

deg_to_direction(0) 'N' deg_to_direction(45) 'NE' deg_to_direction(90) 'E'

Source code in python/src/kma_mcp/utils/weather_codes.py
def deg_to_direction(degree: float) -> str:
    """Convert degree (0-360) to wind direction code (N, NE, E, etc.).

    Args:
        degree: Wind direction in degrees (0-360)

    Returns:
        Direction code (e.g., 'N', 'NE', 'E')

    Example:
        >>> deg_to_direction(0)
        'N'
        >>> deg_to_direction(45)
        'NE'
        >>> deg_to_direction(90)
        'E'
    """
    # Normalize degree to 0-360 range
    degree = degree % 360

    # Find closest direction
    directions = list(DEGREE_TO_DIRECTION.keys())
    closest = min(directions, key=lambda x: min(abs(degree - x), abs(degree - x + 360)))

    return DEGREE_TO_DIRECTION[closest]

deg_to_direction_kr(degree)

Convert degree (0-360) to Korean wind direction.

Parameters:

  • degree (float) –

    Wind direction in degrees (0-360)

Returns:

  • str

    Korean direction (e.g., '북', '북동', '동')

Example

deg_to_direction_kr(0) '북' deg_to_direction_kr(45) '북동' deg_to_direction_kr(90) '동'

Source code in python/src/kma_mcp/utils/weather_codes.py
def deg_to_direction_kr(degree: float) -> str:
    """Convert degree (0-360) to Korean wind direction.

    Args:
        degree: Wind direction in degrees (0-360)

    Returns:
        Korean direction (e.g., '북', '북동', '동')

    Example:
        >>> deg_to_direction_kr(0)
        '북'
        >>> deg_to_direction_kr(45)
        '북동'
        >>> deg_to_direction_kr(90)
        '동'
    """
    direction = deg_to_direction(degree)
    return WIND_DIRECTION_KR[direction]

direction_to_kr(direction)

Convert English wind direction to Korean.

Parameters:

  • direction (str) –

    Wind direction in English (e.g., 'N', 'NE', 'E')

Returns:

  • str

    Korean direction (e.g., '북', '북동', '동')

Example

direction_to_kr('N') '북' direction_to_kr('NE') '북동'

Source code in python/src/kma_mcp/utils/weather_codes.py
def direction_to_kr(direction: str) -> str:
    """Convert English wind direction to Korean.

    Args:
        direction: Wind direction in English (e.g., 'N', 'NE', 'E')

    Returns:
        Korean direction (e.g., '북', '북동', '동')

    Example:
        >>> direction_to_kr('N')
        '북'
        >>> direction_to_kr('NE')
        '북동'
    """
    return WIND_DIRECTION_KR.get(direction.upper(), direction)

enhance_weather_data(data)

Enhance weather data with Korean descriptions.

Adds Korean descriptions for wind direction, precipitation type, sky condition, and weather phenomena based on code values.

Parameters:

  • data (dict[str, Any]) –

    Weather data dictionary

Returns:

  • dict[str, Any]

    Enhanced data dictionary with Korean descriptions

Example

data = {'wdDeg': 45, 'pty': 1, 'sky': 3} enhanced = enhance_weather_data(data) enhanced['wdDeg_kr'] '북동' enhanced['pty_kr'] '비' enhanced['sky_kr'] '구름많음'

Source code in python/src/kma_mcp/utils/weather_codes.py
def enhance_weather_data(data: dict[str, Any]) -> dict[str, Any]:
    """Enhance weather data with Korean descriptions.

    Adds Korean descriptions for wind direction, precipitation type,
    sky condition, and weather phenomena based on code values.

    Args:
        data: Weather data dictionary

    Returns:
        Enhanced data dictionary with Korean descriptions

    Example:
        >>> data = {'wdDeg': 45, 'pty': 1, 'sky': 3}
        >>> enhanced = enhance_weather_data(data)
        >>> enhanced['wdDeg_kr']
        '북동'
        >>> enhanced['pty_kr']
        '비'
        >>> enhanced['sky_kr']
        '구름많음'
    """
    enhanced = data.copy()

    # Wind direction from degrees
    if 'wdDeg' in data and data['wdDeg'] is not None:
        with contextlib.suppress(ValueError, TypeError):
            enhanced['wdDeg_kr'] = deg_to_direction_kr(float(data['wdDeg']))

    # Wind direction from code
    if 'wd' in data and isinstance(data['wd'], str):
        enhanced['wd_kr'] = direction_to_kr(data['wd'])

    # Precipitation type
    if 'pty' in data and data['pty'] is not None:
        with contextlib.suppress(ValueError, TypeError):
            enhanced['pty_kr'] = precipitation_type_to_kr(int(data['pty']))

    # Sky condition
    if 'sky' in data and data['sky'] is not None:
        with contextlib.suppress(ValueError, TypeError):
            enhanced['sky_kr'] = sky_condition_to_kr(int(data['sky']))

    # Weather phenomenon
    if 'wf' in data and data['wf'] is not None:
        with contextlib.suppress(ValueError, TypeError):
            enhanced['wf_kr'] = weather_phenomenon_to_kr(int(data['wf']))

    return enhanced

format_weather_summary(data)

Format weather data as a human-readable Korean summary.

Parameters:

  • data (dict[str, Any]) –

    Weather data dictionary

Returns:

  • str

    Formatted Korean summary string

Example

data = { ... 'stnNm': '서울', ... 'ta': 15.5, ... 'hm': 65, ... 'wdDeg': 45, ... 'ws': 3.2, ... 'pty': 0, ... 'sky': 1 ... } print(format_weather_summary(data)) [서울] 기온: 15.5°C, 습도: 65%, 풍향: 북동, 풍속: 3.2m/s, 강수: 강수 없음, 하늘: 맑음

Source code in python/src/kma_mcp/utils/weather_codes.py
def format_weather_summary(data: dict[str, Any]) -> str:
    """Format weather data as a human-readable Korean summary.

    Args:
        data: Weather data dictionary

    Returns:
        Formatted Korean summary string

    Example:
        >>> data = {
        ...     'stnNm': '서울',
        ...     'ta': 15.5,
        ...     'hm': 65,
        ...     'wdDeg': 45,
        ...     'ws': 3.2,
        ...     'pty': 0,
        ...     'sky': 1
        ... }
        >>> print(format_weather_summary(data))
        [서울] 기온: 15.5°C, 습도: 65%, 풍향: 북동, 풍속: 3.2m/s, 강수: 강수 없음, 하늘: 맑음
    """
    enhanced = enhance_weather_data(data)
    parts = []

    # Station name
    if 'stnNm' in enhanced:
        parts.append(f'[{enhanced["stnNm"]}]')

    # Temperature
    if 'ta' in enhanced and enhanced['ta'] is not None:
        parts.append(f'기온: {enhanced["ta"]}°C')

    # Humidity
    if 'hm' in enhanced and enhanced['hm'] is not None:
        parts.append(f'습도: {enhanced["hm"]}%')

    # Wind direction
    if 'wdDeg_kr' in enhanced:
        parts.append(f'풍향: {enhanced["wdDeg_kr"]}')
    elif 'wd_kr' in enhanced:
        parts.append(f'풍향: {enhanced["wd_kr"]}')

    # Wind speed
    if 'ws' in enhanced and enhanced['ws'] is not None:
        parts.append(f'풍속: {enhanced["ws"]}m/s')

    # Precipitation type
    if 'pty_kr' in enhanced:
        parts.append(f'강수: {enhanced["pty_kr"]}')

    # Sky condition
    if 'sky_kr' in enhanced:
        parts.append(f'하늘: {enhanced["sky_kr"]}')

    # Precipitation amount
    if 'rn' in enhanced and enhanced['rn'] is not None and enhanced['rn'] != 0:
        parts.append(f'강수량: {enhanced["rn"]}mm')

    return ', '.join(parts)

precipitation_type_to_kr(code)

Convert precipitation type code to Korean description.

Parameters:

  • code (int) –

    Precipitation type code (0-7)

Returns:

  • str

    Korean precipitation type description

Example

precipitation_type_to_kr(0) '강수 없음' precipitation_type_to_kr(1) '비' precipitation_type_to_kr(3) '눈'

Source code in python/src/kma_mcp/utils/weather_codes.py
def precipitation_type_to_kr(code: int) -> str:
    """Convert precipitation type code to Korean description.

    Args:
        code: Precipitation type code (0-7)

    Returns:
        Korean precipitation type description

    Example:
        >>> precipitation_type_to_kr(0)
        '강수 없음'
        >>> precipitation_type_to_kr(1)
        '비'
        >>> precipitation_type_to_kr(3)
        '눈'
    """
    return PRECIPITATION_TYPE.get(code, f'알 수 없음 ({code})')

sky_condition_to_kr(code)

Convert sky condition code to Korean description.

Parameters:

  • code (int) –

    Sky condition code (1, 3, 4)

Returns:

  • str

    Korean sky condition description

Example

sky_condition_to_kr(1) '맑음' sky_condition_to_kr(3) '구름많음' sky_condition_to_kr(4) '흐림'

Source code in python/src/kma_mcp/utils/weather_codes.py
def sky_condition_to_kr(code: int) -> str:
    """Convert sky condition code to Korean description.

    Args:
        code: Sky condition code (1, 3, 4)

    Returns:
        Korean sky condition description

    Example:
        >>> sky_condition_to_kr(1)
        '맑음'
        >>> sky_condition_to_kr(3)
        '구름많음'
        >>> sky_condition_to_kr(4)
        '흐림'
    """
    return SKY_CONDITION.get(code, f'알 수 없음 ({code})')

weather_phenomenon_to_kr(code)

Convert weather phenomenon code to Korean description.

Parameters:

  • code (int) –

    Weather phenomenon code (0-4)

Returns:

  • str

    Korean weather phenomenon description

Example

weather_phenomenon_to_kr(0) '없음' weather_phenomenon_to_kr(1) '비'

Source code in python/src/kma_mcp/utils/weather_codes.py
def weather_phenomenon_to_kr(code: int) -> str:
    """Convert weather phenomenon code to Korean description.

    Args:
        code: Weather phenomenon code (0-4)

    Returns:
        Korean weather phenomenon description

    Example:
        >>> weather_phenomenon_to_kr(0)
        '없음'
        >>> weather_phenomenon_to_kr(1)
        '비'
    """
    return WEATHER_PHENOMENON.get(code, f'알 수 없음 ({code})')