
    rbi              
          d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 d
dlmZ d
dlmZ d
dlmZ d
dlmZ d
dlmZ d
dlmZ d
dlmZ d
dlmZ 	 d)dededee         fdZ	 d)dededee         fdZdedefdZdedefdZ	 d)dee         dedeee         ee         f         fdZd eeef         d!eeef         defd"Zd#ee         d$eeef         dee         fd%Z d&ed'edefd(Z!dS )*u5   Helpers to be used with the Météo-France REST API .    N)datetime)Dict)List)Optional)Tuple)timezone)utc   )ALERT_COLOR_LIST_EN)ALERT_COLOR_LIST_FR)ALERT_TYPE_DICTIONARY_EN)ALERT_TYPE_DICTIONARY_FRCOASTAL_DEPARTMENT_LISTVALID_DEPARTMENT_LIST)Place)PhenomenonMaxColorfr	int_colorlangreturnc                 B    |dk    rt           |          S t          |          S )a  Convert the color code (in int) in readable text (Helper).

    Args:
        int_color: Color status in int. Value expected between 1 and 4.
        lang: Optional; If language is equal 'fr' (default value) results will
            be in French. All other value will give results in English.

    Returns:
        Color status in text. French or English according to the lang parameter.
    r   )r   r   )r   r   s     g/var/www/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/meteofrance_api/helpers.py)get_warning_text_status_from_indice_colorr      s#     t||"9--y))    int_phenomenonc                 B    |dk    rt           |          S t          |          S )a  Convert the phenomenon code in readable text (Helper).

    Args:
        int_phenomenon: ID of the phenomenon in int. Value expected between 1 and 9.
        lang: Optional; If language is equal "fr" (default value) results will
            be in French. All other value will give results in English.

    Returns:
        phenomenon in text. French or English according to the lang parameter.
    r   )r   r   )r   r   s     r   get_phenomenon_name_from_indicer   *   s#     t||'77#N33r   department_numberc                     | t           v S )zIdentify when a second bulletin is available for coastal risks (Helper).

    Args:
        department_number: Department number on 2 characters

    Returns:
        True if the department have an additional coastal bulletin. False otherwise.
    r   r    s    r   is_coastal_departmentr#   =   s      777r   c                     | t           v S )a<  Identify if there is a weather alert bulletin for this department (Helper).

    Weather alert bulletins are available only for metropolitan France and Andorre.

    Args:
        department_number: Department number on 2 characters.

    Returns:
        True if a department is metropolitan France or Andorre.
    r   r"   s    r   is_valid_warning_departmentr%   I   s      555r   list_phenomenonslanguagec                 n    i }| D ]/}t          |d         |          |t          |d         |          <   0|S )a  Create a dictionary with human readable keys and values (Helper).

    Args:
        list_phenomenons: Dictionary with phenomenon ID and color code of status.
        language: Optional; If language is equal "fr" (default value) results will
            be in French. All other value will give results in English.

    Returns:
        Dictionary with keys and value human readable.
    phenomenon_max_color_idphenomenon_id)r   r   )r&   r'   readable_dict
phenomenons       r   readable_phenomenons_dictr-   W   s]     M ' 
 

 6018
 
 	+J,GRR	
 	

 r   coord1coord2c                    d}| \  }}|\  }}t          j        |          t          j        |          }}t          j        ||z
            }	t          j        ||z
            }
t          j        |	dz            dz  t          j        |          t          j        |          z  t          j        |
dz            dz  z  z   }d|z  t          j        t          j        |          t          j        d|z
                      z  S )a|  Compute distance in meters between to GPS coordinates using Harvesine formula.

    source: https://janakiev.com/blog/gps-points-distance-python/

    Args:
        coord1: Tuple with latitude and longitude in degrees for first point
        coord2: Tuple with latitude and longitude in degrees for second point

    Returns:
        Distance in meters between the two points
    i=a    r
   )mathradianssincosatan2sqrt)r.   r/   radiuslat1lon1lat2lon2phi1phi2dphidlambdaas               r   	haversinerB   q   s     FJD$JD$d##T\$%7%7$D<t$$Dl4$;''G 	a
(4..48D>>
)DHWq[,A,AQ,F
F	G 
 v:
49Q<<1q51A1ABBBBr   list_places	gps_coordc                 0    t          | fd          }|S )u&  Oder list of places according to the distance to a reference coordinates.

    Note: this helper is compensating the bad results of the API. Results in the API
    are generally sorted, but lot of cases identified where the order is inconsistent
    (example: Montréal)

    Args:
        list_places: List of Place instances to be ordered
        gps_coord: Tuple with latitude and longitude in degrees for the reference point

    Returns:
        List of Place instances ordered by distance to the reference point (nearest
            first)
    c                 p    t          t          | j                  t          | j                  f          S )N)rB   floatlatitude	longitude)xrD   s    r   <lambda>z>sort_places_versus_distance_from_coordinates.<locals>.<lambda>   s*    iqz!2!2E!+4F4F GSS r   )key)sorted)rC   rD   sorted_placess    ` r   ,sort_places_versus_distance_from_coordinatesrO      s0    " SSSS  M r   	timestamplocal_tzc                     t          j        t          j        |                     }|                    t          |                    S )zConvert timestamp in datetime (Helper).

    Args:
        timestamp: Timestamp.
        local_tz: Name of the timezone to be used to convert the timestamp.

    Returns:
        Datetime instance corresponding to the timestamp with a timezone.
    )r	   localizer   utcfromtimestamp
astimezoner   )rP   rQ   dt_utcs      r   $timestamp_to_datetime_with_locale_tzrW      s:     \(3I>>??FXh//000r   )r   )"__doc__r2   r   typingr   r   r   r   pytzr   r	   constr   r   r   r   r   r   model.placer   model.warningr   intstrr   r   boolr#   r%   r-   rG   rB   rO   rW    r   r   <module>rb      s   ; ;                                            & & & & & & & & & & & & + + + + + + + + + + + + * * * * * * ( ( ( ( ( (       - - - - - - !%* ***c]* * * *( &*4 44"4c]4 4 4 4&	8S 	8T 	8 	8 	8 	863 64 6 6 6 6 AE -.:=	(3-#
&'   4CeE5L) C53F C5 C C C C6e).ue|)<	%[   01C 13 18 1 1 1 1 1 1r   