From c42be52076e69eea35318e33787f9a421ee39ffa Mon Sep 17 00:00:00 2001 From: Paul Cockrell Date: Wed, 1 Apr 2020 21:14:34 +0100 Subject: [PATCH] Create AstroCalc app. View info on the Sun and Moon from your wrist. --- apps.json | 21 ++ apps/astrocalc/ChangeLog | 1 + apps/astrocalc/astrocalc-app.js | 348 +++++++++++++++++++++++ apps/astrocalc/astrocalc-icon.js | 1 + apps/astrocalc/astrocalc.png | Bin 0 -> 952 bytes apps/astrocalc/first-quarter-icon.json | 6 + apps/astrocalc/full-icon.json | 7 + apps/astrocalc/last-quarter-icon.json | 7 + apps/astrocalc/new-icon.json | 6 + apps/astrocalc/suncalc.js | 328 +++++++++++++++++++++ apps/astrocalc/waning-crescent-icon.json | 7 + apps/astrocalc/waning-gibbous-icon.json | 6 + apps/astrocalc/waxing-crescent-icon.json | 6 + apps/astrocalc/waxing-gibbous-icon.json | 7 + 14 files changed, 751 insertions(+) create mode 100644 apps/astrocalc/ChangeLog create mode 100644 apps/astrocalc/astrocalc-app.js create mode 100644 apps/astrocalc/astrocalc-icon.js create mode 100644 apps/astrocalc/astrocalc.png create mode 100644 apps/astrocalc/first-quarter-icon.json create mode 100644 apps/astrocalc/full-icon.json create mode 100644 apps/astrocalc/last-quarter-icon.json create mode 100644 apps/astrocalc/new-icon.json create mode 100644 apps/astrocalc/suncalc.js create mode 100644 apps/astrocalc/waning-crescent-icon.json create mode 100644 apps/astrocalc/waning-gibbous-icon.json create mode 100644 apps/astrocalc/waxing-crescent-icon.json create mode 100644 apps/astrocalc/waxing-gibbous-icon.json diff --git a/apps.json b/apps.json index a84a8010c..ea334a0b4 100644 --- a/apps.json +++ b/apps.json @@ -951,5 +951,26 @@ {"name":"chrono.app.js","url":"chrono.js"}, {"name":"chrono.img","url":"chrono-icon.js","evaluate":true} ] + }, + { "id": "astrocalc", + "name": "Astrocalc", + "icon": "astrocalc.png", + "version":"0.01", + "description": "Calculates interesting information on the sun and moon cycles for the current day based on your location.", + "tags": "app,sun,moon,cycles,tool,outdoors", + "allow_emulator":true, + "storage": [ + {"name":"astrocalc.app.js","url":"astrocalc-app.js"}, + {"name":"suncalc.js","url":"suncalc.js"}, + {"name":"astrocalc.img","url":"astrocalc-icon.js","evaluate":true}, + {"name":"first-quarter-icon.json","url":"first-quarter-icon.json"}, + {"name":"last-quarter-icon.json","url":"last-quarter-icon.json"}, + {"name":"waning-crescent-icon.json","url":"waning-crescent-icon.json"}, + {"name":"waning-gibbous-icon.json","url":"waning-gibbous-icon.json"}, + {"name":"full-icon.json","url":"full-icon.json"}, + {"name":"new-icon.json","url":"new-icon.json"}, + {"name":"waxing-gibbous-icon.json","url":"waxing-gibbous-icon.json"}, + {"name":"waxing-crescent-icon.json","url":"waxing-crescent-icon.json"} + ] } ] diff --git a/apps/astrocalc/ChangeLog b/apps/astrocalc/ChangeLog new file mode 100644 index 000000000..0c8adeb61 --- /dev/null +++ b/apps/astrocalc/ChangeLog @@ -0,0 +1 @@ +0.01: Create astrocalc app diff --git a/apps/astrocalc/astrocalc-app.js b/apps/astrocalc/astrocalc-app.js new file mode 100644 index 000000000..655544723 --- /dev/null +++ b/apps/astrocalc/astrocalc-app.js @@ -0,0 +1,348 @@ +/** + * Inspired by: https://www.timeanddate.com + */ + +const SunCalc = require("suncalc.js"); + +function drawMoon(phase, x, y) { + const moonImgFiles = [ + "new-icon.json", + "waxing-crescent-icon.json", + "first-quarter-icon.json", + "waxing-gibbous-icon.json", + "full-icon.json", + "waning-gibbous-icon.json", + "last-quarter-icon.json", + "waning-crescent-icon.json" + ]; + + imgObj = require("Storage").readJSON(moonImgFiles[phase]); + imgObj.buffer = require("heatshrink").decompress(atob(imgObj.image)); + g.drawImage(imgObj, x /*- (imgObj.width / 2)*/, y /*- (imgObj.height)*/); +} + +// linear interpolation between two values a and b +// u controls amount of a/b and is in range [0.0,1.0] +function lerp(a,b,u) { + return (1-u) * a + u * b; +} + +function titlizeKey(key) { + return (key[0].toUpperCase() + key.slice(1)).match(/[A-Z][a-z]+/g).join(" "); +} + +function dateToTimeString(date) { + const hrs = ("0" + date.getHours()).substr(-2); + const mins = ("0" + date.getMinutes()).substr(-2); + const secs = ("0" + date.getMinutes()).substr(-2); + + return `${hrs}:${mins}:${secs}`; +} + +function drawTitle(key) { + const fontHeight = 16; + const x = 0; + const x2 = g.getWidth() - 1; + const y = fontHeight + 26; + const y2 = g.getHeight() - 1; + const title = titlizeKey(key); + + g.setFont("6x8", 2); + g.setFontAlign(0,-1); + g.drawString(title,(x+x2)/2,y-fontHeight-2); + g.drawLine(x,y-2,x2,y-2); +} + +/** + * @params {Number} angle Angle of point around a radius + * @params {Number} radius Radius of the point to be drawn, default 2 + * @params {Object} color Color of the point + * @params {Number} color.r Red 0-1 + * @params {Number} color.g Green 0-1 + * @params {Number} color.b Blue 0-1 + */ +function drawPoint(angle, radius, color) { + const pRad = Math.PI / 180; + const faceWidth = 80; // watch face radius + const centerPx = g.getWidth() / 2; + + const a = angle * pRad; + const x = centerPx + Math.sin(a) * faceWidth; + const y = centerPx - Math.cos(a) * faceWidth; + + if (!radius) radius = 2; + + g.setColor(color.r, color.g, color.b); + g.fillCircle(x, y + 20, radius); +} + +function drawPoints() { + const startColor = {r: 140, g: 255, b: 255}; // light blue + const endColor = {r: 0, g: 0, b: 140}; // dark turquoise + + const steps = 60; + const step_u = 1.0 / (steps / 2); + let u = 0.0; + + for (let i = 0; i < steps; i++) { + const colR = lerp(startColor.r, endColor.r, u) / 255; + const colG = lerp(startColor.g, endColor.g, u) / 255; + const colB = lerp(startColor.b, endColor.b, u) / 255; + const col = {r: colR, g: colG, b: colB}; + + if (i >= 0 && i <= 30) { + u += step_u; + } else { + u -= step_u; + } + + drawPoint((360 * i) / steps, 2, col); + } +} + +function drawData(title, obj, startX, startY) { + g.clear(); + drawTitle(title); + + let xPos, yPos; + + if (typeof(startX) === "undefined" || startX === null) { + // Center text + g.setFontAlign(0,-1); + xPos = (0 + g.getWidth() - 2) / 2; + } else { + xPos = startX; + } + + if (typeof(startY) === "undefined") { + yPos = 5; + } else { + yPos = startY; + } + + g.setFont("6x8", 1); + + Object.keys(obj).forEach((key) => { + g.drawString(`${key}: ${obj[key]}`, xPos, yPos += 20); + }); + + g.flip(); +} + +function drawMoonPositionPage(gps, title) { + const pos = SunCalc.getMoonPosition(new Date(), gps.lat, gps.lon); + + const pageData = { + Azimuth: pos.azimuth.toFixed(2), + Altitude: pos.altitude.toFixed(2), + Distance: `${pos.distance.toFixed(0)} km`, + "Parallactic Ang": pos.parallacticAngle.toFixed(2), + }; + const azimuthDegrees = parseInt(pos.azimuth * 180 / Math.PI); + + drawData(title, pageData, null, 80); + drawPoints(); + drawPoint(azimuthDegrees, 8, {r: 1, g: 1, b: 1}); + + let m = setWatch(() => { + let m = moonIndexPageMenu(gps); + }, BTN3, {repeat: false, edge: "falling"}); +} + +function drawMoonIlluminationPage(gps, title) { + const phaseNames = [ + "New Moon", "Waxing Crescent", "First Quarter", "Waxing Gibbous", + "Full Moon", "Waning Gibbous", "Last Quater", "Waning Crescent", + ]; + + const phase = SunCalc.getMoonIllumination(new Date()); + const pageData = { + Phase: phaseNames[phase.phase], + }; + + drawData(title, pageData, null, 35); + drawMoon(phase.phase, g.getWidth() / 2, g.getHeight() / 2); + + let m = setWatch(() => { + let m = moonIndexPageMenu(gps); + }, BTN3, {repease: false, edge: "falling"}); +} + + +function drawMoonTimesPage(gps, title) { + const times = SunCalc.getMoonTimes(new Date(), gps.lat, gps.lon); + + const pageData = { + Rise: dateToTimeString(times.rise), + Set: dateToTimeString(times.set), + }; + + drawData(title, pageData, null, 105); + drawPoints(); + + // Draw the moon rise position + const risePos = SunCalc.getMoonPosition(times.rise, gps.lat, gps.lon); + const riseAzimuthDegrees = parseInt(risePos.azimuth * 180 / Math.PI); + drawPoint(riseAzimuthDegrees, 8, {r: 1, g: 1, b: 1}); + + // Draw the moon set position + const setPos = SunCalc.getMoonPosition(times.set, gps.lat, gps.lon); + const setAzimuthDegrees = parseInt(setPos.azimuth * 180 / Math.PI); + drawPoint(setAzimuthDegrees, 8, {r: 1, g: 1, b: 1}); + + let m = setWatch(() => { + let m = moonIndexPageMenu(gps); + }, BTN3, {repease: false, edge: "falling"}); +} + +function drawSunShowPage(gps, key, date) { + const pos = SunCalc.getPosition(date, gps.lat, gps.lon); + + const hrs = ("0" + date.getHours()).substr(-2); + const mins = ("0" + date.getMinutes()).substr(-2); + const secs = ("0" + date.getMinutes()).substr(-2); + const time = `${hrs}:${mins}:${secs}`; + + const azimuth = Number(pos.azimuth.toFixed(2)); + const azimuthDegrees = parseInt(pos.azimuth * 180 / Math.PI); + const altitude = Number(pos.altitude.toFixed(2)); + + const pageData = { + Time: time, + Altitude: altitude, + Azimumth: azimuth, + Degrees: azimuthDegrees + }; + + drawData(key, pageData, null, 85); + + drawPoints(); + + // Draw the suns position + drawPoint(azimuthDegrees, 8, {r: 1, g: 1, b: 0}); + + m = setWatch(() => { + m = sunIndexPageMenu(gps); + }, BTN3, {repeat: false, edge: "falling"}); + + return null; +} + +function sunIndexPageMenu(gps) { + const sunTimes = SunCalc.getTimes(new Date(), gps.lat, gps.lon); + + const sunMenu = { + "": { + "title": "-- Sun --", + }, + "Current Pos": () => { + m = E.showMenu(); + drawSunShowPage(gps, "Current Pos", new Date()); + }, + }; + + Object.keys(sunTimes).sort().reduce((menu, key) => { + const title = titlizeKey(key); + menu[title] = () => { + m = E.showMenu(); + drawSunShowPage(gps, key, sunTimes[key]); + }; + return menu; + }, sunMenu); + + sunMenu["< Back"] = () => m = indexPageMenu(gps); + + return E.showMenu(sunMenu); +} + + +function moonIndexPageMenu(gps) { + const moonMenu = { + "": { + "title": "-- Moon --", + }, + "Times": () => { + m = E.showMenu(); + drawMoonTimesPage(gps, "Times"); + }, + "Position": () => { + m = E.showMenu(); + drawMoonPositionPage(gps, "Position"); + }, + "Illumination": () => { + m = E.showMenu(); + drawMoonIlluminationPage(gps, "Illumination"); + }, + "< Back": () => m = indexPageMenu(gps), + }; + + return E.showMenu(moonMenu); +} + +function indexPageMenu(gps) { + const menu = { + "": { + "title": "Select", + }, + "Sun": () => { + m = sunIndexPageMenu(gps); + }, + "Moon": () => { + m = moonIndexPageMenu(gps); + }, + "< Exit": () => { load(); } + }; + + return E.showMenu(menu); +} + +/** + * GPS wait page, shows GPS locating animation until it gets a lock, then moves to the Sun page + */ +function drawGPSWaitPage() { + const img = require("heatshrink").decompress(atob("mEwxH+AH4A/AH4AW43GF1wwsFwYwqFwowoFw4wmFxIwdE5YAPF/4vM5nN6YAE5vMF8YtHGIgvhFpQxKF7AuOGA4vXFyAwGF63MFyIABF6xeWMC4UDLwvNGpAJG5gwSdhIIDRBLyWCIgcJHAgJJDoouQF4vMQoICBBJoeGFx6GGACIfHL6YvaX6gvZeCIdFc4gAFXogvGFxgwFDwovQCAguOGAnMMBxeG5guTGAggGGAwNKFySREcA3N5vM5gDBdpQvXEY4AKXqovGGCKbFF7AwPZQwvZGJgtGF7vGdQItG5gSIF7gASF/44WEzgwRF0wwHF1AwFF1QwDF1gvwAH4A/AFAA==")) + + g.clear(); + g.drawImage(img, 100, 50); + g.setFont("6x8", 1); + g.drawString("Astrocalc v0.01", 80, 105); + g.drawString("Locating GPS", 85, 140); + g.drawString("Please wait...", 80, 155); + g.flip(); + + const DEBUG = false; + if (DEBUG) { + const gps = { + "lat": 56.45783133333, + "lon": -3.02188583333, + "alt": 75.3, + "speed": 0.070376, + "course": NaN, + "time":new Date(), + "satellites": 4, + "fix": 1 + }; + + m = indexPageMenu(gps); + + return; + } + + Bangle.on('GPS', (gps) => { + if (gps.fix === 0) return; + + Bangle.setGPSPower(0); + Bangle.buzz(); + Bangle.setLCDPower(true); + + m = indexPageMenu(gps); + }); +} + +function init() { + Bangle.setGPSPower(1); + drawGPSWaitPage(); +} + +let m; +init(); \ No newline at end of file diff --git a/apps/astrocalc/astrocalc-icon.js b/apps/astrocalc/astrocalc-icon.js new file mode 100644 index 000000000..aa04c2805 --- /dev/null +++ b/apps/astrocalc/astrocalc-icon.js @@ -0,0 +1 @@ +require("heatshrink").decompress(atob("mEwxH+AH4A/AH4AW43GF1wwsFwYwqFwowoFw4wmFxIwdE5YAPF/4vM5nN6YAE5vMF8YtHGIgvhFpQxKF7AuOGA4vXFyAwGF63MFyIABF6xeWMC4UDLwvNGpAJG5gwSdhIIDRBLyWCIgcJHAgJJDoouQF4vMQoICBBJoeGFx6GGACIfHL6YvaX6gvZeCIdFc4gAFXogvGFxgwFDwovQCAguOGAnMMBxeG5guTGAggGGAwNKFySREcA3N5vM5gDBdpQvXEY4AKXqovGGCKbFF7AwPZQwvZGJgtGF7vGdQItG5gSIF7gASF/44WEzgwRF0wwHF1AwFF1QwDF1gvwAH4A/AFAA==")) \ No newline at end of file diff --git a/apps/astrocalc/astrocalc.png b/apps/astrocalc/astrocalc.png new file mode 100644 index 0000000000000000000000000000000000000000..c26a651ec60072f49fa8969f7c7d8e1479e391f0 GIT binary patch literal 952 zcmV;p14sOcP)zXZD_Mrh6h6{gWm$`>@vk&pxfS3vIN~Mrd$m z$vXvJ{cj$Da7x}e{e>pN3d5)~OCF0CuHqSMisz<%85f`+Ux#rKMg=q>Ux!f<#sp}R zufv!KZ2?XJ$Am*&SW|l3IznR*k3k|Pp37CJR6}uNPC&$hj15^E29gkqHr7{9U}GDK zC0H#%&CGzB5sxRDaS{W8q|6hc}=YXTw`Opd^4`eOZh67c9YEU&}I zrItn`xPXWSkH=x)T3{253_^D&yjut?A`;m7yljFiQ4EM+0>e~R=F_ex0crbu z$+V6L-$g7953+V+qmvrE{S2GC;yu*|Pwqf8dhvGIekg9bMc@~9C`UjU+YBTd-xpT| zsh$K}=@QS2zq|%@+6^YX{hEw~bOe;-<1z0Ed%X|#58+`>Op&=IQRaqDyI2RrJD_qb z6CoW5Pr&3GRrYqn(+AQjV|eoccK@hqJyEQ83R#qu#zV>9T<|;3B-}g#tSM$dv1kq} zfq7{|<-;OxJ>#XjSh@zRDQ27kfNSv_9gC>ay$%3Ye#6&QJ(9Pwa5LpCuO7?hzmh!o zY@)UeYsXloB4Qe$%>JW`Us#jjOwk;u|CJ#*xGU;?ug}{k8Ob+{*6POqlZDnxV zZbBY3PGXOUVhQ#XyToW3?&ri;b|V@Qbz_Rqd%p~8TNffPnrWgTC(hU+o>$?hD%Ql# zX~>YgDiWE_n7Yrqg$4oP=ESQdSXx&{q@6Js%jnztzSj8lFM#0rz}%9^aa+a4HhfwR z+&#E}nj?n&izM#R#qxSf*%-8%6W1J(oNw)m-Ni4dKaagK%>M{HZO+5Yt;Fhi%w6sln8qD; zsJ&_(QhKHgLQ8}}^1_^9_=+%0UYHC4QV}7L4;4;<>3@Inz1IHY<8r~7CEsu<) converted to rad: + return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179)); +} + +// general sun calculations + +function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); } + +function eclipticLongitude(M) { + + var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center + P = rad * 102.9372; // perihelion of the Earth + + return M + C + P + PI; +} + +function sunCoords(d) { + + var M = solarMeanAnomaly(d), + L = eclipticLongitude(M); + + return { + dec: declination(L, 0), + ra: rightAscension(L, 0) + }; +} + + +var SunCalc = {}; + + +// calculates sun position for a given date and latitude/longitude + +SunCalc.getPosition = function (date, lat, lng) { + + var lw = rad * -lng, + phi = rad * lat, + d = toDays(date), + + c = sunCoords(d), + H = siderealTime(d, lw) - c.ra; + + return { + azimuth: azimuth(H, phi, c.dec), + altitude: altitude(H, phi, c.dec) + }; +}; + + +// sun times configuration (angle, morning name, evening name) + +var times = SunCalc.times = [ + [-0.833, 'sunrise', 'sunset' ], + [ -0.3, 'sunriseEnd', 'sunsetStart' ], + [ -6, 'dawn', 'dusk' ], + [ -12, 'nauticalDawn', 'nauticalDusk'], + [ -18, 'nightEnd', 'night' ], + [ 6, 'goldenHourEnd', 'goldenHour' ] +]; + +// adds a custom time to the times config + +SunCalc.addTime = function (angle, riseName, setName) { + times.push([angle, riseName, setName]); +}; + + +// calculations for sun times + +var J0 = 0.0009; + +function julianCycle(d, lw) { return Math.round(d - J0 - lw / (2 * PI)); } + +function approxTransit(Ht, lw, n) { return J0 + (Ht + lw) / (2 * PI) + n; } +function solarTransitJ(ds, M, L) { return J2000 + ds + 0.0053 * sin(M) - 0.0069 * sin(2 * L); } + +function hourAngle(h, phi, d) { return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d))); } +function observerAngle(height) { return -2.076 * Math.sqrt(height) / 60; } + +// returns set time for the given sun altitude +function getSetJ(h, lw, phi, dec, n, M, L) { + + var w = hourAngle(h, phi, dec), + a = approxTransit(w, lw, n); + return solarTransitJ(a, M, L); +} + + +// calculates sun times for a given date, latitude/longitude, and, optionally, +// the observer height (in meters) relative to the horizon + +SunCalc.getTimes = function (date, lat, lng, height) { + + height = height || 0; + + var lw = rad * -lng, + phi = rad * lat, + + dh = observerAngle(height), + + d = toDays(date), + n = julianCycle(d, lw), + ds = approxTransit(0, lw, n), + + M = solarMeanAnomaly(ds), + L = eclipticLongitude(M), + dec = declination(L, 0), + + Jnoon = solarTransitJ(ds, M, L), + + i, len, time, h0, Jset, Jrise; + + + var result = { + solarNoon: new Date(fromJulian(Jnoon)), + nadir: new Date(fromJulian(Jnoon - 0.5)) + }; + + for (i = 0, len = times.length; i < len; i += 1) { + time = times[i]; + h0 = (time[0] + dh) * rad; + + Jset = getSetJ(h0, lw, phi, dec, n, M, L); + Jrise = Jnoon - (Jset - Jnoon); + + result[time[1]] = new Date(fromJulian(Jrise) - (dayMs / 2)); + result[time[2]] = new Date(fromJulian(Jset) + (dayMs / 2)); + } + + return result; +}; + + +// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas + +function moonCoords(d) { // geocentric ecliptic coordinates of the moon + + var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude + M = rad * (134.963 + 13.064993 * d), // mean anomaly + F = rad * (93.272 + 13.229350 * d), // mean distance + + l = L + rad * 6.289 * sin(M), // longitude + b = rad * 5.128 * sin(F), // latitude + dt = 385001 - 20905 * cos(M); // distance to the moon in km + + return { + ra: rightAscension(l, b), + dec: declination(l, b), + dist: dt + }; +} + +SunCalc.getMoonPosition = function (date, lat, lng) { + + var lw = rad * -lng, + phi = rad * lat, + d = toDays(date), + + c = moonCoords(d), + H = siderealTime(d, lw) - c.ra, + h = altitude(H, phi, c.dec), + // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998. + pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H)); + + h = h + astroRefraction(h); // altitude correction for refraction + + return { + azimuth: azimuth(H, phi, c.dec), + altitude: h, + distance: c.dist, + parallacticAngle: pa + }; +}; + + +// calculations for illumination parameters of the moon, +// based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and +// Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998. + +// Function updated from gist: https://gist.github.com/endel/dfe6bb2fbe679781948c + +SunCalc.getMoonIllumination = function (date) { + let month = date.getMonth(); + let year = date.getFullYear(); + let day = date.getDate(); + + let c = 0; + let e = 0; + let jd = 0; + let b = 0; + + if (month < 3) { + year--; + month += 12; + } + + ++month; + c = 365.25 * year; + e = 30.6 * month; + jd = c + e + day - 694039.09; // jd is total days elapsed + jd /= 29.5305882; // divide by the moon cycle + b = parseInt(jd); // int(jd) -> b, take integer part of jd + jd -= b; // subtract integer part to leave fractional part of original jd + b = Math.round(jd * 8); // scale fraction from 0-8 and round + + if (b >= 8) b = 0; // 0 and 8 are the same so turn 8 into 0 + + return {phase: b}; +}; + + +function hoursLater(date, h) { + return new Date(date.valueOf() + h * dayMs / 24); +} + +// calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article + +SunCalc.getMoonTimes = function (date, lat, lng, inUTC) { + var t = date; + if (inUTC) t.setUTCHours(0, 0, 0, 0); + else t.setHours(0, 0, 0, 0); + + var hc = 0.133 * rad, + h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc, + h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx; + + // go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set) + for (var i = 1; i <= 24; i += 2) { + h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc; + h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc; + + a = (h0 + h2) / 2 - h1; + b = (h2 - h0) / 2; + xe = -b / (2 * a); + ye = (a * xe + b) * xe + h1; + d = b * b - 4 * a * h1; + roots = 0; + + if (d >= 0) { + 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) x1 = x2; + } + + if (roots === 1) { + if (h0 < 0) rise = i + x1; + else set = i + x1; + + } else if (roots === 2) { + rise = i + (ye < 0 ? x2 : x1); + set = i + (ye < 0 ? x1 : x2); + } + + if (rise && set) break; + + h0 = h2; + } + + var result = {}; + + if (rise) result.rise = hoursLater(t, rise); + if (set) result.set = hoursLater(t, set); + + if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true; + + return result; +}; + + +// export as Node module / AMD module / browser variable +if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc; +else if (typeof define === 'function' && define.amd) define(SunCalc); +else global.SunCalc = SunCalc; + +}()); diff --git a/apps/astrocalc/waning-crescent-icon.json b/apps/astrocalc/waning-crescent-icon.json new file mode 100644 index 000000000..802a79fda --- /dev/null +++ b/apps/astrocalc/waning-crescent-icon.json @@ -0,0 +1,7 @@ + +{ + "width": 92, + "height": 92, + "bpp": 1, + "image": "AH4AI8AKJvgKJj+ABREH/AWJ/4KJn44Jg/+BREB//AEJP8EJP/J5P/J5MfCxMD//wCxF/MxI4BCxP/MxI4BPpA4BCxM/PpI4BPpJPBCxEPCxX/CxJPBShDBBShJPBCxBmBC0BmBCxB9BYRIWBYRAWLBQIWISgIW/C34WTW6wWNfhE/fhMDC0MfCxMBCxR+B/iVK4BnJCxJ+BCxBQBCxJnB8BQJ/wWIKAIWIHIQKIKALDIHIQKIHIKVIHISVIHIIWJOYJ+IYgJ+JEQJnJgZ+JIoJnJEQJQJgJQJRYJQJgEOBRIA/AAIA=" +} \ No newline at end of file diff --git a/apps/astrocalc/waning-gibbous-icon.json b/apps/astrocalc/waning-gibbous-icon.json new file mode 100644 index 000000000..5b7c52c41 --- /dev/null +++ b/apps/astrocalc/waning-gibbous-icon.json @@ -0,0 +1,6 @@ +{ + "width": 92, + "height": 92, + "bpp": 1, + "image": "AGMB/+ABRP+CxH///wBQ9/BYIiHj4KB/gKGg4KB//gG44ACCw0/BQQ5GEIY5GEIn4EJP/4AhJKAwKDKAxlCAAKKFgYWEKAqHCM444ECwwKEM4o4EM4o4FCwpxEM4o4FPwpPFCwkPCxQKFPwhPFSojBECyJmGCwhmFCxasEMwqsEPowWbBQwWDSgwW/C34WOZ1s/CxMDBQv+CxXgBQUBCxUfCyp+GCwaVG/gKDM43APxIWEM4wWEM4v4BQZQGwBQJCwg5GBQhQFVYY5HBQg5FPog5FSgg5GMwhzFPojEGMwgiFMwoiFBQoiEMwoiEJ4xoEJ4qLEHAx0EBQ46CRAoLEEJAAqA=" +} \ No newline at end of file diff --git a/apps/astrocalc/waxing-crescent-icon.json b/apps/astrocalc/waxing-crescent-icon.json new file mode 100644 index 000000000..55d950730 --- /dev/null +++ b/apps/astrocalc/waxing-crescent-icon.json @@ -0,0 +1,6 @@ +{ + "width": 92, + "height": 92, + "bpp": 1, + "image": "AH4ALuAKJgfgBZMfwAKIgP+CxMP+ALJv/ABREHERU//BQJ/4iJj4iJgP/IpMf/5oJ//8M5P/RZP/HJMHHJV/HJIWBHJM/OZJ+BHJIWBKBAWB/4WKKBCVBKBKVBYhAWCKBAWBM5AWZM5DDBPxIWYPxAWBSpIW/C34WLZz78BCxD8C4AWJ/gWV/AWKwAWSgZ9Kn6UJCwJ9JCwJ9Ig5mJgF/MxIWBJ5AWCBRDCBHBDCBJ5IWBHBCUBHBKUBBRAWBHBM/OJKUBYJBmBEJMHEJJmBBRMfEJMBMpMAnwKJg4KJAHw=" +} \ No newline at end of file diff --git a/apps/astrocalc/waxing-gibbous-icon.json b/apps/astrocalc/waxing-gibbous-icon.json new file mode 100644 index 000000000..652710aca --- /dev/null +++ b/apps/astrocalc/waxing-gibbous-icon.json @@ -0,0 +1,7 @@ +{ + "width": 92, + "height": 92, + "bpp": 1, + "transparent": 0, + "image": "AGUf/AKIg//BZP/BZMfBYPgBQ0DBQP/wALGv4KB/wKGh4WCFw4KC//AHBAiHgIWDEQ0/ERJPDEQ5PCAAPwRAwADNAoKE/hmIRY4KEHIpmDHIxmEHIwWFHIhmEOYx9EHIoWGKAgWF/59JKAqUEKAoWGYggWRM4gWGM4arFCwoKFM4gWGPwgWdPwYWGSogW/C34WQZ1vABQUHBQv8Cwd/CyH4CxWACyoKDgaUJgE/MxIWGPoYWGMwkBMxMAj5PJCwwKDVgw4EVgpPECww4EPwo4ESowKEM4o4FPwhxEM4zBDM4whFKAghGM4ghFKAghGKAZlFHIiHFHIosGRQYKIEQIKIAFIA=" +} \ No newline at end of file