from datetime import datetime import requests import os import joblib import pandas as pd import numpy as np import json from dotenv import load_dotenv load_dotenv() def get_weather_json(date, WEATHER_API_KEY): return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/london/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json() def get_weather_data(date): WEATHER_API_KEY = os.getenv('WEATHER_API_KEY') json = get_weather_json(date, WEATHER_API_KEY) data = json['days'][0] return [ json['address'].capitalize(), data['datetime'], data['tempmax'], data['tempmin'], data['temp'], data['feelslikemax'], data['feelslikemin'], data['feelslike'], data['dew'], data['humidity'], data['precip'], data['precipprob'], data['precipcover'], data['snow'], data['snowdepth'], data['windgust'], data['windspeed'], data['winddir'], data['pressure'], data['cloudcover'], data['visibility'], data['solarradiation'], data['solarenergy'], data['uvindex'], data['conditions'] ] def get_weather_df(data): col_names = [ 'city', 'date', 'tempmax', 'tempmin', 'temp', 'feelslikemax', 'feelslikemin', 'feelslike', 'dew', 'humidity', 'precip', 'precipprob', 'precipcover', 'snow', 'snowdepth', 'windgust', 'windspeed', 'winddir', 'pressure', 'cloudcover', 'visibility', 'solarradiation', 'solarenergy', 'uvindex', 'conditions' ] new_data = pd.DataFrame( data, columns=col_names ) new_data.date = new_data.date.apply(timestamp_2_time) return new_data def timestamp_2_time(x): dt_obj = datetime.strptime(str(x), '%Y-%m-%d') dt_obj = dt_obj.timestamp() * 1000 return int(dt_obj) def encoder_range(temps): boundary_list = np.array([0, 50, 100, 150, 200, 300]) redf = np.logical_not(temps<=boundary_list) hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1) cat = np.nonzero(np.not_equal(redf,hift)) air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous'] level = [air_pollution_level[el] for el in cat[1]] return level def get_aplevel(temps:np.ndarray) -> list: boundary_list = np.array([0, 50, 100, 150, 200, 300]) # assert temps.shape == [x, 1] redf = np.logical_not(temps<=boundary_list) # temps.shape[0] x boundary_list.shape[0] ndarray hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1) cat = np.nonzero(np.not_equal(redf,hift)) air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous'] level = [air_pollution_level[el] for el in cat[1]] return level