PHP Weather

PHP Weather is a script written in PHP, that will decode a METAR weather report. Every hour a round the clock airports make a METAR-report where they measure things like the temperature, the wind speed and direction etc. This information is available on the Internet here. Choose a country and then a observing location and you'll get the latest report. It even caches the METARs in a database so subsequent request for the same station will be served as fast as possible.

But the report is not just saved in plain-text. Its coded in a special code, so it has to be decoded before you can use it. This is what PHP Weather is for, decoding a METAR into plain-text, so you can use for something useful.

Your version: 1.58

Your source was last modified at 2001/07/17 11:40:37UTC. Click here to download the newest version from SourceForge. I'm currently working on the next version of PHP Weather. You can try it by downloading a snapshot of CVS, but as I don't make these all the time, the best way to follow the development is to check it out of CVS. It's all explained on the SourceForge page.

If you are having problems with PHP Weather, then please upgrade to the latest version. If that doesn't help, then ask for help at the maillinglist, by sending a mail to phpweather@gimpster.com. To receive the answer you have to subscribe by sending a mail to this address: phpweather-subscribe@gimpster.com. There's also two maillinglists at SourceForge, but these aren't used much right now. When I get the time, I'll move all the pages from gimpster.com to phpweather.sourceforge.net, and then we'll start using the maillinglists at SourceForge for real...

To help speed up the development, you should take a look at the latest code from CVS. Try and see if you can figure out what is happening in there, and then tell me what you think. Or even better: create a user-account at SourceForge, and mail me the username. I'll then add you to the project, so that we can change the code yourself.

A sample METAR-report

The report below is the latest from Aalborg, Denmark (this is where I live). The raw METAR looks this way:

EKYT 090350Z 17001KT 3500 BR BKN008 OVC010 M06/M06 Q1011 76190095

Not exactly a pretty sight? Well by using PHP Weather you could also present the information like this:

25 minutes ago, at 03:50 UTC, the wind was blowing at a speed of 0.5 meters per second (1.2 miles per hour) from South in Aalborg, Denmark. The temperature was -6 degrees Celsius (21 degrees Fahrenheit), and the pressure was 1,011 hPa (29.85 inHg). The relative humidity was 100.0%. There were broken clouds at a height of 244 meter (800 feet) and overcast at a height of 305 meter (1000 feet). The visibility was 3.5 kilometers (2.2 miles). Current weather is Moderate Mist . There was 1,572.26 mm (61.90 inches) of precipitation in the last 24 hours.

Here is the same piece of text with the current weather in Honolulu, Hawaii. This time the information is presented in Spanish. First comes the raw METAR:

PHNL 090353Z 03009KT 10SM FEW029 26/16 A3010 RMK AO2 SLP191 T02560156

and then the pretty-printed output:

22 minutos atrás, de las 03:53 UTC, el viento soplaba a una velocidad de 4.6 metros por segundo (10.4 millas por hora) provenientes del Norte/Noreste en Honolulu, Hawaii. La temperatura era de 26 grados Celsius (79 grados Fahrenheit), y la presión de 1,019 hectopascales (hPa) (30.10 milibares (inHg)). La humedad relativa era de 54.1%. Se observaban un poco nubes a una altura de 884 metros (2900 pies). La visibilidad era de 16.1 kilometros (10.0 millas).

The only thing I changed between the two pieces of code was the identifier of the weather station, and the include-file with the strings used by PHP Weather. The identifier for Aalborg, Denmark is EKYT and the one for Honolulu, Hawaii is PHNL.

Try it out for yourself - choose a city and a language from the lists and you'll see the current weather for the city you selected:

20 minutes ago, at 03:55 UTC, the wind was blowing at a speed of 3.1 meters per second (6.9 miles per hour) from East in Thule A. B., Greenland. The temperature was -20 degrees Celsius (-4 degrees Fahrenheit), and the pressure was 1,017 hPa (30.03 inHg). The relative humidity was 43.9%. The sky was clear. The visibility was >11.3 kilometers (>7 miles).

The METAR for Thule A. B., Greenland, presented in English, was:

BGTL 090355Z AUTO 09006KT 9999 CLR M20/M24 A3003 RMK AO2 SLP184 T12041235 411171213

Using PHP Weather

Using PHP Weather is very simple. You can see how I've made this page by taking a look at the source. First you have to include the file phpweather.inc in your page. Then you call the function get_metar($station) with the four-character station-identifier. This gives you the METAR, which you can then feed to process_metar($metar). This function return an array that contains the various parts of the METAR in decoded form. They are also returned in both empirical (feet, miles, degrees of Fahrenheit, etc.) and metric units (meters, kilometers and degrees Celsius).

This code is all that is necessary to make PHP Weather work:

<?php
include('locale_en.inc');
include(
'phpweather.inc');
$metar get_metar('EKYT');
$data process_metar($metar);
$temp $data['temp_c'];
echo 
"The temperature is $temp degrees Celsius.";
?>

That's it! The above code will tell you what the temperature is in Aalborg, Denmark. To make the examples above I've made a function called pretty_print_metar(). You use it like this:

<?php
$metar 
get_metar('EKYT');
pretty_print_metar($metar'Aalborg, Denmark');
?>

If you wan't to see all the data in the METAR, then try the following code:

<?php
$data 
process_metar(get_metar('EKYT'));
echo 
"<pre>\n";
print_r($data);
echo 
"</pre>\n";
?>

But please note that you'll only find print_r() in PHP4.

Caching the METARs

But you'll soon start to look for a way to improve the response-time of your script. To do this, you first have to decide what database you want to use. You have the choice of setting up a a MySQL, PostgreSQL, an Oracle 8, or a DBM database. You have to edit phpweather.inc to choose the database, just set the appropriate variable to 1 ($useMySQL, $usePSQL, $useOCI, or $useDBM).

If you choose to use MySQL, then create a table called metars with the following create-information:

CREATE TABLE metars (
  metar VARCHAR(255) NOT NULL,
  timestamp TIMESTAMP(14),
  station VARCHAR(4) NOT NULL,
  PRIMARY KEY (station),
  UNIQUE station (station)
);

If you want to use a PostgreSQL database, then create the table this way:

CREATE TABLE metars (
  metar VARCHAR(255) NOT NULL,
  timestamp TIMESTAMP,
  station VARCHAR(4) PRIMARY KEY NOT NULL
);

If you have access to an Oracle 8 database, you should create the table with this SQL statement:

create table metars (
  metar varchar2(255) not null,
  timestamp date,
  station varchar2(4)
);
alter table metars add primary key (station);

Remember that you have to connect to the database by commenting-out the code found in phpweather.inc.

If you don't do this, you'll recieve a lot of errors, saying things like: "MySQL Connection Failed: Access denied for user: 'nobody@127.0.0.1' (Using password: NO) in phpweather.inc" and "Supplied argument is not a valid MySQL result resource in phpweather.inc". These errors are trying to tell you, that PHP Weather couldn't store the METAR in the MySQL-database, because you didn't supply a valid username and password.

If you want to use a DBM database, then all you need to do, is to make sure that the user running the webserver has write-permission to the current directory.

PHP Weather will then store the retrieved METARs in the database, and use the cached METAR if it's less that 1 hour old. If it's older, it is expected that the station has made a new observation, so we should update our data.

Using PHP Weather with WAP

PHP Weather can also be used to serve current weather information to WAP-enables mobile phones. To do this, just put the file wap.php in the same directory as phpweather.inc and then point your WAP-browser on your mobile phone to the page. It should then show you the current weather in Aalborg, Denmark.

The format used in the wap.php-page is a smaller and more compact format than the one shown on this page. It looks like this:

Aalborg, 25 min ago
Wind: 0.5 mps S
Temp: -6 C
Clouds: 5/8 - 7/8

Related information

PHP Weather at Sourceforge.net

PHP Weather has moved to SourceForge. So please check these pages for new versions etc.

Federal Meteorological Handbook No. 1, Chapter 12 Coding

This is the official specification on the METAR-encoding scheme. If you want to learn how to read the raw coded messages, or want to make a parser yourself, you should read this. It might seam a bit complicated at first sight, but when you've read it a couple of times things start to clear up :-)

Geo::METAR

Geo::METAR is written by Jeremy D. Zawodny, and is the Perl module that I used as a template for PHP Weather. I searched the web for a PHP-script that could translate a METAR, but instead I found Geo::METAR. When looking at the Perl-code I realised that I could just translate the Perl-code into PHP-code. So I did, and the result is PHP Weather.

METAR Data Access

Here you'll find the raw METAR data. In PHP Weather I download the reports from http://weather.noaa.gov/pub/data/observations/metar/stations/.

To use any of these services you have to know the four-character ICAO Location Indicator for the station. The easiest way to find the Location Indicator is to go to this page. There you'll be able to choose a country, and the choose a station from a list of stations is that country.