{ "version": 3, "sources": ["../src/index.ts", "../src/suncalc.ts"], "sourcesContent": ["export {getSunPosition, getSunDirection} from './suncalc';\n", "const DEGREES_TO_RADIANS = Math.PI / 180;\n\nconst DAY_IN_MS = 1000 * 60 * 60 * 24;\nconst JD1970 = 2440588; // Julian Day year 1970\nconst JD2000 = 2451545; // Julian Day year 2000\n\n// This angle \u03B5 [epsilon] is called the obliquity of the ecliptic and its value at the beginning of 2000 was 23.4397\u00B0\nconst e = DEGREES_TO_RADIANS * 23.4397; // obliquity of the Earth\n\n// Refer https://www.aa.quae.nl/en/reken/zonpositie.html\n// \"The Mean Anomaly\" section for explanation\nconst M0 = 357.5291; // Earth mean anomaly on start day\nconst M1 = 0.98560028; // Earth angle traverses on average per day seen from the sun\n\nconst THETA0 = 280.147; // The sidereal time (in degrees) at longitude 0\u00B0 at the instant defined by JD2000\nconst THETA1 = 360.9856235; // The rate of change of the sidereal time, in degrees per day.\n\n/**\n * A position in the sky defined by two angles\n * The altitude is 0\u00B0 at the horizon, +90\u00B0 in the zenith (straight over your head), and \u221290\u00B0 in the nadir (straight down).\n * The azimuth is the direction along the horizon, which we measure from south to west.\n * South has azimuth 0\u00B0, west +90\u00B0, north +180\u00B0, and east +270\u00B0 (or \u221290\u00B0, that's the same thing).\n */\nexport type CelestialPosition = {\n azimuth: number;\n altitude: number;\n};\n\n/**\n * Calculate sun position\n * based on https://www.aa.quae.nl/en/reken/zonpositie.html\n * inspired by https://github.com/mourner/suncalc/blob/master/suncalc.js\n */\nexport function getSunPosition(\n timestamp: number | Date,\n latitude: number,\n longitude: number\n): CelestialPosition {\n const longitudeWestInRadians = DEGREES_TO_RADIANS * -longitude;\n const phi = DEGREES_TO_RADIANS * latitude;\n const d = toDays(timestamp);\n\n const c = getSunCoords(d);\n // hour angle\n const H = getSiderealTime(d, longitudeWestInRadians) - c.rightAscension;\n\n return {\n azimuth: getAzimuth(H, phi, c.declination),\n altitude: getAltitude(H, phi, c.declination)\n };\n}\n\nexport function getSunDirection(\n timestamp: number | Date,\n latitude: number,\n longitude: number\n): number[] {\n const {azimuth, altitude} = getSunPosition(timestamp, latitude, longitude);\n\n // solar position to light direction\n return [\n Math.sin(azimuth) * Math.cos(altitude),\n Math.cos(azimuth) * Math.cos(altitude),\n -Math.sin(altitude)\n ];\n}\n\nfunction toJulianDay(timestamp: number | Date): number {\n const ts = typeof timestamp === 'number' ? timestamp : timestamp.getTime();\n return ts / DAY_IN_MS - 0.5 + JD1970;\n}\n\nfunction toDays(timestamp: number | Date): number {\n return toJulianDay(timestamp) - JD2000;\n}\n\nfunction getRightAscension(eclipticLongitude: number, b: number): number {\n const lambda = eclipticLongitude;\n return Math.atan2(Math.sin(lambda) * Math.cos(e) - Math.tan(b) * Math.sin(e), Math.cos(lambda));\n}\n\nfunction getDeclination(eclipticLongitude: number, b: number): number {\n const lambda = eclipticLongitude;\n return Math.asin(Math.sin(b) * Math.cos(e) + Math.cos(b) * Math.sin(e) * Math.sin(lambda));\n}\n\nfunction getAzimuth(hourAngle: number, latitudeInRadians: number, declination: number): number {\n const H = hourAngle;\n const phi = latitudeInRadians;\n const delta = declination;\n return Math.atan2(Math.sin(H), Math.cos(H) * Math.sin(phi) - Math.tan(delta) * Math.cos(phi));\n}\n\nfunction getAltitude(hourAngle: number, latitudeInRadians: number, declination: number): number {\n const H = hourAngle;\n const phi = latitudeInRadians;\n const delta = declination;\n return Math.asin(Math.sin(phi) * Math.sin(delta) + Math.cos(phi) * Math.cos(delta) * Math.cos(H));\n}\n\n// https://www.aa.quae.nl/en/reken/zonpositie.html\n// \"The Observer section\"\nfunction getSiderealTime(dates: number, longitudeWestInRadians: number): number {\n return DEGREES_TO_RADIANS * (THETA0 + THETA1 * dates) - longitudeWestInRadians;\n}\n\nfunction getSolarMeanAnomaly(days: number): number {\n return DEGREES_TO_RADIANS * (M0 + M1 * days);\n}\n\nfunction getEclipticLongitude(meanAnomaly: number): number {\n const M = meanAnomaly;\n // equation of center\n const C =\n DEGREES_TO_RADIANS * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M));\n // perihelion of the Earth\n const P = DEGREES_TO_RADIANS * 102.9372;\n\n return M + C + P + Math.PI;\n}\n\nfunction getSunCoords(dates: number): {\n declination: number;\n rightAscension: number;\n} {\n const M = getSolarMeanAnomaly(dates);\n const L = getEclipticLongitude(M);\n\n return {\n declination: getDeclination(L, 0),\n rightAscension: getRightAscension(L, 0)\n };\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACAA,IAAM,qBAAqB,KAAK,KAAK;AAErC,IAAM,YAAY,MAAO,KAAK,KAAK;AACnC,IAAM,SAAS;AACf,IAAM,SAAS;AAGf,IAAM,IAAI,qBAAqB;AAI/B,IAAM,KAAK;AACX,IAAM,KAAK;AAEX,IAAM,SAAS;AACf,IAAM,SAAS;AAkBT,SAAU,eACd,WACA,UACA,WAAiB;AAEjB,QAAM,yBAAyB,qBAAqB,CAAC;AACrD,QAAM,MAAM,qBAAqB;AACjC,QAAM,IAAI,OAAO,SAAS;AAE1B,QAAM,IAAI,aAAa,CAAC;AAExB,QAAM,IAAI,gBAAgB,GAAG,sBAAsB,IAAI,EAAE;AAEzD,SAAO;IACL,SAAS,WAAW,GAAG,KAAK,EAAE,WAAW;IACzC,UAAU,YAAY,GAAG,KAAK,EAAE,WAAW;;AAE/C;AAEM,SAAU,gBACd,WACA,UACA,WAAiB;AAEjB,QAAM,EAAC,SAAS,SAAQ,IAAI,eAAe,WAAW,UAAU,SAAS;AAGzE,SAAO;IACL,KAAK,IAAI,OAAO,IAAI,KAAK,IAAI,QAAQ;IACrC,KAAK,IAAI,OAAO,IAAI,KAAK,IAAI,QAAQ;IACrC,CAAC,KAAK,IAAI,QAAQ;;AAEtB;AAEA,SAAS,YAAY,WAAwB;AAC3C,QAAM,KAAK,OAAO,cAAc,WAAW,YAAY,UAAU,QAAO;AACxE,SAAO,KAAK,YAAY,MAAM;AAChC;AAEA,SAAS,OAA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