Skip to content

Commit

Permalink
fixed nan issue
Browse files Browse the repository at this point in the history
  • Loading branch information
John Jones committed Sep 9, 2021
1 parent 34330a2 commit f3086e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
28 changes: 12 additions & 16 deletions dashboard/dashboard/computed.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func basePressure(stream FlightData) float64 {
for _, v := range pressures {
sum += v
}
return sum / float64(len(pressures))
return nanSafe(sum / float64(len(pressures)))
}
}
return 0
Expand All @@ -25,38 +25,38 @@ func altitude(bp float64, raw RawDataSegment) float64 {
if bp == 0 {
return 0
}
return 44307.7 * (1 - math.Pow((raw.Pressure/100)/bp, 0.190284))
return nanSafe(44307.7 * (1 - math.Pow((raw.Pressure/100)/bp, 0.190284)))
}

func normalizedPressure(raw RawDataSegment) float64 {
return raw.Pressure / 100.0
return nanSafe(raw.Pressure / 100.0)
}

func velocity(stream FlightData, bp float64, raw RawDataSegment) float64 {
altitude := altitude(bp, raw)
segments := stream.AllSegments()
for i := len(segments) - 1; i >= 0; i -= 1 {
if segments[i].Computed.Altitude != altitude {
return (altitude - segments[i].Computed.Altitude) / (raw.Timestamp - segments[i].Raw.Timestamp)
return nanSafe((altitude - segments[i].Computed.Altitude) / (raw.Timestamp - segments[i].Raw.Timestamp))
}
}
return 0.0
}

func yaw(raw RawDataSegment) float64 {
return math.Atan2(-1.0*raw.Acceleration.X, raw.Acceleration.Z) * (180.0 / math.Pi)
return nanSafe(math.Atan2(-1.0*raw.Acceleration.X, raw.Acceleration.Z) * (180.0 / math.Pi))
}

func pitch(raw RawDataSegment) float64 {
return math.Atan2(-1.0*raw.Acceleration.Y, raw.Acceleration.Z) * (180.0 / math.Pi)
return nanSafe(math.Atan2(-1.0*raw.Acceleration.Y, raw.Acceleration.Z) * (180.0 / math.Pi))
}

func toRadians(degrees float64) float64 {
return degrees * math.Pi / 180
return nanSafe(degrees * math.Pi / 180)
}

func toDegrees(radians float64) float64 {
return radians * 180 / math.Pi
return nanSafe(radians * 180 / math.Pi)
}

func bearing(origin Coordinate, raw RawDataSegment) float64 {
Expand All @@ -73,7 +73,7 @@ func bearing(origin Coordinate, raw RawDataSegment) float64 {
x := math.Cos(startLat)*math.Sin(destLat) - math.Sin(startLat)*math.Cos(destLat)*math.Cos(destLng-startLng)
brng := math.Atan2(y, x)
brng = toDegrees(brng)
return math.Mod(brng+360, 360)
return nanSafe(math.Mod(brng+360, 360))
}

func distance(origin Coordinate, raw RawDataSegment) float64 {
Expand All @@ -87,7 +87,7 @@ func distance(origin Coordinate, raw RawDataSegment) float64 {
Δλ := (raw.Coordinate.Lon - origin.Lon) * math.Pi / 180
a := math.Sin(Δφ/2)*math.Sin(Δφ/2) + math.Cos(φ1)*math.Cos(φ2)*math.Sin(Δλ/2)*math.Sin(Δλ/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return R * c
return nanSafe(R * c)
}

func dataRate(stream FlightData) float64 {
Expand All @@ -104,11 +104,7 @@ func dataRate(stream FlightData) float64 {
for _, secondTotal := range totalsMap {
total += secondTotal
}
rate := total / float64(len(totalsMap))
if math.IsNaN(rate) {
return 0
}
return rate
return nanSafe(total / float64(len(totalsMap)))
}

func averageComputedValue(seconds float64, stream FlightData, raw RawDataSegment, computed ComputedDataSegment, accessor func(seg ComputedDataSegment) float64) float64 {
Expand All @@ -120,7 +116,7 @@ func averageComputedValue(seconds float64, stream FlightData, raw RawDataSegment
n++
i--
}
return total / n
return nanSafe(total / n)
}

func determineFlightMode(stream FlightData, raw RawDataSegment, computed ComputedDataSegment) FlightMode {
Expand Down
9 changes: 9 additions & 0 deletions dashboard/dashboard/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dashboard

import "math"

func singleFlightDataElement(ds FlightData, accessor func(DataSegment) float64) []float64 {
data := make([]float64, len(ds.AllSegments()))
for i, segment := range ds.AllSegments() {
Expand All @@ -11,3 +13,10 @@ func singleFlightDataElement(ds FlightData, accessor func(DataSegment) float64)
func smoothed(alpha float64, xt float64, stm1 float64) float64 {
return alpha*xt + (1-alpha)*stm1
}

func nanSafe(val float64) float64 {
if math.IsNaN(val) {
return 0.0
}
return val
}

0 comments on commit f3086e8

Please sign in to comment.