Skip to content

Commit

Permalink
[weather.metoffice] v4.0.0 (#2486)
Browse files Browse the repository at this point in the history
  • Loading branch information
powlo authored Jul 14, 2023
1 parent 1e6abbe commit a9bf315
Show file tree
Hide file tree
Showing 10 changed files with 770 additions and 689 deletions.
2 changes: 1 addition & 1 deletion weather.metoffice/addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon id="weather.metoffice" version="3.0.1" name="Met Office" provider-name="powlo">
<addon id="weather.metoffice" version="4.0.0" name="Met Office" provider-name="powlo">
<requires>
<import addon="xbmc.python" version="3.0.0" />
<import addon="script.module.pytz" version="2014.2" />
Expand Down
7 changes: 6 additions & 1 deletion weather.metoffice/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ v2.0.1
- added user agent to requests

v3.0.0
- updated for Matrix
- updated for Matrix

v4.0.0
- Cleaned up memory leaks
- Removed unused imaging code.
- Removed default API key and nudged user to get their own.
12 changes: 8 additions & 4 deletions weather.metoffice/resources/settings.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<settings>
<category label="Location Settings">
<setting id="ObservationLocation" label="32000" type="action" action="RunScript($ID, ObservationLocation)" default="Heathrow" />
<setting id="ObservationLocation" label="32000" type="action"
action="RunScript($ID, ObservationLocation)" default="Heathrow" />
<setting id="ObservationLocationID" type="text" visible="false" default="3772" />
<setting id="ForecastLocation" label="32001" type="action" action="RunScript($ID, ForecastLocation)" default="London" />
<setting id="ForecastLocation" label="32001" type="action"
action="RunScript($ID, ForecastLocation)" default="London" />
<setting id="ForecastLocationID" type="text" visible="false" default="352409" />
<setting id="ForecastLocationLatitude" type="text" visible="false" default="51.5081" />
<setting id="ForecastLocationLongitude" type="text" visible="false" default="-0.1248" />
<setting id="RegionalLocation" label="32002" type="action" action="RunScript($ID, RegionalLocation)" default="London and Southeast England" />
<setting id="RegionalLocation" label="32002" type="action"
action="RunScript($ID, RegionalLocation)" default="London and Southeast England" />
<setting id="RegionalLocationID" type="text" visible="false" default="514" />
<setting id="RegionalLocationLatitude" type="text" visible="false" default="51.479" />
<setting id="RegionalLocationLongitude" type="text" visible="false" default="-0.449" />
</category>
<category label="Configuration">
<setting id="ApiKey" label="32003" type="text" default="a5408080-80af-44ea-b3a6-b86d320dc706" />
<setting id="GeoLocation" label="32004" type="bool" default="true" />
<setting id="GeoIPProvider" label="32005" type="enum" values="www.ip-api.com|www.freegeoip.net|www.geoiplookup.net" enable="eq(-1,true)" default="0" />
<setting id="GeoIPProvider" label="32005" type="enum"
values="www.ip-api.com|www.freegeoip.net|www.geoiplookup.net" enable="eq(-1,true)" default="0" />
<setting id="EraseCache" label="32006" type="bool" default="false" />
</category>
</settings>
74 changes: 49 additions & 25 deletions weather.metoffice/src/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,76 @@
# * along with XBMC; see the file COPYING. If not, write to
# * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# * http://www.gnu.org/copyleft/gpl.html
from metoffice.utilities import gettext as _
from metoffice.constants import WINDOW, ADDON, API_KEY, ADDON_DATA_PATH, ADDON_BANNER_PATH
from metoffice import urlcache, properties, utilities
import socket
import sys
from urllib.error import HTTPError

import xbmc

import setlocation
from metoffice import properties, urlcache, utilities
from metoffice.constants import (
ADDON_BANNER_PATH,
ADDON_DATA_PATH,
API_KEY,
addon,
window,
)
from metoffice.utilities import gettext as _

socket.setdefaulttimeout(20)


@utilities.failgracefully
def main():
if ADDON.getSetting('EraseCache') == 'true':
if addon().getSetting("EraseCache") == "true":
try:
urlcache.URLCache(ADDON_DATA_PATH).erase()
finally:
ADDON.setSetting('EraseCache', 'false')
addon().setSetting("EraseCache", "false")

if not API_KEY:
raise Exception(_("No API Key."), _("Enter your Met Office API Key under settings."))
raise Exception(
_("No API Key."), _("Enter your Met Office API Key under settings.")
)

if sys.argv[1] in ['ObservationLocation', 'ForecastLocation', 'RegionalLocation']:
if sys.argv[1] in ["ObservationLocation", "ForecastLocation", "RegionalLocation"]:
setlocation.main(sys.argv[1])

properties.observation()
properties.daily()
properties.threehourly()
properties.sunrisesunset()
try:
properties.observation()
properties.daily()
properties.threehourly()
properties.sunrisesunset()
except HTTPError:
utilities.log(
(
"Error fetching data.\n"
"Ensure the API key in addon configuration is correct and try again.\n"
"You can get an API key by creating an account at\n"
"https://register.metoffice.gov.uk/WaveRegistrationClient/public/register.do?service=datapoint"
),
xbmc.LOGERROR,
)
raise

WINDOW.setProperty('WeatherProvider', ADDON.getAddonInfo('name'))
WINDOW.setProperty('WeatherProviderLogo', ADDON_BANNER_PATH)
WINDOW.setProperty('ObservationLocation', ADDON.getSetting('ObservationLocation'))
WINDOW.setProperty('Current.Location', ADDON.getSetting('ForecastLocation'))
WINDOW.setProperty('ForecastLocation', ADDON.getSetting('ForecastLocation'))
WINDOW.setProperty('RegionalLocation', ADDON.getSetting('RegionalLocation'))
WINDOW.setProperty('Location1', ADDON.getSetting('ForecastLocation'))
WINDOW.setProperty('Locations', '1')
window().setProperty("WeatherProvider", addon().getAddonInfo("name"))
window().setProperty("WeatherProviderLogo", ADDON_BANNER_PATH)
window().setProperty(
"ObservationLocation", addon().getSetting("ObservationLocation")
)
window().setProperty("Current.Location", addon().getSetting("ForecastLocation"))
window().setProperty("ForecastLocation", addon().getSetting("ForecastLocation"))
window().setProperty("RegionalLocation", addon().getSetting("RegionalLocation"))
window().setProperty("Location1", addon().getSetting("ForecastLocation"))
window().setProperty("Locations", "1")

# Explicitly set unused flags to false, so there are no unusual side
# effects/residual data when moving from another weather provider.
WINDOW.setProperty('36Hour.IsFetched', '')
WINDOW.setProperty('Weekend.IsFetched', '')
WINDOW.setProperty('Map.IsFetched', '')
WINDOW.setProperty('Weather.CurrentView', '')
window().setProperty("36Hour.IsFetched", "")
window().setProperty("Weekend.IsFetched", "")
window().setProperty("Map.IsFetched", "")
window().setProperty("Weather.CurrentView", "")


if __name__ == '__main__':
if __name__ == "__main__":
main()
95 changes: 58 additions & 37 deletions weather.metoffice/src/metoffice/astronomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Sunrise and Sunset calculations courtesy of Michel Anders
http://michelanders.blogspot.co.uk/2010/12/calulating-sunrise-and-sunset-in-python.html
"""
from math import cos, sin, acos, asin, tan
from math import degrees as deg, radians as rad
from datetime import datetime, time
from math import acos, asin, cos
from math import degrees as deg
from math import radians as rad
from math import sin, tan

from .constants import TZ

Expand Down Expand Up @@ -63,11 +65,11 @@ def __timefromdecimalday(day):
day is a decimal day between 0.0 and 1.0, e.g. noon = 0.5
"""
hours = 24.0*day
hours = 24.0 * day
h = int(hours)
minutes = (hours-h)*60
minutes = (hours - h) * 60
m = int(minutes)
seconds = (minutes-m)*60
seconds = (minutes - m) * 60
s = int(seconds)
return time(hour=h, minute=m, second=s)

Expand All @@ -81,14 +83,14 @@ def __preptime(self, when):
# OpenOffice spreadsheets with days numbered from
# 1/1/1900. The difference are those numbers taken for
# 18/12/2010
self.day = when.toordinal()-(734124-40529)
self.day = when.toordinal() - (734124 - 40529)
t = when.time()
self.time = (t.hour + t.minute/60.0 + t.second/3600.0)/24.0
self.time = (t.hour + t.minute / 60.0 + t.second / 3600.0) / 24.0

self.timezone = 0
offset = when.utcoffset()
if offset is not None:
self.timezone = offset.seconds/3600.0
self.timezone = offset.seconds / 3600.0

def __calc(self):
"""
Expand All @@ -99,37 +101,56 @@ def __calc(self):
sunrise_t, sunset_t and solarnoon_t
"""
timezone = self.timezone # in hours, east is positive
longitude = self.lng # in decimal degrees, east is positive
latitude = self.lat # in decimal degrees, north is positive
longitude = self.lng # in decimal degrees, east is positive
latitude = self.lat # in decimal degrees, north is positive

time = self.time # percentage past midnight, i.e. noon is 0.5
day = self.day # daynumber 1=1/1/1900

jday = day+2415018.5+time-timezone/24 # Julian day
jcent = (jday-2451545)/36525 # Julian century

manom = 357.52911+jcent*(35999.05029-0.0001537*jcent)
mlong = 280.46646+jcent*(36000.76983+jcent*0.0003032) % 360
eccent = 0.016708634-jcent*(0.000042037+0.0001537*jcent)
mobliq = 23+(26+((21.448-jcent*(46.815+jcent*(0.00059-jcent*0.001813))))/60)/60
obliq = mobliq+0.00256*cos(rad(125.04-1934.136*jcent))
vary = tan(rad(obliq/2))*tan(rad(obliq/2))
seqcent = sin(rad(manom))*(1.914602-jcent*(0.004817+0.000014*jcent)) +\
sin(rad(2*manom))*(0.019993-0.000101*jcent)+sin(rad(3*manom))*0.000289
struelong = mlong+seqcent
sapplong = struelong-0.00569-0.00478*sin(rad(125.04-1934.136*jcent))
declination = deg(asin(sin(rad(obliq))*sin(rad(sapplong))))

eqtime = 4*deg(vary*sin(2*rad(mlong))-2*eccent*sin(rad(manom)) +
4*eccent*vary*sin(rad(manom))*cos(2*rad(mlong)) -
0.5*vary*vary*sin(4*rad(mlong))-1.25*eccent*eccent*sin(2*rad(manom)))

hourangle = deg(acos(cos(rad(90.833))/(cos(rad(latitude)) *
cos(rad(declination)))-tan(rad(latitude))*tan(rad(declination))))

self.solarnoon_t = (720-4*longitude-eqtime+timezone*60)/1440
self.sunrise_t = self.solarnoon_t-hourangle*4/1440
self.sunset_t = self.solarnoon_t+hourangle*4/1440
day = self.day # daynumber 1=1/1/1900

jday = day + 2415018.5 + time - timezone / 24 # Julian day
jcent = (jday - 2451545) / 36525 # Julian century

manom = 357.52911 + jcent * (35999.05029 - 0.0001537 * jcent)
mlong = 280.46646 + jcent * (36000.76983 + jcent * 0.0003032) % 360
eccent = 0.016708634 - jcent * (0.000042037 + 0.0001537 * jcent)
mobliq = (
23
+ (
26
+ ((21.448 - jcent * (46.815 + jcent * (0.00059 - jcent * 0.001813))))
/ 60
)
/ 60
)
obliq = mobliq + 0.00256 * cos(rad(125.04 - 1934.136 * jcent))
vary = tan(rad(obliq / 2)) * tan(rad(obliq / 2))
seqcent = (
sin(rad(manom)) * (1.914602 - jcent * (0.004817 + 0.000014 * jcent))
+ sin(rad(2 * manom)) * (0.019993 - 0.000101 * jcent)
+ sin(rad(3 * manom)) * 0.000289
)
struelong = mlong + seqcent
sapplong = struelong - 0.00569 - 0.00478 * sin(rad(125.04 - 1934.136 * jcent))
declination = deg(asin(sin(rad(obliq)) * sin(rad(sapplong))))

eqtime = 4 * deg(
vary * sin(2 * rad(mlong))
- 2 * eccent * sin(rad(manom))
+ 4 * eccent * vary * sin(rad(manom)) * cos(2 * rad(mlong))
- 0.5 * vary * vary * sin(4 * rad(mlong))
- 1.25 * eccent * eccent * sin(2 * rad(manom))
)

hourangle = deg(
acos(
cos(rad(90.833)) / (cos(rad(latitude)) * cos(rad(declination)))
- tan(rad(latitude)) * tan(rad(declination))
)
)

self.solarnoon_t = (720 - 4 * longitude - eqtime + timezone * 60) / 1440
self.sunrise_t = self.solarnoon_t - hourangle * 4 / 1440
self.sunset_t = self.solarnoon_t + hourangle * 4 / 1440


"""
Expand Down
Loading

0 comments on commit a9bf315

Please sign in to comment.