Skip to content

Commit

Permalink
fix: pass null through true/mag conversions
Browse files Browse the repository at this point in the history
Input value null should result in null output values.

Fixes #120.
  • Loading branch information
tkurki committed Nov 20, 2023
1 parent ec806ac commit 0e54d67
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It currently calculates:
* True Wind Direction (based on AWA and headingTrue)
* Ground Wind Angle and Speed (based on SOG, AWA and AWS)
* Magnetic Wind Direction (based on AWA and headingMagnetic)
* Magnetic Wind Direction (based on wind.directionTrue and magneticVarition)
* Magnetic Wind Direction (based on wind.directionTrue and magneticVariation)
* Wind Shift (experimental)
* Moon illumination and times (based on time and navigation.position)
* Sunlight Times: sunrise, sunriseEnd, goldenHourEnd, solarNoon, goldenHour, sunsetStart, sunset, dusk, nauticalDusk, night, nadir, nightEnd, nauticalDawn, dawn (based on time and navigation.position)
Expand Down
10 changes: 10 additions & 0 deletions calcs/courseOverGroundMagnetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module.exports = function (app, plugin) {
return
}
}
if (
_.isUndefined(courseOverGroundTrue) ||
courseOverGroundTrue === null
) {
return [{ path: 'navigation.courseOverGroundMagnetic', value: null }]
}
var courseOverGroundMagnetic = courseOverGroundTrue - magneticVariation
if (courseOverGroundMagnetic < 0) {
courseOverGroundMagnetic = Math.PI * 2 + courseOverGroundMagnetic
Expand All @@ -36,6 +42,10 @@ module.exports = function (app, plugin) {
{
input: [1.0, 0.1],
expected: [{ path: 'navigation.courseOverGroundMagnetic', value: 0.9 }]
},
{
input: [null, -0.01],
expected: [{ path: 'navigation.courseOverGroundMagnetic', value: null }]
}
]
}
Expand Down
14 changes: 13 additions & 1 deletion calcs/courseOverGroundTrue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module.exports = function (app, plugin) {
return
}
}
if (
_.isUndefined(courseOverGroundMagnetic) ||
courseOverGroundMagnetic === null
) {
return [{ path: 'navigation.courseOverGroundTrue', value: null }]
}
var courseOverGroundTrue = courseOverGroundMagnetic + magneticVariation
if (courseOverGroundTrue < 0) {
courseOverGroundTrue = Math.PI * 2 + courseOverGroundTrue
Expand All @@ -28,6 +34,12 @@ module.exports = function (app, plugin) {
return [
{ path: 'navigation.courseOverGroundTrue', value: courseOverGroundTrue }
]
}
},
tests: [
{
input: [null, 0.01],
expected: [{ path: 'navigation.courseOverGroundTrue', value: null }]
}
]
}
}
11 changes: 10 additions & 1 deletion calcs/headingTrue.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ module.exports = function (app, plugin) {
return
}
}
if (_.isUndefined(heading) || heading === null) {
return [{ path: 'navigation.headingTrue', value: null }]
}
var headingTrue = heading + magneticVariation
if (headingTrue < 0) {
headingTrue = Math.PI * 2 + headingTrue
} else if (headingTrue > Math.PI * 2) {
headingTrue = headingTrue - Math.PI * 2
}
return [{ path: 'navigation.headingTrue', value: headingTrue }]
}
},
tests: [
{
input: [null, 0.01],
expected: [{ path: 'navigation.headingTrue', value: null }]
}
]
}
}
11 changes: 10 additions & 1 deletion calcs/windDirectionMagnetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = function (app, plugin) {
'navigation.magneticVariation'
],
calculator: function (directionTrue, magneticVariation) {
if (directionTrue === null) {
return [{ path: 'environment.wind.directionMagnetic', value: null }]
}
var directionMagnetic = directionTrue - magneticVariation
if (directionMagnetic < 0) {
directionMagnetic = Math.PI * 2 + directionMagnetic
Expand All @@ -17,6 +20,12 @@ module.exports = function (app, plugin) {
return [
{ path: 'environment.wind.directionMagnetic', value: directionMagnetic }
]
}
},
tests: [
{
input: [null, -0.01],
expected: [{ path: 'environment.wind.directionMagnetic', value: null }]
}
]
}
}

0 comments on commit 0e54d67

Please sign in to comment.