if (count($ozoneData) > 0) {
    $timestamp = $ozoneData[0][0];
    // epoch from data obtained from db
}
if (count($ozoneData) > 0) {
    $responseData = array('status' => 'success', 'labels' => $bandHelper->getDesc(), 'timestamp' => $timestamp);
    for ($i = 0; $i < count($ozoneData); $i++) {
        $lat = $ozoneData[$i][1];
        $lng = $ozoneData[$i][2];
        $o3 = floor($ozoneData[$i][3] + 0.5);
        $o3_flag = $ozoneData[$i][4];
        $siteDesc = $ozoneData[$i][5];
        if ($o3_flag != 'K') {
            continue;
        }
        $label_id = $bandHelper->getLabelId($o3);
        $responseData['data'][] = array('label_id' => $label_id, 'lat' => $lat, 'lng' => $lng, 'flag' => $o3_flag, 'value' => $o3, 'site_desc' => $siteDesc);
    }
    // if there wasn't even a single data with K flag, status=fail
    if (empty($responseData['data'])) {
        $responseData['status'] = 'fail';
        unset($responseData['labels']);
        $responseData['message'] = 'No data with K flag';
        $responseData['data'] = $ozoneData;
    }
} else {
    $responseData = array('status' => 'fail', 'message' => 'No data available', 'timestamp' => $timestamp);
}
$end = microtime(true);
if ($requestType == 'json') {
    header('Content-Type: text/javascript');
             $latest_gen_time = $loadedData[0]['gen_time'];
         }
     }
 } else {
     $loadedData = $queryManager->getPointData($t, $pointsOfInterest, $gridReference, $useLatestDataWhenFail);
     if (!empty($loadedData)) {
         $latest_gen_time = $queryManager->getGridUpdatedTime($t);
     }
 }
 $queryTime += microtime(true) - $queryStart;
 // now prepare data
 if (!empty($loadedData)) {
     $latlngData = array();
     // count($pointsOfInterest) should be same as count($loadedData)
     for ($i = 0; $i < count($pointsOfInterest); $i++) {
         $labelId = $bandHelper->getLabelId($loadedData[$i]['o3']);
         $gridPoint = $loadedData[$i]['lat'] . ':' . $loadedData[$i]['lng'];
         $attr = array('ozone_level' => intval($loadedData[$i]['o3']), 'exp' => $loadedData[$i]['exp'], 'gridpoint' => "{$gridPoint}");
         if ($add_gen_time) {
             $attr['gen_time'] = $loadedData[$i]['gen_time'];
         }
         $latlngData[] = array('label_id' => $labelId, 'lat' => $pointsOfInterest[$i][0], 'lng' => $pointsOfInterest[$i][1], 'attr' => $attr);
     }
     $data[] = array('timestamp' => intval($t), 'data' => $latlngData);
 } else {
     // assume there is no data available and do nothing
     //$latlngData = array();
     /*
     	// commented out by TMH 2012-07-19 ; assuming all data are processed in the back end
     
             // query based on timestamp
    $bandHashMap->put($key, doubleval($val));
}
$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);
    $loadedData = $queryManager->getPointData($t, $pointsOfInterest, $gridReference);
    $queryTime += microtime(true) - $queryStart;
    if (!empty($loadedData)) {
        $latlngData = array();
        // count($pointsOfInterest) should be same as count($loadedData)
        for ($i = 0; $i < count($pointsOfInterest); $i++) {
            $labelId = $bandHelper->getLabelId($loadedData[$i]['o3']);
            $gridPoint = $loadedData[$i]['lat'] . ':' . $loadedData[$i]['lng'];
            $latlngData[] = array('label_id' => $labelId, 'lat' => $pointsOfInterest[$i][0], 'lng' => $pointsOfInterest[$i][1], 'attr' => array('ozone_level' => $loadedData[$i]['o3'], 'exp' => $loadedData[$i]['exp'], 'gridpoint' => "{$gridPoint}"));
        }
        $data[] = array('timestamp' => $t, 'data' => $latlngData);
    } else {
        // query based on timestamp
        // need to do the following for each of $timestamp to $timestamp2
        $queryStart = microtime(true);
        $allData = $queryManager->getDataForWindBasedInterpolation($t, 6);
        if (empty($allData['ozoneValues'])) {
            continue;
        }
        $queryTime += microtime(true) - $queryStart;
        $r = $driver->getGridData($allData['ozoneStations'], $allData['windStations'], $allData['ozoneValues'], $allData['windSpeed'], $allData['windDirection'], $stepSize);
        $grid = java_values($r);