forked from cemac/LivingLabDataApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weather.py
83 lines (68 loc) · 2.75 KB
/
Weather.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script name: Weather.py
Author: Nick
Date: July 2018
Purpose: Used to fetch and process weather data from ncas' Leeds Weather Data archive
"""
import matplotlib
matplotlib.use('Agg')
import os
import sys
import ssl
import pandas as pd
import datetime
import math
import cmath
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
def fetchWeatherData(date):
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
yearstring = str(date.year)
monthstring = str(date.month) if date.month >= 10 else "0"+str(date.month)
daystring = str(date.day) if date.day >= 10 else "0"+str(date.day)
datestring = yearstring + "-" + monthstring + "-" + daystring
url = "https://sci.ncas.ac.uk/leedsweather/Archive/CUSTOM-ARC-"+datestring+"-METRIC.csv"
weatherdata = pd.read_csv(url, parse_dates=[0], index_col=[0])
ambientData = weatherdata[['Temp / °C','Humid%','Pressure / hPa']]
ambientData = ambientData.resample('H').mean()
ambientData = ambientData.iloc[[date.hour]]
ambientData = ambientData.round(1)
windData = weatherdata[['Wind / ms¯¹','Winddir / °']]
startTime = date.strftime("%H:%M:%S")
timeplushour = date + datetime.timedelta(0,3600)
endTime = timeplushour.strftime("%H:%M:%S")
windData = windData.between_time(startTime, endTime)
arrSpeed = windData['Wind / ms¯¹'].tolist()
arrDirection = windData['Winddir / °'].tolist()
wDirection, wSpeed = polarAverage(arrDirection, arrSpeed)
print(ambientData)
viewModel = {
'Temp / °C': ambientData.values[0][0]
,'Humid%': ambientData.values[0][1]
,'Pressure / hPa': ambientData.values[0][2]
,'Wind / ms¯¹': wSpeed
,'Winddir / °': wDirection
}
return viewModel
# http://www.intellovations.com/2011/01/16/wind-observation-calculations-in-fortran-and-python/
def polarAverage(arrDirection, arrSpeed):
wind_vector_sum = None
for i in range(0, arrDirection.__len__()-1):
direction = math.radians(arrDirection[i])
wind_polar = cmath.rect(arrSpeed[i], direction)
if wind_vector_sum is None:
wind_vector_sum = wind_polar
else:
wind_vector_sum += wind_polar
r, phi = cmath.polar(wind_vector_sum / arrDirection.__len__())
rwdir = math.degrees(phi) % 360
rwspd = r
# rwdir = int(round(int(round(math.degrees(phi) % 360)) / 10.0))
# rwspd = int(round(r * 10)) / 10.0
return '%2.0f' % rwdir, '%5.1f' % rwspd