Skip to content

Commit

Permalink
notebook add, current data for chosen city
Browse files Browse the repository at this point in the history
  • Loading branch information
hannhegg committed Mar 26, 2025
1 parent 0b6144e commit 68063bb
Showing 1 changed file with 240 additions and 0 deletions.
240 changes: 240 additions & 0 deletions notebooks/get_current_data.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>main.temp</th>\n",
" <th>main.feels_like</th>\n",
" <th>main.temp_min</th>\n",
" <th>main.temp_max</th>\n",
" <th>main.pressure</th>\n",
" <th>main.humidity</th>\n",
" <th>main.sea_level</th>\n",
" <th>main.grnd_level</th>\n",
" <th>wind.speed</th>\n",
" <th>wind.gust</th>\n",
" <th>clouds.all</th>\n",
" <th>sys.type</th>\n",
" <th>sys.id</th>\n",
" <th>sys.country</th>\n",
" <th>sys.sunrise</th>\n",
" <th>sys.sunset</th>\n",
" </tr>\n",
" <tr>\n",
" <th>dt</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2025-03-26 14:04:13</th>\n",
" <td>Stavanger</td>\n",
" <td>8.76</td>\n",
" <td>7.55</td>\n",
" <td>8.37</td>\n",
" <td>9.52</td>\n",
" <td>1019</td>\n",
" <td>94</td>\n",
" <td>1019</td>\n",
" <td>1015</td>\n",
" <td>2.24</td>\n",
" <td>3.58</td>\n",
" <td>75</td>\n",
" <td>2</td>\n",
" <td>2031843</td>\n",
" <td>NO</td>\n",
" <td>2025-03-26 05:21:03</td>\n",
" <td>2025-03-26 18:04:12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}

0 comments on commit 68063bb

Please sign in to comment.