\n"; echo "\n"; echo "\n"; # configuration from Saratoga template if available global $SITE; if(file_exists("Settings.php")) {include_once("Settings.php"); } if (isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; } if (isset($SITE['uomTemp'])) {$tUOM = $SITE['uomTemp']; } if (isset($SITE['uomRain'])) {$rUOM = trim($SITE['uomRain']);} $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; print "\n"; if ($cache_age >= $cache_life){ $url2 = gen_url($utcUse); print "\n"; list($html2,$RC) = curl_get_contents($url2); if($html2 === false or $RC <> '200') { print "\n"; $utcUse = $utcYMD2; // use day before $url2 = gen_url($utcUse); print "\n"; list($html2,$RC) = curl_get_contents($url2); } if($html2 !== false and $RC == '200') { file_put_contents($cacheFile2,$html2); echo "\n"; } else { echo "\n"; $filesize = filesize($cacheFile2); $html2 = file_get_contents($cacheFile2); echo "\n"; } } else { echo "\n"; $filesize = filesize($cacheFile2); $html2 = file_get_contents($cacheFile2); } /* NOW looking for stuff in CSV like: stn_id,stn_call,city,state,country,date,lat,lon,elev,tmax,tmin,report_p,final_p,p_flag,num_6hr_p,wxchars,trace,vp,vp_def,slp_6,slp_12,slp_18,slp_0,max_rh,min_rh,at,wc,wspd_3,wspd_6,wspd_9,wspd_12,wspd_15,wspd_18,wspd_21,wspd_24 The CSV Fields are: 0 stn_id 1 stn_call 2 city 3 state 4 country 5 date 6 lat 7 lon 8 elev 9 tmax 10 tmin 11 report_p 12 final_p 13 p_flag 14 num_6hr_p 15 wxchars 16 trace 17 vp 18 vp_def 19 slp_6 20 slp_12 21 slp_18 22 slp_0 23 max_rh 24 min_rh 25 at 26 wc 27 wspd_3 28 wspd_6 29 wspd_9 30 wspd_12 31 wspd_15 32 wspd_18 33 wspd_21 34 wspd_24 Units are C,kts,mb(hPa),mm for temp, wind, baro, rain See: https://ftp.cpc.ncep.noaa.gov/cadb_v2/docs/CADB_Daily_v2_Output_Documentation.pdf for docs */ $worldStations = array(); $reportDate = ''; list($headers2,$content2) = explode("\r\n\r\n",$html2); $rawrecs = explode("\n",$content2); // process the file $idx=0; $datesSeen = array(); $countrySeen = array(); # load up the raw data for further processing .. reject the ones with no country or no city foreach ($rawrecs as $n => $rec) { $V = explode(',',$rec); if(!isset($V[9])) {continue; } $tC =substr($V[4],0,1); if($tC == '-' or $tC == '*') { continue; } if($V[0] == 'stn_id') { continue; } if($V[9] < -999 or $V[10] < -999 or $V[16] < -999) { continue; } if(abs($V[9]-$V[10]) < 2.0) { continue;} if(in_array($V[0],$ignoreStations)) { continue; } $tCountry = $V[4]; $tCountry = strtolower( trim($tCountry) ); $tCountry = str_replace('/','| ',$tCountry); $tCountry = ucwords( str_replace('_',' ', $tCountry) ); $tCountry = str_replace('| ','/',$tCountry); if(!isset($countrySeen[$tCountry])) { $countrySeen[$tCountry] = 1; } else { $countrySeen[$tCountry]++; } if(in_array($tCountry,$ignoreCountrys)) { continue; } if(abs($V[9]-$V[10]) > $tempDiffAllowed) { continue; } $recmarker = $V[13]; if($recmarker > '0') { $tHigh = cvtCtemp($V[9],$tUOM); $tLow = cvtCtemp($V[10],$tUOM); $tPrecip=cvtMrain($V[12],$rUOM); $tPrecipEstim=cvtMrain($V[11],$rUOM); $tData=$V[15]; $tState=$V[3]; if(substr($tState,0,1) == '-') {$tState = ''; } $tStation=$V[2]; $tStation = strtolower( trim($tStation) ); $tStation = str_replace('/','| ',$tStation); $tStation = ucwords( str_replace('_',' ', $tStation) ); $tStation = str_replace('| ','/',$tStation); if(!isset($datesSeen[$V[5]])) { $datesSeen[$V[5]] = 1; } else { $datesSeen[$V[5]]++; } if(isset($_REQUEST['debug2'])) { print "\n"; print "\n"; } $worldStations[$idx] = join("\t",array($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation,$tCountry)); $idx++; } } //print "\n"; //$tDate = array_key_first($datesSeen); // php 7 only $rDates = array_keys($datesSeen); // get the date(s) seen $tDate = array_pop($rDates); // get first one $utcUse = substr($tDate,0,4).'-'.substr($tDate,4,2).'-'.substr($tDate,6,2); $reportDate = gmdate($reportDateFormat,strtotime($utcUse.'T1200')); print "\n"; // now scan for the highs/lows list($worldhigh,$worldlow,$worldprecip) = scan_for($worldStations,'','',$tUOM,$rUOM); $omittedCountry = join(',',$ignoreCountrys); print "\n"; print "\n"; print "\n"; print "\n"; list($countryhigh,$countrylow,$countryprecip) = scan_for($worldStations,$selectedCountry,'',$tUOM,$rUOM); print "\n"; print "\n"; print "\n"; print "\n"; list($usahigh,$usalow,$usaprecip) = scan_for($worldStations,'United States','',$tUOM,$rUOM); print "\n"; print "\n"; print "\n"; print "\n"; list($statehigh,$statelow,$stateprecip) = scan_for($worldStations,'United States',$selectedState,$tUOM,$rUOM); print "\n"; print "\n"; print "\n"; print "\n"; $stateReportDate = $reportDate; // for compatibility with stateextremes.php $selectState = $selectedState; // for compatibility with stateextremes.php if(isset($_REQUEST['list'])) { print "
List of available country names in CPS data\n";
	print "Use in this script as \$selectedCountry='name'; in settings area\n\n";
	ksort($countrySeen);
	foreach ($countrySeen as $name => $obs) {
	  print "'$name'\t($obs observations)\n";
	}
	print "
\n"; } # end of main program # --------------------------------------------------------------------- # functions # --------------------------------------------------------------------- function scan_for($worldStations,$country,$state,$tUOM,$rUOM) { $worldhighStation = ''; $worldhighValue = -999; $worldlowStation = ''; $worldlowValue = 999; $worldprecipStation = ''; $worldprecipValue = 0; if (count($worldStations) > 0) { $worldhigh = ''; $worldlow = ''; $worldprecip = ''; } else { $worldhigh = 'N/A'; $worldlow = 'N/A'; $worldprecip = 'N/A'; } $selectLower48 = false; if($country == 'United States' and $state == '') { $selectLower48 = true; } foreach ($worldStations as $n => $vals) { list($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation,$tCountry) = explode("\t",$vals); if(strlen($country) > 0 and strcasecmp($tCountry,$country) !== 0) { continue; } if(strlen($state) > 0 and strcasecmp($tState,$state) !== 0) { continue; } if($selectLower48 and in_array($tState,array('AK','HI')) ) { continue; } if($tHigh < -90 or $tLow < -90 ) { continue; } if($tHigh > $worldhighValue) { if(isset($_REQUEST['debug'])) { print "\n"; } $worldhighValue = $tHigh; $worldhighStation = $n; // remember for later } if($tLow < $worldlowValue) { if(isset($_REQUEST['debug'])) { print "\n"; } $worldlowValue = $tLow; $worldlowStation = $n; // remember for later } if($tPrecip > $worldprecipValue) { if(isset($_REQUEST['debug'])) { print "\n"; } $worldprecipValue = $tPrecip; $worldprecipStation = $n; } } if(isset($_REQUEST['debug'])) { print "\n"; print "\n"; print "\n"; if($worldprecipValue > 0.0) { print "\n"; } else { print "\n"; } } $showState = (strlen($state)>0)?true:false; $showCountry = (strlen($country) == 0)?true:false; if(strcasecmp('United States',$country) == 0 and strlen($state) < 1) { $showState = true; $showCountry = false; } // now pass through to see if any duplicate high, low, high precip exists and format the strings foreach ($worldStations as $n => $vals) { list($tHigh,$tLow,$tPrecip,$tPrecipEstim,$tData,$tState,$tStation,$tCountry) = explode("\t",$vals); if(strlen($country) > 0 and strcasecmp($tCountry,$country) !== 0) {continue; } if(strlen($state) > 0 and strcasecmp($tState,$state) !== 0) {continue; } if($tHigh < -90 or $tLow < -90 ) { continue; } if($selectLower48 and in_array($tState,array('AK','HI')) ) { continue; } if($tHigh == $worldhighValue) { $worldhigh .= "$tHigh$tUOM at $tStation"; $worldhigh .= $showState?", $tState":""; $worldhigh .= $showCountry?", $tCountry":""; $worldhigh .= "\n"; } if($tLow == $worldlowValue) { $worldlow .= "$tLow$tUOM at $tStation"; $worldlow .= $showState?", $tState":""; $worldlow .= $showCountry?", $tCountry":""; $worldlow .= "\n"; } if($tPrecip == $worldprecipValue and $tPrecip > 0) { $tPrecipFmt = $tPrecip; $worldprecip .= "$tPrecipFmt$rUOM at $tStation"; $worldprecip .= $showState?", $tState":""; $worldprecip .= $showCountry?", $tCountry":""; $worldprecip .= "\n"; } } // Make results HTML pretty if(strlen($worldprecip) < 10) { $worldprecip = "none\n"; } $worldhigh = str_replace("\n","
\n",$worldhigh); $worldlow = str_replace("\n","
\n",$worldlow); $worldprecip = str_replace("\n","
\n",$worldprecip); return(array($worldhigh,$worldlow,$worldprecip)); } # --------------------------------------------------------------------- function check_ignore_station($str, array $arr) { foreach($arr as $a) { if (stripos($str,$a) !== false) {return true;} } return false; } # --------------------------------------------------------------------- function cvtCtemp($val,$uom) { //return($val); if(strpos($uom,'F') !== false) { $out = round(1.8 * $val + 32); } else { $out = $val; } return($out); } # --------------------------------------------------------------------- function cvtMrain($val,$uom) { //return($val); if(strpos($uom,'in') !== false) { $out = sprintf("%01.2f",$val / 25.4,2); } else { $out = $val; } return($out); } # --------------------------------------------------------------------- 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 (worldextremes.php - saratoga-weather.org)"); $data = curl_exec($ch); $cinfo = curl_getinfo($ch); // get info on curl exec. $RC = $cinfo['http_code']; $Status = "\n"; echo $Status; if($data === false or $RC <> '200') { echo "\n"; } else { echo "\n"; } curl_close($ch); return (array($data,$RC)); } # --------------------------------------------------------------------- function gen_url($date) { $urlTemplate = "https://ftp.cpc.ncep.noaa.gov/cadb_v2/daily/%s/%s/daily_summary_%s%s%s_v2.csv"; list($YY,$MM,$DD) = explode('-',$date); $outUrl = sprintf($urlTemplate,$YY,$MM,$YY,$MM,$DD); return($outUrl); }