Code Monkey home page Code Monkey logo

metoffer's Introduction

metoffer v.2.0

metoffer is a simple wrapper for the API provided by the British Met Office known as DataPoint. It can be used to retrieve weather observations and forecasts. At its heart is the MetOffer class which has methods to retrieve data available through the API and make them available as Python objects. Also included are a couple of functions and classes useful for interpretting the data.

This project is now maintained at https://github.com/sludgedesk/metoffer.

Example

Get forecast for Met Office site closest to supplied latitude and longitude, the forecast to be given in three-hourly intervals:

>>> import metoffer
>>> api_key = '01234567-89ab-cdef-0123-456789abcdef'
>>> M = metoffer.MetOffer(api_key)
>>> x = M.nearest_loc_forecast(51.4033, -0.3375, metoffer.THREE_HOURLY)

It's worth noting here that, if you expect many requests for forecast data to be made, it is probably better to use the functions called by this convenience function so that data that does not change often (e.g. data about Met Office sites) may be cached.

Parse this data into a metoffer.Weather instance:

>>> y = metoffer.Weather(x)
>>> y.name
'HAMPTON COURT PALACE'
>>> y.country
'ENGLAND'
>>> y.continent
'EUROPE'
>>> y.lat
51.4007
>>> y.lon
-0.3337
>>> y.elevation
4.0
>>> y.ident # The Met Office site ident
'351747'
>>> y.data_date
'2014-06-14T23:00:00Z'
>>> y.dtype
'Forecast'
>>> import pprint
>>> pprint.pprint(y.data)
[{'Feels Like Temperature': (17, 'C', 'F'),
  'Max UV Index': (1, '', 'U'),
  'Precipitation Probability': (7, '%', 'Pp'),
  'Screen Relative Humidity': (63, '%', 'H'),
  'Temperature': (19, 'C', 'T'),
  'Visibility': ('VG', '', 'V'),
  'Weather Type': (7, '', 'W'),
  'Wind Direction': ('NNE', 'compass', 'D'),
  'Wind Gust': (18, 'mph', 'G'),
  'Wind Speed': (11, 'mph', 'S'),
  'timestamp': (datetime.datetime(2014, 6, 14, 18, 0), '')},
 {'Feels Like Temperature': (15, 'C', 'F'),
  'Max UV Index': (0, '', 'U'),
  'Precipitation Probability': (0, '%', 'Pp'),
  'Screen Relative Humidity': (72, '%', 'H'),
  'Temperature': (16, 'C', 'T'),
  'Visibility': ('VG', '', 'V'),
  'Weather Type': (0, '', 'W'),
  'Wind Direction': ('NNE', 'compass', 'D'),
  'Wind Gust': (18, 'mph', 'G'),
  'Wind Speed': (9, 'mph', 'S'),
  'timestamp': (datetime.datetime(2014, 6, 14, 21, 0), '')},

    [...]

 {'Feels Like Temperature': (16, 'C', 'F'),
  'Max UV Index': (0, '', 'U'),
  'Precipitation Probability': (2, '%', 'Pp'),
  'Screen Relative Humidity': (66, '%', 'H'),
  'Temperature': (16, 'C', 'T'),
  'Visibility': ('VG', '', 'V'),
  'Weather Type': (0, '', 'W'),
  'Wind Direction': ('NNE', 'compass', 'D'),
  'Wind Gust': (13, 'mph', 'G'),
  'Wind Speed': (7, 'mph', 'S'),
  'timestamp': (datetime.datetime(2014, 6, 18, 21, 0), '')}]

Interpret the data further:

>>> for i in y.data:
...     print("{} - {}".format(i["timestamp"][0].strftime("%d %b, %H:%M"), metoffer.WEATHER_CODES[i["Weather Type"][0]]))
...
14 Jun, 18:00 - Cloudy
14 Jun, 21:00 - Clear night
15 Jun, 00:00 - Clear night
15 Jun, 03:00 - Cloudy

    [...]

18 Jun, 09:00 - Partly cloudy (day)
18 Jun, 12:00 - Partly cloudy (day)
18 Jun, 15:00 - Cloudy
18 Jun, 18:00 - Cloudy
18 Jun, 21:00 - Clear night
>>> metoffer.VISIBILITY[y.data[0]["Visibility"][0]]
'Very good - Between 20-40 km'
>>> metoffer.guidance_UV(y.data[0]["Max UV Index"][0])
'Low exposure. No protection required. You can safely stay outside'

The MetOffer Class

Available methods:

  • loc_forecast. Return location-specific forecast data (including lists of available sites and time capabilities) for given time step.
  • nearest_loc_forecast. Work out nearest possible site to lat & lon coordinates and return its forecast data for the given time step.
  • loc_observations. Return location-specific observation data, including a list of available sites (time step will be HOURLY).
  • nearest_loc_obs. Work out nearest possible site to lat & lon coordinates and return observation data for it.
  • text_forecast. Return textual forecast data for regions, national parks or mountain areas.
  • text_uk_extremes. Return textual data of UK extremes.
  • stand_alone_imagery. Returns capabilities data for stand alone imagery and includes URIs for the images.
  • map_overlay_forecast. Returns capabilities data for forecast map overlays.
  • map_overlay_obs. Returns capabilities data for observation map overlays.

The Site Class

Describes object to hold site metadata. Also describes method (distance_to_coords) to return a Site instance's 'distance' from any given lat & lon coordinates. This 'distance' is a value which is used to guide MetOffer.nearest_loc_forecast and MetOffer.nearest_loc_obs. It simply calculates the difference between the two sets of coordinates and arrives at a value through Pythagorean theorem.

The Weather Class

A hold-all for returned weather data, including associated metadata. It parses returned dict of MetOffer location-specific data into a Weather instance. Works with single or multiple time steps. There are a couple of points to note:

  • All dict keys have a tuple, even where there is no obvious need, such as with 'timestamp' and 'Weather Type'. 'timestamp' is a 2-tuple, all else is a 3-tuple. This is a feature.
  • When the Met Office does not have a recorded observation against a category, metoffer will return None.
  • For parsed DAILY forecasts, the hours and minutes of the 'timestamp' datetime.datetime object are superfluous. In fact, it would be misleading to follow them. Rather, this time there is a sensible entry in the second part of the tuple. This alternates between 'Day' and 'Night' with each successive dict. The categories are often specific to the time of day. This is how the API provides it. Take note as it may catch you out.

The TextForecast Class

A hold-all for returned textual regional forecasts, including associated meta- data, created by parsing the data returned by MetOffer.text_forecast.

Useful Functions

  • parse_sitelist. Return list of Site instances from retrieved sitelist data.
  • get_nearest_site. Return a list of strings (site IDs) which can be used as 'request' in calls to loc_forecast and loc_observations.
  • guidance_UV. Return Met Office guidance regarding UV exposure based on UV
    index.
  • extract_data_key. Returns a dict that maps measurement type to its description and measurement unit.

Feedback & Bug Reports

Get in touch:

Stephen B Murray <[email protected]> @sludgedesk

Legal

Copyright 2012-2014, 2018 Stephen B Murray

Distributed under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

You should have received a copy of the GNU General Public License along with this package. If not, see <http://www.gnu.org/licenses/>

metoffer's People

Contributors

domdfcoding avatar pecnut avatar sludgedesk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

metoffer's Issues

Pythagoras' theorem wrongly implemented

The brackets are misplaced in Pythagoras' theorem in distance_to_coords:

The function distance_to_coords says

def distance_to_coords(self, lat_a, lon_a):
        self.distance = (abs(self.lat - lat_a) ** 2) + (abs(self.lon - lon_a) ** 2) ** .5

but of course should be

def distance_to_coords(self, lat_a, lon_a):
        self.distance = ((self.lat - lat_a) ** 2 + (self.lon - lon_a) ** 2) ** .5

Currently it's working out "a^2 + โˆšb^2" instead of "โˆš(a^2 + b^2)"

(I've also removed the abs commands which are of course not needed)

Trouble installing metoffer

I am using windows and I cannot install the "metoffer". Do you have any suggestions?
I am getting this "No module named 'metoffer". I have tried pip install metoffer but it doesn't work for me.

pip install doesn't work any more

Since the update, "pip install"ing doesn't appear to work: The MetOffer-2.0.dist-info folder appears in site-packages, but the metoffer.py file doesn't.

Weather method fail when downloading All sites

When downloading all sites in observations the resulting data is a list of sites.

M = metoffer.MetOffer(APIkey)
x = M.loc_observations(request = metoffer.ALL) # same as using 'all'
y = metoffer.Weather(x)
>> TypeError: list indices must be integers or slices, not str

This bit of code on the init method is failing because Location is a list of dictionaries.

returned_data["SiteRep"]["DV"]["Location"]["i"]
>> Error
returned_data["SiteRep"]["DV"]["Location"][0]["i"]
>> Works

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.