Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Aviation METAR or TAF Reports with PHP or WordPress Shortcode

The code on this page will provide all that is necessary to render an aviation METAR or TAF report into your website with PHP or WordPress shortcode. It's the first of a few posts that deals with different ways of dealing with aviation weather. There's another post scheduled that deals specifically with Australian reports - both METAR/TAF reports and the ARFOR.

What is a METAR and TAF? METAR and TAF reports are weather observations written in a global format standardised by the International Civil Aviation Organization (ICAO). Aircrew and operations departments usually make fuel and other operational determinations based on the actual observation (Metar) or the short-term forecast (TAF). The area forecast includes wind information, temperature, cloud, and other weather data that assists in route planning.

If you're using our Metar WordPress plugin, continue to use it. METAR will soon be completely rewritten to include more features than simple shortcode functions might provide.

METAR WordPress Shortcode

The result of [metar loc="klax"] will return the following report.

KLAX 251253Z 24005KT 6SM BR FEW006 SCT013 18/17 A2996 RMK AO2 SLP144 T01830167 $

Available attributes are p and cache. The former if true (p="1") will return the result in paragraph tags (this overcomes some WP issues). The latter defines the period for which the report is cached locally. Since the reports are generally valid for an hour we cache results for 60 minutes by default.

The METAR station must be listed in the NOAA database .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
5
*/
6
 
7
function beliefmedia_metar($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'loc' => 'yssy',
11
    'p' => true,
12
    'cache_temp' => 600,
13
    'cache' => 3600
14
  ), $atts);
15
 
16
  $transient = 'bmmet_' . md5(serialize($atts));
17
  $dbresult =  get_transient($transient);
18
 
19
  if ($dbresult !== false) {
20
  $result = $dbresult;
21
 
22
  } else {
23
 
24
     /* Get text file from NOAA.gov */
25
     $data = @file('http://tgftp.nws.noaa.gov/data/observations/metar/stations/' . strtoupper($atts['loc']) . '.TXT');
26
 
27
     if ($data != false) {
28
 
29
      list($i, $date) = each($data);
30
      while (list($i, $line) = each($data)) $metar .= ' ' . trim($line);
31
      $result = trim(preg_replace('!\s+!', ' ', $metar));
32
 
33
      /* Return and cache .. */
34
      if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
35
      set_transient($transient, $result, $atts['cache']);
36
 
37
      } else {
38
 
39
      /* Data temporarily unavailable */
40
      $result = 'METAR is temporarily unavailable';
41
 
42
      /* Return and cache .. */
43
      if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
44
      set_transient($transient, $result, $atts['cache_temp']);
45
 
46
    }
47
 
48
  }
49
 
50
 return $result;
51
}
52
add_shortcode('metar', 'beliefmedia_metar');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

TAF WordPress Shortcode

The result of [taf loc="kjfk"] will return the following report (I've included it in a blockquote).

TAF AMD KJFK 251301Z 2513/2618 18015KT P6SM SCT025 OVC035 TEMPO 2513/2514 3SM -SHRA BR FM251400 18016G21KT P6SM BKN025 OVC035 FM251700 19016KT P6SM SCT035 BKN050 FM251900 19016KT P6SM VCTS SCT035 BKN050CB FM260000 20012KT P6SM SCT035 BKN050 FM260500 26005KT P6SM SCT050 FM261200 24005KT P6SM SCT050 FM261700 22010KT P6SM SCT050

Available attributes are p and cache. The former if true (p="1") will return the result in paragraph tags (this overcomes some WP issues). The latter defines the period for which the report is cached locally. Since the reports are generally valid for an hour we cache results for 60 minutes by default.

The TAF station must be listed in the NOAA database .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
5
*/
6
 
7
function beliefmedia_taf($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'loc' => 'yssy',
11
    'p' => true,
12
    'cache_temp' => 600,
13
    'cache' => 3600
14
  ), $atts);
15
 
16
  $transient = 'bmtaf_' . md5(serialize($atts));
17
  $dbresult =  get_transient($transient);
18
 
19
  if ($dbresult !== false) {
20
  $result = $dbresult;
21
 
22
  } else {
23
 
24
    /* Get text file from NOAA */
25
    $data = @file('ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/' . strtoupper($atts['loc']) . '.TXT');
26
 
27
    if ($data != false) {
28
 
29
      list($i, $date) = each($data);
30
      while (list($i, $line) = each($data)) $taf .= ' ' . trim($line);
31
      $result = trim(preg_replace('!\s+!', ' ', $taf));
32
 
33
      /* Return and cache .. */
34
      if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
35
      set_transient($transient, $result, $atts['cache']);
36
 
37
      } else {
38
 
39
      /* Data temporarily unavailable */
40
      $result = 'TAF is temporarily unavailable';
41
 
42
      /* Return and cache .. */
43
      if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
44
      set_transient($transient, $result, $atts['cache_temp']);
45
 
46
    }
47
 
48
  }
49
 
50
 return $result;
51
}
52
add_shortcode('taf', 'beliefmedia_taf');

PHP Functions

Used outside of WordPress, the following functions may be used. Usage of our Simple Cache plugin is required (it emulates the features of the WP transient API and avoids repeated requests to the slow NOAA server for data).

METAR

The following PHP function will return (and cache) a report for 60 minutes. Arguments are passed to the function in an array.

1
<?php 
2
/*
3
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
5
*/
6
 
7
function beliefmedia_metar($loc = 'kjfk', $args = '') {
8
 
9
  $atts = array(
10
    'p' => true,
11
    'cache' => 3600
12
  );
13
 
14
  /* Merge $args with $atts */
15
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
16
 
17
  $transient = 'bm_metar_' . md5(serialize($loc));
18
  $dbresult =  beliefmedia_get_transient($transient, $atts['cache']);
19
 
20
  if ($dbresult !== false) {
21
  $result = $dbresult;
22
 
23
  } else {
24
 
25
     /* Get text file from NOAA.gov */
26
     $data = @file('http://tgftp.nws.noaa.gov/data/observations/metar/stations/' . strtoupper($loc) . '.TXT');
27
 
28
     if ($data != false) {
29
 
30
      list($i, $date) = each($data);
31
      while (list($i, $line) = each($data)) $metar .= ' ' . trim($line);
32
      $result = trim(preg_replace('!\s+!', ' ', $metar));
33
 
34
      /* Cache .. */
35
      beliefmedia_set_transient($transient, $result);
36
 
37
      } else {
38
 
39
      /* Data temporarily unavailable */
40
      $result = 'Current METAR unavailable: ' . beliefmedia_get_transient_data($transient);
41
 
42
    }
43
 
44
  }
45
 
46
 /* Return */
47
 if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
48
 return $result;
49
}

Usage:

1
/* Retrieve METAR for KJFK */
2
echo beliefmedia_metar($loc = 'kjfk');

TAF

The TAF function is a variation on the METAR theme. The arguments are passed to the function as an array.

1
<?php 
2
/*
3
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
4
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
5
*/
6
 
7
function beliefmedia_taf($loc = 'klax', $args = '') {
8
 
9
  $atts = array(
10
    'p' => true,
11
    'cache' => 3600
12
  );
13
 
14
  /* Merge $args with $atts */
15
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
16
 
17
  $transient = 'bm_taf_' . md5(serialize($loc));
18
  $dbresult =  beliefmedia_get_transient($transient, $atts['cache']);
19
 
20
  if ($dbresult !== false) {
21
  $result = $dbresult;
22
 
23
  } else {
24
 
25
    /* Get text file from NOAA */
26
    $data = @file('ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/' . strtoupper($loc) . '.TXT');
27
 
28
    if ($data != false) {
29
 
30
      list($i, $date) = each($data);
31
      while (list($i, $line) = each($data)) $taf .= ' ' . trim($line);
32
      $result = trim(preg_replace('!\s+!', ' ', $taf));
33
 
34
      /* Cache */
35
      beliefmedia_set_transient($transient, $result);
36
 
37
      } else {
38
 
39
      /* Data temporarily unavailable */
40
      $result = 'Current TAF unavailable: ' . beliefmedia_get_transient_data($transient);
41
 
42
    }
43
 
44
  }
45
 
46
 /* Return result .. */
47
 if ($atts['p'] !== false) $result = '<p>' . $result . '</p>';
48
 return $result;
49
}

Example usage (TAF for KLAX).

1
/* Retrieve TAF for KLAX */
2
echo beliefmedia_taf($loc = 'klax');

Returning Temperature and QNH with PHP

The WP plugin handles returning the QNH (air pressure) and temperature (although it's in desperate need of a rewrite). Not unlike the other two PHP functions, we require the use of Simple Cache to cache the result.

Temperature

The first function simply takes the report and converts the aviation temperature format to an integer (basically, adding a minus sign).

1
<?php 
2
/*
3
 Convert temperature to Integer value
4
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
5
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
6
*/
7
 
8
function beliefmedia_metar_temp_int($temp) {
9
  $temp = str_replace('M', '-', &quot;$temp&quot;);
10
 return intval($temp);
11
}

The second function will first check for a cached temperature value. If it doesn't exist or exceeds the cache period, it will make a request for a new valid METAR report and then parse the data for a result.

1
<?php 
2
/*
3
 Convert temperature to Integer value
4
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
5
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
6
*/
7
 
8
function beliefmedia_metar_temp($loc = 'YSSY', $unit = 1, $format = '%d, %d', $cache = '3600') {
9
 
10
  $temperature_transient = 'bm_metar_temp_' . md5(serialize(func_get_args()));
11
  $dbresult =  beliefmedia_get_transient($temperature_transient, $cache);
12
 
13
  if ($dbresult !== false) {
14
   $return = $dbresult;
15
 
16
  } else {
17
 
18
    /* Cache new metar and return report */
19
    $transient = 'bm_metar_' . md5($loc);
20
    $result = beliefmedia_metar($loc);
21
    beliefmedia_set_transient($transient, $result);
22
 
23
    /* Match the temp and DP temp */
24
    preg_match('/(M?[0-9]{2})\/(M?[0-9]{2}|[X]{2})/', $result, $results);
25
    $temp_c = $results[1]; $dewpoint_c = $results[2];
26
 
27
    /* Get temperature as integer */
28
    $temp_int = beliefmedia_metar_temp_int($temp_c);
29
    $dewpoint_int = beliefmedia_metar_temp_int($dewpoint_c);
30
 
31
    if ($unit == '0') {
32
      $temp_int = (round(1.8 * $temp_int + 32));
33
      $dewpoint_int = (round(1.8 * $dewpoint_int + 32));
34
    }
35
 
36
    /* Data to return & cache result */
37
    $return = sprintf($format, $temp_int, $dewpoint_int);
38
    beliefmedia_set_transient($temperature_transient, $return);
39
 
40
   }
41
 
42
 return $return;
43
}

Usage is as follow:

1
/* Usage */
2
echo beliefmedia_metar_temp($loc = 'KLAX', $unit = 1, $format = '%d/%d', $cache = '3600');

The format of the the returned data can be altered with something like $format = '%d/%d', $format = 'T %d DP %d', $format = 'Temperature: %d', etc.

Use $unit = 0 to return Fahrenheit.

QNH (Altimeter)

The hectopascal (hPa) QNH is returned in most parts of the world. The USA, however, is stuck in its little imperial vacuum and still uses the archaic inches of mercury to measure pressure. Both values are returned by our function.

1
<?php 
2
/*
3
 Get QNH from METAR Report
4
 Ref: ISA 1013.25, 29.92
5
 Since HPA is ICAO standard, defaults to true
6
 $unit: 1 Hectopascals (MB), 0 Mercury
7
 Aviation METAR or TAF Reports with PHP or WordPress Shortcode
8
 http://www.beliefmedia.com/aviation-metar-taf-wordpress
9
*/
10
 
11
function beliefmedia_metar_qnh($loc = 'YSSY', $unit = 1, $cache = '3600') {
12
 
13
  $qnh_transient = 'bm_metar_qnh_' . md5(serialize(func_get_args()));
14
  $altimeter =  beliefmedia_get_transient($qnh_transient, $cache);
15
 
16
  if ($altimeter !== false) {
17
   $return = $altimeter;
18
 
19
   } else {
20
 
21
     /* Cache new metar and return report */
22
     $transient = 'bm_metar_' . md5($loc);
23
     $result = beliefmedia_metar($loc);
24
     beliefmedia_set_transient($transient, $result);
25
 
26
     /* Get QNH */
27
     preg_match('/(A|Q)([0-9]{4})/', $result, $results);
28
     $altimeter_raw = $results['2'];
29
 
30
     /* Is the report in Hecopascals? */
31
     $hec = ($results[1] == 'Q') ? true : false;
32
 
33
     /* Convert Inches to QNH */
34
     if ( ($hec === false) && ($unit == 1) ):
35
       $altimeter = round( ($altimeter_raw / 0.02953) / 100, 0);
36
 
37
     /* Convert MB to Inches */
38
     elseif ( ($hec === true) && ($unit == 0) ):
39
       $altimeter = round(0.02953 * $altimeter_raw, 2);
40
 
41
     /* Convert Inches */
42
     elseif ( ($hec === false) && ($unit == 0) ):
43
       $altdiv = ($altimeter_raw/100);
44
       $altimeter = number_format($altdiv , 2, '.', '');
45
 
46
     else:
47
       $altimeter = ltrim($altimeter_raw, '0');
48
     endif;
49
 
50
     /* Cache result */
51
     beliefmedia_set_transient($qnh_transient, $altimeter);
52
  }
53
 
54
 return $altimeter;
55
}

Usage is as follows:

1
/* Usage */
2
echo beliefmedia_metar_qnh($loc = 'KLAX', $unit = 0, $cache = '3600');

Considerations

  • We previously provided versions of this code on a few websites. However, this proved problematic with updates and other revisions. All other posts will now redirect to BeliefMedia for up-to-date code.
  • Our WordPress plugin is in desperate need of an update (and it is forthcoming). It was submitted to WP because of a need at the time.
  • Other information can be extracted from either report (wind, cloud etc). The next update to our plugin should include these features.
  • Our client platform manages aviation weather. It's a default module... whether you want it or not. The system provides an API to graph changes in reports (dating back several years).

Download

Note: The WordPress plugin will soon get a substantial update.


Title: Aviation METAR and TAF Reports
Description: Aviation METAR or TAF Reports with PHP or WordPress Shortcode.
  Download • Version 0.3, 860.0B, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.7K)    

Plugin Title: Author:
Description:
Download (downloaded 0 times) | Plugin Page

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433