<?php
require_once("../Settings.php");
require_once("../common.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="post" action="createnoaa.php">
Input Date in yyyy-mm format:
<input type="text" name="reportdate" id="reportdate" size="35" value="<?php echo $_POST['reportdate'] ?>" />
<input type="submit" value="Submit" />
</form>
<div id="cal1Container"></div>


<?php
if ($_POST['reportdate'] != "") {
	if(strlen($_POST['reportdate']) != 7 OR !preg_match('/\d{4}-\d{2}/',$_POST['reportdate'])) {
		echo "Invalid date format, date must be in yyyy-mm format";
		goto invalid;
	}
	include("WCTtags.php");
	$tz = $SITE['tz'];
	date_default_timezone_set($tz);
	$reportdate = $_POST['reportdate'];
	$s = $reportdate."-01 00:00:00";
	$e = $reportdate."-31 23:59:59";
	$db = mysql_connect("your_db_path", "your_db_user", "your_db_password"); 
	mysql_select_db("your_db_name",$db);
	$datesearch = "SELECT * FROM WeatherData WHERE CONVERT_TZ(Time,'UTC','$tz') BETWEEN '$s' AND '$e' ORDER BY Time ASC";
	$result = mysql_query($datesearch);
	$c = 0;
	$curr_day = "01";
	$tot_temp = 0;
	$tot_dew = 0;
	$tot_press = 0;
	$MaxHiT[$curr_day] = 0;
	$MinHiT[$curr_day] = 1000;
	$RainDay[$curr_day] = 0;
	while ($row = mysql_fetch_assoc($result)) {
	
		$local_time = strtotime($row['Time'].'UTC');
		$local_date_time = date('Y-m-d H:i', $local_time);
		$local_date = date('Y-m-d', $local_time);
		$local_month_name = date('M', $local_time);
		$local_month_num = date('m', $local_time);
		$local_year = date('Y', $local_time);
		$local_day_2 = date('d', $local_time);
		$local_day_1 = date('n', $local_time);
		
		if ($local_day_2 == $curr_day) {
			$curr_day = $local_day_2;
			if ($row['Temperature_C'] > $MaxHiT[$curr_day]) {
				$MaxHiT[$curr_day] = $row['Temperature_C'];
				$MaxHiTime[$curr_day] = date('H:i',strtotime($row['Time'].'UTC'));
			}
			if ($row['Temperature_C'] < $MinHiT[$curr_day]) {
				$MinHiT[$curr_day] = $row['Temperature_C'];
				$MinHiTime[$curr_day] = date('H:i',strtotime($row['Time'].'UTC'));
			}
			if ($row['RawRain_mm'] > $RainDay[$curr_day]) $RainDay[$curr_day] = $row['RawRain_mm'];
			$tot_temp = $tot_temp + $row['Temperature_C'];
			$tot_dew = $tot_dew + $row['Dewpoint_C'];
			$tot_press = $tot_press + $row['Pressure_mB'];
			$c++;
		} else {
			$Ave_Temp[$curr_day] = 9/5 * ($tot_temp/$c) + 32;
			$Ave_dew[$curr_day] = 9/5 * ($tot_dew/$c) + 32;
			$Ave_press[$curr_day] = $tot_press/$c;
			$deg_days = $Ave_Temp[$curr_day]-65;
			if ($deg_days < 0) $heat_days[$curr_day] = sprintf("%04.1f",abs($deg_days)); else $heat_days[$curr_day] = sprintf("%04.1f",0);
			if ($deg_days > 0) $cool_days[$curr_day] = sprintf("%04.1f",$deg_days); else $cool_days[$curr_day] = sprintf("%04.1f",0);
			$curr_day = $local_day_2;
			$c = 0;
			$tot_temp = 0;
			$tot_dew = 0;
			$tot_press = 0;
			$MaxHiT[$curr_day] = 0;
			$MinHiT[$curr_day] = 1000;
			$RainDay[$curr_day] = 0;
			if ($row['Temperature_C'] > $MaxHiT[$curr_day]) {
				$MaxHiT[$curr_day] = $row['Temperature_C'];
				$MaxHiTime[$curr_day] = date('H:i',strtotime($row['Time'].'UTC'));
			}
			if ($row['Temperature_C'] < $MinHiT[$curr_day]) {
				$MinHiT[$curr_day] = $row['Temperature_C'];
				$MinHiTime[$curr_day] = date('H:i',strtotime($row['Time'].'UTC'));
			}
			if ($row['RawRain_mm'] > $RainDay[$curr_day]) $RainDay[$curr_day] = $row['RawRain_mm'];
			$tot_temp = $tot_temp + $row['Temperature_C'];
			$tot_dew = $tot_dew + $row['Dewpoint_C'];
			$tot_press = $tot_press + $row['Pressure_mB'];
			$c++;
			
		}
	
	
	}
			$Ave_Temp[$curr_day] = 9/5 * ($tot_temp/$c) + 32;
			$Ave_dew[$curr_day] = 9/5 * ($tot_dew/$c) + 32;
			$Ave_press[$curr_day] = $tot_press/$c;
			$deg_days = $Ave_Temp[$curr_day]-65;
			if ($deg_days < 0) $heat_days[$curr_day] = sprintf("%04.1f",abs($deg_days)); else $heat_days[$curr_day] = sprintf("%04.1f",0);
			if ($deg_days > 0) $cool_days[$curr_day] = sprintf("%04.1f",$deg_days); else $cool_days[$curr_day] = sprintf("%04.1f",0);
	
	
	$datesearch = "SELECT * FROM WindData WHERE CONVERT_TZ(Time,'UTC','$tz') BETWEEN '$s' AND '$e' ORDER BY Time ASC";
	$result = mysql_query($datesearch);
	$c = 0;
	$curr_day = "01";
	$tot_wspd = 0;
	$gust[$curr_day] = 0;
	while ($row = mysql_fetch_assoc($result)) {
	
		$local_time = strtotime($row['Time'].'UTC');
		$local_date_time = date('Y-m-d H:i', $local_time);
		$local_date = date('Y-m-d', $local_time);
		$local_month_name = date('M', $local_time);
		$local_month_num = date('m', $local_time);
		$local_year = date('Y', $local_time);
		$local_day_2 = date('d', $local_time);
		$local_day_1 = date('n', $local_time);
		
		if ($local_day_2 == $curr_day) {
			$curr_day = $local_day_2;
			if ($row['WindspeedGust_kmh'] > $gust[$curr_day]) {
				$gust[$curr_day] = $row['WindspeedGust_kmh'];
				$gusttime[$curr_day] = date('H:i',strtotime($row['Time'].'UTC'));
			}
			$tot_wspd = $tot_wspd + $row['Windspeed_kmh'];
			$c++;
		} else {
			$Ave_windspd[$curr_day] = $tot_wspd/$c;
			$curr_day = $local_day_2;
			$c = 0;
			$tot_wspd = 0;
			$gust[$curr_day] = 0;
			if ($row['WindspeedGust_kmh'] > $gust[$curr_day]) $gust[$curr_day] = $row['WindspeedGust_kmh'];
			$tot_wspd = $tot_wspd + $row['Windspeed_kmh'];
			$c++;
			
		}
	
	
	}
			$Ave_windspd[$curr_day] = $tot_wspd/$c;
			
	
	$contents = "<HTML><HEAD>\n<TITLE>Daily NOAA report</TITLE>\n</HEAD><BODY>\n\n<PRE>\n";
	$contents .= "                            MONTHLY CLIMATOLOGICAL SUMMARY for ".$local_month_name.". ".$local_year."\n\n";
	$contents .= "NAME: ".$WX['L']."\tLOCATION: ".$SITE['location']."\n";
	$contents .= "ELEV: ".$WX['Lelevf']."\tLAT: ".$WX['Llat']."\tLONG: ".$WX['Llong']."\n\n";
	$contents .= "                            TEMPERATURE ( ".$WX['TEMPUNITS']."), RAIN (".$WX['RAINUNITS'].".), WIND SPEED (".$WX['WINDSPEEDUNITS'].")\n\n";
	$contents .= "\t\t\t\t\t\tHEAT\tCOOL\t\tAVG\n";
	$contents .= "\tMEAN\t\t\t\t\tDEG\tDEG \t\tWIND\t\t\tDOM\n";
	$contents .= "DAY\tTEMP\tHIGH\tTIME\tLOW\tTIME\tDAYS\tDAYS\tRAIN\tSPEED\tHIGH\tTIME\tDIR\n";
	$contents .= "----------------------------------------------------------------------------------------------------------------------------------------------------------\n";
	
	$filename = "dailynoaareport".$local_day_1.$local_year.".htm";
		
	for ($i=01;$i<=count($MaxHiT);$i++) {
	
		$c = sprintf("%02d",$i);
		$MaxHiT[$c] = 9/5 * $MaxHiT[$c] + 32;
		$MinHiT[$c] = 9/5 * $MinHiT[$c] + 32;
		$RainDay[$c] = $RainDay[$c]* 0.03937;
		$Ave_windspd[$c] = $Ave_windspd[$c]*0.621371;
		$gust[$c] = $gust[$c]*0.621371;
		$Ave_press[$c] = $Ave_press[$c]/33.8637526;
		
		$contents .= $c."\t".sprintf("%04.1f",$Ave_Temp[$c])."\t".sprintf("%05.1f",$MaxHiT[$c])."\t".$MaxHiTime[$c]."\t".sprintf("%04.1f",$MinHiT[$c])."\t".$MinHiTime[$c]."\t".$heat_days[$c]."\t".$cool_days[$c]."\t".sprintf("%05.2f",$RainDay[$c])."\t".sprintf("%04.1f",$Ave_windspd[$c])."\t".sprintf("%04.1f",$gust[$c])."\t".$gusttime[$c]."\t".$wind_dom_dir."\n";
	}
	$contents .= "----------------------------------------------------------------------------------------------------------------------------------------------------------\n";
	$contents .= "</PRE></BODY></HTML>";

//	file_put_contents('test.html', $contents) or die("Unable to create file. Verify the permissions of the directory where you are attempting to save the file.");
	file_put_contents($filename, $contents) or die("Unable to create file. Verify the permissions of the directory where you are attempting to save the file.");
	echo "<br /><br />".$filename." successfully written to directory with the following data:<br /><br />";
	echo $contents;
	invalid:
}
?>
</body>
</html>