Created
June 13, 2021 09:06
-
-
Save allandequeiroz/0ed60771e277002ae7921b3425a264c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A replacement to Darksky is [Openweather](https://openweathermap.org/darksky-openweather), the API is quite similar, once you create your account, just fetch your API_KEY [here](https://home.openweathermap.org/api_keys)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import dateutil.parser\n", | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"API_KEY = \"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lat, lon = 51.4859062, -0.0976355\n", | |
"resp = requests.get(f\"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={API_KEY}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"resp.text" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"d = resp.json()\n", | |
"d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"time_ = int(dateutil.parser.parse('08-06-2021', dayfirst=True).timestamp())\n", | |
"url = f'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat={lat}&lon={lon}&dt={time_}&exclude=current,minutely,alerts&units=metric&appid={API_KEY}'\n", | |
"\n", | |
"resp = requests.get(url)\n", | |
"weather_small_df = pd.DataFrame(resp.json()['hourly'])\n", | |
"weather_small_df['dt'] = pd.to_datetime(weather_small_df['dt'], unit='s')\n", | |
"# weather_small_df.index = weather_small_df.set_index(['dt'], append=True)\n", | |
"weather_small_df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"weather_small_df_idx = weather_small_df.set_index(['dt'], append=False)\n", | |
"# weather_small_df_idx.index\n", | |
"weather_small_df_idx.feels_like.plot()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# time = int(dateutil.parser.parse('2021-06-08T00:00:00', dayfirst=False).timestamp())\n", | |
"def get_daily_weather(time):\n", | |
"# print(time)\n", | |
" url = f'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat={lat}&lon={lon}&dt={time}&exclude=current,minutely,alerts&units=metric&appid={API_KEY}'\n", | |
" resp = requests.get(url)\n", | |
" hourly_weather = resp.json()\n", | |
" hourly_weather = pd.DataFrame(resp.json()['hourly'])\n", | |
" hourly_weather['dt'] = pd.to_datetime(hourly_weather['dt'], unit='s')\n", | |
" hourly_weather_result = hourly_weather.set_index(['dt'], append=False)\n", | |
" return hourly_weather_result\n", | |
"\n", | |
"# hourly_weather = get_daily_weather(time)\n", | |
"# hourly_weather.feels_like.plot()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Openweather only provides with the last 5 days of historical data so, set your dates here" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"range = pd.date_range(start='2021-06-08', end='2021-06-13', freq='d')\n", | |
"range" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"weather_list = []\n", | |
"for t in range:\n", | |
"# print(int(t.timestamp()))\n", | |
" daily_weather = get_daily_weather(int(t.timestamp()))\n", | |
" weather_list.append(daily_weather)\n", | |
"# weather_list\n", | |
"weather = pd.concat(weather_list)\n", | |
"# weather.head(100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"weather.feels_like.plot()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"file_path = 'data/chicago_weather/weather.csv'\n", | |
"weather.to_csv(file_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"new_weather = pd.read_csv(file_path, index_col=0, parse_dates=True)\n", | |
"new_weather.feels_like.plot()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment