CoolFace
Apppublic

Juliabelloni/weather-forecasting

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
load_weather.cpython-311.pyc50 linesDownload Raw Back to __pycache__
1�

2"!g���R�ddlmZddlZddlZddlZddlmZGd�de��ZdS)�)�datetimeN�)�LoadABCc�Z�eZdZdedefd�Zdedejfd�Zde	dededejfd	�Z3d4S)�LoadWeather�line�returnc�p�|�d��}|D]�}|���}|�d��r2|dd��ddd�����}n;|����ddd�����}|s��dSdS)z�Check if a line is numeric.5 6        Args:7            line (str): The line to check.8 9        Returns:10            bool: True if the line is numeric, False otherwise.11        �,�-rN�.�T)�split�strip�12startswith�replace�isdigit)�selfr�parts�part�
stripped_part�
part_is_digits      �L/Users/juliabelloni/Desktop/ML_industry/ML4IProject/src/data/load_weather.py�_is_numeric_linezLoadWeather._is_numeric_lines����13�14�3�����		�		�D� �J�J�L�L�M��'�'��,�,�
K�+�A�B�B�/�7�7��R��C�C�K�K�M�M�
�
� $�15�16��� 4� 4�S�"�a� @� @� H� H� J� J�
� �
���4�4�		�		��txt_filec	��d}|���}t|��D]!\}}|�|��r|dz17}n�"tjtjd�||d�����d���}|S)z�18        Convert a  .txt file to a pandas DataFrame.19 20        Args:21            txt_file (str): The contents of the .txt file as a string.22 23        Returns:24            pd.DataFrame: The DataFrame of the numeric data.25        Nr�26r)�sep)�27splitlines�	enumerater�pd�read_csv�io�StringIO�join)rr�	start_idx�lines�ir�dfs       r�28_txt_to_dfzLoadWeather._txt_to_df#s����	��#�#�%�%�� ��'�'�	�	�G�A�t��$�$�T�*�*�
���E�	���
��[���T�Y�Y�u�Y�Z�Z�/@�%A�%A�B�B��
L�
L�
L���	r�29station_id�30start_date�end_datec��d}tj|���d��}tj|���d��}||d|dd�}tj||���}|jdkrBd|�d	|�d31|��}	|j�|	���|�|j	��}32|33Std|j�����)
a�34        Retrieves data from the KNMI website for a given station and time period.35 36        Args:37            station_id (int): The id of the station to retrieve data from.38            start_date (str): The start date of the data to retrieve (inclusive).39            end_date (str): The end date of the data to retrieve (inclusive).40 41        Returns:42            pd.DataFrame: The DataFrame containing the retrieved data.43 44        Raises:45            KeyError: If the request fails.46        z8https://www.daggegevens.knmi.nl/klimatologie/daggegevensz%Y-%m-%d�ALL�csv)�start�end�vars�stns�fmt)�data��z(Data successfully retrieved for station z from z to )�info_strzFailed to retrieve data: )r�
fromisoformat�strftime�requests�post�status_code�_logs�	load_logsr+�text�KeyError)rr,r-r.�url�start_date_str�end_date_str�params�responser9r*s           r�get_datazLoadWeather.get_data7s��� I��"�/�47�;�;�D�D�Z�P�P�� �.�x�8�8�A�A�*�M�M��$�����48�49���=��6�2�2�2����3�&�&�v�*�v�v�Tb�v�v�ht�v�v�H��J� � �H� �5�5�5������/�/�B��I��M�x�7K�M�M�N�N�NrN)�__name__�50__module__�__qualname__�str�boolrr"�	DataFramer+�intrH�rrrr	s��������C��D�����.�#��"�,�����(&O�3�&O�C�&O�3�&O�2�<�&O�&O�&O�&O�&O�&Orr)rr$r<�pandasr"�load_abcrrrPrr�<module>rSs���������	�	�	�	���������������TO�TO�TO�TO�TO�'�TO�TO�TO�TO�TOr