andrmoel / astronomy-bundle-js

This library provides tools and methods for astronomical calculations. With this bundle it is possible to calculate the position of moon, sun and planets and several coordinate systems. For a higher accuracy, several corrections, like nutation and precision, were taken into account. It is also possible to calculate rise, set and culmination events for celestial objects.
42 stars 9 forks source link

getUpcomingLastQuarter skipping actual next date of last quarter moon phase #21

Open swgordon opened 3 years ago

swgordon commented 3 years ago

If I run this

const startDate: Date = new Date(Date.UTC(2021, 1, 1));
const toi = createTimeOfInterest.fromDate(startDate);
const lastQuarterDate = createMoon(toi).getUpcomingLastQuarter();

Assuming I am not missing something, lastQuarterDate should be on the 4th of Feb 2021. Instead, what I am getting is the 6th of March 2021, which is the date of the last quarter moon phase after the upcoming last quarter. (library version is 5.17.8)

hjy1210 commented 3 years ago

quote from MDN docs : Date

monthIndex
Integer value representing the month, beginning with 0 for January to 11 for December.

So new Date(Date.UTC(2021, 1, 1)) cooresponding to Feb. 1, 2021 new Date(Date.UTC(2021, 0, 1)) cooresponding to Jan. 1, 2021

andrmoel commented 3 years ago

@hjy1210 thank. That is correct. I do not recommend to use the Date object. Better use

createTimeOfInterest.fromTime(2021, 1, 1);
swgordon commented 3 years ago

1) @hjy1210 just to be clear. I am providing Date.UTC(2021, 1, 1) expecting to get moon phase for FEB Instead I am getting moon phase for MARCH

I am not expecting to get moon phase for Jan, which absolutely would mean I had screwed up the month.

2) Also to be clear, new moon/full moon/first quarter work as expected. In hindsight should have said this initially. For example,

const startDate: Date = new Date(Date.UTC(2021, 2, 1));
const toi = createTimeOfInterest.fromDate(startDate);

const fullMoon = createMoon(toi).getUpcomingFullMoon(); // (RIGHT) yields  Sun Mar 28 2021 18:49:11 GMT+0000 (GMT)
const newMoon = createMoon(toi).getUpcomingNewMoon(); // (RIGHT) yields Sat Mar 13 2021 10:22:44 GMT+0000 (GMT)
const lastQuarterMoon = createMoon(toi).getUpcomingLastQuarter(); // (*WRONG*) yields   Sun Apr 04 2021 10:03:50 GMT+0000 (GMT)
const firstQuarterMoon = createMoon(toi).getUpcomingFirstQuarter(); // (RIGHT) yields  Sun Mar 21 2021 14:41:52 GMT+0000 (GMT)
hjy1210 commented 3 years ago

@swgordon

Now I understand what you mean. Sorry for mis-understanding.

I modify your snippet as follows:

function test(year,month,date,hours=0,minutes=0,seconds=0) {
    const toi = createTimeOfInterest.fromTime(year,month,date,hours,minutes,seconds);
    console.log("start date:",toi.getDate())

    console.log("getUpcomingFullMoon: ", createMoon(toi).getUpcomingFullMoon().getDate()) 
    console.log("getUpcomingNewMoon: ", createMoon(toi).getUpcomingNewMoon().getDate()); 
    console.log("getUpcomingLastQuarter: ", createMoon(toi).getUpcomingLastQuarter().getDate());
    console.log("getUpcomingFirstQuarter: ", createMoon(toi).getUpcomingFirstQuarter().getDate());
}
test(2021,3,14)

Then I got result:

start date: 2021-03-14T00:00:00.000Z
getUpcomingFullMoon:  2021-03-28T18:49:11.000Z
getUpcomingNewMoon:  2021-03-13T10:22:44.000Z
getUpcomingLastQuarter:  2021-04-04T10:03:50.000Z
getUpcomingFirstQuarter:  2021-03-21T14:41:52.000Z

Note: "getUpcomingNewMoon: 2021-03-13T10:22:44.000Z" is BEFORE "start date: 2021-03-14T00:00:00.000Z"

This issue thread is closed by the author, so I suggest you raise new issue about this fact.

hjy1210 commented 3 years ago

@andrmoel

I got data from https://tidesandcurrents.noaa.gov/moon_phases.shtml?year=2021&data_type=monMar and https://tidesandcurrents.noaa.gov/moon_phases.shtml?year=2021&data_type=monApr

NewMoon      Mar        13      10:21
FirstQuarter Mar        21      14:40
FullMoon     Mar        28      18:48
LastQuatrer  Apr        04      10:02

Compare with result got from astronomy-bundle

start date: 2021-03-14T00:00:00.000Z
getUpcomingNewMoon:  2021-03-13T10:22:44.000Z
getUpcomingFirstQuarter:  2021-03-21T14:41:52.000Z
getUpcomingFullMoon:  2021-03-28T18:49:11.000Z
getUpcomingLastQuarter:  2021-04-04T10:03:50.000Z

There is more than one minute of difference for every event.

Following are my suggestions:

  1. The computation is based on TD/JDE not UT/JD, maybe we should output time in UT, note deltaT = DT-UT is more than 60 seconds in year 2021. (Meeus 2009, page 353 example 49.a do make adjustment for deltaT)
  2. In function getTimeOfInterestOfUpcomingPhase(decimalYear, moonPhase), about the statement
    var k = math_1.round((decimalYear - 2000) * 12.3685) + moonPhase;

    formula k in Meeus is just an approximation, so may be we should try from a little early and check the computed JDE of event with JDE of started date to determine is it IS the required.