Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Commit

Permalink
fix julian date calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay LaPorte committed Feb 3, 2020
1 parent aaa5153 commit abfc728
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/julian.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";
const J1970 = -10957.5;
const J2000 = Date.parse("2000-01-01T11:58:55.816Z");

function to(ms) {
return J1970 + ms / 86400000;
return (ms - J2000) / 86400000;
}

function from(t) {
return (t - J1970) * 86400000;
return t * 86400000 + J2000;
}

exports.to = to;
Expand Down
8 changes: 4 additions & 4 deletions lib/lunar.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function _distance(t) {
function _right_ascension_direct(lat, lon, obl) {
return Math.atan2(
Math.sin(lon) * Math.cos(obl) - Math.tan(lat) * Math.sin(obl),
Math.cos(lon)
Math.cos(lon),
);
}

Expand All @@ -71,7 +71,7 @@ function _right_ascension(t) {
function _declination_direct(lat, lon, obl) {
return Math.asin(
Math.sin(lat) * Math.cos(obl) +
Math.cos(lat) * Math.sin(obl) * Math.sin(lon)
Math.cos(lat) * Math.sin(obl) * Math.sin(lon),
);
}

Expand All @@ -92,7 +92,7 @@ function _transit_direct(t, ha) {
function _hour_angle(t, lon) {
return _hour_angle_direct(
sidereal.local(t, lon),
_right_ascension(t)
_right_ascension(t),
);
}

Expand Down Expand Up @@ -139,7 +139,7 @@ function _elevation(t, lat, lon) {

return Math.asin(
Math.sin(lat) * Math.sin(decl) +
Math.cos(lat) * Math.cos(decl) * Math.cos(ha)
Math.cos(lat) * Math.cos(decl) * Math.cos(ha),
);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/solar.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function _altitude(sin_h0, rise, t, lat, lon) {
const cos_dec = Math.sqrt(1 - sin_dec * sin_dec);

return _transit(t, lon) + rise * Math.acos(
(sin_h0 - Math.sin(lat) * sin_dec) / (Math.cos(lat) * cos_dec)
(sin_h0 - Math.sin(lat) * sin_dec) / (Math.cos(lat) * cos_dec),
);
}

Expand All @@ -116,7 +116,7 @@ function _altitude(sin_h0, rise, t, lat, lon) {
function _azimuth(sin_lat, cos_lat, tan_dec, sin_hour_angle, cos_hour_angle) {
return Math.PI + Math.atan2(
sin_hour_angle,
cos_hour_angle * sin_lat - tan_dec * cos_lat
cos_hour_angle * sin_lat - tan_dec * cos_lat,
);
}

Expand Down Expand Up @@ -187,7 +187,7 @@ class Location {
cos_hour_angle,
sin_elevation,
sin_azimuth,
cos_azimuth
cos_azimuth,
) {
this.sin_hour_angle = sin_hour_angle;
this.cos_hour_angle = cos_hour_angle;
Expand Down Expand Up @@ -247,7 +247,7 @@ class Time {
cos_hour_angle,
_sin_elevation(sin_lat, cos_lat, sin_dec, cos_dec, cos_hour_angle),
cos_hour_angle * sin_lat - tan_dec * cos_lat,
sin_hour_angle
sin_hour_angle,
);
}
}
Expand All @@ -265,7 +265,7 @@ function position(ms) {
l,
e,
sin_l * sin_e, // sin declination
Math.atan2(sin_l * cos_e, cos_l) // right ascension
Math.atan2(sin_l * cos_e, cos_l), // right ascension
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/lunar.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("lunar", () => {
}
else {
expect(
calculated_value
calculated_value,
).to.be.closeTo(result, 10 * 60 * 1000); // Allow error of 10min
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/solar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ describe("solar", () => {

it("should return the nearest civil dawn to a given time", () => {
expect(solar.dawn(ms, lat, lon)).
to.be.closeTo(Date.parse("2006-03-20T05:45-0700"), 120000);
to.be.closeTo(Date.parse("2006-03-20T05:45-0700"), 60000);
});

it("should return the nearest sunrise to a given time", () => {
expect(solar.rise(ms, lat, lon)).
to.be.closeTo(Date.parse("2006-03-20T06:10-0700"), 120000);
to.be.closeTo(Date.parse("2006-03-20T06:10-0700"), 60000);
});

it("should return the nearest solar transit to a given time", () => {
expect(solar.transit(ms, lat, lon)).
to.be.closeTo(Date.parse("2006-03-20T12:14-0700"), 120000);
to.be.closeTo(Date.parse("2006-03-20T12:14-0700"), 60000);
});

it("should return the nearest sunset to a given time", () => {
expect(solar.set(ms, lat, lon)).
to.be.closeTo(Date.parse("2006-03-20T18:18-0700"), 120000);
to.be.closeTo(Date.parse("2006-03-20T18:18-0700"), 60000);
});

it("should return the nearest civil dusk to a given time", () => {
expect(solar.dusk(ms, lat, lon)).
to.be.closeTo(Date.parse("2006-03-20T18:43-0700"), 120000);
to.be.closeTo(Date.parse("2006-03-20T18:43-0700"), 60000);
});

it("should look up the position of the sun for an observer", () => {
Expand All @@ -55,7 +55,7 @@ describe("solar", () => {
// (NOTE: I cheated a little here! Syene is now Aswan, and Aswan is no
// longer on the Tropic of Cancer (since the tropics migrate about 15 m/yr).
// So I took Aswan's longitude but used the modern latitude.)
const pos = solar.position(Date.parse("2019-06-21T11:50:07+0200"));
const pos = solar.position(Date.parse("2019-06-21T11:49:03+0200"));

const syene = pos.observer(23.43679, 32.899722);
expect(90 - syene.elevation * 180 / Math.PI).to.be.closeTo(0, 0.01);
Expand Down

0 comments on commit abfc728

Please sign in to comment.