MyWay timeliness study data collection
[busui.git] / labs / myway_timeliness_calculate.php
blob:a/labs/myway_timeliness_calculate.php -> blob:b/labs/myway_timeliness_calculate.php
--- a/labs/myway_timeliness_calculate.php
+++ b/labs/myway_timeliness_calculate.php
@@ -40,8 +40,10 @@
 		echo "error, route '{$obsv['myway_route']}' unknown";
 		continue;
 	}
-	//		:convert timestamp into time of day and date
-	$time = date("H:i:s", strtotime($obsv['time']));
+	// convert timestamp into time of day and date
+// timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html
+	$time = date("H:i:s AESST", strtotime($obsv['time']));
+        $search_time = date("H:i:s", strtotime($obsv['time'])-(30*60)); // 30 minutes margin
 	$date = date("c", strtotime($obsv['time']));
 	$timing_period = service_period(strtotime($date));
 	$potentialStops = getStopsByStopCode($obsv["stop_code"], $obsv["stop_street"]);
@@ -64,7 +66,7 @@
 		echo "error, route '{$obsv["route_full_name"]}' unknown";
 		continue;
 	}
-	echo "Matched route: {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']}<br>";
+	echo "Matched route: {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>";
 	$timeDeltas = Array();
 	foreach ($potentialStops as $potentialStop) {
 		$stopRoutes = getStopRoutes($potentialStop['stop_id'], $timing_period);
@@ -72,10 +74,10 @@
 		foreach ($stopRoutes as $stopRoute) {
 			//Check if this route stops at each stop
 			if ($stopRoute['route_short_name'] . $stopRoute['route_long_name'] == $obsv["route_full_name"]) {
-				echo "Matching route found at {$potentialStop['stop_code']}<br>";
+				echo "Matching route {$stopRoute['route_id']} found at {$potentialStop['stop_code']}<br>";
 				$foundRoute = $stopRoute;
 				//if does get tripstoptimes for this route
-				$trips = getStopTrips($potentialStop['stop_id'], $timing_period, $time);
+				$trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time);
 				foreach ($trips as $trip) {
 					//echo $trip['route_id']." ".$stopRoute['route_id'].";";
 					if ($trip['route_id'] == $stopRoute['route_id']) {
@@ -86,9 +88,11 @@
 						//work out time delta, put into array with index of delta
 						$timeDeltas[] = Array(
 							"timeDiff" => $timeDiff,
-							"stop_code" => $potentialStop['stop_code']
+							"stop_code" => $potentialStop['stop_code'],
+                                                        "stop_sequence" => $timedTrip['stop_sequence']
 						);
-						echo "Found trip at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
+						echo "Found trip {$trip['trip_id']} at stop {$potentialStop['stop_code']} (#{$potentialStop['stop_id']}, sequence #{$trip['stop_sequence']})<br>";
+                                                echo "Arriving at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
 					}
 				}
 				break; // because have found route
@@ -98,6 +102,7 @@
 		if (sizeof($foundRoute) < 1) {
 			//print out that stops/does not stop
 			echo "No matching routes found at {$potentialStop['stop_code']}<br>";
+                        var_dump($stopRoutes);
 		}
 	}
 	//   lowest delta is recorded delta
@@ -107,18 +112,18 @@
 		echo "Lowest difference of " . round($lowestDelta / 60, 2) . " minutes will be recorded for this observation<br>";
 		$observation_id = $obsv['observation_id'];
 		$route_full_name = $obsv['route_full_name'];
-		$myway_route = $obsv['myway_stop'];
 		$stop_code = $timeDeltas[0]["stop_code"];
-		$stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_full_name, myway_route, stop_code, timing_delta, time, date, timing_period)
-				      values (:observation_id, :route_full_name, :myway_route, :stop_code, :timing_delta, :time, :date, :timing_period)");
+		$stop_sequence = $timeDeltas[0]["stop_sequence"];
+		$stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_full_name, stop_code, timing_delta, time, date, timing_period, stop_sequence)
+				      values (:observation_id, :route_full_name, :stop_code, :timing_delta, :time, :date, :timing_period, :stop_sequence)");
 		$stmt->bindParam(':observation_id', $observation_id);
 		$stmt->bindParam(':route_full_name', $route_full_name);
-		$stmt->bindParam(':myway_route', $myway_route);
 		$stmt->bindParam(':stop_code', $stop_code);
 		$stmt->bindParam(':timing_delta', $lowestDelta);
 		$stmt->bindParam(':time', $time);
 		$stmt->bindParam(':date', $date);
 		$stmt->bindParam(':timing_period', $timing_period);
+                $stmt->bindParam(':stop_sequence', $stop_sequence);
 		// insert a record
 		$stmt->execute();
 		if ($stmt->rowCount() > 0) {