\r";
/*------------------------------------------------
//WU-History.php
//PHP script by Jim McMurry - jmcmurry@mwt.net - jcweather.us
//Current version maintained at http://jcweather.us/scripts.php
//Version 1.0 February 18, 2008
// 1.1 February 19, 2008 - Fixed a broken link to WU & added optional selection of other stations.
// 1.2 February 20, 2008 - Fixed an obscure link bug dealing with the new "other station" option.
// 1.3 February 22, 2008 - Added option to omit cloud conditions from tabular listing. Fixed solar & rain issue in the summary section.
// 1.4 February 22, 2008 - Supressed Solar & UV graphs in monthly & longer modes. Supressed current conditions in the summary except when viewing current day.
// 1.5 February 23, 2008 - Fixed precip error when viewing previous days. Added optional top link in header to return to "today" when off somewhere else.
// 1.6 February 27, 2008 - Fixed some missing tags that were missing in the English mode that were preventing validation.
// 1.7 March 25, 2008 - Removed a superfluous
tag that caused a failed validation when using the option for multiple stations.
// 1.8 April, 05, 2008 - added Settings.php awareness to script (K True/saratoga-weather.org) for Carterlake/WD/AJAX/PHP templates.
// 1.9 April 07, 2008 - Made the tabular data listing optional.
// 2.0 December 23, 2008 - Fixes bugs in code to determine units output. (Thanks to Jozef (Pinto) for finding those.
// 2.1 December 23, 2008 - Fixes the modes other than Daily that were affected. (Jozef is now officially a co-author)
// 2.2 January 31, 2009 - Fixes errors in average wind and some metric rain conversions. (Thanks to Paul Gogan for finding these)
// 2.3 February 10, 2009 - A better fix to the average wind issue and a variable we can turn off when they fix the problem.
// 2.4 April 18, 2009 - Changed cm to mm in tabular section when showing units "both" in daily mode. Added Solar to daily tabular data
// 2.5 April 15, 2010 - Fixed a bug found by Pelle where metric rain wasn't being converted to mm in the summary section
// 2.6 August 8, 2011 - Chenged the above back to get the rain right again due to problem reported by Oebel
// 2.7 February 3, 2012 - Put in a check to preclude selecting a future date which was causing problems for Wunderground
// 2.8 October 10, 2013 - Fixed the spacing on some of the units. Found by Han
// 2.9 December 1, 2013 - Fixed first of the week/month or other times that there's no data in the csv file. Found by Han
// 3.0 December 29, 2014 - WU doesn't remove deleted lines, but makes temps -999 so this removes those. Found by Jerry Callahan
// 3.1 April 28, 2015 - Jachym found - If data is missing WU will usually report -999.9 or -573.3 (that is -999.9 F converted to C....)
// 3.2 January 28, 2016 - ereg_replace changed to str_replace for PHP 7.0. Thanks Wim
// 3.3 March 4, 2016 - Changed code for getting WU data. Thanks once again Wim
//
//Portions of the code was borrowed from:
//Weather Underground - wunderground.com
//Ken True - saratoga-weather.org
//Tom Chaplin - carterlake.org
//Kevin Reed - tnetweather.com
//and probably several others.
//This script retrieves a weather station's raw data from Weather Underground
//and displays it in a similar way to how they do it on their site.
//Weather Underground is very willing to share the graphics and data
//on their site with those who provide weather data to them.
//In a correspondence with Ken True, John Celenza, Director of Weather
//Technology at Wunderground stated:
//"Please feel free to use Wunderground images and data on personal sites,
//as long as you link those images to Wunderground or give direct credit.
//Something like 'This image courtesy of Weather Underground' is appropriate."
//This script does NOT generate a complete HTML page and is intended for use
//ONLY by being included in an existing webpage on your site by:
// include("./WU-History.php");
//You'll also have to place the following in the section of your calling
//page:
//
//See the enclosed test file TestHistory.php to see how this is done.
//If your WU-history files are to be kept in a folder other than where your
//calling page is, you'll have to adjust the above. As an example,
//if your calling page is in the root folder of your web site and the WU files
//have been placed in a folder named /abc/, the paths above would need to be
// ./abc/WU-History.xxx
//If using the optional files below to place information to the right of the
//summary area, the same path will have to be used with those files as well.
//Just remember that the paths are always relative to the calling page.
//The supporting graphics are in the enclosed wuicons folder, and that folder
//must be placed in the same folder with this script. If you wish the icons
//elsewhere, just make the appropriate changes in WU-History.css.
//Optional parameters are normally not necessary, but there are a few available:
//ID=xxxxxxx should you wish to call it with a different station ID for some reason
//day=xx for a day other than today
//month=xx for a month other than this month
//year=xxxx for a different year
//There must be a "?" to signal the beginning of the parameters and "&" between items
//So, if you wished to call it for my station and show last Christmas, you'd use
//WU-History.php?ID=KWIMAUST1&day=25&month=12&year=2007
//Folks with graphic talent may want to modify the css and create graphics in other
//colors in order to come up with different color schemes. If you put something
//together that you'd like to share, please send me copies of the .css and the
//graphics and I'd be happy to add them to this package for others to use.
//This script Is Valid XHTML 1.0 Strict!
$WunderWrong
//settings ----------------------------- */
// Special Temporary Setting. In week, month or year modes, Wundergrund is sending Average Wind in mph instead of km/h. Only affects metric users.
// I'll get the word out if they fix it and you can turn it off.
$WunderWrong = true;
//
$timezone = "America/Chicago"; // Change to your TZ. Ken True has a list available at http://saratoga-weather.org/timezone.txt
$WUID = "KWIMAUST1"; // Your stations Wunderground ID
$units = "B"; // Default units which are changeable at runtime. "M" for Metric, "E" for English or "B" for Both
$birthday = "02-06-2005"; // Stations first day of operation format dd-mm-yyyy. This will determine years on the date selector.
$gwidth = "500"; // Width of the graph - normally 500
$gtemp = true; // =true if you want the temperature graph =false if no
$gpress = true; // =true if baro graph =false if no
$gwind = true; // =true if wind velocity graph =false if no
$gwindir = true; // =true if wind direction graph =false if no
$grain = true; // =true if rain graph =false if no
$gsolar = true; // =true if solar graph =false if no
$guv = true; // =true if UV graph =false if no
$pwidth = "100%"; // The width of the summary and graph portion of the page (% or px). Normally 100%
// Optional header info
$header = true; // true if you wish to use it
$Langtitle = "Station Historical Data"; // The Bold text at the top
$LcurDay = "Return to Current Day"; // The link back to the current day if off in one of the other modes. "" to disable it.
// Optional footer bar
$footer = true; // true if you wish to have the colored footer bar at the bottom
$LangFtext = "Juneau County Weather"; // Anything you wish or "" for a plain bar
// Optional content to be placed to the right of the Summary/Graph portion of the page
$inboxfile = "./top.htm"; // A file of html to be placed in the right outlined box. Make it "" if not using, or just don't have a file available.
$outboxfile = "./bottom.htm"; // Same but for the area below the right blue box. Paths to the files must be relative to the calling page.
// Optional "Return to Top" link on the right side
$toTop = false; // Most will want this, but some folks have alternative methods.
$LtopPg = "Return to Top";
// Optional selector for other PWS data
$selOthers = false; // true if you wish to show other stations, false if not
$otherIds = array('KWIMAUST1', 'ISILKEBO2', 'IVLAAMSG7', 'IBOUCHES4'); // Only works for PWS - Not Airports!
$otherLocat = array('Mauston, WI', 'Silkeborg, DK', 'Kampenhout, BE', 'Cassis, FR');
// Option to not show sky conditions in the daily tabular listing
$skipSolar = false; // true to skip solar data, false to include them
$skipSky = false; // true to skip sky conditions, false to include them
// Option to not show the tabular data
$skipTab = false; // true if you wish to suppress the tabular data
//
// Language changes follow. If unsure about any of them, try it and see what happens.
$mnthname = array('Nil', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// Next are for the Summary Table
$Langtabs = array('Daily', 'Weekly', 'Monthly', 'Yearly', 'Custom'); // Tabs above the Summary Table
$LangSumHeads = array("Temperature", "Dew Point", "Humidity", "Wind Speed", "Wind Gust", "Wind", "Pressure", "Precipitation", "Solar");
// Headings for the Summary Table
$LangSumCols = array("Current", "High", "Low", "Average");
$LSumfor = "Summary for";
$Lunits = "Units";
$Lboth = "Both";
$Lenglish = "English";
$Lmetric = "Metric";
$Lnext = "Next";
$Lprev = "Previous";
$Ltarget = array("Day", "Week", "Month", "Year");
$Lview = "View";
$LNoData = "No Data Available for This Period"; // added 12/1/13
$Ltab1 = "Tabular Data for"; // Next 5 for Blue bar above the Tabular listing
$Ltab2 = "Weeks Tabular Data";
$Ltab3 = "Months Tabular Data";
$Ltab4 = "Years Tabular Data";
$Ltab5 = "Custom Date Range Tabular Data";
// Next are for the Tabular Table
$Lheadings = array('Time', 'Temperature', 'Dew Point', 'Pressure', 'Wind', 'Wind Speed', 'Wind Gust', 'Humidity', 'Rainfall Rate (Hourly)', 'solar', 'Conditions');
// Headings when in weekly, monthly etc modes
$Lhdngs2 = array("Temp", "Dew Point", "Humidity", "Sea Level Pressure", "Wind", "Gust Speed", "Precip");
$Lcols2 = array("high", "ave", "low", "sum");
$Lcommafile= "Comma Delimited File";
$Lthanks = "Compliments of";
//
// end of settings
//------------------------------------------------
// overrides from Settings.php if available
global $SITE;
if (isset($SITE['tz'])) {$timezone = $SITE['tz'];}
if (isset($SITE['WUID'])) {$WUID = $SITE['WUID'];}
if (isset($SITE['uomTemp'])) {
$units = preg_match('|C|i',$SITE['uomTemp']) ? 'M':'E';
}
if (isset($SITE['WUunits'])) {$units = $SITE['WUunits'];}
if (isset($SITE['WUstationname'])) {$LangFtext = $SITE['WUstationname'];}
if (isset($SITE['WUbirthday'])) {$birthday = $SITE['WUbirthday'];}
if (isset($SITE['UV'])) {$guv = $SITE['UV'];}
if (isset($SITE['SOLAR'])) {$gsolar = $SITE['SOLAR'];}
if (isset($SITE['timeFormat'])) {$timeFormat = $SITE['timeFormat'];}
// end of overrides from Settings.php if available
if (!function_exists('date_default_timezone_set')) {
putenv("TZ=" . $timezone);
} else {
date_default_timezone_set($timezone);
}
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
//--self downloader --
$filenameReal = __FILE__;
$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;
}
echo $Version;
if(isset($_REQUEST['debug'])) {$debug = true; }
// Set some dates
$mo = date("m");
$da = date("d");
$yr = date("Y");
$FIRST_YEAR = substr($birthday,6,4);
$LAST_YEAR = $yr;
if ($debug) {
print "\n";
print "\n";
}
// Defaults if called without parameters
$PHP_SELF = $_SERVER['PHP_SELF'];
if ( empty($_REQUEST['ID']) )
$_REQUEST['ID']=$WUID;
if ( empty($_REQUEST['day']) )
$_REQUEST['day']=$da;
if ( empty($_REQUEST['dayend']) )
$_REQUEST['dayend']=$da;
if ( empty($_REQUEST['month']) )
$_REQUEST['month']=$mo;
if ( empty($_REQUEST['monthend']) )
$_REQUEST['monthend']=$mo;
if ( empty($_REQUEST['year']) )
$_REQUEST['year']=$yr;
if ( empty($_REQUEST['yearend']) )
$_REQUEST['yearend']=$yr;
if ( empty($_REQUEST['units']) )
$_REQUEST['units']=$units;
if ( empty($_REQUEST['mode']) )
$_REQUEST['mode']=1;
if ($debug) {print "\n";}
//Pass into PHP variables
//------------------------------------------------
$WUID = $_REQUEST['ID'];
$da = $_REQUEST['day'];
$mo = $_REQUEST['month'];
$yr = $_REQUEST['year'];
$da2 = $_REQUEST['dayend'];
$mo2 = $_REQUEST['monthend'];
$yr2 = $_REQUEST['yearend'];
$units = $_REQUEST['units'];
$mode = $_REQUEST['mode'];
print "\n";
// Find out if it's today or in the past
$reqdate = $da . "-" . $mo . "-" . $yr;
$isToday = strtotime($reqdate) == strtotime(date("d-m-Y"));
if (time() < strtotime($mo . "/" . $da . "/" . $yr)) { // preclude any dates in the future that were causing problems for Wunderground
$mo = date("n");
$da = date("j");
$yr = date("Y");
}
// Gather the csv data
$WUgraphstr = "http://www.wunderground.com/cgi-bin/wxStationGraphAll";
$WUdatastr = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp";
if ($mode == 1) {
$wunderstring = $WUdatastr . "?ID=" . $WUID . "&month=" . $mo . "&day=" . $da . "&year=" . $yr . "&format=1&graphspan=day"; // Day
} elseif ($mode == 2) {
$wunderstring = $WUdatastr . "?ID=" . $WUID . "&month=" . $mo . "&day=" . $da . "&year=" . $yr . "&format=1&graphspan=week"; // Week
} elseif ($mode == 3) {
$wunderstring = $WUdatastr . "?ID=" . $WUID . "&month=" . $mo . "&day=" . $da . "&year=" . $yr . "&format=1&graphspan=month"; // Month
} elseif ($mode == 4) {
$wunderstring = $WUdatastr . "?ID=" . $WUID . "&month=" . $mo . "&day=" . $da . "&year=" . $yr . "&format=1&graphspan=year"; // Year
} elseif ($mode == 5) {
$wunderstring = $WUdatastr . "?ID=" . $WUID . "&month=" . $mo . "&day=" . $da . "&year=" . $yr . "&monthend=" . $mo2 . "&dayend=" . $da2 . "&yearend=" . $yr2 . "&format=1&graphspan=custom"; // Custom
}
$csvraw=getcsvWithoutHanging($wunderstring);
//echo $wunderstring;
//print_r($csvraw);
//exit;
$csvdata = array_pure($csvraw); //$csvdata has headings in row 0. Saving a copy for no good reason
echo "\r"; // Thanks to Jerry Callahan for finding this issue
foreach($csvdata as $key => &$line) { // See if there are any deleted entries and remove them
if ($line[1] < -100 || $line[1] > 150) { // If data is missing WU will usually report -999.9 or -573.3 (that is -999.9 F converted to C....)
// if ($line[1] == -999.0) { // Temp is this when the line's been deleted by the user
echo "\r";
unset($csvdata[$key]);
}
}
unset($line); // break the reference with the last element
echo "\r";
$csvarray = $csvdata;
if ($mode == 1){
array_shift($csvarray); // Now $csvarray has nothing but 2D data. The other modes don't need this treatment
if ($csvarray[0][3] > 50) { // Use Baro to determine whether raw data is metric or not
$rawunits = "metric"; // Depends on how the user's wunderground cookie is set
} else {
$rawunits = "english";
}
if ($debug) { echo "\n"; }
} else {
if ($csvarray[0][10] > 50) { // Baro is in a different position in the other modes
$rawunits = "metric";
} else {
$rawunits = "english";
}
if ($debug) {echo "\n";}
}
sizeof($csvarray) > 0 ? $DataGood = true : $DataGood = false; // changed 12/1/13
$wunderCSVstring = str_replace("&","&",$wunderstring); // So the link to the csv output will validate
?>
' . $Langtitle . '
' . "\r";
// Display link back to current day if not currently showing "today"
if (($mode > 1 or ! $isToday) && $LcurDay <> "") {
$callstr = $PHP_SELF . '?ID=' . $WUID . '&month=' . date("m") . '&day=' . date("d") . '&year=' . date("Y") . '&mode=1&units=' . $units;
echo '
\n";
if ($mode == 1) {
$result=temp_stats($csvarray, 1); // Temp
$current=convertTemps($csvarray[count($csvarray)-1][1]);
} else {
$t = temp_stats($csvarray, 1); // Process column 1 of the raw data
$result[0] = $t[0]; // Store just the Hi (of the Hi's)
$t = temp_stats($csvarray, 3); // Then column 2 which are the Lo's
$result[1] = $t[1]; // Store the Lo (of the Lo's)
$t = temp_stats($csvarray, 2); // Lastly proces column 3 which are the ave's
$result[2] = $t[2]; // Store the Ave (of the Ave's)
}
if ($units == "M") {
if ($mode == 1 && $isToday) echo '
' . "\n";
for ($col=0; $col<$columns; $col++) {
$data = $csvarray[$row][$col];
if ($col == 0) {
$pmo = substr($data, 5, 2);
$pda = substr($data, strrpos($data,"-")+1, 2);
$pyr = substr($data, 0, 4);
$tdate = AddDate($pmo, $pda, $pyr, 0); // just to get the name of the day
if ($row !== count($csvarray) - 1) { // ie not the last row
if (substr($data, 0, 7) !== substr($csvarray[$row+1][0], 0, 7)) $needheading = true;
}
$data = '
';
}
if ($col == 13) {
if ($debug) echo $data . " ";
$convarray = convertWind($data); // Returns an array with km in [0] and mi in [1]
if ($WunderWrong) {
if ($rawunits == "metric") $convarray[0] = miTokm($convarray[0],1); // Wunderground is sending in mph vs km/h
}
$data = merge_data($convarray);
/*
if ($rawunits == "metric") $data = miTokm($data,1); // PG 28/01/2009 (WU send mi/h instead of km/h for average wind speed)
*/
$data = '