Big Data/Analytics Zone is brought to you in partnership with:

Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with phpwomen.org and her local user group PHP North West, and writes for a variety of outlets, including her own blog at lornajane.net. She is an active member of the PHP and open source communities and contributes to the joind.in event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 77 posts at DZone. You can read more from them at their website. View Full User Profile

Datapoint: Weather API from the MetOffice

12.05.2012
| 3465 views |
  • submit to reddit

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. 


Published at DZone with permission of Lorna Mitchell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)