section ;-) // // TIP - Get noaarec.php?noaabug working BEFORE you commit to modifying dashboard // there have been several cases where a previous 'include' changed TZ to UTC // If this happens 'include' noaarec.php higher up the page OR uncomment and set TZ BELOW // date_default_timezone_set("America/??????????"); date_default_timezone_set("America/Mexico_City"); $wfo = "bis"; $sid = "bis"; $showNoaaAvg = true; // Like it says display max avg / min avg [true / false] // Only used in html patch which has a 'switch' // **** NO USER VARS BELOW THIS LINE ****************************************** // Belts 'n' braces - preset important vars $thisDayMax = $thisDayMax1 = $thisDayCacheMax = $thisDayCacheMax1 = null; $thisDayMaxYear = $thisDayMaxYear1 = $thisDayCacheMaxYear = $thisDayCacheMaxYear1 = null; $thisDayMin = $thisDayMin1 = $thisDayCacheMin = $thisDayCacheMin1 = null; $thisDayMinYear = $thisDayMinYear1 = $thisDayCacheMinYear = $thisDayCacheMinYear1 = null; $thisDayMaxAvg = $thisDayMaxAvg1 = $thisDayCacheMaxAvg = $thisDayCacheMaxAvg1 = null; $thisDayMinAvg = $thisDayMinAvg1 = $thisDayCacheMinAvg = $thisDayCacheMinAvg1 = null; $bugout = $bugout2 = $bugout3 = ""; $noaagrab = false; $doTomorrow = false; $all = false; echo "\n\n"; if(isset($_REQUEST['noaabug'])) { $noaabug = true; } else { $noaabug = false; } $err = false; if($noaabug) { echo "

Running in DeBug Mode

\n"; if(date("T") == "UTC") echo "POSSIBLE TZ ERROR - Please set in prog

\n"; echo (file_exists("noaarecdat.txt")? "noaarecdat was previously: " . date ("l jS \of F Y h:i:s A T", filemtime('noaarecdat.txt'))."

\n" : "noaarecdat.txt was NOT found

\n"); echo "Local Server Time NOW:  " .date('l jS \of F Y h:i:s A T, e - \U\T\C\ P')."

\n"; echo "\$wfo = $wfo
\n \$sid = $sid

\n"; } $check_it = time() - (60 * 60); // 1hr ago - only used when no 'tomorrow' data, or missing data $serverDay = date('j'); $tomorrow = date('j',strtotime('tomorrow')); // initial age check of cache is unimportant - it's the contents that matter !!! // include cachefile here for check purposes - a LOT of belt 'n' braces $tod = $tom = ""; if($test = @file_get_contents("noaarecdat.txt")) { // set valid date vars from cache, code is mostly debug ! $mimi = 0; $test = explode("\n", $test); foreach($test as $v) { $check = explode(",", $v); if($check[0] == $serverDay) { echo "\n\n"; $tod = "

server day found in cache $test[$mimi]
\n"; if(in_array("", $check)) $noaagrab = true; // at least one value missing if($check[1]) { $thisDayCacheMax = $check[1]; } else { echo "\n"; $tod .= "thisDayMax MISSING
\n"; } if($check[2]) { $thisDayCacheMaxYear = $check[2]; } else { echo "\n"; $tod .= "thisDayMaxYear MISSING
\n"; } if($check[3]) { $thisDayCacheMin = $check[3]; } else { echo "\n"; $tod .= "thisDayMin MISSING
\n"; } if($check[4]) { $thisDayCacheMinYear = $check[4]; } else { echo "\n"; $tod .= "thisDayMinYear MISSING
"; } if($check[5]) { $thisDayCacheMaxAvg = $check[5]; } else { echo "\n"; $tod .= "thisDayMaxAvg MISSING
\n"; } if($check[6]) { $thisDayCacheMinAvg = $check[6]; } else { echo "\n"; $tod .= "thisDayMinAvg MISSING
\n"; } } else { if($check[0] == $tomorrow) { echo "\n\n"; $tom = "tomorrow found in cache $test[$mimi]
\n"; if(in_array("", $check)) { // at least one value missing echo "\n"; $tom .= "at least one value missing
\n"; if(filemtime("noaarecdat.txt") < $check_it ) $noaagrab = true; } $thisDayCacheMax1 = $check[1]; $thisDayCacheMaxYear1 = $check[2]; $thisDayCacheMin1 = $check[3]; $thisDayCacheMinYear1 = $check[4]; $thisDayCacheMaxAvg1 = $check[5]; $thisDayCacheMinAvg1 = $check[6]; } // end tomorrow check } // end check day $mimi++; } // end foreach } else { // cachefile dosen't exist echo "\n"; // if($noaabug) echo "NO cachefile

\n"; $noaagrab = true; } // end if test if($tod) { if($noaabug) echo $tod; } else { echo "\n"; if($noaabug) echo "NO today found in cache
\n"; $noaagrab = true; } if($tom) { if($noaabug) echo $tom; } else { echo "\n"; if($noaabug) echo "NO tomorrow found in cache
\n"; if(filemtime("noaarecdat.txt") < $check_it ) $noaagrab = true; } // if required noaagrab ... // *********************************************************************** if($noaagrab || $noaabug) { // OK need to get the data echo "\n\n"; $bugout .= "

Ring NOAA ....

\n"; # if($all = file_get_contents("https://w2.weather.gov/climate/getclimate.php?pil=CLI&recent=yes&wfo=".$wfo."&sid=".$sid)) { # https://forecast.weather.gov/product.php?site=BIS&issuedby=BIS&product=CLI&format=txt&version=1&glossary=0 $fnameIn = "https://forecast.weather.gov/product.php?site=".strtoupper($wfo)."&issuedby=".strtoupper($sid)."&product=CLI&format=txt&version=1&glossary=0"; echo "\n"; $STRopts = array( 'http'=>array( 'method'=>"GET", 'protocol_version' => 1.1, 'header'=>"Cache-Control: no-cache, must-revalidate\r\n" . "Cache-control: max-age=0\r\n" . "Connection: close\r\n" . "User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0)\r\n" ), 'https'=>array( 'method'=>"GET", 'protocol_version' => 1.1, 'header'=>"Cache-Control: no-cache, must-revalidate\r\n" . "Cache-control: max-age=0\r\n" . "Connection: close\r\n" . "User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0)\r\n" ) ); $STRcontext = stream_context_create($STRopts); if($all = file_get_contents($fnameIn,0,$STRcontext)) { echo "\n"; #echo "\n"; #$stage1 = explode("

", $all);
		#$stage2 = explode("
", $stage1[1]); // stage2[0] now has the contents of the
 tags
		#$thedata = explode("\n", $stage2[0]); // create an array of 'lines'
// If there are problems NOAA returns "Sorry - no records found ..
// usually caused by incorrect station id(s)
    preg_match_all('!]*>(.*)!Uis',$all,$matches);
		#print "\n";
		if(isset($matches[1][0])) {$thedata = explode("\n",$matches[1][0]); } else {$thedata = array(); }
		if(strpos($thedata[0], "Sorry")) {
			$err = true;
			echo "\n\n";
			echo ($noaabug ? " NOAA says NO RECORDS FOUND !!
\nDouble Check sid and wfo !!

/n" : ""); } // step through the lines looking for max and min (occurs twice, we may want second event) // ** ALL CHANGE - need to see if it says today or yesterday - is it that simple ? (not quite) ** if($err === false){ $rec_found = 0; $firstblock = false; $maxLine = false; $minLine = false; $mass = false; foreach($thedata as $aline) { $rec_found++; if(strpos($aline, "CLIMATE SUMMARY")) { $cheese = explode(" ", $aline); $recDay = $cheese[(count($cheese) - 2)]; if($noaabug) echo "

recDay: " .$recDay. ", serverDay: " .$serverDay. ", Tomorrow: " .$tomorrow. "
\n"; $bugout .= $aline; if($recDay == $serverDay) { $firstblock = true; echo "\n\n"; $bugout .= "Use first block ....
\n"; } else { $firstblock = false; echo "\n\n"; $bugout .= "Use second block ....
\n"; } $bugout .= $thedata[$rec_found]. "
\n" .$thedata[$rec_found +1]. "
\n" .$thedata[$rec_found +2]. "
\n" .$thedata[$rec_found +3]."
\n" .$thedata[$rec_found +4]. "
\n"; } // if(strpos($aline, "(F)")) echo "Found @ $rec_found
"; if(strpos($aline, "(F)") && $rec_found <30) $mass = true; if($mass === true) { if(strpos($aline, "TODAY") && $rec_found < 25 && $recDay == $serverDay) { // echo "test line $rec_found
"; $bugout .= $thedata[$rec_found -2]. "
\n" .$thedata[$rec_found -1]. "
\n"; } if($firstblock === true && $rec_found < 70) { if( strpos($aline, "MAXIMUM" ) !== false) { $bugout .= "

".$aline."
\n"; $thisDayMax = trim(substr($aline, 31, 3), " M"); // remove white space and possible M[issing] $thisDayMaxYear = trim(substr($aline, 38, 4), " M"); // remove white space and possible M[issing] $thisDayMaxAvg = trim(substr($aline, 43, 3), " M"); // remove white space and possible M[issing] $bugout .= "This Day Record Max. Temp: ".$thisDayMax."°F @ ".$thisDayMaxYear."
Norm. ".$thisDayMaxAvg."°F
\n"; } // END if max if( strpos($aline, "MINIMUM" ) !== false) { $bugout .= "
".$aline."
\n"; $thisDayMin = trim(substr($aline, 31, 3), " M"); // remove white space and possible M[issing] $thisDayMinYear = trim(substr($aline, 38, 4), " M"); // remove white space and possible M[issing] $thisDayMinAvg = trim(substr($aline, 43, 3), " M"); // remove white space and possible M[issing] $bugout .= "This Day Record Min. Temp: ".$thisDayMin."°F @ ".$thisDayMinYear."
Norm. ".$thisDayMinAvg."°F
\n"; } // END Min } // END firstblock rec < 30 } // end mass true if($rec_found > 65) { if( strpos($aline, "NORMALS" ) !== false) { if($noaabug && $firstblock === false) { $bugout .= (strpos($aline, "TODAY") !== false ? "NO genuine Tomorrow ($tomorrow) Data Available" : ""). "

\n"; $bugout .= $thedata[$rec_found -1]. "
\n"; } else { $bugout2 = "\n

Checking second block ...

\n"; $bugout2 .= $thedata[$rec_found -1]. "
\n"; } } if( strpos($aline, "MAXIMUM TEMPERATURE" ) !== false) { if($firstblock !== false) { $bugout2 .= "

* ".$aline."
\n"; $thisDayMax1 = trim(substr($aline, 36, 3), " RM"); // remove white space and possible R[ecord] / M[issing] $thisDayMaxYear1 = trim(substr($aline, 45, 4), " M"); $thisDayMaxAvg1 = trim(substr($aline,26,3), " M"); $bugout2 .= "Record Max. Temp: ".$thisDayMax1."°F @ ".$thisDayMaxYear1."
\nNorm. ".$thisDayMaxAvg1."°F
\n"; } else { $bugout .= "
* ".$aline."
\n"; $thisDayMax = trim(substr($aline, 36, 3), " RM"); // remove white space and possible R[ecord] / M[issing] $thisDayMaxYear = trim(substr($aline, 45, 4), " M"); $thisDayMaxAvg = trim(substr($aline,26,3), " M"); $bugout .= "Record Max. Temp: ".$thisDayMax."°F @ ".$thisDayMaxYear."
\nNorm. ".$thisDayMaxAvg."°F
\n"; } } // END if max if( strpos($aline, "MINIMUM TEMPERATURE" ) !== false) { if($firstblock !== false) { $bugout2 .= "
* ".$aline."
\n"; $thisDayMin1 = trim(substr($aline, 36, 3), " RM"); // remove white space and possible R[ecord] / M[issing] $thisDayMinYear1 = trim(substr($aline, 45, 4), " M"); $thisDayMinAvg1 = trim(substr($aline,26,3), " M"); $bugout2 .= "This Day Record Min. Temp: ".$thisDayMin1."°F @ ".$thisDayMinYear1."
\nNorm. ".$thisDayMinAvg1."°F
\n"; } else { $bugout .= "
* ".$aline."
\n"; $thisDayMin = trim(substr($aline, 36, 3), " RM"); // remove white space and possible R[ecord] / M[issing] $thisDayMinYear = trim(substr($aline, 45, 4), " M"); $thisDayMinAvg = trim(substr($aline,26,3), " M"); $bugout .= "This Day Record Min. Temp: ".$thisDayMin."°F @ ".$thisDayMinYear."
\nNorm. ".$thisDayMinAvg."°F
\n"; break; // we be done, get out of loop } } // END if min } // END firstblock false, rec > 70 } // END foreach if($noaabug) echo $bugout; } // End if err } else { echo "\n"; } // END if file_get_contents } else { // END create new cachefile echo "\n"; } // END noaagrab() // ***************************************************** // now we need to inspect data - repair if necessary - write to cache if necessary // cache should be 'best' data unless new grab value if($thisDayMax != $thisDayCacheMax) { // there is a difference of opinion ! if($thisDayMax) { // it has a value from NOAA grab (preset value is null) $thisDayCacheMax = $thisDayMax; // prepare to save in cache } else { // NO noaa data - do the reverse regardless of cache validity $thisDayMax = $thisDayCacheMax; } } // now do a vailidity check if(!$thisDayMax) { // we couldn't fix it $thisDayMax = -100; // for output echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMax
\n"; } // repeat for all other values ... ($thisDayMaxYear != $thisDayCacheMaxYear ? ($thisDayMaxYear ? $thisDayCacheMaxYear = $thisDayMaxYear : $thisDayMaxYear = $thisDayCacheMaxYear) : ""); if(!$thisDayMaxYear) { // we couldn't fix it $thisDayMaxYear = "----"; echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMaxYear
\n"; } ($thisDayMin != $thisDayCacheMin ? ($thisDayMin ? $thisDayCacheMin = $thisDayMin : $thisDayMin = $thisDayCacheMin) : ""); if(!$thisDayMin) { // we couldn't fix it $thisDayMin = -100; echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMin
\n"; } ($thisDayMinYear != $thisDayCacheMinYear ? ($thisDayMinYear ? $thisDayCacheMinYear = $thisDayMinYear : $thisDayMinYear = $thisDayCacheMinYear) : ""); if(!$thisDayMinYear) { // we couldn't fix it $thisDayMinYear = "----"; echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMinYear
\n"; } ($thisDayMaxAvg != $thisDayCacheMaxAvg ? ($thisDayMaxAvg ? $thisDayCacheMaxAvg = $thisDayMaxAvg : $thisDayMaxAvg = $thisDayCacheMaxAvg) : ""); if(!$thisDayMaxAvg) { // we couldn't fix it $thisDayMaxAvg = -100; echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMaxAvg
\n"; } ($thisDayMinAvg != $thisDayCacheMinAvg ? ($thisDayMinAvg ? $thisDayCacheMinAvg = $thisDayMinAvg : $thisDayMinAvg = $thisDayCacheMinAvg) : ""); if(!$thisDayMinAvg) { // we couldn't fix it $thisDayMinAvg = 100; echo "\n"; if($noaabug) echo "Couldn't get valid thisDayMinAvg
\n"; } // that's the 'today' figures handled // handle tomorrows **** if($thisDayMax1 . $thisDayMaxYear1 . $thisDayMin1 . $thisDayMinYear1 . $thisDayMaxAvg1 . $thisDayMinAvg1 == false) { // nothing valid from noaagrab if($noaabug) echo "\n
NO Tomorrow ($tomorrow) data in noaagrab
\n"; // no point in messing with cache (right or wrong) } else { // re-use today code in a slightly different way to update / repair cache if($thisDayMax1 != $thisDayCacheMax1) { // there is a difference of opinion ! if($thisDayMax1) { // it has a value from NOAA grab (preset value is null) $thisDayCacheMax1 = $thisDayMax1; // prepare to save in cache } } // now do a vailidity check if(!$thisDayCacheMax1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMax1
\n"; } // do the rest ! ($thisDayMaxYear1 != $thisDayCacheMaxYear1 ? ($thisDayMaxYear1 ? $thisDayCacheMaxYear1 = $thisDayMaxYear1 : "") : ""); if(!$thisDayCacheMaxYear1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMaxYear1
\n"; } ($thisDayMin1 != $thisDayCacheMin1 ? ($thisDayMin1 ? $thisDayCacheMin1 = $thisDayMin1 : "") : ""); if(!$thisDayCacheMin1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMin1
\n"; } ($thisDayMinYear1 != $thisDayCacheMinYear1 ? ($thisDayMinYear1 ? $thisDayCacheMinYear1 = $thisDayMinYear1 : "") : ""); if(!$thisDayCacheMinYear1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMinYear1
\n"; } ($thisDayMaxAvg1 != $thisDayCacheMaxAvg1 ? ($thisDayMaxAvg1 ? $thisDayCacheMaxAvg1 = $thisDayMaxAvg1 : "") : ""); if(!$thisDayCacheMaxAvg1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMaxAvg1
\n"; } ($thisDayMinAvg1 != $thisDayCacheMinAvg1 ? ($thisDayMinAvg1 ? $thisDayCacheMinAvg1 = $thisDayMinAvg1 : "") : ""); if(!$thisDayCacheMinAvg1) { // we couldn't fix it echo "\n"; if($noaabug) echo "Couldn't get valid thisDayCacheMinAvg1
\n"; } } // end tomorrows // now handle cache file ... if($noaagrab || $noaabug) { // something was updated or fixed ! // if($noaagrab) { // something was updated or fixed ! // MUST have today values, use corrected Cache values (may contain "") ... $recout = $serverDay.",".$thisDayCacheMax.",".$thisDayCacheMaxYear.",".$thisDayCacheMin.",".$thisDayCacheMinYear.",".$thisDayCacheMaxAvg.",".$thisDayCacheMinAvg."\n"; // **** final cache tomorrow valid check (at least one value) if($thisDayCacheMax1 . $thisDayCacheMaxYear1 . $thisDayCacheMin1 . $thisDayCacheMinYear1 . $thisDayCacheMaxAvg1 . $thisDayCacheMinAvg1 == false) { // nothing valid in cache if($noaabug) echo "\n
NO Tomorrow ($tomorrow) data for NEW cache
\n"; } else { $recout .= $tomorrow.",".$thisDayCacheMax1.",".$thisDayCacheMaxYear1.",".$thisDayCacheMin1.",".$thisDayCacheMinYear1.",".$thisDayCacheMaxAvg1.",".$thisDayCacheMinAvg1."\n"; } file_put_contents("noaarecdat.txt", $recout); $bugout3 = "

NEW cache out: ".$recout."

"; echo "\n"; } // END MAIN PROG if($noaabug) {?>

Output decided by program:

This Day

°F

°F
Optional

°F

°F
".$all; } else { // echo $stage2[0]; } ?>