\n";
global $SITE;
if (isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; }
$cacheFile2 = $cacheFileDir . $cacheFile2;
if (file_exists($cacheFile2)) {
$filemtime = filemtime($cacheFile2);
$filesize = filesize($cacheFile2);
if (0 == $filesize){
$filemtime = 0;
}
} else {
$filemtime = 0;
}
// open the cache file and write the new data and then close the file.
$forceRefresh = (isset($_REQUEST['force']))?true:false;
$current_time = time();
$cache_age = $current_time - $filemtime;
if ($forceRefresh or $cache_age >= $cache_life){
print "\n";
$html2 = curl_get_contents($url2);
$fp2 = fopen($cacheFile2, 'w');
fwrite($fp2, $html2);
fclose($fp2);
echo "\n";
} else {
echo "\n";
// Open the cache file, read it, then close it
$handle2 = fopen($cacheFile2, "r");
$filesize = filesize($cacheFile2);
$html2 = fread($handle2, $filesize);
fclose($handle2);
}
function curl_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/* looking for:
*****NOTE***************NOTE*********************NOTE************************
FILE FORMAT: FIRST RECORD IS DATE IN THE FORM YYYYMMDD. THE REMAINING RECORDS
CONTAIN MAXT(F),MINT(F),REPORTED AND ESTIMATED PRECIP IN HUNDRETHS OF INCHES,
9 WEATHER CHARACTERS,ID AND CITY NAMES WHERE AVAILABLE.
*****NOTE***************NOTE*********************NOTE********************
20160910
--- 0---|--- 10---|--- 20---|--- 30---|--- 40---|--- 50---|--- 60---|--- 70---|--- 80---|--- 90---|
123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|
37 32 1 1 9DSSS////70026 AK BARROW/W.POST W.ROGERS
41 34 20 20 9R/RRSSR/70086 AK BARTER ISLAND/DEW STN
46 39 2 2 9MMM/RRRM70104 AK CAPE_LISBURNE(AWOS)
44 41 1 1 9/D/RMDDD70117 AK TIN_CITY_AFS_(AWOS)
51 46 0 0 9////////70133 AK KOTZEBUE/RALPH WIEN ME
55 33 0 0 9----////70173 AK INDIAN_MTN_AFS_AWOS
90 75 0 0 8/T-//TTT78514 PR RAMEY AFB
90 78 15 15 8/////RR/78526 PR SAN JUAN
90 76 0 0 8///R/RRR78535 PU ROOSEVELT_ROADS_NAS
89 81 122 122 8-/-/-/--78543 IS CHARLOTTE AMALIE VIRGI
84 74 1 1 8////////91066 HI MIDWAY ISLAND NAVAL AI
88 73 0 0 8////////91162 HI KEKAHA, PACIFIC MISSIL
87 78 0 0 8//R/////91165 HI LIHUE
79 68 8 8 8///RR//R91170 HI WHEELER
83 75 12 12 8/////RRR91176 HI KANEOHE
88 73 0 0 8////////91178 HI BARBERS_PT_NAS/OAHU
86 73 4 4 8///R////91182 HI HONOLULU
87 73 0 0 8///////R91186 HI MOLOKUI
88 73 1 1 8///R////91190 HI KAHULUI
84 70 5 5 8/R/RRR//91285 HI HILO/LYMAN_FIELD
91 73 0 0 8//MS////9911R TX BRENHAM, BRENHAM MUNIC
*/
$usahighStation = '';
$usahighValue = -999;
$usalowStation = '';
$usalowValue = 999;
$usaprecipStation = '';
$usaprecipValue = 0;
$usaStations = array();
$reportDate = '';
list($headers2,$content2) = explode("\r\n\r\n",$html2);
$rawrecs = explode("\n",$content2); // process the file
$idx=0;
foreach ($rawrecs as $n => $rec) {
if($reportDate == '' and preg_match('/^\s+(\d+)$/',$rec,$matches)) {
$reportDate = gmdate("l, F j, Y",strtotime($matches[1].'T1200'));
print "\n";
continue;
}
if(strlen($rec) < 30) { continue; }
if(check_ignore_station($rec,$ignoreStations)) {
//print "\n";
continue;
}
$recmarker = substr($rec,19,1);
if($recmarker == '9' or $recmarker == '8') {
list($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation) =
sscanf($rec,' %d %d %d %d %s %s %[^\n]s');
$tStation = strtolower( trim($tStation) );
$tStation = str_replace('/','| ',$tStation);
$tStation = ucwords( str_replace('_',' ', $tStation) );
$tStation = str_replace('| ','/',$tStation);
if(isset($_REQUEST['debug'])) {
print "\n";
print "\n";
}
if(preg_match('|STATION|i',$tState)) {
if(isset($_REQUEST['debug'])) {
print "\n";
}
continue;
}
$usaStations[$idx] = join("\t",array($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation));
$idx++;
}
}
// now scan for the highs/lows
if (count($usaStations) > 0) {
$usahigh = '';
$usalow = '';
$usaprecip = '';
} else {
$usahigh = 'N/A';
$usalow = 'N/A';
$usaprecip = 'N/A';
}
foreach ($usaStations as $n => $vals) {
list($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation) = explode("\t",$vals);
if($tHigh < -90 or $tLow < -90 ) { continue; }
if($tHigh > $usahighValue) {
if(isset($_REQUEST['debug'])) {
print "\n";
}
$usahighValue = $tHigh;
$usahighStation = $n; // remember for later
}
if($tLow < $usalowValue) {
if(isset($_REQUEST['debug'])) {
print "\n";
}
$usalowValue = $tLow;
$usalowStation = $n; // remember for later
}
if($tPrecip > $usaprecipValue) {
if(isset($_REQUEST['debug'])) {
print "\n";
}
$usaprecipValue = $tPrecip;
$usaprecipStation = $n;
}
}
if(isset($_REQUEST['debug'])) {
print "\n";
print "\n";
print "\n";
}
// now pass through to see if any duplicate high, low, high precip exists and format the strings
foreach ($usaStations as $n => $vals) {
list($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation) = explode("\t",$vals);
if($tHigh < -90 or $tLow < -90 ) { continue; }
if($tHigh == $usahighValue) {
$usahigh .= "$tHigh$tUOM at $tStation, $tState\n";
}
if($tLow == $usalowValue) {
$usalow .= "$tLow$tUOM at $tStation, $tState\n";
}
if($tPrecip == $usaprecipValue and $tPrecip > 0) {
$tPrecipFmt = sprintf("%01.2f",$tPrecip/100);
$usaprecip .= "$tPrecipFmt$rUOM at $tStation, $tState\n";
}
}
// Make results HTML pretty
$usahigh = str_replace("\n","
\n",$usahigh);
$usalow = str_replace("\n","
\n",$usalow);
$usaprecip = str_replace("\n","
\n",$usaprecip);
print "\n";
print "\n";
print "\n";
print '\n";
print "\n";
function check_ignore_station($str, array $arr)
{
foreach($arr as $a) {
if (stripos($str,$a) !== false) {return true;}
}
return false;
}
?>