#!/usr/bin/awk -f
# $Id$
# $HeadURL$
#
# Example:
#   awk -f exercise_26_smoke_test.awk "$FLO_LOG"
#   awk -f exercise_26_smoke_test.awk "$ED_LOG"
#
# BLE Exercise 26 GPS smoke test.

BEGIN {
    FS = ","

    # Approximate feet per degree latitude.
    # Good enough for a smoke test around Salem, Oregon.
    FEET_PER_DEG_LAT = 364000
}

NR == 1 { next }

$4 != "" && $5 != "" {
    n++

    lat = $4 + 0
    lon = $5 + 0
    gps_age = $6 + 0

    if (n == 1) {
        first_time = $1
        first_lat = lat
        first_lon = lon

        min_lat = max_lat = lat
        min_lon = max_lon = lon
        min_gps_age = max_gps_age = gps_age
    }

dlat_ft = (lat - first_lat) * FEET_PER_DEG_LAT

pi = atan2(0, -1)
first_lat_rad = first_lat * pi / 180
feet_per_deg_lon_here = FEET_PER_DEG_LAT * cos(first_lat_rad)

dlon_ft = (lon - first_lon) * feet_per_deg_lon_here
dist_from_start_ft = sqrt(dlat_ft * dlat_ft + dlon_ft * dlon_ft)

if (dist_from_start_ft > max_dist_from_start_ft) {
    max_dist_from_start_ft = dist_from_start_ft
    max_dist_time = $1
    max_dist_lat = lat
    max_dist_lon = lon
}


    last_time = $1
    last_lat = lat
    last_lon = lon

    if (n % 2 == 0) {
        mid_time = $1
        mid_lat = lat
        mid_lon = lon
    }

    key = sprintf("%.7f,%.7f", lat, lon)
    seen[key]++

    sum_lat  += lat
    sum_lon  += lon
    sum2_lat += lat * lat
    sum2_lon += lon * lon

    sum_gps_age += gps_age
    if (gps_age < min_gps_age) min_gps_age = gps_age
    if (gps_age > max_gps_age) max_gps_age = gps_age

    if (lat < min_lat) min_lat = lat
    if (lat > max_lat) max_lat = lat
    if (lon < min_lon) min_lon = lon
    if (lon > max_lon) max_lon = lon
}

END {
    if (n < 2) {
        print "Not enough coordinate rows"
        exit
    }

    mean_lat = sum_lat / n
    mean_lon = sum_lon / n

    sd_lat = sqrt((sum2_lat - n * mean_lat * mean_lat) / (n - 1))
    sd_lon = sqrt((sum2_lon - n * mean_lon * mean_lon) / (n - 1))

    lat_range_deg = max_lat - min_lat
    lon_range_deg = max_lon - min_lon

    pi = atan2(0, -1)
    mean_lat_rad = mean_lat * pi / 180
    feet_per_deg_lon = FEET_PER_DEG_LAT * cos(mean_lat_rad)

    lat_range_ft = lat_range_deg * FEET_PER_DEG_LAT
    lon_range_ft = lon_range_deg * feet_per_deg_lon

    sd_lat_ft = sd_lat * FEET_PER_DEG_LAT
    sd_lon_ft = sd_lon * feet_per_deg_lon

    diag_ft = sqrt(lat_range_ft * lat_range_ft + lon_range_ft * lon_range_ft)

    printf "rows:                      %d\n", n
    printf "distinct coordinate pairs: %d\n", length(seen)
    printf "\n"

    printf "first: %s  %.7f, %.7f\n", first_time, first_lat, first_lon
    printf "mid:   %s  %.7f, %.7f\n", mid_time,   mid_lat,   mid_lon
    printf "last:  %s  %.7f, %.7f\n", last_time,  last_lat,  last_lon
    printf "\n"

    printf "mean_lat:                  %.8f\n", mean_lat
    printf "mean_lon:                  %.8f\n", mean_lon
    printf "sd_lat:                    %.10f degrees  %.2f ft\n", sd_lat, sd_lat_ft
    printf "sd_lon:                    %.10f degrees  %.2f ft\n", sd_lon, sd_lon_ft
    printf "\n"

    printf "min_lat:                   %.8f\n", min_lat
    printf "max_lat:                   %.8f\n", max_lat
    printf "min_lon:                   %.8f\n", min_lon
    printf "max_lon:                   %.8f\n", max_lon
    printf "\n"

    printf "lat_range:                 %.10f degrees  %.2f ft north/south\n", lat_range_deg, lat_range_ft
    printf "lon_range:                 %.10f degrees  %.2f ft east/west\n", lon_range_deg, lon_range_ft
    printf "bbox_diagonal:             %.2f ft\n", diag_ft
    printf "\n"

    printf "gps_age_ms min/avg/max:    %.0f / %.1f / %.0f\n", min_gps_age, sum_gps_age / n, max_gps_age

printf "max_dist_from_start:       %.2f ft\n", max_dist_from_start_ft
printf "max_dist_time:             %s\n", max_dist_time
printf "max_dist_coord:            %.7f, %.7f\n", max_dist_lat, max_dist_lon
}
