No description
Find a file
2026-05-30 18:48:57 -04:00
.github chore: P1 standardization — finalize src/wiki/ci 2026-05-30 18:48:57 -04:00
bin Major fix to core time calculations 2025-05-04 07:50:00 -04:00
coverage chore: P1 standardization — finalize src/wiki/ci 2026-05-30 18:48:57 -04:00
lib feat(e6): portfolio polish — docs, CI, TypeScript standard, wiki 2026-05-28 13:59:43 -04:00
src chore: P1 standardization — finalize src/wiki/ci 2026-05-30 18:48:57 -04:00
.editorconfig v2.0.0: TypeScript rewrite with dual CJS/ESM build 2026-02-25 11:01:38 -05:00
.gitignore refactor: code quality improvements across the board 2026-03-08 11:34:05 -04:00
.npmrc refactor: code quality improvements across the board 2026-03-08 11:34:05 -04:00
.nvmrc v2.0.0: TypeScript rewrite with dual CJS/ESM build 2026-02-25 11:01:38 -05:00
CHANGELOG.md fix: revert type:module to restore CJS compatibility (2.0.2 patch) 2026-05-28 14:17:44 -04:00
eslint.config.mjs chore: adopt shared config packages (tsconfig, eslint, prettier) 2026-05-30 15:03:07 -04:00
LICENSE v2.0.0: TypeScript rewrite with dual CJS/ESM build 2026-02-25 11:01:38 -05:00
package.json docs: add TypeDoc API generation (typedoc@0.28.19 + typedoc-plugin-markdown@4.11.0) 2026-05-30 16:41:57 -04:00
pnpm-lock.yaml docs: add TypeDoc API generation (typedoc@0.28.19 + typedoc-plugin-markdown@4.11.0) 2026-05-30 16:41:57 -04:00
pnpm-workspace.yaml v2.0.0: TypeScript rewrite with dual CJS/ESM build 2026-02-25 11:01:38 -05:00
README.md chore: P1 standardization — finalize src/wiki/ci 2026-05-30 18:48:57 -04:00
test-cjs.cjs refactor: code quality improvements across the board 2026-03-08 11:34:05 -04:00
test.mjs refactor: code quality improvements across the board 2026-03-08 11:34:05 -04:00
tsconfig.json chore: adopt shared config packages (tsconfig, eslint, prettier) 2026-05-30 15:03:07 -04:00
tsup.config.ts feat(e6): portfolio polish — docs, CI, TypeScript standard, wiki 2026-05-28 13:59:43 -04:00
typedoc.json docs: add TypeDoc API generation (typedoc@0.28.19 + typedoc-plugin-markdown@4.11.0) 2026-05-30 16:41:57 -04:00

nrel-spa

npm version CI license wiki

Pure JavaScript implementation of the NREL Solar Position Algorithm (SPA). Computes solar zenith angle, azimuth, sunrise, sunset, and solar noon for any location and date. Zero dependencies, synchronous. Validated to produce identical results to the original NREL C reference implementation.

Installation

npm install nrel-spa

Quick Start

import { getSpa, calcSpa } from 'nrel-spa';

const date = new Date('2025-06-21T00:00:00Z');

// Raw fractional hours
const raw = getSpa(date, 40.7128, -74.006, -4); // New York, EDT
console.log(raw.sunrise);   // 5.417
console.log(raw.solarNoon); // 12.965
console.log(raw.sunset);    // 20.509

// Formatted HH:MM:SS strings
const fmt = calcSpa(date, 40.7128, -74.006, -4);
console.log(fmt.sunrise);   // "05:25:03"
console.log(fmt.solarNoon); // "12:57:56"
console.log(fmt.sunset);    // "20:30:35"

CommonJS:

const { getSpa } = require('nrel-spa');

Pass a zenith angles array as the sixth argument to getSpa/calcSpa for civil (96°), nautical (102°), or astronomical (108°) twilight times.

TypeScript

import { getSpa, calcSpa, formatTime, SPA_ZA_RTS } from 'nrel-spa';
import type { SpaOptions, SpaResult, SpaFunctionCode } from 'nrel-spa';

Documentation

Full API reference, algorithm notes, and twilight calculation guide: GitHub Wiki

  • solar-spa: WASM build of the same algorithm, async, for high-throughput batch work
  • pray-calc: Islamic prayer times built on nrel-spa

Acknowledgments

The core algorithm is a JavaScript port of the NREL SPA by Ibrahim Reda and Afshin Andreas:

Reda, I., Andreas, A. (2004). "Solar Position Algorithm for Solar Radiation Applications." Solar Energy, 76(5), 577-589.

License

MIT (TypeScript wrapper and build tooling). The core algorithm in lib/spa.js is a port of NREL's SPA C source, subject to its own terms. See LICENSE.