![]() |
|||||||||||||||
|
Forecast for Wichita Falls CRH.noaa.gov redirects (no point forecasts) // Version 2.02 - 02-Mar-2007 - added auto-failover to CRH and better include in page. // Version 2.03 - 29-Apr-2007 - modified for /images/wtf -> /forecast/images change // Version 2.04 - 05-Jun-2007 - improvement to auto-failover // Version 2.05 - 29-Jun-2007 - additional check for alternative no-icon forecast, then failover. // //import NOAA Forecast info //data ends up in three different arrays: //$forecasticons[x] x = 0 thru 9 This is the icon and text around it //$forecasttemp[x] x= 0 thru 9 This is forecast temperature with styling //$forecasttitles[x] x = 0 thru 12 This is the title word for the time period //$forecasttext[x] x = 0 thru 12 This is the detail text for the forecast // //$forecastupdate This is the time of last update //$forecastcity This is the city name for the forecast //$forecastoffice This is the NWS Office providing the forecast // //Also, in order for this to work correctly, you need the NOAA icons (or make your own... //there are over 200!). These need to be placed in the path where the original NOAA icons //are located. In my case, they are at: \forecast\images\ //properly (so make a folder in your web HTML root called "forecast", then make a folder in it //called "images", and place the icons in this folder) // //http://members.cox.net/carterlakeweather/forecasticons.zip (380K) // //URL below --MUST BE-- the Printable Point Forecast from the NOAA website // //Not every area of the US has a printable point forecast // //This script will ONLY WORK with a printable point forecast! // //To find yours in your area: // //Go to www.noaa.gov //Click on the Storm Watch map (middle lower left of page) //Click on your state //Click on your area of the state //Scroll down to the "Additional Forecasts & Info" //Click on Printable Forecast //------------------------------------------------*/ // // also set your NOAA warning zone here to use for automatic backup in case // the point printable forecast is not available. $NOAAZone = 'ALZ064'; // change this line to your NOAA warning zone. // Get the forecast.txt file or a new one from NOAA // You have to have a forecast.txt in place for this script to work // You can see ours at http://www.carterlake.org/forecast.txt // This is version 1.2 with Ken's modifications from Saratoga Weather // http://saratoga-weather.org/ // You can now force the cache to update by adding ?force=1 to the end of the URL if ( empty($_REQUEST['force']) ) $_REQUEST['force']="0"; $Force = $_REQUEST['force']; $forceBackup = false; if ($Force > 1) {$forceBackup = true; } $cacheName = "forecast.txt"; $fileName = "http://forecast.weather.gov/MapClick.php?CityName=Wichita+Falls&state=TX&site=OUN&textField1=33.8979&textField2=-98.5163&TextType=2"; // dont change the next line.... $backupfileName = "http://www.crh.noaa.gov/zones/$NOAAZone.php?zo=1"; $Status = "\n"; if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) { //--self downloader -- $filenameReal = $_SERVER["PATH_TRANSLATED"]; $filenameLcl = substr($PHP_SELF,1); $download_size = filesize($filenameReal); header('Pragma: public'); header('Cache-Control: private'); header('Cache-Control: no-cache, must-revalidate'); header("Content-type: text/plain"); header("Accept-Ranges: bytes"); header("Content-Length: $download_size"); header('Connection: close'); readfile($filenameReal); exit; } $usingFile = ""; if ($Force==1) { $html = fetchUrlWithoutHanging($fileName,$cacheName); if (preg_match('/Temporary|Location:|defaulting to/Uis',$html)) { $usingFile = "(Zone forecast)"; $html = fetchUrlWithoutHanging($backupfileName,$cacheName); } $fp = fopen($cacheName, "w"); $write = fputs($fp, $html); fclose($fp); } if ($Force==2) { $html = fetchUrlWithoutHanging($backupfileName,$cacheName); $fp = fopen($cacheName, "w"); $write = fputs($fp, $html); fclose($fp); $usingFile = "(Zone forecast)"; } // The number 1800 below is the number of seconds the cache will be used instead of pulling a new file // 1800 = 60s x 30m so it retreives every 30 minutes. if (filemtime($cacheName) + 1800 > time()) { $html = implode('', file($cacheName)); if (preg_match('/Temporary|Location:|defaulting to/is',$html)) { $usingFile = "(Zone forecast)"; $html = fetchUrlWithoutHanging($backupfileName,$cacheName); } } else { $html = fetchUrlWithoutHanging($fileName,$cacheName); if (preg_match('/Temporary|Location:|defaulting to/Uis',$html)) { $usingFile = "(Zone forecast)"; $html = fetchUrlWithoutHanging($backupfileName,$cacheName); } $fp = fopen($cacheName, "w"); $write = fputs($fp, $html); fclose($fp); } // Just get the top of the NWS page for editing $ERHtoCRH = preg_match('|/HW3php/images/fcicons/|i',$html); // here with ERH->CRH redirect? if ($ERHtoCRH) { // using the ERH->CRH redirect to zone forecast preg_match( '| \s+ | |i',$forecaststuff[$i]) ) { $forecaststuff[$i] .= ' '; } $forecaststuff[$i+18] = preg_replace('| |is',' ',$forecaststuff[$i+18]); $forecaststuff[$i+27] = trim($forecaststuff[$i+27]); $forecaststuff[$i+27] = '' . preg_replace( '|(.*)|Uis', "$2",$forecaststuff[$i+27]) . ''; $forecaststuff[$i+27] = preg_replace('| |is',' ',$forecaststuff[$i+27]); $forecaststuff[$i+36] = trim($forecaststuff[$i+36]); preg_match( // just get numeric value for pop if any. '|(\d+)|is',$forecaststuff[$i+36],$temp); if (! empty($temp[1]) ) { $PoP[$i] = $temp[1]; } else { $PoP[$i] = ''; } // $Status .= "\n"; if ($PoP[$i] > 0) { // change name to include PoP $forecaststuff[$i+9] = preg_replace('|\.jpg|s',$PoP[$i] . '.jpg',$forecaststuff[$i+9]); } // fix up |is',' ',$tag); $Status .= "\n"; $forecaststuff[$i+9] = preg_replace('|">$|i', "\" alt=\"$tag\" title=\"$tag\" />",$forecaststuff[$i+9]); $forecasticons[$i] = $forecaststuff[$i] . ' ' . $forecaststuff[$i+9] . ' ' . $forecaststuff[$i+18]; $forecasttemp[$i] = $forecaststuff[$i+27]; $forecastpop[$i] = $forecaststuff[$i+36]; } } else { // original srh/crh/erh format point printable forecast preg_match('| |s', $html, $betweenspan); $forecast = $betweenspan[1]; // Chop up each title text and place in array preg_match_all('|(.*): |Ui', $forecast, $headers); $forecasttitles = $headers[1]; // Chop up each forecast text and place in array preg_match_all('|(.*) |Ui', $forecast, $headers); $forecasttext = $headers[1]; // Grab the Last Update date and time. preg_match('|Last Update: (.*)|', $html, $betweenspan); $forecastupdated = $betweenspan[1]; $forecastupdated = preg_replace('|<[^>]+>|Uis','',$forecastupdated); // remove html markup // saratoga-weather.org mod: // Grab the NWS Forecast for (city name) preg_match('|NWS Forecast for: (.*)|',$html,$betweenspan); $forecastcity = $betweenspan[1]; // Grab the Issued by office preg_match('|Issued by: (.*) |',$html,$betweenspan); $forecastoffice = $betweenspan[1]; } // end regular handling $IncludeMode = false; $PrintMode = true; if (isset($doPrintNWS) && ! $doPrintNWS ) { return; } if (isset($_REQUEST['inc']) && strtolower($_REQUEST['inc']) == 'noprint' ) { return; } if (isset($_REQUEST['inc']) && strtolower($_REQUEST['inc']) == 'y') { $IncludeMode = true; } if (isset($doIncludeNWS)) { $IncludeMode = $doIncludeNWS; } // end saratoga-weather.org mod function fetchUrlWithoutHanging($url,$cacheurl) { // Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed $numberOfSeconds=4; // Suppress error reporting so Web site visitors are unaware if the feed fails error_reporting(0); // Extract resource path and domain from URL ready for fsockopen $url = str_replace("http://","",$url); $urlComponents = explode("/",$url); $domain = $urlComponents[0]; $resourcePath = str_replace($domain,"",$url); // Establish a connection $socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds); if (!$socketConnection) { $html = implode('', file($cacheurl)); return($html); // You may wish to remove the following debugging line on a live Web site // print(""); } // end if else { $xml = ''; fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n"); // Loop until end of file while (!feof($socketConnection)) { $xml .= fgets($socketConnection, 4096); } // end while fclose ($socketConnection); } // end else return($xml); } // end function if (! $IncludeMode and $PrintMode) { ?> Forecast blank? Force Update '; } if ($PrintMode) {?>
Forecast from NOAA-NWS for . Questions or Comments? Contact Us
|
![]() Featured On: ![]() ![]() ![]() ![]()
Copyright 2009 (C) TriCountyWeather.com
Scott McLaughlin |
|
|
| | |||||||||
