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

Commit

Permalink
v1.4.1: bring astro deps in line with api
Browse files Browse the repository at this point in the history
  • Loading branch information
J. T. L committed Sep 21, 2018
1 parent 730fbbf commit 41df68d
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 181 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@darkskyapp"
}
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
/.nyc_output
/coverage
/node_modules
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function julian_to_date(t) {

// Greenwich mean sidereal time, accurate to ~1 second.
// http://aa.usno.navy.mil/faq/docs/GAST.php
const GMST0 = 18.697374558 * Math.PI / 12,
GMST1 = 24.06570982441908 * Math.PI / 12;
const GMST0 = 18.697374558 * Math.PI / 12;
const GMST1 = 24.06570982441908 * Math.PI / 12;

function greenwich_mean_sidereal_time(jt) {
return GMST0 + GMST1 * jt;
Expand Down
40 changes: 40 additions & 0 deletions lib/astro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";
const Lunar = require("./lunar");
const Solar = require("./solar");

class Astro {
constructor() {
this.lunar_cache = null;
this.solar_cache = null;
}

lunar(time_ms) {
if(this.lunar_cache === null) {
this.lunar_cache = new Map();
}

let lunar = this.lunar_cache.get(time_ms);
if(lunar === undefined) {
lunar = new Lunar(time_ms);
this.lunar_cache.set(time_ms, lunar);
}

return lunar;
}

solar(time_ms) {
if(this.solar_cache === null) {
this.solar_cache = new Map();
}

let solar = this.solar_cache.get(time_ms);
if(solar === undefined) {
solar = new Solar(time_ms);
this.solar_cache.set(time_ms, solar);
}

return solar;
}
}

module.exports = Astro;
13 changes: 13 additions & 0 deletions lib/julian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
const J1970 = -10957.5;

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

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

exports.to = to;
exports.from = from;
87 changes: 45 additions & 42 deletions lib/lunar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ function limit_angle(radians) {
}

/* Lunar position, accurate to about ten minutes between 1950-2050,
* http://aa.quae.nl/en/reken/hemelpositie.html#2 */
const Q0 = 218.316 * Math.PI / 180,
Q1 = 13.176396 * Math.PI / 180,
M0 = 134.963 * Math.PI / 180,
M1 = 13.064993 * Math.PI / 180,
F0 = 93.272 * Math.PI / 180,
F1 = 13.229350 * Math.PI / 180,
L0 = 6.289 * Math.PI / 180,
L1 = 5.128 * Math.PI / 180,
E0 = 23.439 * Math.PI / 180,
E1 = -0.00000036 * Math.PI / 180;
* http://aa.quae.nl/en/reken/hemelpositie.html#2 */
const Q0 = 218.316 * Math.PI / 180;
const Q1 = 13.176396 * Math.PI / 180;
const M0 = 134.963 * Math.PI / 180;
const M1 = 13.064993 * Math.PI / 180;
const F0 = 93.272 * Math.PI / 180;
const F1 = 13.229350 * Math.PI / 180;
const L0 = 6.289 * Math.PI / 180;
const L1 = 5.128 * Math.PI / 180;
const E0 = 23.439 * Math.PI / 180;
const E1 = -0.00000036 * Math.PI / 180;

/* Geocentric ecliptic longitude compared to the equinox */
function ecliptic_equinox_longitude(t) {
Expand Down Expand Up @@ -87,8 +87,8 @@ function hour_angle(t, lon) {

function hour_angle_iterative(t, lon, ha0) {
// Iteratively improve by walking back in time toward a smaller hour angle.
const t1 = transit_direct(t, ha0),
ha1 = hour_angle(t1, lon) + ha0;
const t1 = transit_direct(t, ha0);
const ha1 = hour_angle(t1, lon) + ha0;
return ha1;
}

Expand All @@ -104,7 +104,7 @@ function hour_angle_refined(t, lon) {
}

function hours_later(t, hrs) {
return t + hrs / 24;
return t + hrs / 24;
}

function transit(t, lat, lon) {
Expand All @@ -123,17 +123,17 @@ function transit(t, lat, lon) {
}

function altitude(t, lat, lon) {
const decl = declination(t),
ha = hour_angle(t, lon);
const decl = declination(t);
const ha = hour_angle(t, lon);

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

/* http://www.stargazing.net/kepler/moonrise.html article */
const PARALLAX = 0.0023212879051524586;
const PARALLAX = 0.0023212879051524586;

function rise_and_set(t, lat, lon) {
const h = -PARALLAX;
Expand All @@ -145,50 +145,53 @@ function rise_and_set(t, lat, lon) {
// Go in 2 hour chunks.
for (let i=0; i <= 24; i+=2) {
if (i !== 0) {
h1 = altitude(hours_later(t, i), lat, lon) - h;
h1 = altitude(hours_later(t, i), lat, lon) - h;
}
const h2 = altitude(hours_later(t, i+1), lat, lon) - h;

// Fit h0, h1, h2 to a parabola
const a = (h2 + h0) / 2 - h1,
b = (h2 - h0) / 2,
xe = -b / (2 * a), // vertex of parabola
ye = (a * xe + b) * xe + h1;
const a = (h2 + h0) / 2 - h1;
const b = (h2 - h0) / 2;
const xe = -b / (2 * a); // vertex of parabola
const ye = (a * xe + b) * xe + h1;

// Discriminant
const d = b * b - 4 * a * h1;
let roots = 0;
let x1, x2;
let x1;
let x2;

// Count roots
if (d >= 0) {
const dx = Math.sqrt(d) / (Math.abs(a) * 2);
x1 = xe - dx;
x2 = xe + dx;

if (Math.abs(x1) <= 1) {
roots++;
}
if (Math.abs(x2) <= 1) {
roots++;
}
if (x1 < -1) {
if (x1 < -1) {
x1 = x2;
}
}

if (roots === 1) {
if (h0 < 0 && isNaN(moonrise)) {
moonrise = i + x1;
} else if(isNaN(moonset)) {
}
else if(isNaN(moonset)) {
moonset = i + x1;
}
} else if (roots === 2) {
}
else if (roots === 2) {
if(isNaN(moonrise)) {
moonrise = i + (ye < 0 ? x2 : x1);
moonrise = i + (ye < 0 ? x2 : x1);
}
if(isNaN(moonset)) {
moonset = i + (ye < 0 ? x1 : x2);
moonset = i + (ye < 0 ? x1 : x2);
}
}

Expand All @@ -198,14 +201,14 @@ function rise_and_set(t, lat, lon) {
}
// Move two hours of altitude
h0 = h2;
}
}
return {
moonrise: hours_later(t, moonrise),
moonset: hours_later(t, moonset),
};
}

/* In the next 24 hours */
/* In the next 24 hours */
function rise(t, lat, lon) {
return rise_and_set(t, lat, lon).moonrise;
}
Expand All @@ -214,12 +217,12 @@ function set(t, lat, lon) {
return rise_and_set(t, lat, lon).moonset;
}

exports.latitude = latitude;
exports.longitude = longitude;
exports.latitude = latitude;
exports.longitude = longitude;
exports.right_ascension = right_ascension;
exports.declination = declination;
exports.altitude = altitude;
exports.transit = transit;
exports.rise_and_set = rise_and_set;
exports.rise = rise;
exports.set = set;
exports.declination = declination;
exports.altitude = altitude;
exports.transit = transit;
exports.rise_and_set = rise_and_set;
exports.rise = rise;
exports.set = set;
Loading

0 comments on commit 41df68d

Please sign in to comment.