From 8caff5aee7484d52eabcee31aa840a32fdd76d0e Mon Sep 17 00:00:00 2001 From: toravest Date: Mon, 21 Apr 2025 18:51:06 +0200 Subject: [PATCH] clean function placement, and add universal functions --- notebooks/notebook_compare_one_day_data.ipynb | 12 +- .../notebook_compare_one_week_data.ipynb | 12 +- .../notebook_compare_statistic_data.ipynb | 12 +- notebooks/notebook_current_data.ipynb | 4 +- notebooks/notebook_one_day_data.ipynb | 8 +- notebooks/notebook_one_week_data.ipynb | 8 +- src/README.md | 19 ++- src/my_package/data.py | 139 ++++++++++++++++++ src/my_package/fetch_current_data.py | 38 ----- src/my_package/fetch_data.py | 40 ----- src/my_package/get_record.py | 23 --- src/my_package/util.py | 90 +++++------- src/my_package/write_data.py | 19 --- src/my_package/year_data.py | 38 ----- 14 files changed, 217 insertions(+), 245 deletions(-) create mode 100644 src/my_package/data.py delete mode 100644 src/my_package/fetch_current_data.py delete mode 100644 src/my_package/fetch_data.py delete mode 100644 src/my_package/get_record.py delete mode 100644 src/my_package/write_data.py delete mode 100644 src/my_package/year_data.py diff --git a/notebooks/notebook_compare_one_day_data.ipynb b/notebooks/notebook_compare_one_day_data.ipynb index 0520029..e9f1bec 100644 --- a/notebooks/notebook_compare_one_day_data.ipynb +++ b/notebooks/notebook_compare_one_day_data.ipynb @@ -93,7 +93,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -105,7 +105,7 @@ "start_date, end_date = timestamps[0], timestamps[-1]\n", "\n", "# Stores the values in the variables\n", - "data_city_1, folder = fetch_data(start_date, end_date, city_1)" + "data_city_1, folder = fetch_time_data(start_date, end_date, city_1)" ] }, { @@ -129,7 +129,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -141,7 +141,7 @@ "start_date, end_date = timestamps[0], timestamps[-1]\n", "\n", "# Stores the values in the variables\n", - "data_city_2, folder = fetch_data(start_date, end_date, city_2)" + "data_city_2, folder = fetch_time_data(start_date, end_date, city_2)" ] }, { @@ -176,7 +176,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "# Overwrites the folder stored inside the function\n", "folder = \"../data/output_sammenligning_dag\"\n", @@ -212,7 +212,7 @@ "\n", "file_path = f'../data/output_sammenligning_dag/data_{filename}.json'\n", "\n", - "from my_package.util import extract_city_df\n", + "from my_package.data import extract_city_df\n", "\n", "# Load the whole JSON file\n", "with open(file_path, 'r') as f:\n", diff --git a/notebooks/notebook_compare_one_week_data.ipynb b/notebooks/notebook_compare_one_week_data.ipynb index 30b0c88..aa1c3ee 100644 --- a/notebooks/notebook_compare_one_week_data.ipynb +++ b/notebooks/notebook_compare_one_week_data.ipynb @@ -86,7 +86,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -95,7 +95,7 @@ "city_1 = input_place()\n", "\n", "# Stores the values in the variables\n", - "data_city_1, folder = fetch_data(unix_start_date, unix_end_date, city_1)" + "data_city_1, folder = fetch_time_data(unix_start_date, unix_end_date, city_1)" ] }, { @@ -121,7 +121,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -130,7 +130,7 @@ "city_2 = input_place()\n", "\n", "# Stores the values in the variables\n", - "data_city_2, folder = fetch_data(unix_start_date, unix_end_date, city_2)" + "data_city_2, folder = fetch_time_data(unix_start_date, unix_end_date, city_2)" ] }, { @@ -167,7 +167,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "# Overwrites the folder stored inside the function\n", "folder = \"../data/output_sammenligning_uke\"\n", @@ -205,7 +205,7 @@ "\n", "file_path = f'../data/output_sammenligning_uke/data_{filename}.json'\n", "\n", - "from my_package.util import extract_city_df\n", + "from my_package.data import extract_city_df\n", "\n", "# Load the whole JSON file\n", "with open(file_path, 'r') as f:\n", diff --git a/notebooks/notebook_compare_statistic_data.ipynb b/notebooks/notebook_compare_statistic_data.ipynb index 3a94c06..1ceb959 100644 --- a/notebooks/notebook_compare_statistic_data.ipynb +++ b/notebooks/notebook_compare_statistic_data.ipynb @@ -38,7 +38,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.year_data import fetch_data\n", + "from my_package.data import fetch_stat_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -46,7 +46,7 @@ "# User input the city, for the weather\n", "city_1 = input_place()\n", "\n", - "data_city_1, folder = fetch_data(city_1)" + "data_city_1, folder = fetch_stat_data(city_1)" ] }, { @@ -71,7 +71,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.year_data import fetch_data\n", + "from my_package.data import fetch_stat_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -79,7 +79,7 @@ "# User input the city, for the weather\n", "city_2 = input_place()\n", "\n", - "data_city_2, folder = fetch_data(city_2)" + "data_city_2, folder = fetch_stat_data(city_2)" ] }, { @@ -114,7 +114,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "# Overwrites the folder stored inside the function\n", "folder = \"../data/output_sammenligning_statistikk\"\n", @@ -149,7 +149,7 @@ "\n", "file_path = f'../data/output_sammenligning_statistikk/data_{filename}.json'\n", "\n", - "from my_package.util import extract_city_data_stat\n", + "from my_package.data import extract_city_data_stat\n", "\n", "# Load the whole JSON file\n", "with open(file_path, 'r') as f:\n", diff --git a/notebooks/notebook_current_data.ipynb b/notebooks/notebook_current_data.ipynb index b020d0f..d2d01a9 100644 --- a/notebooks/notebook_current_data.ipynb +++ b/notebooks/notebook_current_data.ipynb @@ -30,7 +30,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_current_data import fetch_current_data\n", + "from my_package.data import fetch_current_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -64,7 +64,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "# The user choose the filename\n", "filename = input(\"Write filename: \")\n", diff --git a/notebooks/notebook_one_day_data.ipynb b/notebooks/notebook_one_day_data.ipynb index ce02da1..b88b58c 100644 --- a/notebooks/notebook_one_day_data.ipynb +++ b/notebooks/notebook_one_day_data.ipynb @@ -95,7 +95,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -107,7 +107,7 @@ "start_date, end_date = timestamps[0], timestamps[-1]\n", "\n", "# Stores the values in the variables\n", - "weather_data, folder = fetch_data(start_date, end_date, city_name)" + "weather_data, folder = fetch_time_data(start_date, end_date, city_name)" ] }, { @@ -131,7 +131,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "filename = input(\"Write filename: \")\n", "\n", @@ -159,7 +159,7 @@ "# Reads from file using pandas\n", "weather_data = pd.read_json(f'../data/output_stedsnavn/data_{filename}.json')\n", "\n", - "from my_package.util import extract_city_df\n", + "from my_package.data import extract_city_df\n", "\n", "df = extract_city_df(weather_data)\n", "display(df)" diff --git a/notebooks/notebook_one_week_data.ipynb b/notebooks/notebook_one_week_data.ipynb index 52118e5..e315da3 100644 --- a/notebooks/notebook_one_week_data.ipynb +++ b/notebooks/notebook_one_week_data.ipynb @@ -84,7 +84,7 @@ "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", "# Now we can import the fucntion from the module\n", - "from my_package.fetch_data import fetch_data\n", + "from my_package.data import fetch_time_data\n", "\n", "# Import function to for input_place, replace æøå\n", "from my_package.util import input_place\n", @@ -93,7 +93,7 @@ "city_name = input_place()\n", "\n", "# Stores the values in the variables\n", - "data, folder = fetch_data(unix_start_date, unix_end_date, city_name)\n" + "data, folder = fetch_time_data(unix_start_date, unix_end_date, city_name)\n" ] }, { @@ -118,7 +118,7 @@ "# Gets the absolute path to the src folder\n", "sys.path.append(os.path.abspath(\"../src\"))\n", "\n", - "from my_package.write_data import write_data\n", + "from my_package.data import write_data\n", "\n", "# User chose the name for the file\n", "filename = input(\"Write filename: \")\n", @@ -147,7 +147,7 @@ "# Reads from file using pandas\n", "weather_data = pd.read_json(f'../data/output_stedsnavn/data_{filename}.json')\n", "\n", - "from my_package.util import extract_city_df\n", + "from my_package.data import extract_city_df\n", "\n", "df = extract_city_df(weather_data)\n", "display(df)" diff --git a/src/README.md b/src/README.md index fca3fd8..b1fb22f 100644 --- a/src/README.md +++ b/src/README.md @@ -6,11 +6,18 @@ Mye av funksjonaliteten og funksjonener er skrevet i en vanlig `.py` fil, før d Her kommer en kjapp forklaring av de ulike filene og deres funksjoner: - `date_to_unix.py` bruker innebygde moduler som datetime og time, for å gjøre om datoer og tider til unix timestamp, sekunder fra 1. januar 1970. -- `fetch_current_data.py` funksjon for å hente nåværende data for ønsket sted fra API-en. Sender feilkode dersom statusen ikke har 200, altså ok. -- `fetch_data.py` henter data for ønsket sted, fra ønsket starttid til sluttid. Sender feilkode dersom statusen ikke har 200, altså ok. -- `get_record.py` brukt i `notebook_statistic_data.ipynb` for å finne rekord-målinger som høyeste og laveste målte temperatur. - `setup.py` funskjon for å hjelpe brukeren å lage en .env fil for å lagre API-key og email. +- `data.py` inneholder flere ulike funksjoner relatert til dataen. Til for eksempel: + - Hente current data + - Hente data for ønsket periode/dag + - Hente statistisk data + - Skrive data til json fil + - Hente data fra json fil, og fjerne metadata +- `util.py` inneholder ulike funksjoner som brukes flere ganger og derfor samles i en funksjon. Til for eksempel: + - Input funksjon, som renser for nordiske 'æøå' + - Konverterer grader fra kelvin til celsius + - Sjekker at ønskede kolonner eksisterer i datasettet + - Fyller "NaN" verdier med 0, i ønskede kolonner + - Fjerner alle uønskede kolonner med samme endelse (notebook_statistic_data.ipynb) + - Henter rekord målinger (notebook_statistic_data.ipynb) - `test_module.py` en test funksjon for å sjekke at venv og implementering til notebook funker som det skal. -- `util.py` inneholder funksjoner for å erstatte nordiske (æøå) og å omgjøre temperaturer fra kelvin til celsius. Altså funksjoner som bare er en enkel del av noe større. -- `write_data.py` lagrer data i json-format, med ønsket filnavn til en 'passende' mappe basert på hvor funksjonen brukes. -- `year_data.py` henter statistisk værdata basert på historikk for ønsket sted. Sender feilkode dersom statusen ikke har 200, altså ok. \ No newline at end of file diff --git a/src/my_package/data.py b/src/my_package/data.py new file mode 100644 index 0000000..23889d1 --- /dev/null +++ b/src/my_package/data.py @@ -0,0 +1,139 @@ +# Import of needed libaries +import requests +import os +from dotenv import load_dotenv +import json +import pandas as pd + +load_dotenv() + +# Gets the key, from my env file +API_KEY = os.getenv("API_KEY") + +# Gets the current data from the API - openweathermap.org +def fetch_current_data(city_name): + + + # f-string url, to add the "custom" variables to the API-request + url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name},NO&units=metric&appid={API_KEY}" + + # Saves the API-request for the url + response = requests.get(url) + + # Checks if the status code is OK + if response.status_code == 200: + + # Converts the data into json + data = response.json() + folder = "../data/output_current_data" + + print("Data fetch: ok") + return data, folder + + + else: + # If html status code != 200, print the status code + print("Failed to fetch data from API. Status code:", response.status_code) + + +# Gets the wanted timeperiod data from the API - openweathermap.org +def fetch_time_data(start_date, end_date, city_name): + + + # f-string url, to add the "custom" variables to the API-request + url = f"https://history.openweathermap.org/data/2.5/history/city?q={city_name},NO&units=metric&type=hour&start={start_date}&end={end_date}&appid={API_KEY}" + + # Saves the API-request for the url + response = requests.get(url) + + # Checks if the status code is OK + if response.status_code == 200: + + # Converts the data into json + data = response.json() + folder = "../data/output_stedsnavn" + + print("Data fetch: ok") + return data, folder + + else: + # If html status code != 200, print the status code + print(f"Failed to fetch data for {city_name} from API. Status code:", response.status_code) + + +# Gets statistical data from the API - openweathermap.org +def fetch_stat_data(city_name): + + + # f-string url, to add the "custom" variables to the API-request + url = f"https://history.openweathermap.org/data/2.5/aggregated/year?q={city_name},NO&appid={API_KEY}&units=metric" + + # Saves the API-request for the url + response = requests.get(url) + + # Checks if the status code is OK + if response.status_code == 200: + + # Converts the data into json + data = response.json() + folder = "../data/output_statistikk" + + print("Data fetch: ok") + return data, folder + + else: + # If html status code != 200, print the status code + print(f"Failed to fetch data for city: {city_name} from API. Status code:", response.status_code) + + +# Write data to json-file +def write_data(data, folder, filename): + # Ensure the 'output_stedsdata' folder exists inside the 'data' folder at the root of the project + script_dir = os.path.dirname(os.path.abspath(__file__)) # Get the directory of the script + project_root = os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir)) # Navigate to the root of the project + data_dir = os.path.join(project_root, 'data', folder) + os.makedirs(data_dir, exist_ok=True) # Creates 'data/output_stedsdata' folder if it doesn't exist + + # Write the JSON data to a file inside the 'output_stedsdata' folder + file_path = os.path.join(data_dir, f'data_{filename}.json') # Creates 'data/output_stedsdata/data_{filename}.json' + + # Opens and write the data to a json file + with open(file_path, 'w') as json_file: + json.dump(data, json_file, indent=4) + + # Prints when succed + print(f"Data has been written to {file_path}") + + +# Function to 'normalize' the dataset, with index-changing and dropping meta-data +def extract_city_df(weather_data): + if 'list' in weather_data: + # Normalize the json for better readability + df = pd.json_normalize(weather_data['list']) + + # Delete duplicates based on the dt row, all the other values can appear more than once, but the date should only appear once + df = df.drop_duplicates(subset=['dt']) + + # The weather column dosnt have any releated information, therefor we delete it + df = df.drop(columns="weather") + + # Convert 'dt' column from Unix timestamp to datetime and set it as the index + df['dt'] = pd.to_datetime(df['dt'], unit='s') + df.set_index('dt', inplace=True) + return df + + else: + return None + +# Function to 'normalize' the dataset for statistic-data, with index-changing and dropping meta-data +def extract_city_data_stat(data): + # Checks if the 'result' column is in the data + if 'result' in data: + # Normalize the json and store it as a dataframe for better readability + df = pd.json_normalize(data['result']) + + # Display the dataframe + return df + else: + print("'result' not in data") + return None \ No newline at end of file diff --git a/src/my_package/fetch_current_data.py b/src/my_package/fetch_current_data.py deleted file mode 100644 index 787f3c3..0000000 --- a/src/my_package/fetch_current_data.py +++ /dev/null @@ -1,38 +0,0 @@ -# Import of needed libaries -import requests -import os -from dotenv import load_dotenv - -load_dotenv() - -# Gets the key, from my env file -API_KEY = os.getenv("API_KEY") - -# city_name = "Trondheim" -country_code = "NO" - - -# Gets the data from the API - openweathermap.org -def fetch_current_data(city_name): - - - # f-string url, to add the "custom" variables to the API-request - url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name},NO&units=metric&appid={API_KEY}" - - # Saves the API-request for the url - response = requests.get(url) - - # Checks if the status code is OK - if response.status_code == 200: - - # Converts the data into json - data = response.json() - folder = "../data/output_current_data" - - print("Data fetch: ok") - return data, folder - - - else: - # If html status code != 200, print the status code - print("Failed to fetch data from API. Status code:", response.status_code) \ No newline at end of file diff --git a/src/my_package/fetch_data.py b/src/my_package/fetch_data.py deleted file mode 100644 index f087d28..0000000 --- a/src/my_package/fetch_data.py +++ /dev/null @@ -1,40 +0,0 @@ -# Import of needed libaries -import requests -import os -from dotenv import load_dotenv - -load_dotenv() - -# Gets the key, from my env file -API_KEY = os.getenv("API_KEY") - -# city_name = "Trondheim" -# country_code = "NO" - -# Temporarily standard times -# start_date = 1735686000 -# end_date = 1740009323 - -# Gets the data from the API - openweathermap.org -def fetch_data(start_date, end_date, city_name): - - - # f-string url, to add the "custom" variables to the API-request - url = f"https://history.openweathermap.org/data/2.5/history/city?q={city_name},NO&units=metric&type=hour&start={start_date}&end={end_date}&appid={API_KEY}" - - # Saves the API-request for the url - response = requests.get(url) - - # Checks if the status code is OK - if response.status_code == 200: - - # Converts the data into json - data = response.json() - folder = "../data/output_stedsnavn" - - print("Data fetch: ok") - return data, folder - - else: - # If html status code != 200, print the status code - print(f"Failed to fetch data for {city_name} from API. Status code:", response.status_code) diff --git a/src/my_package/get_record.py b/src/my_package/get_record.py deleted file mode 100644 index 7454681..0000000 --- a/src/my_package/get_record.py +++ /dev/null @@ -1,23 +0,0 @@ -import pandas as pd - -def get_records(df, city_name): - if df.empty: - print("df is empty") - - else: - max_temp_mean = df['temp.mean_celsius'].max() - min_temp_mean = df['temp.mean_celsius'].min() - - max_temp = df['temp.record_max_celsius'].max() - min_temp = df['temp.record_min_celsius'].min() - - summary_data = { - "Metric": ["Max Temp mean (°C)", "Min Temp Mean (°C)", "Max Temp (°C)", "Min temp (°C)"], - "Values": [max_temp_mean, min_temp_mean, max_temp, min_temp] - } - - summary_df = pd.DataFrame(summary_data) - folder = "../data/output_record" - filename = f"records_{city_name}" - - return summary_df, filename, folder \ No newline at end of file diff --git a/src/my_package/util.py b/src/my_package/util.py index c667960..e23afcb 100644 --- a/src/my_package/util.py +++ b/src/my_package/util.py @@ -10,6 +10,7 @@ def replace_nordic(city_name): city_name = city_name.replace('å', 'aa') return city_name +# Get the city_name input, convert nordic 'æøå' def input_place(): city_name = input("Enter city name: ") city_name = replace_nordic(city_name) @@ -20,68 +21,51 @@ def kelvin_to_celsius(temp_in_kelvin): temp_in_celsius = temp_in_kelvin - 273.15 return temp_in_celsius -# Fucntion to check if there are a ['rain.1h'] column in the dataset, if not make one -def ensure_rain_column(df): - try: - _ = df['rain.1h'] - - # If no rain, make the rain column and fill it with NaN - except KeyError: - print("'Rain' is not present in the JSON file.") - df['rain.1h'] = np.nan - +# Ensure wqnted columns, fill with "NaN" if they dont exsist +def ensure_column(df, columns): + for col in columns: + if col not in df.columns: + print(f"'{col}' is not present in the DataFrame. Filling with NaN.") + df[col] = np.nan return df -# Fucntion to check if there are a ['snow.1h'] column in the dataset, if not make one -def ensure_snow_column(df): - try: - _ = df['snow.1h'] - - # If no snow, make the snow column and fill it with NaN - except KeyError: - print("'Snow' is not present in the JSON file.") - df['snow.1h'] = np.nan - +# Fill "NaN" in wanted columns with 0 +def fill_column_0(df, columns): + for col in columns: + try: + df[col] = df[col].fillna(0) + except KeyError: + print(f"'{col}' not in DataFrame") return df -# Function to fill NaN values in ['rain.1h'] columns with 0 -def fill_rain_column(df): - try: - # Replace the NaN with 0, using pandas function - df['rain.1h'] = df['rain.1h'].fillna(0) - - except KeyError: - print(["'rain.1h', not in df"]) +# Cleans statistic-data, drop all columns that end with '...' using the filter function +def clean_df(df): + df = df.drop(columns=df.filter(like='.p25').columns) + df = df.drop(columns=df.filter(like='.p75').columns) + df = df.drop(columns=df.filter(like='.st_dev').columns) + df = df.drop(columns=df.filter(like='.num').columns) return df -# Function to fill NaN values in ['snow.1h'] columns with 0 -def fill_snow_column(df): - try: - # Replace the NaN with 0, using pandas function - df['snow.1h'] = df['snow.1h'].fillna(0) - - except KeyError: - print(["'snow.1h', not in df"]) +# Find highest and lowest temp from statistical data +def get_records(df, city_name): + if df.empty: + print("df is empty") - return df - -# Function to 'normalize' the dataset, with index-changing and dropping meta-data -def extract_city_df(weather_data): - if 'list' in weather_data: - # Normalize the json for better readability - df = pd.json_normalize(weather_data['list']) + else: + max_temp_mean = df['temp.mean_celsius'].max() + min_temp_mean = df['temp.mean_celsius'].min() - # Delete duplicates based on the dt row, all the other values can appear more than once, but the date should only appear once - df = df.drop_duplicates(subset=['dt']) + max_temp = df['temp.record_max_celsius'].max() + min_temp = df['temp.record_min_celsius'].min() - # The weather column dosnt have any releated information, therefor we delete it - df = df.drop(columns="weather") + summary_data = { + "Metric": ["Max Temp mean (°C)", "Min Temp Mean (°C)", "Max Temp (°C)", "Min temp (°C)"], + "Values": [max_temp_mean, min_temp_mean, max_temp, min_temp] + } - # Convert 'dt' column from Unix timestamp to datetime and set it as the index - df['dt'] = pd.to_datetime(df['dt'], unit='s') - df.set_index('dt', inplace=True) - return df + summary_df = pd.DataFrame(summary_data) + folder = "../data/output_record" + filename = f"records_{city_name}" - else: - return None \ No newline at end of file + return summary_df, filename, folder \ No newline at end of file diff --git a/src/my_package/write_data.py b/src/my_package/write_data.py deleted file mode 100644 index e46eb2a..0000000 --- a/src/my_package/write_data.py +++ /dev/null @@ -1,19 +0,0 @@ -import json -import os - -def write_data(data, folder, filename): - # Ensure the 'output_stedsdata' folder exists inside the 'data' folder at the root of the project - script_dir = os.path.dirname(os.path.abspath(__file__)) # Get the directory of the script - project_root = os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir)) # Navigate to the root of the project - data_dir = os.path.join(project_root, 'data', folder) - os.makedirs(data_dir, exist_ok=True) # Creates 'data/output_stedsdata' folder if it doesn't exist - - # Write the JSON data to a file inside the 'output_stedsdata' folder - file_path = os.path.join(data_dir, f'data_{filename}.json') # Creates 'data/output_stedsdata/data_{filename}.json' - - # Opens and write the data to a json file - with open(file_path, 'w') as json_file: - json.dump(data, json_file, indent=4) - - # Prints when succed - print(f"Data has been written to {file_path}") \ No newline at end of file diff --git a/src/my_package/year_data.py b/src/my_package/year_data.py deleted file mode 100644 index 3d6f780..0000000 --- a/src/my_package/year_data.py +++ /dev/null @@ -1,38 +0,0 @@ -# Import of needed libaries -import requests -import os -from dotenv import load_dotenv - - -load_dotenv() - -# Gets the key, from my env file -API_KEY = os.getenv("API_KEY") - -# city_name = "Maura" - -# Gets the data from the API - openweathermap.org -def fetch_data(city_name): - - - # f-string url, to add the "custom" variables to the API-request - url = f"https://history.openweathermap.org/data/2.5/aggregated/year?q={city_name},NO&appid={API_KEY}&units=metric" - - # Saves the API-request for the url - response = requests.get(url) - - # Checks if the status code is OK - if response.status_code == 200: - - # Converts the data into json - data = response.json() - folder = "../data/output_statistikk" - - print("Data fetch: ok") - return data, folder - - else: - # If html status code != 200, print the status code - print(f"Failed to fetch data for city: {city_name} from API. Status code:", response.status_code) - -# myData = fetch_data(city_name) \ No newline at end of file