Page 1 of 3

UK Extremes from the MetOffice

Posted: Mon 02 Mar 2015 4:50 pm
by mcrossley
I'm playing around with a little PHP script that gets regional and UK wide extremes for a limited set of readings from the Met Office.

It is part of their DataPoint offering, you have to sign up to get an API code, but the sign up and data is free.

I am using this dataset: http://www.metoffice.gov.uk/datapoint/p ... servations

And I have it running on this page in the right-hand side bar: https://weather.wilmslowastro.com/today_yest.php
I need to sort out the text lengths somehow...

I can make the PHP class available if people are interested (once I have finalised the code).
As is often the case the data is supplied in a format that is convenient to generate, but no so convenient to use. If you want a specific value for a specific region, you have to walk the regions until you find the right one, then walk the extremes until you find the one you want.

So my code reformats the data so you can index directly in to specific regions and values.

Re: UK Extremes from the MetOffice

Posted: Mon 02 Mar 2015 7:23 pm
by mcrossley
Actually this is real test page - just a basic demo of the data: https://weather.wilmslowastro.com/test/ ... mes24h.php

Re: UK Extremes from the MetOffice

Posted: Mon 02 Mar 2015 10:37 pm
by duke
I was only looking at the wealth of free data that they offer at the weekend.
mcrossley wrote: I can make the PHP class available if people are interested (once I have finalised the code).
I'm sure there will be a few of us ;)

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 12:35 am
by water01
Me included!!

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 10:04 am
by mcrossley
Give this a whirl - I'm a bit busy at the mo so limited support. Version 0.2

Warning, there isn't much error checking in there yet!

EDIT: Updated the test file to add example cookie handling.
EDIT2: Updated the class file, if the cache file is stale, but there is no new data it now just performs a touch to reset the cache timer rather than rewriting the file. Ver 0.2
EDIT3: (17 Sept 18) updated sample PHP page to use a newer version of JQuery

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 5:44 pm
by jdc
That is rather good!

I had a wee play with it this afternoon, simple setup and it's working fine. Neat.

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 7:19 pm
by duke
Working 'out of the box' here too, thank you Mark :)

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 7:28 pm
by mcrossley
Phew! :lol:

It will be interesting to see how you integrate the data into your web sites.

Now, what else is lurking in DataPoint.... :groan:

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 7:48 pm
by duke
mcrossley wrote: It will be interesting to see how you integrate the data into your web sites.
Tomorrow nights task ;)

Mark, just a thought, is it possible to store the selected region in a cookie so a returning visitor automatically sees their last choice? Presumably in most cases this would be the region they live in.

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 7:53 pm
by jdc
mcrossley wrote:Phew! :lol:

It will be interesting to see how you integrate the data into your web sites.
http://lethamshank.co.uk/met/wx_extremes.php

Just some style then I will fit it somewhere! Unless you write something else to fill the page. :)

Knowing nil about js, how would I select only one region to create a snippet? (eg 'dg')

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 8:14 pm
by mcrossley
duke wrote:Mark, just a thought, is it possible to store the selected region in a cookie so a returning visitor automatically sees their last choice? Presumably in most cases this would be the region they live in.
What, like this?: https://weather.wilmslowastro.com/test/ ... mes24h.php

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 8:31 pm
by mcrossley
jdc wrote:
mcrossley wrote:Phew! :lol:

It will be interesting to see how you integrate the data into your web sites.
http://lethamshank.co.uk/met/wx_extremes.php

Just some style then I will fit it somewhere! Unless you write something else to fill the page. :)

Knowing nil about js, how would I select only one region to create a snippet? (eg 'dg')
John, just look a the test page, where it prints out the UK values...

Code: Select all

echo 'UK Extremes for ' . $data->{'UkExtremes'}->{'extremeDate'} . '<br><br>' . PHP_EOL;

echo 'Region name: ' . $data->{'UkExtremes'}->{'Regions'}->{'uk'}->{'name'} . '<br>' . PHP_EOL;
// Just print out each extreme for the UK region as they are stored in the data blob
foreach ($data->{'UkExtremes'}->{'Regions'}->{'uk'}->{'Extremes'} as $extreme => $value) {
	echo $conv[$extreme] . ' ' . $value->{'$'} . ' ' . $value->{'uom'} . ' ' . $value->{'locationName'} . '<br>' . PHP_EOL;
}
Just change all the instances of {'uk'} to {'dg'}.


You can print individual values rather than using the 'blob' order so...

Code: Select all

$regVals = $data->{'UkExtremes'}->{'Regions'}->{'dg'}->{'Extremes'};
echo 'Max temp = ' . $regVals->{'HMAXT'}->{'$'} . ' ' . $regVasl->{'HMAXT'}->{'uom'} . ' at ' . $regVals->{'HMAXT'}->{'locationName'};
//..etc

Re: UK Extremes from the MetOffice

Posted: Tue 03 Mar 2015 10:36 pm
by jdc
Thank you again for that. I was looking at the wrong end of the script!

Re: UK Extremes from the MetOffice

Posted: Wed 04 Mar 2015 9:24 am
by mcrossley
I have made a minor optimisation to the zip file above, if the cache file is stale, but there is no new data to just touches the file to reset the time rather than re-saving it.

Re: UK Extremes from the MetOffice

Posted: Wed 04 Mar 2015 4:55 pm
by mcrossley
mcrossley wrote:Now, what else is lurking in DataPoint.... :groan:
I have the 5 day summary day/night data for a specified location working now - just need to figure out how to format it nicely like the Saratoga templates do!
I was surprised Wilmslow was on the location list - there are 5000 entries.

Very basic data presentation as a test...
https://weather.wilmslowastro.com/test/ ... recast.php