Datapoint: Weather API from the MetOffice

I’m working on a little hobby project which needs to know what the weather is going to be. I had a look around and noticed that the MetOffice had released a new API called DataPoint. They have a selection of APIs, including some map overlays and some actual weather data (more on that another day) but I was especially charmed by their text APIs – this is basically the basis of weather forecasts used everywhere :)

To use this API, you need to log in and acquire a key from here: http://www.metoffice.gov.uk/datapoint/support/API. (I had a weird problem where it wasn’t activated right away and they didn’t reply to support email but then it was all working a day later) and supply it when you make the API requests.

Text-based Weather Forecast

This would make a lovely addition to any site where the weather in the immediate or middle-ish future would be an asset (which is basically anywhere in the UK, we have a lot of weather here which is why we talk about it so much!).

First of all, you need to know which region you want the forecast for – the areas don’t change often so I confess that I just pulled the one I wanted from the sample output on the page describing the available sites and how to get a list of them! My examples show site 509 “Yorkshire and the Humber” as that’s where I live.

Here’s a quick PHP script for getting the text-based forcasts – it depends on config.php which just sets $api_key to my API key and therefore I don’t commit it to my repo or paste it into my blog posts.

<?php

require 'config.php';

// text forecast
function print_entry($entry) {
    echo '<i>' . $entry['title'] . '</i> '
	. $entry['$'] . "\n";
}
$forecast_url = 'http://datapoint.metoffice.gov.uk/public/data/'
    . 'txt/wxfcs/regionalforecast/json/509?key=' . $api_key;
$forecast = json_decode(file_get_contents($forecast_url), true);
foreach($forecast['RegionalFcst']['FcstPeriods']['Period'] as $period) {
    if(($period['id'] == 'day1to2') || ($period['id'] == 'day3to5')) {
        if(isset($period['Paragraph'][0])) {
            foreach($period['Paragraph'] as $entry) {
                print_entry($entry);
            }
        } else {
            print_entry($period['Paragraph']);
        }
    }
}

Look out that sometimes you get one text item, and sometimes you get an array of them, this tripped me up a bit to start with! I made the print_entry() function so I could display them the same way regardless. Using this, I get:

Headline: Becoming drier, bright and breezy. Feeling cold.

Today: Rain and misty conditions, with possibly temporarily sleet or snow over the highest ground at first, soon clearing away east. Brighter conditions will follow, with sunny intervals and also scattered showers, showers mainly to the Pennines. Becoming breezy. Maximum Temperature 7C.

Tonight: Scattered showers will persist across the Pennines, some possibly becoming heavy. Elsewhere clear periods and isolated showers. Slight frost developing, leading to a risk of ice on untreated surfaces. Minimum Temperature 0C.

Tuesday: Frosty start, ice risk on untreated surfaces. Across the Pennines scattered sometimes heavy showers will occur, sunny intervals in-between. Areas further east sunny spells and isolated showers. Generally feeling cold. Maximum Temperature 6C.

Outlook for Wednesday to Friday: Generally rather cold or cold. Frosts overnight and a risk of icy stretches. Wednesday and Thursday rain clearing southwards each day, with temporary snow likely. Friday snow showers.

(yes, it’s winter!)

I’m excited that we have this data released – there are a bunch of cool things happening with the .gov.uk stuff and I think this is a nice example. Now I’m looking for reasons to use it so give me a shout if you can think of any good ones … :)

6 thoughts on “Datapoint: Weather API from the MetOffice

  1. Ooh, that’s rather nice.

    Just for interest, here’s a URL that’ll get you the latest weather observation (known as a METAR) from Leeds Bradford Airport:

    http://weather.aero/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=EGNM&hoursBeforeNow=1&mostRecent=true

    It comes in an XML format, but it’s fairly easy to decode.

    Using the same service, you can also get the latest 24 hour forecast (known as a TAF):

    http://weather.aero/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&stationString=EGNM&hoursBeforeNow=1&mostRecent=true

    Changing the “stationString” attribute to the ICAO code for the relevant airport (Heathrow is EGLL, for example) in either query will allow you to get weather for any civil airport in the world.

    Documentation for the service is here: http://weather.aero/tools/dataservices/textdataserver

  2. I del with an API that sometimes gives me an item and sometimes an array of items for nearly all its endpoints. I generally turn the single results into arrays and then work form there in the rest of the app.

    i.e. I would do this:

    [code]
    if(!isset($period[‘Paragraph’][0])) {
    $t = $period[‘Paragraph’];
    $period[‘Paragraph’] = array($t);
    }
    [/code]

  3. Pingback: Building on Datapoint: Weather With Icons | LornaJane

  4. Pingback: The weather from the Met Office with Datapoint |

  5. Pingback: The weather from the Met Office with Datapoint | Apple Crumble And Custard

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.