//require_once("$site_root/form-validate.php");
if ($_REQUEST['mode'] == 'idw') {
    $modeltype = 'idw';
} else {
    $modeltype = 'ordk';
}
// These are manual data
$lat = -33.818884;
$lng = 151.033354;
$radius = 50;
$datetime = '2010-10-07 14:00';
$numresults = 500;
$pollutant = 'pm10';
$maxfit = 'true';
//$array_gen_start = microtime(true);
$array = model_location_and_time_array($lat, $lng, $radius, $datetime, $numresults, $pollutant, $maxfit);
//$array_gen_stop = microtime(true);
//$array_gen_time = $array_gen_stop - $array_gen_start;
//header("Content-type: text/html");
//echo "Took array in $array_gen_time s from $array_gen_start s to $array_gen_stop  <br />";
//$grid_start = microtime(true);
$grid = test_model_pollutant_grid($lat, $lng, $radius, $datetime, $numresults, $pollutant, $modeltype, $maxfit);
//$grid_stop = microtime(true);
//$grid_time = $grid_stop - $grid_start;
//echo "Query database, interpolate and generate grid array in $grid_time s <br />" /*from $grid_start to $grid_stop  <br />"*/;
$draw_start = microtime(true);
$img = test_draw_gnuplot($grid);
$draw_stop = microtime(true);
$draw_time = $draw_stop - $draw_start;
//   echo '<img src="data:image/jpeg;base64>'.base64_encode(imagejpeg($img));
//	echo "</img>";
function model_pollutant_points_for_locations_and_times_custom_window($latitudes, $longitudes, $datetimes, $pollutant, $time_window_size, $location_window_size)
{
    $time_window_start = 0;
    $location_window_start_lat = 0;
    $location_window_start_long = 0;
    $window_array = NULL;
    $final_values = array();
    for ($i = 0; $i < count($datetimes); $i++) {
        $t = strtotime($datetimes[i]);
        $dx = ($latitudes[$i] - $location_window_start_lat) * 110.8;
        // 110.8 km per degree of latitude
        $dy = ($longitudes[$i] - $location_window_start_long) * 96.5;
        // 96.5 km per degree of longitude
        if ($t - $time_window_start > $time_window_size || $dx > $location_window_size || $dy > $location_window_size) {
            // new window
            $location_window_start_lat = $latitudes[$i];
            $location_window_start_long = $longitudes[$i];
            $time_window_start = $t;
            $window_array = model_location_and_time_array($latitudes[$i], $longitudes[$i], $location_window_size * 2, $datetimes[$i], 200, $pollutant, true);
        }
        $value = calc_pollutant_idw($latitudes[$i], $longitudes[$i], $pollutant, $window_array);
        array_push($final_values, $value);
    }
    return $final_values;
}