-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
64 lines (52 loc) · 1.94 KB
/
services.py
File metadata and controls
64 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from datetime import datetime
import numpy as np
from requests import get
from constants import EXCLUDED, UNITS, API_KEY
def get_params_from_data(data):
date = datetime.utcfromtimestamp(data.get("dt"))
temp = data.get("temp").get("day")
pcp = data.get("rain")
clouds = data.get("clouds")
pressure = data.get("pressure")
humidity = data.get("humidity")
wind_speed = data.get("wind_speed")
return {
'date': date,
'temp': temp,
'pcp': pcp,
'clouds': clouds,
'pressure': pressure,
'humidity': humidity,
'wind_speed': wind_speed
}
def get_forecast_by_cords_in_json_format(cords):
lat, lon = cords
link = f"https://api.openweathermap.org/data/2.5/onecall" \
f"?lat={lat}" \
f"&lon={lon}" \
f"&exclude={EXCLUDED}" \
f"&units={UNITS}" \
f"&appid={API_KEY}"
try:
result = get(link).json()
except Exception as e:
print(f'[ERROR] : {e}')
return
return result
def sum_values_in_array_by_value_name(array_of_forecasts, value_type):
array_of_values = []
for forecast in array_of_forecasts:
value = getattr(forecast, value_type)
if value:
array_of_values.append(value)
return sum(array_of_values) / len(array_of_forecasts)
def moving_average(x, w):
list_of_averages = list(np.convolve(x, np.ones(w), 'valid') / w)
result = list(map(float, list_of_averages))
return result
def get_dict_of_lists_of_moving_means(array_of_forecasts, value_type):
array = np.array([getattr(forecast, value_type) for forecast in array_of_forecasts])
moving_average_array = [float(array[0])] # Here we want to save first value, that can't be average
moving_average_array.extend(moving_average(array, 2))
list_of_forecast_dates = [str(forecast.date) for forecast in array_of_forecasts]
return dict(zip(list_of_forecast_dates, moving_average_array))