Changing KLFK to your station code, and Lufkin, Texas to a description of your location.
---------------------------------------------------------------------------------------------*/
function WeatherIcon($station='KLFK', $location='Lufkin, Texas') {
global $Itemid;
// get the parameters
//$station = 'KLFK'; //XXXX (the metar code) //moved to the function..
//$location = 'Lufkin, Texas'; //Location to Display in output (Something like 'lisbon, portugal') //moved to the function..
$temperature = 'C'; //C|F (Temperature in Celsius or Fahrenheit)
$display_temperature = 'yes'; //yes|no Whether to display the temp in the output
$display_humidity = 'yes'; //yes|no Whether to display the Humidity in the output
$display_wind = 'yes'; //yes|no Whether to display the Wind Speed and Direction in the output
$display_dew_point = 'no'; //yes|no Whether to display the Dew Point in the output
$display_visibility = 'no'; //yes|no Whether to display the Visibility in the output
$display_clouds = 'yes'; //yes|no Whether to display the Cloud Conditions in the ALT attribute of the icon
$display_barometer = 'no'; //yes|no Whether to display the Barometer in the output
$display_wind_chill = 'yes'; //yes|no Whether to display the Wind Chill in the output
$display_heat_index = 'yes'; //yes|no Whether to display the Heat Index in the output
$local_time = '+10'; //X difference in hours from GMT - remember that if you observe daylight savings time you need to set it here.
$sunrise_hour = '6'; //Hour to start using the Daylight icons instead of nightime (reccomend using a number between 3 and 10)
$sunset_hour = '18'; //Hour to start using the Nightime Icons instead of daytime (reccomend using a number between 16 and 23)
$use_icon_status = 'yes'; //yes|no Whether to display the Weather Icon in the output
$wind_unit = 'km/h'; //km/h|mph|kt (kilometers, miles or knots)
$visibility_unit = 'km'; //km|miles (kilometers or miles)
$icon_location = 'http://leftjustified.net/images/wicons/'; //path to the icons.. this can be a sub-folder, but works better if it's a full http address
$icon_extension = '.png'; //the three letter extension for your wicon's (usually '.png' or '.gif')
// no longer needed $icon_alt_text = 'Current Conditions'; //The alt text for the icon display
$cache_path = '/home/httpd/vhosts/leftjustified.net/httpdocs/weather_cache'; // absolute path & no trailing slashes | make sure you set write access to this folder.
$cache_timeout = '1800'; //Time in seconds before we try to update the weather info
$cache_debug = '0'; //0 for False, 1 for True | activate this to make sure that your time and cache are working.
// end parameters
// The Folowing is the language strings file.. if you want to change the
// language displayed, then changes the strings in this file
include_once('language.php');
// end language File.
$wxInfo['CONDITIONS_STATUS']='';
$wxInfo['CONDITIONS_SEVERITY']='';
$weather =""; //This is the variable used to store the weather output.
$wxInfo['STATION'] = $station;
$wxInfo['TEMPERATURE'] = $temperature;
$wxInfo['CACHE_TIMEOUT'] = $cache_timeout;
$wxInfo['CACHE_DEBUG'] = $cache_debug;
$wxInfo['CACHE_PATH'] = $cache_path;
$metar = get_metar($station,$wxInfo);
process_metar($metar,$wxInfo,$visibility_unit,$wind_unit);
if ($metar==''||$metar==false||$metar==null||($wxInfo['TEMP_C']==''&&$wxInfo['TEMP_F']==''))
{
$weather = $weather . '
'. $location. "
"._NOTAVAILABLE. "
";
}
else
{
$weather = $weather . ''. "$location
\n";
// icon
$no_night=0;
$now=gmdate( "H", time()+$local_time*60*60 );
//Debug The Time
if ($wxInfo['CACHE_DEBUG']) {echo "Hour: ".$now."
";}
// fog
if ($wxInfo['CONDITIONS_STATUS']=='8')
{
$wxInfo['ICON']="1cloud_fog$icon_extension";
$no_night=1;
}
// heavy snow
if ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='2')
{
$wxInfo['ICON']="4cloud_heavysnow$icon_extension";
$no_night=1;
}
// light snow
elseif ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='1')
{
$wxInfo['ICON']="4cloud_modsnow$icon_extension";
$no_night=1;
}
// snow
elseif ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='0')
{
$wxInfo['ICON']="4cloud_lightsnow$icon_extension";
$no_night=1;
}
// general snow
elseif ($wxInfo['CONDITIONS_STATUS']=='5')
{
$wxInfo['ICON']="4cloud_lightsnow$icon_extension";
$no_night=1;
}
// heavy rain
if ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='2')
{
$wxInfo['ICON']="4cloud_heavyrain$icon_extension";
$no_night=1;
}
// light rain
elseif ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='0')
{
$wxInfo['ICON']="4cloud_lightrain$icon_extension";
$no_night=1;
}
// rain
elseif ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='1')
{
$wxInfo['ICON']="4cloud_modrain$icon_extension";
$no_night=1;
}
// thunder
elseif ($wxInfo['CONDITIONS_STATUS']=='2')
{
$wxInfo['ICON']="3cloud_thunders$icon_extension";
$no_night=0;
}
// scattered clouds
elseif ($wxInfo['CONDITIONS_STATUS']=='' && ($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='2')
{
$wxInfo['ICON']="2cloud_norain$icon_extension";
$no_night=0;
}
// mostly cloudy
elseif ($wxInfo['CONDITIONS_STATUS']=='' && ($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='3')
{
$wxInfo['ICON']="3cloud_norain$icon_extension";
$no_night=0;
}
// partly cloudy
elseif ($wxInfo['CONDITIONS_STATUS']=='' && ($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='1')
{
$wxInfo['ICON']="1cloud_norain$icon_extension";
$no_night=0;
}
// clear skies
elseif ($wxInfo['CLOUDS_STATUS']=='0')
{
$wxInfo['ICON']="0cloud$icon_extension";
$no_night=0;
}
// overcast
elseif ($wxInfo['CLOUDS_STATUS']==_OVERCAST)
{
$wxInfo['ICON']="4cloud_norain$icon_extension";
$no_night=1;
}
// if night, icon is different
if((intval($now)>=$sunset_hour && intval($now)<='24' || intval($now)>= '0' && intval($now)<=$sunrise_hour) && !$no_night) $wxInfo['ICON']= "n_".$wxInfo['ICON'];
// debug
//echo "Cond status ".$wxInfo['CONDITIONS_STATUS']." Cloud stat ". $wxInfo['CLOUDS_STATUS']." Cond severity ".$wxInfo['CONDITIONS_SEVERITY'];
/////////
/* ID FOR at end of this line */
if ($use_icon_status == 'yes' && $wxInfo['ICON']<>'') $weather = $weather . "
\n";
if ($display_temperature == 'yes')
{
if ($temperature=='C') $weather = $weather . "- " ._TEMPERATURE."
- ".$wxInfo['TEMP_C']."°C
\n";
else $weather = $weather . "- " ._TEMPERATURE."
- ".$wxInfo['TEMP_F']."°F
\n";
}
if ($display_wind_chill=='yes' && $wxInfo['WIND CHILL']<>'') $weather = $weather . "- " ._WIND_CHILL."
- ".$wxInfo['WIND CHILL']."
\n";
if ($display_heat_index=='yes' && $wxInfo['HEAT INDEX']<>'') $weather = $weather . "- " ._HEAT_INDEX."
- ".$wxInfo['HEAT INDEX']."
\n";
if ($display_humidity=='yes' && $wxInfo['HUMIDITY']<>'') $weather = $weather . "- " ._HUMIDITY_W."
- ".$wxInfo['HUMIDITY']."
\n";
if ($display_wind=='yes' && $wxInfo['WIND']<>'') $weather = $weather . "- " ._WIND."
- ".$wxInfo['WIND']." $wind_unit
\n";
if ($display_dew_point == 'yes')
{
if ($temperature=='C') $weather = $weather . "- " ._DEWPOINT."
- ".$wxInfo['DEWPT_C']."°C
\n";
else $weather = $weather . "- " ._DEWPOINT."
- ".$wxInfo['DEWPT_F']."°F
\n";
}
if ($display_barometer=='yes' && $wxInfo['BAROMETER']<>'') $weather = $weather . "- " ._BAROMETER."
- ".$wxInfo['BAROMETER']."
\n";
if ($wxInfo['CONDITIONS']<>'') $weather = $weather . "- " ._CONDITION."
- ".$wxInfo['CONDITIONS']."
\n";
if ($display_visibility=='yes' && $wxInfo['VISIBILITY']<>'') $weather = $weather . "- " ._VISIBILITY."
- ".$wxInfo['VISIBILITY']."
\n";
$weather = $weather . "
\n";
}// else
echo $weather; // This is the call that writes out the content.
} // End WeatherIcon Function
///////////////////////////////////////////////////////////////////////
// Below here is the Noaa Query Code and the code to parse the output//
///////////////////////////////////////////////////////////////////////
function get_metar($station, &$wxInfo) {
if ($wxInfo['CACHE_DEBUG']) {echo "
Cache Timeout: ".$wxInfo['CACHE_TIMEOUT']."";}
$metar_cache= $wxInfo['CACHE_PATH']. "/metar_cache_$station.txt";
if (file_exists($metar_cache)) {
$cache_age=(time()-filemtime($metar_cache));
}
if ($wxInfo['CACHE_DEBUG']) {echo "Cache File Age: $cache_age";}
if (isset($cache_age) && $cache_age < $wxInfo['CACHE_TIMEOUT']) {
$fp = fopen($metar_cache, "r");
$metar=fread($fp, filesize($metar_cache));
fclose($fp);
}
else {
if ($wxInfo['CACHE_DEBUG']) {echo "Fetching New Metar Data";}
$host = 'weather.noaa.gov';
$location = "/pub/data/observations/metar/stations/$station.TXT";
$request = "HTTP/1.1\r\n" .
"If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" .
"Pragma: no-cache\r\n".
"Cache-Control: no-cache\r\n";
@$fp = fsockopen($host, 80);
$request = "GET $location $request" .
"Host: $host\r\n" .
"Content-Type: text/html\r\n" .
"Connection: Close\r\n\r\n";
$metar = false;
if ($fp) {
fputs($fp, $request);
if (strpos(fgets($fp, 1024), '200 ')) {
do {
$line = fgets($fp, 1024);
} while ($line != "\r\n");
while ($line = fgets($fp, 1024)) {
$metar = $line;
}
fclose($fp);
}
}
if ($fp = fopen($metar_cache, "w")) {
fwrite($fp, $metar);
fclose($fp);
}
}
return $metar;
}
function process_metar($metar, &$wxInfo,$visibility_unit,$wind_unit) {
if ($metar != '') {
$metarParts = explode(' ',$metar);
$groupName = array('get_station','get_time','get_station_type','get_wind','get_var_wind','get_visibility','get_runway','get_conditions','get_cloud_cover','get_temperature','get_altimeter');
$metarPtr = 1; // get_station identity is ignored
$group = 1;
while ($group < count($groupName)) {
$part = $metarParts[$metarPtr];
$groupName[$group]($part,$metarPtr,$group,$wxInfo,$visibility_unit,$wind_unit); // $groupName is a function variable
}
}
else $wxInfo['ERROR'] = 'Data not available';
}
function get_station($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (strlen($part) == 4 and $group == 0) {
$group++;
$metarPtr++;
}
}
function get_time($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (substr($part,-1) == 'Z') $metarPtr++;
$group++;
}
function get_station_type($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if ($part == 'AUTO' || $part == 'COR') $metarPtr++;
$group++;
}
function get_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (!function_exists('speed')) {
function speed($part, $unit,$wind_unit) {
if ($unit == 'KT' && $wind_unit=='mph') $speed = round(1.1508 * $part); // from knots
elseif ($unit == 'MPS' && $wind_unit=='mph') $speed = round(2.23694 * $part); // from meters per second
elseif ($unit == 'KMH' && $wind_unit=='mph') $speed = round(0.621371 * $part); // from km per hour
if ($unit == 'KT' && $wind_unit=='km/h') $speed = round((1.1508 * $part)*1.61); // from knots
elseif ($unit == 'MPS' && $wind_unit=='km/h') $speed = round($part/1000); // from meters per second
elseif ($unit == 'KM' && $wind_unit=='km/h') $speed = $part; // from km per hour
if ($unit == 'KT' && $wind_unit=='kt') $speed = $part; // from knots
elseif ($unit == 'MPS' && $wind_unit=='kt') $speed = round($part*0.868976242); // from meters per second
elseif ($unit == 'KM' && $wind_unit=='kt') $speed = round($part*0.539956803); // from km per hour
$speed = "$speed";
return $speed;
}
}
if (ereg('^([0-9G]{5,10}|VRB[0-9]{2,3})(KT|MPS|KMH)$',$part,$pieces)) {
$part = $pieces[1];
$unit = $pieces[2];
if ($part == '00000') {
$wxInfo['WIND'] = _CALM; // no wind
}
else {
ereg('([0-9]{3}|VRB)([0-9]{2,3})G?([0-9]{2,3})?',$part,$pieces);
if ($pieces[1] == 'VRB') $direction = _VARIES;
else {
$angle = (integer) $pieces[1];
$compass = array(_N,_NNE,_NE,_ENE,_E,_ESE,_SE,_SSE,_S,_SSW,_SW,_WSW,_W,_WNW,_NW,_NNW);
$direction = $compass[round($angle / 22.5) % 16];
}
if ($pieces[3] == 0) $gust = '';
else $gust = ', '._GUSTINGTO.' ' . speed($pieces[3], $unit,$wind_unit);
$wxInfo['WIND'] = $direction . ' '._WINDAT.' ' . speed($pieces[2], $unit,$wind_unit) . $gust;
}
$metarPtr++;
}
$group++;
}
function get_var_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (ereg('([0-9]{3})V([0-9]{3})',$part,$pieces)) $metarPtr++;
$group++;
}
function get_visibility($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
static $integerMile = '';
if (strlen($part) == 1) { // visibility is limited to a whole mile plus a fraction part
$integerMile = $part . ' ';
$metarPtr++;
}
elseif (substr($part,-2) == 'SM') { // visibility is in miles
$part = substr($part,0,strlen($part)-2);
if (substr($part,0,1) == 'M') {
$prefix = _LESSTHAN.' ';
$part = substr($part, 1);
}
else $prefix = '';
if (($integerMile == '' && ereg('[/]',$part,$pieces)) || $part == '1' && $visibility_unit='miles') $unit = ' mile';
elseif (($integerMile == '' && ereg('[/]',$part,$pieces)) || $part == '1' && $visibility_unit='km') $unit = ' km';
else
{
if ($visibility_unit=='miles') $unit = ' miles';
else $unit = ' km';
}
$wxInfo['VISIBILITY'] = $prefix . $integerMile . $part . $unit;
$metarPtr++;
$group++;
}
elseif (substr($part,-2) == 'KM') { // unknown (Reported by NFFN in Fiji)
$metarPtr++;
$group++;
}
elseif (ereg('^([0-9]{4})$',$part,$pieces)) { // visibility is in meters
if ($visibility_unit=='miles') $distance = round($part/ 1609.344, 1); // convert to miles
else $distance = $part/1000;
if ($distance > 5) $distance = round($distance);
if ($distance <= 1 && $visibility_unit=='miles') $unit = ' mile';
elseif ($distance <> 1 && $visibility_unit=='miles') $unit = ' miles';
if ($visibility_unit=='km') $unit = ' km';
$wxInfo['VISIBILITY'] = $distance . $unit;
$metarPtr++;
$group++;
}
elseif ($part == 'CAVOK') { // good weather
if ($visibility_unit=='miles') $wxInfo['VISIBILITY'] = _GREATERTHAN.' 7 '._MILES;
else $wxInfo['VISIBILITY'] =_GREATERTHAN.' 10 km';
$wxInfo['CONDITIONS'] = '';
$wxInfo['CLOUDS'] = _CLEARSKIES;
$wxInfo['CLOUDS_STATUS']='0';
$metarPtr++;
$group += 4; // can skip the next 3 groups
}
else {
$group++;
}
}
function get_runway($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (substr($part,0,1) == 'R') $metarPtr++;
else $group++;
}
function get_conditions($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
static $conditions = '';
static $wxCode = array(
'VC' => _NEARBY,
'MI' => _SHALLOW,
'PR' => _PARTIAL,
'BC' => _PATCHESOF,
'DR' => _LOWDRIF,
'BL' => _BLOWING,
'SH' => _SHOWERS,
'TS' => _THUNDERSTORM,
'FZ' => _FREEZING,
'DZ' => _DRIZZLE,
'RA' => _RAIN,
'SN' => _SNOW,
'SG' => _SNOWGRAINS,
'IC' => _ICE_CRYSTALS,
'PE' => _ICE_PEL,
'GR' => _HAIL,
'GS' => _SMALL_HAIL, // and/or snow pellets
'UP' => _UNKNOWN,
'BR' => _MIST,
'FG' => _FOG,
'FU' => _SMOKE,
'VA' => _VOLCANIC,
'DU' => _WIDESPREAD,
'SA' => _SAND,
'HZ' => _HAZE,
'PY' => _SPRAY,
'PO' =>_WELLDEV,
'SQ' => _SQUALLS,
'FC' => _FUNNEL,
'SS' => _SANDSTORM);
static $wxIcon = array(
'TS' => '2',
'FZ' => '3',
'RA' => '4',
'SN' => '5',
'SG' => '5',
'IC' => '7',
'PE' => '7',
'FG' => '8',
'FU' => '9',
'SA' => '10',
'HZ' => '11');
if (ereg('^(-|\+|VC)?(TS|SH|FZ|BL|DR|MI|BC|PR|RA|DZ|SN|SG|GR|GS|PE|IC|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)+$',$part,$pieces)) {
if (strlen($conditions) == 0)
{
$join = '';
$wxInfo['CONDITIONS_SEVERITY'] = '0';
}
else $join = ' & ';
if (substr($part,0,1) == '-') {
$wxInfo['CONDITIONS_SEVERITY'] = '1';
$prefix = _LIGHT.' ';
$part = substr($part,1);
}
elseif (substr($part,0,1) == '+') {
$wxInfo['CONDITIONS_SEVERITY'] = '2';
$prefix = _HEAVY.' ';
$part = substr($part,1);
}
else $prefix = ''; // moderate conditions have no descriptor
$conditions .= $join . $prefix;
// The 'showers' code 'SH' is moved behind the next 2-letter code to make the English translation read better.
if (substr($part,0,2) == 'SH') $part = substr($part,2,2) . substr($part,0,2). substr($part, 4);
while ($code = substr($part,0,2)) {
$conditions .= $wxCode[$code] . ' ';
if ($wxCode[$code]==_SNOW || $wxCode[$code]==_RAIN) $wxInfo['CONDITIONS_STATUS'] = $wxIcon[$code];
$part = substr($part,2);
}
$wxInfo['CONDITIONS'] = $conditions;
$metarPtr++;
}
else {
$wxInfo['CONDITIONS'] = $conditions;
$group++;
}
$wxInfo['CONDITIONS'] = $conditions;
if (ereg('^(-|\+|VC)?(SH|RA)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '1'; //showers rain
if (ereg('^(-|\+|VC)?(SH|SN)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '2'; //showers snow
}
function get_cloud_cover($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
static $cloudCode = array(
'SKC' => _CLEARSKIES,
'CLR' => _CLEARSKIES,
'FEW' => _PARTLY,
'SCT' => _SCATTERED,
'BKN' => _MOSTLY,
'OVC' => _OVERCAST,
'VV' => _VERTICAL);
static $cloudIcon = array(
'SKC' => '0',
'CLR' => '0',
'FEW' => '1',
'SCT' => '2',
'BKN' => '3',
'OVC' => _OVERCAST,
'VV' => _VERTICAL);
if ($part == 'SKC' || $part == 'CLR') {
$wxInfo['CLOUDS'] = $cloudCode[$part];
$wxInfo['CLOUDS_STATUS'] = '0';
$metarPtr++;
$group++;
}
else {
if (ereg('^([A-Z]{2,3})([0-9]{3})',$part,$pieces)) { // codes for CB and TCU are ignored
$wxInfo['CLOUDS'] = $cloudCode[$pieces[1]];
$wxInfo['CLOUDS_STATUS'] = $cloudIcon[$pieces[1]];
if ($pieces[1] == 'VV') {
$altitude = (integer) 100 * $pieces[2]; // units are feet
$wxInfo['CLOUDS'] .= " to $altitude ft";
}
$metarPtr++;
}
else {
$group++;
}
}
}
function get_temperature($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (!function_exists('get_heat_index')) {
function get_heat_index($tempF, $rh, &$wxInfo) {
// Calculate Heat Index based on temperature in F and relative humidity (65 = 65%)
if ($tempF > 79 && $rh > 39) {
$hiF = -42.379 + 2.04901523 * $tempF + 10.14333127 * $rh - 0.22475541 * $tempF * $rh;
$hiF += -0.00683783 * pow($tempF, 2) - 0.05481717 * pow($rh, 2);
$hiF += 0.00122874 * pow($tempF, 2) * $rh + 0.00085282 * $tempF * pow($rh, 2);
$hiF += -0.00000199 * pow($tempF, 2) * pow($rh, 2);
$hiF = round($hiF);
$hiC = round(($hiF - 32) / 1.8);
if ($wxInfo['TEMPERATURE']=='F') $wxInfo['HEAT INDEX'] = "$hiF°F";
else $wxInfo['HEAT INDEX'] = "$hiC°C";
}
}
}
if (!function_exists('get_wind_chill')) {
function get_wind_chill($tempF, &$wxInfo) {
// Calculate Wind Chill Temperature based on temperature in F and
// wind speed in miles per hour
if ($tempF < 51 && $wxInfo['WIND'] != _CALM) {
$pieces = explode(' ', $wxInfo['WIND']);
$windspeed = (integer) $pieces[2]; // wind speed must be in miles per hour
if ($windspeed > 3) {
$chillF = 35.74 + 0.6215 * $tempF - 35.75 * pow($windspeed, 0.16) + 0.4275 * $tempF * pow($windspeed, 0.16);
$chillF = round($chillF);
$chillC = round(($chillF - 32) / 1.8);
if ($wxInfo['TEMPERATURE']=='F') $wxInfo['WIND CHILL'] = "$chillF°F";
else $wxInfo['WIND CHILL'] = "$chillC°C";
}
}
}
}
if (ereg('^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?$',$part,$pieces)) {
$tempC = (integer) strtr($pieces[1], 'M', '-');
$tempF = round(1.8 * $tempC + 32);
$wxInfo['TEMP_C'] = $tempC;
$wxInfo['TEMP_F'] = $tempF;
get_wind_chill($tempF, $wxInfo);
if (strlen($pieces[2]) != 0 && $pieces[2] != 'XX') {
$dewC = (integer) strtr($pieces[2], 'M', '-');
$dewF = round(1.8 * $dewC + 32);
$wxInfo['DEWPT_C'] = $dewC;
$wxInfo['DEWPT_F'] = $dewF;
$rh = round(100 * pow((112 - (0.1 * $tempC) + $dewC) / (112 + (0.9 * $tempC)), 8));
$wxInfo['HUMIDITY'] = $rh . '%';
get_heat_index($tempF, $rh, $wxInfo);
}
$metarPtr++;
$group++;
}
else {
$group++;
}
}
function get_altimeter($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) {
if (ereg('^(A|Q)([0-9]{4})',$part,$pieces)) {
if ($pieces[1] == 'A') {
$pressureIN = substr($pieces[2],0,2) . '.' . substr($pieces[2],2); // units are inches Hg
$pressureHPA = round($pressureIN / 0.02953); // convert to hectoPascals
}
else {
$pressureHPA = (integer) $pieces[2]; // units are hectoPascals
$pressureIN = round(0.02953 * $pressureHPA,2); // convert to inches Hg
}
$wxInfo['BAROMETER'] = "$pressureIN"._IN_W." Hg ($pressureHPA hPa)";
$metarPtr++;
$group++;
}
else {
$group++;
}
}
?>