diff --git a/notebooks/get_current_data.ipynb b/notebooks/get_current_data.ipynb deleted file mode 100644 index 468797b..0000000 --- a/notebooks/get_current_data.ipynb +++ /dev/null @@ -1,240 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Data fetch: ok\n" - ] - } - ], - "source": [ - "import sys\n", - "import os\n", - "\n", - "# Gets the absolute path to the src folder\n", - "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", - "\n", - "# User input the city, for the weather\n", - "city_name = input(\"Enter a city in Norway: \")\n", - "\n", - "data, folder = fetch_current_data(city_name)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Data has been written to /Users/hanne/Documents/anvendt prosjekt/anvendt_mappe/data/../data/output_current_data/data_stavg_current.json\n" - ] - } - ], - "source": [ - "# 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", - "\n", - "filename = input(\"Write filename: \")\n", - "\n", - "write_data(data, folder, filename)" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
namemain.tempmain.feels_likemain.temp_minmain.temp_maxmain.pressuremain.humiditymain.sea_levelmain.grnd_levelwind.speedwind.gustclouds.allsys.typesys.idsys.countrysys.sunrisesys.sunset
dt
2025-03-26 14:04:13Stavanger8.767.558.379.52101994101910152.243.587522031843NO2025-03-26 05:21:032025-03-26 18:04:12
\n", - "
" - ], - "text/plain": [ - " name main.temp main.feels_like main.temp_min \\\n", - "dt \n", - "2025-03-26 14:04:13 Stavanger 8.76 7.55 8.37 \n", - "\n", - " main.temp_max main.pressure main.humidity \\\n", - "dt \n", - "2025-03-26 14:04:13 9.52 1019 94 \n", - "\n", - " main.sea_level main.grnd_level wind.speed wind.gust \\\n", - "dt \n", - "2025-03-26 14:04:13 1019 1015 2.24 3.58 \n", - "\n", - " clouds.all sys.type sys.id sys.country \\\n", - "dt \n", - "2025-03-26 14:04:13 75 2 2031843 NO \n", - "\n", - " sys.sunrise sys.sunset \n", - "dt \n", - "2025-03-26 14:04:13 2025-03-26 05:21:03 2025-03-26 18:04:12 " - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import pandas as pd\n", - "import json\n", - "\n", - "# data = pd.read_json(f'../data/output_current_data/data_{filename}.json')\n", - "\n", - "# Les JSON-filen\n", - "with open(f\"../data/output_current_data/data_{filename}.json\", \"r\", encoding=\"utf-8\") as file:\n", - " data = json.load(file)\n", - "\n", - "# Flate ut JSON-strukturen med json_normalize\n", - "df = pd.json_normalize(data)\n", - "\n", - "# Delete duplicates based on the dt row, all the other values can appear more than once, but the date should only appear once\n", - "df = df.drop_duplicates(subset=['dt'])\n", - "\n", - "# Deleted the columns that was not relevant\n", - "df = df.drop(columns=\"weather\")\n", - "df = df.drop(columns=\"base\")\n", - "df = df.drop(columns=\"visibility\")\n", - "df = df.drop(columns=\"timezone\")\n", - "df = df.drop(columns=\"id\")\n", - "df = df.drop(columns=\"cod\")\n", - "df = df.drop(columns=\"coord.lon\")\n", - "df = df.drop(columns=\"coord.lat\")\n", - "df = df.drop(columns=\"wind.deg\")\n", - "\n", - "#change from unix to datetime for sunrise and sunset\n", - "df['sys.sunrise'] = pd.to_datetime(df['sys.sunrise'], unit='s')\n", - "df['sys.sunset'] = pd.to_datetime(df['sys.sunset'], unit='s')\n", - "\n", - "# Convert 'dt' column from Unix timestamp to datetime and set it as the index\n", - "df['dt'] = pd.to_datetime(df['dt'], unit='s')\n", - "df.set_index('dt', inplace=True)\n", - "\n", - "# Skriv ut DataFrame\n", - "display(df)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/notebooks/notebook_current_data.ipynb b/notebooks/notebook_current_data.ipynb new file mode 100644 index 0000000..4526089 --- /dev/null +++ b/notebooks/notebook_current_data.ipynb @@ -0,0 +1,180 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Notebook - Current Data\n", + "Denne notebooken er for å hente, skrive og vise nåværende data for ønsket lokasjon." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Velg sted og få nåværende data\n", + "\n", + "Skriv inn et sted du ønsker å få nåværende data fra, foreløpig er det begrenset til Norge" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "import os\n", + "\n", + "# Gets the absolute path to the src folder\n", + "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", + "\n", + "# User input the city, for the weather\n", + "city_name = input(\"Enter a city in Norway: \")\n", + "\n", + "# Stores the return of the function\n", + "data, folder = fetch_current_data(city_name)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lagre data i en json-fil\n", + "\n", + "Skriv inn navn for til filen du vil lagre med dataen.\n", + "\n", + "Eks. test\n", + "Da vil filen lagres som data_**test**.json, i mappen \"../data/output_stedsnavn/data_{filnavn}.json\"\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 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", + "\n", + "# The user choose the filename\n", + "filename = input(\"Write filename: \")\n", + "\n", + "# Writes the data, using user input filename\n", + "write_data(data, folder, filename)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lese fra fil\n", + "\n", + "Henter opp data lagret i filen, lagd over, og skriver ut lesbart ved hjelp av pandas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "# Read from the json-file\n", + "with open(f\"../data/output_current_data/data_{filename}.json\", \"r\") as file:\n", + " data = json.load(file)\n", + "\n", + "# Display data\n", + "display(data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Rydde i data\n", + "For å gjøre det enkelre å lese dataen, normaliserer vi json-filen ved hjelp av pandas.\n", + "\n", + "Vi fjerner også irrellevante kolonner som:\n", + "- weather: denne inneholder informasjon om været (beskrivelse, id, icon osv.)\n", + "- coord.lon og coord.lat: vi trengre ikke koordinatene når vi har valgt basert på ønsket sted\n", + "- sys.type, sys.id, base, cod: interne parametre\n", + "- temp_max og temp_min: er ikke store endringer av temperatur innenfor en times tid\n", + "- visibility: sikt avstand i forhold til tåke, vi anser den som urelevant\n", + "\n", + "Deretter konverteres datetime [dt] fra unix_timestamp til vanlig tid, for å brukes som index\n", + "\n", + "Tiden for soloppgang og solnedgang konverteres også fra unix til vanlig tid, for å lettere leses og forstås." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "# Normalize the json-structure, to add better readability\n", + "df = pd.json_normalize(data)\n", + "\n", + "# Delete duplicates based on the dt row, all the other values can appear more than once, but the date should only appear once\n", + "df = df.drop_duplicates(subset=['dt'])\n", + "\n", + "# Delete columns that is not relevant\n", + "df = df.drop(columns=\"weather\")\n", + "df = df.drop(columns=\"base\")\n", + "df = df.drop(columns=\"visibility\")\n", + "df = df.drop(columns=\"timezone\")\n", + "df = df.drop(columns=\"id\")\n", + "df = df.drop(columns=\"cod\")\n", + "df = df.drop(columns=\"coord.lon\")\n", + "df = df.drop(columns=\"coord.lat\")\n", + "df = df.drop(columns=\"wind.deg\")\n", + "df = df.drop(columns=\"main.temp_min\")\n", + "df = df.drop(columns=\"main.temp_max\")\n", + "df = df.drop(columns=\"sys.type\")\n", + "df = df.drop(columns=\"sys.id\")\n", + "\n", + "# Change from unix to datetime for sunrise and sunset\n", + "df['sys.sunrise'] = pd.to_datetime(df['sys.sunrise'], unit='s')\n", + "df['sys.sunset'] = pd.to_datetime(df['sys.sunset'], unit='s')\n", + "\n", + "# Convert 'dt' column from Unix timestamp to datetime and set it as the index\n", + "df['dt'] = pd.to_datetime(df['dt'], unit='s')\n", + "df.set_index('dt', inplace=True)\n", + "\n", + "# Display the df after changes\n", + "display(df)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}