mourner / suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
BSD 2-Clause "Simplified" License
3.07k stars 411 forks source link

Moon Positions are Off #102

Open aemkei opened 7 years ago

aemkei commented 7 years ago

I really love this library!

Unfortunately the #getMoonPosition method is returning wrong values.

Note: I'd love to use it for some eclipse visualizations, but so fare I couldn't get the right values.

Take Hopkinsville as an example (the point with the maximum eclipse): https://www.timeanddate.com/eclipse/in/usa/hopkinsville?iso=20170821

The moon and sun position should both be 64° (altitude) and 198° (azimuth).

These are also the values calculated at suncalc.org and mooncalc.org.

But If you pass these values to suncalc.js, we got this:

const lat = 36.8656;
const lng = -87.4886;
const dateTime = new Date("2017-08-21T13:25:00-05:00");
const sun = SunCalc.getPosition(dateTime, lat, lng);
const moon = SunCalc.getMoonPosition(dateTime, lat, lng);

function toDegree(radians) {
  return radians * 180 / Math.PI;
}

var values = {
  sun: {
    azimuth: 180 + toDegree(sun.azimuth),
    altitude: toDegree(sun.altitude)
  },
  moon: {
    azimuth: 180 + toDegree(moon.azimuth),
    altitude: toDegree(moon.altitude)
  }
};

console.log(values);

Logs:

{
  "sun": {
    "azimuth": 197.79,
    "altitude": 64.12
  },
  "moon": {
    "azimuth": 195.05,
    "altitude": 64.01
  }
}
aemkei commented 7 years ago

Playing a bit with the values… Looks like position of moon and sun will match ~2 hours earlier:

...
const dateTime = new Date("2017-08-21T13:25:00-05:00");
...

But the values are still way off to the original.

aemkei commented 7 years ago

Note: had to update the code above to use North as 0° azimuth.

PS: Values from @Fabiz Meeus are more precise.

sun azi:198.1004, alt:63.9873
moon azi:198.1186, alt:63.989
aoizoijoizj commented 7 years ago

Also test this in your example console.log(moon.distance) It should be 372107 km In suncalc it is 375556 km It's a big difference!

mourner commented 7 years ago

@aemkei hey, I don't have enough bandwidth to look into the mismatches now, but I'd suggest using Meeus for precision. Meeus formulas are known to be more precise than what I've used — in fact I even started poking around rewriting this library with these formulas (see meeus branch). Will get to it some day.

aemkei commented 7 years ago

Okay, thanks for the comment! Switching to Meeus was not that hard because the interface is quite similar. I still prefer the structure and source of Suncalc and will definitely come back once that formulas were updated.