Easily post an aviation TAF report into your WordPress post or page with shortcode

A few weeks ago I posted some code that would retrieve an aviation metar from the NOAA website and display it within your WordPress blog post or page. As expected, it was followed with a few emails asking how we could output a TAF (or Terminal Aerodrome Forecast) instead. Using the same recycled code, it’s easily accomplished.

For all you non-aviation folk, a TAF is a format for reporting weather forecast information normally associated with a specific radius from an aerodrome. It is usually valid for a specified time period – usually from 3 to 24 hours. The validity of the report is included in the first line of the report.

The function is handy if you build a page that lists reports (perhaps in company with a Metar report) associated with your local airport. If you’re hosting an aviation event or airshow, it’s also nice to display weather.

As always, there are two functions you can choose from. The first function will retrieve the TAF and simply display it on your website. As we’ve previously discussed, making repeated attempts to call the NOAA website may be deemed to be abuse (particularly if you have a busy website) so it’s better to locally cache the results for a specified time period. The second function below will do this using the WordPress Transient API.

Shortcode is the easiest way of including dynamic functionality into your blog so this is how the two functions are presented.

Example TAF report for Sydney (YSSY):

TAF FOR YSSY (Issued: Wed, October 30th 2013 6:30 AM UTC):TAF YSSY 300506Z 3006/3112 13015KT 9999 FEW035 FM301000 06008KT 9999 FEW030 FM302000 35005KT 9999 SCT025 FM310100 08015KT 9999 SCT035 FM310900 12008KT 9999 -SHRA FEW015 BKN025

This is the most basic function. It will not cache results.

function get_taf($atts, $content = null) {
  extract(shortcode_atts(array(
    'loc' => 'YSSY'
  ), $atts));
$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$loc.TXT";
	$taf = '';
	$fileData = @file($fileName) or die('TAF not available');
	if ($fileData != false) {
		list($i, $date) = each($fileData);

		$utc = strtotime(trim($date));
		$time = date("D, F jS Y g:i A",$utc);

		while (list($i, $line) = each($fileData)) {
			$taf .= ' ' . trim($line);
			}
		$taf = trim(str_replace('  ', ' ', $taf));
		}
	return "
TAF FOR $loc (Issued: $time UTC):
$taf
"; } add_shortcode('taf', 'get_taf');

The preferred function is below. It will cache the results for a time period as specified. It may be worth caching the results for the validity of the report or simply for one hour. By default, it will cache the results for 60 minutes although you can alter this by passing the parameter of time=”something” as detailed in the shortcode example below.

You will want to paste this code into your WordPress theme’s functions.php file.

function get_taf($atts, $content = null) {
  extract(shortcode_atts(array(
    'loc' => 'YSSY',
    'time' => '3600'
  ), $atts));

$transient = "$loc.taf";
$dbresult =  get_transient($transient);

if ($dbresult == true  )
	{
		return $dbresult;
	} else {

	$fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$loc.TXT";
	$taf = '';
	$transient = "$loc.taf";
	$fileData = @file($fileName) or die('TAF not available');
	if ($fileData != false) {
		list($i, $date) = each($fileData);

		$utc = strtotime(trim($date));
		$time = date("D, F jS Y g:i A",$utc);

		while (list($i, $line) = each($fileData)) {
			$taf .= ' ' . trim($line);
			}
		$taf = trim(str_replace('  ', ' ', $taf));
		}
	$result = "
TAF FOR $loc (Issued: $time UTC):$taf
"; // 3600 will store the transient result for 1 hour set_transient($transient, $result, $time); return $result; } } add_shortcode('taf', 'get_taf');

You can output your default TAF (as specified in the function) using the shortcode of:

[taf]

If you want to generate a report of another airport (remembering that it has to be listed on the NOAA website), use the following format:

[taf loc=”YBBN”]

If you find this code useful, it would be nice to point others to the source.

Blue skies!



Related posts:
  1. Easily post an aviation Metar report into your WordPress post or page with shortcode
  2. Output METAR Data on your Webpage with PHP
  3. WordPress Shortcode for FMA Annunciations, FMC Messages & the MCP
  4. This Day in Aviation History on Your Website
  5. Metar to Twitter – Free service
  6. Korean Airlines Internal Audit Report – an airline waiting to happen
  7. Add Episodes of Flight Podcast to your website or blog

About Marty

Marty is an International airline pilot, commercial helicopter pilot and experienced flight instructor. He is also the Director of a media company based in Sydney, Australia. Connect with Marty on Twitter, Flight Podcast or Google+.

Trackbacks

  1. [...] used to source and manipulate data. We’ve previously provided shortcode that would render a TAF or METAR in your post. Shortcode can be tricky if you’re not overly tech savvy, so if you [...]

Please leave a comment or question

*