#!/usr/pkg/bin/php
<?php
// A simple PHP script for use on your gopher server
// to provide the current weather conditions in your area
// works using the NOAA weather XML feeds, and thus is
// only for the US and US territories covered by the NOAA
// there may be a similar feed system in other countries.

// NOAA weather XML link
$url = 'http://w1.weather.gov/xml/current_obs/KSOW.xml';

// Create a stream (user agent is required for this service)
$opts = array(
 'http'=>array(
   'method'=>"GET",
   'header'=>"Accept-language: en\r\n" .
             "User-agent: Mozilla/5.0 (Windows NT 6.3; rv:35.5) Gecko/20110101 Firefox/35.5\r\n"
 )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);

// turn it into xml object
$xml = simplexml_load_string($file);

$terminate = "\tx\tx\t0\r\n";

// Output
echo "hCurrent weather at Jozhaus, from the NOAA XML feed       URL:http://w1.weather.gov       gopher.jozhaus.com      70"."\r\n";
echo "i+--------------------------------------------------------+".$terminate;
echo "i| We're located near Show Low, AZ, USA. This is the      |".$terminate;
echo "i| forest portion of Arizona, so our weather is moderate. |".$terminate;
echo "i| Except for our wind. Our wind is above average.        |".$terminate;
echo "i+--------------------------------------------------------+".$terminate;
echo "i".$terminate;
echo "i".$terminate;

// Weather "icon"
$weather = $xml->weather;

$weatherIcons["Fair and Windy"] = "sun_wind.txt";
$weatherIcons["Fair"] = "sun.txt";
$weatherIcons["Fair and Breezy"] = "sun_wind.txt";
$weatherIcons["Partly Cloudy"] = "partly_cloudy.txt";
$weatherIcons["Mostly Cloudy and Windy"] = "cloud_wind.txt";
$weatherIcons["A Few Clouds and Windy"] = "cloud_wind.txt";
$weatherIcons["Mostly Cloudy"] = "cloudy.txt";
$weatherIcons["Partly Cloudy and Breezy"] = "cloud_wind.txt";
$weatherIcons["A Few Clouds"] = "cloudy.txt";
$weatherIcons["Overcast"] = "cloudy.txt";

if(array_key_exists(''.$weather,$weatherIcons)) {
       $icon = $weatherIcons[''.$weather];
}

if(isset($icon)) {
       $handle = fopen("/sdf/ftp/pub/users/tfurrows/icons/$icon", "r");
       if ($handle) {
               while (($line = fgets($handle)) !== false) {
                       echo "i".rtrim($line).$terminate;
               }
               fclose($handle);
       } else {
               // error opening the file.
               foreach(error_get_last() as $key=>$val) {
                       echo "i$key=$val".$terminatel;
               }
       }
}


echo "i".$terminate;
echo "iWeather for ".$xml->location.$terminate;
echo "i".$xml->observation_time.$terminate;
echo "i".$terminate;

echo "iWeather ......... ".$xml->weather.$terminate;
echo "iTemperature ..... ".$xml->temperature_string.$terminate;
echo "iHumidity ........ ".$xml->relative_humidity.$terminate;

// Shorten the wind string, it can be quite long
$windString = $xml->wind_string;
$removeWords = array("gusting","gust","gusts");
$windString = str_ireplace($removeWords,"",$windString);

$windString = str_ireplace("North","N",$windString);
$windString = str_ireplace("South","S",$windString);
$windString = str_ireplace("East","E",$windString);
$windString = str_ireplace("West","W",$windString);


echo "iWind ............ ".$windString.$terminate;
echo "iPressure(in) .... ".$xml->pressure_in.$terminate;
echo "iDewpoint ........ ".$xml->dewpoint_string.$terminate;
echo "iVisibility(mi) .. ".$xml->visibility_mi.$terminate;

echo "i".$terminate;
echo "i".$terminate;
echo "0View the PHP for this script     /getWeather.txt gopher.jozhaus.com      70\r\n";
echo "1View the ascii icons     /icons  gopher.jozhaus.com      70\r\n";

//include("/var/gopher/footer.php");

/*
Available xml values from this feed:

key: credit = NOAA's National Weather Service
key: credit_URL = http://weather.gov/
key: image =
key: suggested_pickup = 15 minutes after the hour
key: suggested_pickup_period = 60
key: location = Show Low, Show Low Regional Airport, AZ
key: station_id = KSOW
key: latitude = 34.26528
key: longitude = -110.00556
key: observation_time = Last Updated on Apr 21 2016, 1:55 pm MST
key: observation_time_rfc822 = Thu, 21 Apr 2016 13:55:00 -0700
key: weather = Fair
key: temperature_string = 72.0 F (22.0 C)
key: temp_f = 72.0
key: temp_c = 22.0
key: relative_humidity = 8
key: wind_string = Southwest at 4.6 MPH (4 KT)
key: wind_dir = Southwest
key: wind_degrees = 210
key: wind_mph = 4.6
key: wind_kt = 4
key: pressure_in = 30.12
key: dewpoint_string = 8.6 F (-13.0 C)
key: dewpoint_f = 8.6
key: dewpoint_c = -13.0
key: visibility_mi = 10.00
key: icon_url_base = http://forecast.weather.gov/images/wtf/small/
key: two_day_history_url = http://www.weather.gov/data/obhistory/KSOW.html
key: icon_url_name = skc.png
key: ob_url = http://www.weather.gov/data/METAR/KSOW.1.txt
key: disclaimer_url = http://weather.gov/disclaimer.html
key: copyright_url = http://weather.gov/disclaimer.html
key: privacy_policy_url = http://weather.gov/notice.html

*/

?>