<?php ini_set('display_errors', 1);
session_start();
$stations = $_GET['Stations'];
$station_initials = $_GET['StationInitials'];
$year = $_GET['Year'];
$month = $_GET['Month'];
$day = $_GET['Day'];
$from_hour = $_GET['FromHour'];
$from_minute = $_GET['FromMinute'];
$to_hour = $_GET['ToHour'];
$to_minute = $_GET['ToMinute'];
$plot_format = $_GET['PlotFormat'];
$spectrogram_freq = $_GET['SpectrogramFreq'];
$spectrogram_nfft = $_GET['SpectrogramNfft'];
$spectrogram_step = $_GET['SpectrogramStep'];
$timeseries_min = $_GET['TimeSeriesMin'];
$timeseries_max = $_GET['TimeSeriesMax'];
$colorscale_max = $_GET['ColorScaleMax'];
$colorscale_min = $_GET['ColorScaleMin'];
/*$spectrogram_min = $_GET['SpectrogramMin'];
$spectrogram_max = $_GET['SpectrogramMax'];
*/
//$current_date = date("Ymd_His_");
error_reporting(E_ALL);


// set date, start time and end time
$date = "";
$date .= $year;
if ($month < 10)
	$date .= "0" . $month;
else 
	$date .= $month;
if ($day < 10)
	$date .= "0" . $day;
else 
	$date .= $day;

$start_time = "";	
if ($from_hour < 10)
	$start_time .= "0" . $from_hour;	
else
	$start_time .= $from_hour;	
if ($from_minute < 10)
	$start_time .= "0" . $from_minute;	
else
	$start_time .= $from_minute;	
$start_time .= "00";

$end_time = "";
if ($to_hour < 10)
	$end_time .= "0" . $to_hour;	
else
	$end_time .= $to_hour;	
if ($to_minute < 10)
	$end_time .= "0" . $to_minute;	
else
	$end_time .= $to_minute;
$end_time .= "00";

// set file path and file name
$file_name = $date . "_" . $start_time . "_" . $end_time . "_" . $station_initials;
if ($plot_format == 0)
	$file_name .= ".jpg";
else if ($plot_format == 1)
	$file_name .= ".ps";

$idlsav_path = "/home/mirl/idl/web_plot_wrapper"; // idl sav file path
$database_dir = "/mirl/ULF/Database"; // database path
$file_dir = "/var/www/html/ULF/query_plot"; // plot file path
$file_path = $file_dir . "/" . $file_name;

/* new code that runs idl sav process
$command = "/usr/local/bin/idl ";
$command .= $idlsav_path;
$command .= " -args";
$command .= " " . $database_dir;
$command .= " " . $date;
$command .= " " . $start_time;
$command .= " " . $end_time;
$command .= " " . $file_dir;
$command .= " " . $plot_format;
$command .= " " . $stations[0];
$command .= " " . $stations[1];
$command .= " " . $stations[2];
$command .= " " . $stations[3];
$command .= " " . $stations[4];
$command .= " " . $stations[5];
$command .= " " . $stations[6];
$command .= " " . $stations[7];
$command .= " " . $spectrogram_freq;
$command .= " " . $spectrogram_nfft;
$command .= " " . $spectrogram_step;
$command .= " " . $timeseries_max;
$command .= " 0";
$command .= " " . $spectrogram_freq . "\" "; */

// old code meant for BatchModuleApp.exe
$command = "/usr/local/bin/idl ";
$command .= " \"" . $idlsav_path . "\" ";
$command .= " -args ";
$command .= " \"" . $database_dir . "\" ";
$command .= " \"" . $date . "\" ";
$command .= " \"" . $start_time . "\" ";
$command .= " \"" . $end_time . "\" ";
$command .= " \"" . $file_dir . "\" ";
$command .= " \"" . $file_path . "\" ";
$command .= " \"0\" ";
$command .= " \"" . $plot_format . "\" ";
$command .= " \"" . $stations . "\" ";
$command .= " \"" . $spectrogram_freq . "\" ";
$command .= " \"" . $spectrogram_nfft . "\" ";
$command .= " \"" . $spectrogram_step . "\" ";
$command .= " \"0\" ";
$command .= " \"" . $spectrogram_freq . "\" ";
$command .= " \"" . $timeseries_min . "\" ";
$command .= " \"" . $timeseries_max . "\" ";
$command .= " \"" . $colorscale_max . "\" ";
$command .= " \"" . $colorscale_min . "\" >> /dev/null ";



//chdir($idlrt_dir);  dont need because idl directory is included into the system environment
/* echo $command; */
echo "<br>";
system($command);
echo $file_path;
$plot_result = file_exists($file_path);
/*
$plot_result = mirl_batchmod_genplot(
	$idlsav_path, $database_dir, 
	$stations, $date, $start_time, $end_time, 
	$file_dir, $file_path, 0, $plot_format, 
	$spectrogram_freq, $spectrogram_nfft, $spectrogram_step, 
	0, $spectrogram_freq, $timeseries_min, $timeseries_max);
*/
?>


<html>
<head>
<title>ULF Plot Query Result</title>

<script type="text/javascript">

function Done(filename)
{
	document.location.href="ulf_plotcleanup.php?filename=" + filename;
}

</script>
</head>

<body>

<!-- Banner Start -->
<table border="0" cellpadding="0" cellspacing="0" height="80" width="100%" bgcolor="#004E9B">
	<tr>
		<td>
		<img src="mirl_logo.jpg" width="94" height="100%">
		</td>
		<td align="left">
		<font style="font-family:arial; font-size:20px; color:#FFFFFF"><b>ULF Magnetometer Database Query and Plot Generator<br>
		<font style="font-family:arial; font-size:18px; color:#FFFFAA">Magnetosphere and Ionosphere Research Laboratory at UNH
		</td>
	</tr>
</table>
<!-- Banner End -->

<!-- Header Start -->
<div id="header">
<h3>Plot result: 
<?php 
if ($plot_result == false) { 
	echo("Failure"); 
} 
else { 
	$file_name = $date . "_" . $start_time . "_" . $end_time . "_" . $station_initials . ".pdf";
	echo("Completed<br>");
	echo("Click to download: ");
	echo("<a href='query_plot/" . $file_name . "'> " . $file_name . " </a><br><br>");
	
	echo("When you download the generated postscript file, click the 'done' button below<br><br>");
	echo("<input type=\"button\" name=\"Done\" value=\"Done\" onclick=\"Done('" . $file_name . "');\">");
}
?>
</h3>
</div>
<!-- Header End -->




</body>
</html>
