From 68063bb6e8a2a407073d2c1d95812b17b83a398c Mon Sep 17 00:00:00 2001 From: Hanne Heggdal Date: Wed, 26 Mar 2025 15:05:08 +0100 Subject: [PATCH] notebook add, current data for chosen city --- notebooks/get_current_data.ipynb | 240 +++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 notebooks/get_current_data.ipynb diff --git a/notebooks/get_current_data.ipynb b/notebooks/get_current_data.ipynb new file mode 100644 index 0000000..468797b --- /dev/null +++ b/notebooks/get_current_data.ipynb @@ -0,0 +1,240 @@ +{ + "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 +}