Coder Social home page Coder Social logo

ejep / datapoint-python Goto Github PK

View Code? Open in Web Editor NEW
43.0 43.0 27.0 637 KB

A Python module for accessing UK weather data via the Met Office's open data API known as Datapoint.

License: GNU General Public License v3.0

Python 100.00%
hacktoberfest

datapoint-python's People

Contributors

alexhilson avatar deparkes avatar dependabot-preview[bot] avatar ejep avatar jacobtomlinson avatar lgraham1 avatar mrharcombe avatar pelson avatar rprudden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

datapoint-python's Issues

Require point to be within a maximum distance of a forecast site

Currently requesting data for coordinates which are outside the UK results in misleading data or no data (see #15, #37). Once haversine is implemented (issue #10, pull request #69) the distance from the requested location to the nearest site will be known in kilometres. The problem of the requested point being out of bounds would be solved by requiring that the point be within a set distance of the nearest forecast site. @jacobtomlinson do you have an idea of what a suitable distance would be?

simple_forecast falling over

Running simple_forecast.py with

site = conn.get_nearest_forecast_site(52.603370,-2.1413670)

gives the following error

image

Version 0.7.0

Renaming functions in #77 and limiting the distance of sites for responses (#74, #76) have the potential to break things. Along with some bug fixes, this seems like a good time to move to version 0.7.0

@jacobtomlinson what are your thoughts?

Improve Forecast logic

Currently Forecast.now() returns the latest time step on the day of the forecast which is in the past. If this does not exist, a problem near midnight, this is worked around. E.g. if it is currently 16:00, the 15:00 forecast will be returned. If it is 17:45 the 15:00 forecast will be returned, not the 18:00

I think better logic would be to return the nearest forecast in time, whether that is in the future or the past. This should cope with the midnight issue (#19) as the second day in the forecast (days[1]) could be checked.

Status of maintenance

I created this project a few years ago to simplify accessing the Met Office Datapoint API for my projects. I was working in IT Support at the Met Office at the time and specifically wanted it to automate some vanity features like changing wallpaper automatically based on the weather. Despite being employed at the Met Office this is an unofficial and unsupported way of accessing the APIs, hence why it lives under my personal github namespace.

Around 4 years ago I moved to the @informatics-lab in the Met Office and my maintenance on this project decreased with time as I was not actively using it in my day to day activities.

Recently @EJEP picked up maintenance of the project and has been extremely helpful. I've only weighed in from time to time with some project direction.

I've recently left the Met Office and therefore my interest in this project has decreased even further. I know it is a dependency of other projects such as @home-assistant and so I would like to see it maintained, but I just can't justify this activity myself any longer.

Therefore I would like to discuss our options here. Would @EJEP like to take on sole ownership of the project? If so should it be transferred to your GitHub account? If not then what other options are there?

Convert documentation to ReStructured Text

The documentation has quite a few tables in, which are written as html in the markdown files. These are rather hard to read. ReStructured Text has a built in table syntax which is easier to read. Changing this would not require changing the documentation text. It would require changing the documentation build system to use sphinx

Versioned changelog

There are tools which can help automate the change log between versions.This would be nice to have.

Implement haversine

Within the function _distance_between_coords in the Manager object I initially implemented a haversine function for finding the nearest forecast site. However it didn't work properly and so I replaced it with a far simpler formula.

Despite this being more acurate than my original broken haversine function it probably isn't 100% correct. It should be replaced with a correct haversine function.

Remove python 2.6?

Python 2.6 was end-of-lifed some time ago. @jacobtomlinson do you have any objections to removing it from the listed versions supported and from the tests? This probably ought to be done in a point release.

Cache Sites

Once the site list has been requested there is no need to request it again as it is unlikely to change.

The get_all_sites() function should cache the site list for a specified amount of time.

There is no guidance in the DataPoint documentation on how frequently this list changes, but I expect it to be so infrequently that caching the list for up to 24 hours wouldn't be overkill. To keep it responsive though I'd be tempted to suggest 1 hour would be fine.

Clean up weather type results (night/day)

It seems that the API occasionally returns weather values which are specified as "night" or "day" (e.g "hail shower night" or "hail shower day") but at the wrong time of day. Usually around changeover times, it sometimes stays as night until around 9am, 3 hours after sunrise. And very occasionally returns a day value in the middle of the night.

I can imagine two solutions for this:

A) Calculate when sunrise/sunset is and just correct the values if they are wrong.
This remains inline with the API and just cleans the data.

B) Strip the day/night from weather types completely (both "hail shower night" and "hail shower day" would just become "hail shower"). Then calculate day/night and store as a separate attribute.
Allows most flexibility when using the data.

Make timesteps iterable

It would be nice to be able to get every element in a timestep easily, for instance

for element in timestep:
    print(element.value)

Next version

@jacobtomlinson The observation code is now finished (pull request #53). Along with swapping longitude and latitude (pull request #31) and adding timeouts (a minor change in __call_api, pull request #54) there are probably enough changes to justify a bump in version number to 0.5.0, especially since #31 is not backwards compatible. What are your thoughts?

There are also some other things to do like:

  • The PyPi page doesn't list python versions 3.5 and 3.6 which are tested by Travis. This was correct in the master setup.py
  • The version of the requests module used in travis can be increased
  • python 3.7 could be tested against.
  • change longitude and latitude in observation code to ensure consistency
  • Add unit test for observation
  • Update object image in documentation to include observations

I will list anything else I think of over the next few days.

Retry connection

Occasionally the connection to the datapoint API fails. It would be nice if this module could handle retrying the connection. The requests module has support for this.

Raise errors for Forcast errors

At present errors with Forecast functions result in a return value of None, which causes later errors. Fatal errors should instead raise errors, like is done for some other types of error in Manager.

AttributeError: 'bool' object has no attribute 'weather'

Hi,

Having updated the API key and location, I am running current_weather.py but get the following error:

Worthing (Beach)
Traceback (most recent call last):
File "current_weather.py", line 21, in
print now.weather.text
AttributeError: 'bool' object has no attribute 'weather'

I then tried with:

current_timestep = forecast.now()
print current_timestep.weather.text

And that gives an error too:

Worthing (Beach)
Traceback (most recent call last):
File "temp.py", line 22, in
print current_timestep.weather.text
AttributeError: 'bool' object has no attribute 'weather'

I've been investigating and can't work out what is wrong. I saw a recent bug due to the forecast.now problem but this seems to have been resolved and I do have the latest version.

Any ideas what might be wrong?

Rob

Ugly element mapping

Despite returning basically the same data types for a 'Day', 'Night' and '3hourly' timestep Datapoint feels the need to provide different keys for the same thing.

For example Probability of Precipitation will be 'Pp' if you requested 3hourly. But if you request Daily you will get 'PPd' in the Day timestep and 'PPn' in the Night timestep. And it is not even consistent with some things being the same (Visibility is always 'V') and some being different again e.g Humidity is either 'H' for 3hourly, or 'Hn' and 'Hm' for 'Humidity Noon' and 'Humidity Midday'.

The only difference between the data returned for any of the timesteps is that you don't get UV data for a 'Night' timestep in a Daily. Even though you do for midnight in a 3hourly.

Also Temperature (and Feels Like Temperature) in a 3hourly is the average temperature for that timestep but in a daily it's the minimum temperature for the Night timestep and maximum temperature for the Day timestep.

To work around this the Manager object holds a hardcoded array of mappings and has some additional logic to read the correct array items into the Timestep object properties.

This makes the code a but ugly and overly complicated. I'd like to neaten this up if possible.

get_all_sites returns none rarely

Sometimes 'None' is returned from get_all_sites in Manager.py. This has happened a few times, but I have not managed to reproduce it in code trying to look at the bug. I suspect that it is related to the way I am trying to retry connections in code outside this module.

My (unconfirmed) suspicion is that the sites_last_update_time is set, then there is a connection error which is raises an exception. manager.sites_last_request is then never changed from None.

If the same connection is then used (as in my code), the sites list is not updated by get_all_sites as very little time has passed.

Typecasting - Timestep name

The Timestep object currently has a 'name' property. The maps directly to the 'name' property returned from Datapoint and is the number of minutes after midnight (UTC) the timestep starts.

This should probably be converted into a datetime object and perhaps be renamed to something like 'date' or 'time'

Error with Python 2.7

Hi I got this error when trying to reproduce one of the examples.
AttributeError Traceback (most recent call last)
in ()
----> 1 print now.weather.text

AttributeError: 'NoneType' object has no attribute 'weather'

KeyError for certain locations

This example is using Zurich, Switzerland with the coordinates from the Metoffice website.

$ python3
Python 3.6.6 (default, Jul 19 2018, 14:25:17) 
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datapoint
>>> conn = datapoint.connection(api_key="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
>>> site = conn.get_nearest_site(47.383, 8.567)
>>> forecast = conn.get_forecast_for_site(site.id, "3hourly")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fab/Documents/repos/ha/home-assistant/lib64/python3.6/site-packages/datapoint/Manager.py", line 217, in get_forecast_for_site
    forecast.continent = data['SiteRep']['DV']['Location']['continent']
KeyError: 'Location'
>>> 

It works for Paris and London.

Original issue: home-assistant/core#17492

Error experienced when requesting data at midnight

From log file for site Clifton Hill Ski Slope Exeter

Tue Dec  2 21:00:03 2014 - Partly cloudy (night)
Tue Dec  2 22:00:02 2014 - Partly cloudy (night)
Tue Dec  2 23:00:01 2014 - Partly cloudy (night)
Unexpected error: <type 'exceptions.NameError'> on line 64
Wed Dec  3 01:00:01 2014 - Clear night
Wed Dec  3 02:00:03 2014 - Clear night
Wed Dec  3 03:00:04 2014 - Clear night

This issue happens every day when requesting data at midnight.

Longitude and Latitude

If longittude and latitude are the wrong way round, it returns st margarets bay beach as that is furthest point on edge england. Could make a check to make sure that this long/lat it actually in UK else return error.

Typecasting - Day date

The date property for a Day object should probably be a datetime object.

Currently it is a string returned from datapoint in the format yyyy-mm-ddZ.

Better error when API key wrong

If you get the API key wrong, you get an obscure message:

>>> import datapoint
>>> 
>>> # Create datapoint connection
... conn = datapoint.Manager(api_key="aaa")
>>> 
>>> site = conn.get_nearest_site(-0.124626, 51.500728)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
  File "/Users/pelson/miniconda/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Read the docs webhook

@jacobtomlinson, setting up the webhook needs access to the settings page of the repository on GitHub, which I think only the owner has. From the readthedocs documentation all that needs doing is copying the URL from the Admin->Integrations page on the read the docs settings to the GitHub integrations settings section and ticking a few boxes.

Timeouts

Please add timeout to all requests. Thanks.

Timesteps are wrong way around in daily forecast

In the daily forecast, there are two timesteps per day. The first timestep in the list is later than the second. The first is for 12:00 and the second is for 00:00. This should be the other way around. This looks like it is related to the way that the data is supplied by the API.

Forecast now function allow "plus an hour"

Currently the forecast object has a method called now() which returns the current timestep from that forecast.

It would be good to be able to provide a datetime object as an argument to return different timesteps.

Tidy functions for sites

At the moment the names of the functions for getting sites are untidy: The ones for observations mention observations in the name; the ones for forecasts don't and therefore appear global.

Tidying this would make it more obvious what the functions do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.