@math.gl/sun
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -1,2 +0,1 @@ | ||
export { getSunPosition, getSunDirection } from './suncalc'; | ||
//# sourceMappingURL=index.d.ts.map | ||
export { getSunPosition, getSunDirection } from "./suncalc.js"; |
export { getSunPosition, getSunDirection } from "./suncalc.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export declare type CelestialPosition = { | ||
export type CelestialPosition = { | ||
azimuth: number; | ||
@@ -19,2 +19,1 @@ altitude: number; | ||
export declare function getSunDirection(timestamp: number | Date, latitude: number, longitude: number): number[]; | ||
//# sourceMappingURL=suncalc.d.ts.map |
const DEGREES_TO_RADIANS = Math.PI / 180; | ||
const DAY_IN_MS = 1000 * 60 * 60 * 24; | ||
const JD1970 = 2440588; | ||
const JD2000 = 2451545; | ||
const e = DEGREES_TO_RADIANS * 23.4397; | ||
const M0 = 357.5291; | ||
const M1 = 0.98560028; | ||
const THETA0 = 280.147; | ||
const THETA1 = 360.9856235; | ||
const JD1970 = 2440588; // Julian Day year 1970 | ||
const JD2000 = 2451545; // Julian Day year 2000 | ||
// This angle ε [epsilon] is called the obliquity of the ecliptic and its value at the beginning of 2000 was 23.4397° | ||
const e = DEGREES_TO_RADIANS * 23.4397; // obliquity of the Earth | ||
// Refer https://www.aa.quae.nl/en/reken/zonpositie.html | ||
// "The Mean Anomaly" section for explanation | ||
const M0 = 357.5291; // Earth mean anomaly on start day | ||
const M1 = 0.98560028; // Earth angle traverses on average per day seen from the sun | ||
const THETA0 = 280.147; // The sidereal time (in degrees) at longitude 0° at the instant defined by JD2000 | ||
const THETA1 = 360.9856235; // The rate of change of the sidereal time, in degrees per day. | ||
/** | ||
* Calculate sun position | ||
* based on https://www.aa.quae.nl/en/reken/zonpositie.html | ||
* inspired by https://github.com/mourner/suncalc/blob/master/suncalc.js | ||
*/ | ||
export function getSunPosition(timestamp, latitude, longitude) { | ||
const longitudeWestInRadians = DEGREES_TO_RADIANS * -longitude; | ||
const phi = DEGREES_TO_RADIANS * latitude; | ||
const d = toDays(timestamp); | ||
const c = getSunCoords(d); | ||
const H = getSiderealTime(d, longitudeWestInRadians) - c.rightAscension; | ||
return { | ||
azimuth: getAzimuth(H, phi, c.declination), | ||
altitude: getAltitude(H, phi, c.declination) | ||
}; | ||
const longitudeWestInRadians = DEGREES_TO_RADIANS * -longitude; | ||
const phi = DEGREES_TO_RADIANS * latitude; | ||
const d = toDays(timestamp); | ||
const c = getSunCoords(d); | ||
// hour angle | ||
const H = getSiderealTime(d, longitudeWestInRadians) - c.rightAscension; | ||
return { | ||
azimuth: getAzimuth(H, phi, c.declination), | ||
altitude: getAltitude(H, phi, c.declination) | ||
}; | ||
} | ||
export function getSunDirection(timestamp, latitude, longitude) { | ||
const { | ||
azimuth, | ||
altitude | ||
} = getSunPosition(timestamp, latitude, longitude); | ||
return [Math.sin(azimuth) * Math.cos(altitude), Math.cos(azimuth) * Math.cos(altitude), -Math.sin(altitude)]; | ||
const { azimuth, altitude } = getSunPosition(timestamp, latitude, longitude); | ||
// solar position to light direction | ||
return [ | ||
Math.sin(azimuth) * Math.cos(altitude), | ||
Math.cos(azimuth) * Math.cos(altitude), | ||
-Math.sin(altitude) | ||
]; | ||
} | ||
function toJulianDay(timestamp) { | ||
const ts = typeof timestamp === 'number' ? timestamp : timestamp.getTime(); | ||
return ts / DAY_IN_MS - 0.5 + JD1970; | ||
const ts = typeof timestamp === 'number' ? timestamp : timestamp.getTime(); | ||
return ts / DAY_IN_MS - 0.5 + JD1970; | ||
} | ||
function toDays(timestamp) { | ||
return toJulianDay(timestamp) - JD2000; | ||
return toJulianDay(timestamp) - JD2000; | ||
} | ||
function getRightAscension(eclipticLongitude, b) { | ||
const lambda = eclipticLongitude; | ||
return Math.atan2(Math.sin(lambda) * Math.cos(e) - Math.tan(b) * Math.sin(e), Math.cos(lambda)); | ||
const lambda = eclipticLongitude; | ||
return Math.atan2(Math.sin(lambda) * Math.cos(e) - Math.tan(b) * Math.sin(e), Math.cos(lambda)); | ||
} | ||
function getDeclination(eclipticLongitude, b) { | ||
const lambda = eclipticLongitude; | ||
return Math.asin(Math.sin(b) * Math.cos(e) + Math.cos(b) * Math.sin(e) * Math.sin(lambda)); | ||
const lambda = eclipticLongitude; | ||
return Math.asin(Math.sin(b) * Math.cos(e) + Math.cos(b) * Math.sin(e) * Math.sin(lambda)); | ||
} | ||
function getAzimuth(hourAngle, latitudeInRadians, declination) { | ||
const H = hourAngle; | ||
const phi = latitudeInRadians; | ||
const delta = declination; | ||
return Math.atan2(Math.sin(H), Math.cos(H) * Math.sin(phi) - Math.tan(delta) * Math.cos(phi)); | ||
const H = hourAngle; | ||
const phi = latitudeInRadians; | ||
const delta = declination; | ||
return Math.atan2(Math.sin(H), Math.cos(H) * Math.sin(phi) - Math.tan(delta) * Math.cos(phi)); | ||
} | ||
function getAltitude(hourAngle, latitudeInRadians, declination) { | ||
const H = hourAngle; | ||
const phi = latitudeInRadians; | ||
const delta = declination; | ||
return Math.asin(Math.sin(phi) * Math.sin(delta) + Math.cos(phi) * Math.cos(delta) * Math.cos(H)); | ||
const H = hourAngle; | ||
const phi = latitudeInRadians; | ||
const delta = declination; | ||
return Math.asin(Math.sin(phi) * Math.sin(delta) + Math.cos(phi) * Math.cos(delta) * Math.cos(H)); | ||
} | ||
// https://www.aa.quae.nl/en/reken/zonpositie.html | ||
// "The Observer section" | ||
function getSiderealTime(dates, longitudeWestInRadians) { | ||
return DEGREES_TO_RADIANS * (THETA0 + THETA1 * dates) - longitudeWestInRadians; | ||
return DEGREES_TO_RADIANS * (THETA0 + THETA1 * dates) - longitudeWestInRadians; | ||
} | ||
function getSolarMeanAnomaly(days) { | ||
return DEGREES_TO_RADIANS * (M0 + M1 * days); | ||
return DEGREES_TO_RADIANS * (M0 + M1 * days); | ||
} | ||
function getEclipticLongitude(meanAnomaly) { | ||
const M = meanAnomaly; | ||
const C = DEGREES_TO_RADIANS * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M)); | ||
const P = DEGREES_TO_RADIANS * 102.9372; | ||
return M + C + P + Math.PI; | ||
const M = meanAnomaly; | ||
// equation of center | ||
const C = DEGREES_TO_RADIANS * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M)); | ||
// perihelion of the Earth | ||
const P = DEGREES_TO_RADIANS * 102.9372; | ||
return M + C + P + Math.PI; | ||
} | ||
function getSunCoords(dates) { | ||
const M = getSolarMeanAnomaly(dates); | ||
const L = getEclipticLongitude(M); | ||
return { | ||
declination: getDeclination(L, 0), | ||
rightAscension: getRightAscension(L, 0) | ||
}; | ||
const M = getSolarMeanAnomaly(dates); | ||
const L = getEclipticLongitude(M); | ||
return { | ||
declination: getDeclination(L, 0), | ||
rightAscension: getRightAscension(L, 0) | ||
}; | ||
} | ||
//# sourceMappingURL=suncalc.js.map |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"keywords": [ | ||
@@ -28,5 +28,5 @@ "javascript", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
"require": "./dist/index.cjs" | ||
} | ||
@@ -38,6 +38,3 @@ }, | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.12.0" | ||
}, | ||
"gitHead": "b7af99a25965af5112307552084fbaf5d4d53b7d" | ||
"gitHead": "33f369ba3a259f79acc3fa8181190c9da8841648" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
321
24365
11
- Removed@babel/runtime@^7.12.0
- Removed@babel/runtime@7.26.0(transitive)
- Removedregenerator-runtime@0.14.1(transitive)