Over a million developers have joined DZone.

Datapoint: Weather API from the MetOffice

DZone's Guide to

Datapoint: Weather API from the MetOffice

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

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.

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) {
        } else {

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. 

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}