This website recently got a major rebuild; if you're missing something, let Lorna know.

Building on Datapoint: Weather With Icons



I wrote the other day about the new datapoint API from the MetOffice (there were some great links to other weather APIs in the comments, if you like weather). I've been using it to create a detailed forecast of the weather over the next few days, mixing in some lovely weather icons by Adam Whitcroft, from The Noun Project (the same site that the icons on my own site came from) - so I have something like this for each kind of weather:

noun_project_2590

To achieve this, I hit the datapoint API from PHP (but if you use any other scripting language this should be easy enough to follow):

<?php

$leeds_site_id = '352241';
$fcast_url = 'http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/' . $leeds_site_id . '?res=3hourly&key=' . $api_key;
$fcast = json_decode(file_get_contents($fcast_url), true);
foreach($fcast['SiteRep']['DV']['Location']['Period'] as $day) {
    $fdate = new DateTime($day['value']);
    echo $fdate->format('D jS M') . "\n";;
    foreach($day['Rep'] as $slot) {
        if($slot['$'] > 0) {
            // makes 3, 12 into 0300 and 1200 respectively
            echo str_pad(($slot['$'] / 60) . "00", 4, "0", STR_PAD_LEFT);
        } else {
            // avoid divide by zero
            echo "0000";
        }
        echo ": "
            . "<img src="images/" /> "
            . $weathers[$slot['W']] . "<br />\n";
    }
}

// lovely nounproject icons need some attribution
echo $attr;

I live in Leeds so I've used the site_id for that city (annoyingly they don't seem to document them, just request all locations for one time period and then grab whichever one you want is my advice). When you build the URL like this, the response is in JSON format so I convert that quickly to an array and then foreach over the various forecasts. One challenge is figuring out what time the forecast relates to, I'm certain this can't be the most elegant solution but it does seem to work!

I also added pretty images, by taking the list of weather types and grabbing some icons to work with those. So my HTML output ends up (not styled because I'm pasting it into the middle of my blog, but you get the idea) looking like this:

Tue 18th Dec
1500: image1 Overcast
1800: image2 Cloudy
2100: image3 Cloudy
Wed 19th Dec
0000: image4 Mist
0300: image5 Overcast
0600: image6 Overcast
0900: image7 Overcast
1200: image8 Overcast
1500: image9 Heavy rain
1800: image10 Heavy rain
2100: image11 Heavy rain
Thu 20th Dec
0000: image12 Heavy rain
0300: image13 Heavy rain
0600: image14 Heavy rain
0900: image15 Heavy rain
1200: image16 Light rain
1500: image17 Heavy rain
1800: image18 Light rain
2100: image19 Overcast
Fri 21st Dec
0000: image20 Cloudy
0300: image21 Cloudy
0600: image22 Cloudy
0900: image23 Cloudy
1200: image24 Cloudy
1500: image25 Partly cloudy (day)
1800: image26 Partly cloudy (night)
2100: image27 Partly cloudy (night)
Sat 22nd Dec
0000: image28 Cloudy
0300: image29 Cloudy
0600: image30 Cloudy
0900: image31 Light rain
1200: image32 Heavy rain
1500: image33 Heavy rain
1800: image34 Light rain
2100: image35 Light rain
Icons by Adam Whitcroft, from The Noun Project

This is a nice way of illustrating one of the data fields that comes from the datapoint API, and I love the excellent icons, they are wonderfully clear and well made (most of the icons on this site are noun project icons). In fact you could use any of the various data points with any output mechanism you please ... my next datapoint post will probably be about how I use this data with my blink(1) :)


In: tech
Tags: #datapoint #icons #noun project