| .. | ||
| README.md | ||
| test.js | ||
bin/README.md
This folder contains the C reference executable (spa_cli) and the JavaScript test harness (test.js) to compare your JS port of the NREL Solar Position Algorithm (SPA) against the original C implementation.
Prerequisites
- Node.js (v14+) installed on your machine.
- C compiler (e.g.
gcc) supporting C99.
Files
spa_cli.c– C CLI wrapper to parse command-line arguments into the SPA structure and output Sunrise, Solar Noon, Sunset inHH:MM:SSformat.spa.c,spa.h– The NREL SPA reference source (download separately).test.js– Node.js script that runs 10 diverse test cases through bothspa_cliand your JS port (getSpa) and prints a side-by-side comparison.
Setup
-
Download the NREL SPA source
cd bin curl -O https://midcdmz.nrel.gov/spa/spa.c curl -O https://midcdmz.nrel.gov/spa/spa.h -
Copy or create
spa_cli.cPlace thespa_cli.cfile (provided alongside this README) into this folder. -
Compile the C executable
gcc -std=c99 -O2 -o spa_cli spa.c spa_cli.c -lm- Do NOT include
spa_tester.cfor this purpose. The customspa_cli.chandles all required argument parsing and output.
- Do NOT include
-
Install Node.js dependencies From the project root (one level up):
npm installThis ensures your JS port (
index.jsanddist/spa.js) is available.
Running the Tests
Inside the bin/ folder, execute:
node test.js
You should see a table with each city/date, and matching Sunrise, Solar Noon, and Sunset times from both the C reference and your JS implementation.
Example output:
Location | Date | C Rise | JS Rise | C Noon | JS Noon | C Set | JS Set
-----------------------------------------------------------------------------------
New York Summer | 2025-06-21 | 05:25:03 | 05:25:03 | 12:57:56 | 12:57:56 | 20:30:35 | 20:30:35
...
Notes
- If you update your JS port (
index.js), rerunnode test.jsto verify that drift remains within a second. - Ensure
spa_cliis executable (chmod +x spa_cli) and located in the same directory astest.js.
Results
Results from my personal tests when comparing original C version to this JS version is below:
% node test.js
Location | Date | C Rise | JS Rise | C Noon | JS Noon | C Set | JS Set
-----------------------------------------------------------------------------------
New York Summer | 2025-06-21 | 05:25:03 | 05:25:03 | 12:57:56 | 12:57:56 | 20:30:35 | 20:30:35
New York Winter | 2025-12-21 | 07:16:41 | 07:16:41 | 11:54:19 | 11:54:19 | 16:31:56 | 16:31:56
London Summer | 2025-06-21 | 04:43:07 | 04:43:07 | 13:02:22 | 13:02:22 | 21:21:37 | 21:21:37
London Winter | 2025-12-21 | 08:03:52 | 08:03:52 | 11:58:42 | 11:58:42 | 15:53:32 | 15:53:32
Tokyo Summer | 2025-06-21 | 04:25:52 | 04:25:52 | 11:43:00 | 11:43:00 | 19:00:22 | 19:00:22
Sydney Winter | 2025-06-21 | 07:00:12 | 07:00:12 | 11:56:56 | 11:56:56 | 16:53:52 | 16:53:52
Reykjavik Mids | 2025-06-21 | 02:55:10 | 02:55:10 | 13:29:38 | 13:29:38 | 00:03:54 | 00:03:54
Cape Town Summer | 2025-12-21 | 05:31:55 | 05:31:55 | 12:44:28 | 12:44:28 | 19:57:01 | 19:57:01
Quito Equinox | 2025-03-20 | 06:17:54 | 06:17:54 | 12:21:10 | 12:21:10 | 18:24:25 | 18:24:25
Tromso Polar | 2025-12-21 | N/A | N/A | N/A | N/A | N/A | N/A