// add gen_time to JSON reply
$add_gen_time = 0;
if (!empty($_REQUEST['gen_time'])) {
    $add_gen_time = intval($_REQUEST['gen_time']);
}
// read data from flat file
$use_flat = 0;
if (!empty($_REQUEST['flat'])) {
    $use_flat = intval($_REQUEST['flat']);
}
// Request parsing is completed.. Act according to what has been requested
if ($requestType == 'json' && empty($pointsOfInterest)) {
    sendJsonResponse(array(), $timestamp, $timestamp2, "No latlon specified in request");
    exit(0);
}
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandSchema);
$ozoneBand = $bandHelper->getBand();
//$bandHashMap = new Java("java.util.HashMap");
//foreach($ozoneBand as $key => $val) {
//    $bandHashMap->put($key, doubleval($val));
//}
// latest generated time
$latest_gen_time = -1;
//$queryManager = new QueryManager();
$start = microtime(true);
$grid = array();
$queryTime = 0;
//$driver = new Java('LatLongInterpolation.LatLngDriver', $bandHashMap);
for ($t = $timestamp; $t <= $timestamp2; $t += $interval) {
    $queryStart = microtime(true);
    if ($use_flat) {
if (is_numeric($argv[1]) && is_numeric($argv[2])) {
    $timestamp = getBoundaryTimestamp($argv[1]);
    if (doubleval($argv[2]) == -1) {
        $txnAt = -1;
    } else {
        $txnAt = getBoundaryTimestamp($argv[2]);
    }
} else {
    echo "Usage: " . $argv[0] . " <epoch> <txnepoch> [<bandScheme>]\n";
    echo "Force: " . $argv[0] . " <epoch> -1 [<bandScheme>]\n";
    exit(0);
}
$bandScheme = !empty($argv[3]) && is_numeric($argv[3]) ? intval($argv[3]) : 0;
$grid = array();
$queryTime = 0;
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandScheme);
$ozoneBand = $bandHelper->getBand();
$bandHashMap = new Java("java.util.HashMap");
foreach ($ozoneBand as $key => $val) {
    $bandHashMap->put($key, doubleval($val));
}
//print "# debug: gridExtent: ";
//print_r ($gridExtent);
//print "\n";
//$driver = new Java('LatLongInterpolation.LatLngDriver', $bandHashMap);
// use grid configuration in $gridExtent
//$driver = new Java('LatLongInterpolation.LatLngDriver', $bandHashMap, $gridExtent);
$gridExtentMap = new Java("java.util.HashMap");
foreach ($gridExtent as $key => $val) {
    $gridExtentMap->put($key, doubleval($val));
}
if (isset($_REQUEST['type'])) {
    if (strtolower($_REQUEST['type']) == 'json') {
        $requestType = 'json';
    } elseif (strtolower($_REQUEST['type']) == 'html') {
        $requestType = 'html';
    }
}
$callback = '';
if (!empty($_REQUEST['callback'])) {
    $callback = $_REQUEST['callback'];
}
$bandSchema = 0;
if (!empty($_REQUEST['bandschema'])) {
    $bandSchema = intval($_REQUEST['bandschema']);
}
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandSchema);
$queryManager = new QueryManager();
//db connected.. do remaining stuff
$ozoneData = array();
if (!empty($_REQUEST['timestamp']) && is_numeric($_REQUEST['timestamp'])) {
    $timestamp = $_REQUEST['timestamp'];
    $month = date('m', $timestamp);
    $day = date('d', $timestamp);
    $year = date('Y', $timestamp);
    $hour = date('H', $timestamp);
    $min = date('i', $timestamp);
    $min = intval($min) - intval($min) % 5;
    $timestamp = mktime($hour, $min, 0, $month, $day, $year);
}
$ozoneData = $queryManager->getOzoneForTimestamp($timestamp);
if (count($ozoneData) > 0) {
date_default_timezone_set("America/Chicago");
// central time
require_once 'http://localhost:8082/JavaBridge/java/Java.inc';
include_once 'nebula/QueryManager.php';
include_once 'nebula/GridConfig.php';
include_once 'nebula/DiscreteBandHelper.php';
if (is_numeric($argv[1])) {
    $timestamp = getBoundaryTimestamp($argv[1]);
} else {
    echo "Usage: " . $argv[0] . " <epoch>\n";
    exit(0);
}
$bandScheme = 0;
$start = microtime(true);
$grid = array();
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandScheme);
$ozoneBand = $bandHelper->getBand();
$bandHashMap = new Java("java.util.HashMap");
foreach ($ozoneBand as $key => $val) {
    $bandHashMap->put($key, doubleval($val));
}
$driver = new Java('LatLongInterpolation.LatLngDriver', $bandHashMap);
$driver->setWriteDebugImages(false);
$queryTime = 0;
// query based on timestamp
$readOnly = true;
$queryManager = new QueryManager($readOnly);
$queryStart = microtime(true);
$trailPoints = 6;
$allData = $queryManager->getDataForWindBasedInterpolation($timestamp, $trailPoints);
if (empty($allData['ozoneValues'])) {
if (!empty($_REQUEST['month']) && !empty($_REQUEST['year']) && is_numeric($_REQUEST['month']) && is_numeric($_REQUEST['year'])) {
    $year = intval($_REQUEST['year']);
    $month = intval($_REQUEST['month']);
    $monthDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
} else {
    $year = date('Y');
    $month = date('m');
    $monthDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
}
$monthStartEpoch = mktime(0, 0, 0, $month, 1, $year);
// month values 1,2,3..
$monthEndEpoch = mktime(23, 59, 59, $month, $monthDays, $year);
$queryManager = new QueryManager();
$allData = $queryManager->getMonthlyAvg($monthStartEpoch, $monthEndEpoch);
$bandSchema = 4;
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandSchema);
$ozoneBand = $bandHelper->getBand();
$bandHashMap = new Java("java.util.HashMap");
foreach ($ozoneBand as $key => $val) {
    $bandHashMap->put($key, doubleval($val));
}
$start = microtime(true);
$outFile = '';
$queryTime = 0;
$outFile = '/tmp/binaryout.png';
$driver = new Java('LatLongInterpolation.LatLngDriver', $bandHashMap);
$r = $driver->getContours($allData['ozoneStations'], $allData['windStations'], $allData['ozoneValues'], $allData['windSpeed'], $allData['windDirection'], $stepSize);
$responseObject = prepareResponseObject(java_values($r), $monthStartEpoch);
if (!empty($allData['ozoneStations'])) {
    for ($i = 0; $i < count($allData['ozoneStations']); $i++) {
        $lat = $allData['ozoneStations'][$i][0];
    $callback = preg_replace('/[^][._a-zA-Z0-9]/', '', $_REQUEST['callback']);
}
$bandSchema = 0;
if (!empty($_REQUEST['bandschema'])) {
    $bandSchema = intval($_REQUEST['bandschema']);
}
// forece using JSONP by default
$use_jsonp = true;
// option to return JSON, instead of jsonp
if (isset($_REQUEST['forcejson']) && $_REQUEST['forcejson'] == 1) {
    $use_jsonp = false;
}
//$useGzip = true;
$useGzip = false;
$queryManager = new QueryManager();
$bandHelper = new DiscreteBandHelper(DiscreteBandHelper::LABEL_TYPE_BAND, $bandSchema);
$contourDataStore = new ContourCacheHelper($useGzip, $bandHelper->getSchemeId());
$ozoneBand = $bandHelper->getBand();
//$bandHashMap = new Java("java.util.HashMap");
//foreach($ozoneBand as $key => $val) {
//    $bandHashMap->put($key, doubleval($val));
//}
$timestamp = '';
if (isset($_REQUEST['timestamp']) && is_numeric($_REQUEST['timestamp'])) {
    $timestamp = $_REQUEST['timestamp'];
    $month = date('m', $timestamp);
    $day = date('d', $timestamp);
    $year = date('Y', $timestamp);
    $hour = date('H', $timestamp);
    $min = date('i', $timestamp);
    $min = intval($min) - intval($min) % 5;