A C++23 implementation of the solar azimuth formula from Taiping Zhang, Paul W. Stackhouse, Bradley Macpherson and Colleen Mikovitz (2021).
A solar azimuth formula that renders circumstantial treatment unnecessary without compromising mathematical rigor: Mathematical setup, application and extension of a formula based on the subsolar point and atan2 function Renewable Energy, 172, 1333-1340
This project provides a compact, self-contained implementation of azimuth computation using the approach described in the paper. It outputs the solar azimuth and zenith for a given date, time, and location.
- Implements the atan2-based azimuth formula that avoids case-by-case circumstantial adjustments
- Computes subsolar point for a given UTC datetime
- Returns azimuth relative to north
- Dependencies: C++23 (or above) and p-ranav/argparse for CLI argument parsing
g++ -std=c++23 -O3 *.cpp -o sunpos
./sunpos --help
# output:
# Usage: sunpos [--help] [--version] [--date DATE] [--track] latitude longitude
#
# Calculate the position of the sun
#
# Positional arguments:
# latitude our latitude
# longitude our longitude
#
# Optional arguments:
# -h, --help shows help message and exits
# -v, --version prints version information and exits
# -d, --date DATE specify the UTC date as "YYYY-MM-DD HH:MM:SS"
# -t, --track track the current time
./sunpos 51.340333 12.374750 -d "2025-10-31 18:33:00"
# output:
# 2025-10-31 18:33:00 51.340333 12.374750 -26.541346 280.398407Add -DSP_FLOATING_POINT_TYPE=double to compiler flags for double precision.
- main.cpp — CLI program
- sunpos.cpp and sunpos.h — contains
compute_sun_position(), include for own programs - sunpos.cbp — Code::Blocks project file for quick start
GNU Public License, see LICENSE file.
- Zhang, T., Stackhouse, P.W., Macpherson, B., and Mikovitz, J.C., 2021. A solar azimuth formula that renders circumstantial treatment unnecessary without compromising mathematical rigor: Mathematical setup, application and extension of a formula based on the subsolar point and atan2 function. Renewable Energy, 172, 1333-1340. DOI: https://doi.org/10.1016/j.renene.2021.03.047
For issues or contributions, open an issue or pull request in the repository.