Spaces:
Running
Running
Update pages/Weather API.py
Browse files- pages/Weather API.py +0 -31
pages/Weather API.py
CHANGED
|
@@ -10,19 +10,12 @@ import streamlit as st
|
|
| 10 |
from geopy.extra.rate_limiter import RateLimiter
|
| 11 |
from geopy.geocoders import Nominatim
|
| 12 |
from matplotlib import colors as colors
|
| 13 |
-
from streamlit_folium import st_folium
|
| 14 |
import rioxarray
|
| 15 |
import xarray as xr
|
| 16 |
import cdsapi
|
| 17 |
import os
|
| 18 |
|
| 19 |
|
| 20 |
-
def mapvalue2color(value, cmap):
|
| 21 |
-
if np.isnan(value):
|
| 22 |
-
return (1, 0, 0, 0)
|
| 23 |
-
else:
|
| 24 |
-
return colors.to_rgba(cmap(value), 0.7)
|
| 25 |
-
|
| 26 |
|
| 27 |
def geocode(address):
|
| 28 |
try:
|
|
@@ -39,30 +32,6 @@ def geocode(address):
|
|
| 39 |
return lat, lon
|
| 40 |
|
| 41 |
|
| 42 |
-
def graph_within_date_range(d, number_days_range):
|
| 43 |
-
year, month, day = d[:4], d[4:6], d[6:8]
|
| 44 |
-
date = pd.Timestamp(d)
|
| 45 |
-
start_date, end_date = date - \
|
| 46 |
-
pd.Timedelta(days=number_days_range), date + \
|
| 47 |
-
pd.Timedelta(days=number_days_range+1)
|
| 48 |
-
start_date = start_date.strftime("%Y-%m-%d")
|
| 49 |
-
end_date = end_date.strftime("%Y-%m-%d")
|
| 50 |
-
url = f'https://archive-api.open-meteo.com/v1/archive?latitude={lat}&longitude={lon}&start_date={start_date}&end_date={end_date}&hourly=temperature_2m,precipitation,windspeed_10m,wind_gusts_10m&models=best_match&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch'
|
| 51 |
-
df = pd.read_json(url).reset_index()
|
| 52 |
-
data = pd.DataFrame({c['index']: c['hourly'] for r, c in df.iterrows()})
|
| 53 |
-
data['time'] = pd.to_datetime(data['time'])
|
| 54 |
-
data['date'] = pd.to_datetime(data['time'].dt.date)
|
| 55 |
-
data = data.query("temperature_2m==temperature_2m")
|
| 56 |
-
|
| 57 |
-
data_agg = data.groupby(['date']).agg({'temperature_2m': ['min', 'mean', 'max'],
|
| 58 |
-
'precipitation': ['sum'],
|
| 59 |
-
'windspeed_10m': ['min', 'mean', 'max'],
|
| 60 |
-
'wind_gusts_10m': ['min', 'mean', 'max']
|
| 61 |
-
})
|
| 62 |
-
data_agg.columns = data_agg.columns.to_series().str.join('_')
|
| 63 |
-
data_agg = data_agg.query("temperature_2m_min==temperature_2m_min")
|
| 64 |
-
return data.drop(columns=['date']), data_agg
|
| 65 |
-
|
| 66 |
|
| 67 |
|
| 68 |
def get_weather_data(lat, lon, start_date, end_date):
|
|
|
|
| 10 |
from geopy.extra.rate_limiter import RateLimiter
|
| 11 |
from geopy.geocoders import Nominatim
|
| 12 |
from matplotlib import colors as colors
|
|
|
|
| 13 |
import rioxarray
|
| 14 |
import xarray as xr
|
| 15 |
import cdsapi
|
| 16 |
import os
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def geocode(address):
|
| 21 |
try:
|
|
|
|
| 32 |
return lat, lon
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def get_weather_data(lat, lon, start_date, end_date):
|