SolTrack
SolTrack is a simple, free, fast and accurate C routine to compute the position of the Sun
All Data Structures Files Functions Variables Macros
Data Structures | Macros | Functions
SolTrack.h File Reference

(SolTrack version 2.2, revision 13, hash 9f77eee, 2017-09-09)

#include "stdio.h"
#include "math.h"
Include dependency graph for SolTrack.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Time
 Date and time to compute the Sun's position for, in UT. More...
 
struct  Location
 Location to compute the Sun's position for. More...
 
struct  Position
 Position of the Sun. More...
 
struct  RiseSet
 Rise and set data for the Sun. More...
 

Macros

#define PI   3.14159265358979323846
 
#define TWO_PI   6.28318530717958647693
 
#define MPI   3.14159265358979323846e6
 
#define R2D   57.2957795130823208768
 
#define R2H   3.81971863420548805845
 

Functions

void SolTrack (struct Time time, struct Location location, struct Position *position, int useDegrees, int useNorthEqualsZero, int computeRefrEquatorial, int computeDistance)
 Main function to compute the position of the Sun. More...
 
double computeJulianDay (int year, int month, int day, int hour, int minute, double second)
 Compute the Julian Day from the date and time. More...
 
void computeLongitude (int computeDistance, struct Position *position)
 Compute the ecliptic longitude of the Sun for a given Julian Day. More...
 
void convertEclipticToEquatorial (double longitude, double cosObliquity, double *rightAscension, double *declination)
 Convert ecliptic coordinates to equatorial coordinates. More...
 
void convertEquatorialToHorizontal (struct Location location, struct Position *position)
 Convert equatorial to horizontal coordinates. More...
 
void eq2horiz (double sinLat, double cosLat, double longitude, double rightAscension, double declination, double agst, double *azimuth, double *sinAlt)
 Convert equatorial coordinates to horizontal coordinates. More...
 
void convertHorizontalToEquatorial (double sinLat, double cosLat, double azimuth, double altitude, double *hourAngle, double *declination)
 Convert (refraction-corrected) horizontal coordinates to equatorial coordinates. More...
 
void setNorthToZero (double *azimuth, double *hourAngle, int computeRefrEquatorial)
 Convert the South=0 convention to North=0 convention for azimuth and hour angle. More...
 
void convertRadiansToDegrees (double *longitude, double *rightAscension, double *declination, double *altitude, double *azimuthRefract, double *altitudeRefract, double *hourAngle, double *declinationRefract, int computeRefrEquatorial)
 Convert final results from radians to degrees. More...
 
double STatan2 (double y, double x)
 My version of the atan2() function - ~39% faster than built-in (in terms of number of instructions) More...
 
void SolTrack_RiseSet (struct Time time, struct Location location, struct Position *position, struct RiseSet *riseSet, double sa0, int useDegrees, int useNorthEqualsZero)
 Compute rise, transit and set times for the Sun, as well as their azimuths/altitude. More...
 
double rev (double angle)
 Fold an angle to take a value between 0 and 2pi. More...
 
double rev2 (double angle)
 Fold an angle to take a value between -pi and pi. More...
 

Macro Definition Documentation

◆ PI

#define PI   3.14159265358979323846

◆ TWO_PI

#define TWO_PI   6.28318530717958647693

◆ MPI

#define MPI   3.14159265358979323846e6

Referenced by rev(), and rev2().

◆ R2D

#define R2D   57.2957795130823208768

◆ R2H

#define R2H   3.81971863420548805845

Referenced by SolTrack_RiseSet().

Function Documentation

◆ SolTrack()

void SolTrack ( struct Time  time,
struct Location  location,
struct Position position,
int  useDegrees,
int  useNorthEqualsZero,
int  computeRefrEquatorial,
int  computeDistance 
)

Main function to compute the position of the Sun.

Parameters
[in]timeStruct containing date and time to compute the position for, in UT
[in,out]locationStruct containing the geographic location to compute the position for
[out]positionStruct containing the position of the Sun in horizontal (and equatorial if desired) coordinates
[in]useDegreesUse degrees for input and output angular variables, rather than radians
[in]useNorthEqualsZeroUse the definition where azimuth=0 denotes north, rather than south
[in]computeRefrEquatorialCompute refraction correction for equatorial coordinates: 0-no, 1-yes
[in]computeDistanceCompute distance to the Sun (in AU): 0-no, 1-yes

Example Usage:

SolTrack(time, location, &position, 1);

References Position::altitude, Position::altitudeRefract, Position::azimuthRefract, computeJulianDay(), computeLongitude(), convertEclipticToEquatorial(), convertEquatorialToHorizontal(), convertHorizontalToEquatorial(), convertRadiansToDegrees(), Location::cosLat, Position::cosObliquity, Time::day, Position::declination, Position::declinationRefract, Time::hour, Position::hourAngleRefract, Position::julianDay, Location::latitude, Location::longitude, Position::longitude, Time::minute, Time::month, R2D, Position::rightAscension, Time::second, setNorthToZero(), Location::sinLat, Position::tJC, Position::tJC2, Position::tJD, and Time::year.

Referenced by SolTrack_RiseSet().

◆ computeJulianDay()

double computeJulianDay ( int  year,
int  month,
int  day,
int  hour,
int  minute,
double  second 
)

Compute the Julian Day from the date and time.

Gregorian calendar only (>~1582).

Parameters
[in]yearYear of date
[in]monthMonth of date
[in]dayDay of date
[in]hourHour of time
[in]minuteMinute of time
[in]secondSecond of time
Return values
JulianDayJulian day for the given date and time

Referenced by SolTrack().

◆ computeLongitude()

void computeLongitude ( int  computeDistance,
struct Position position 
)

Compute the ecliptic longitude of the Sun for a given Julian Day.

Also computes the obliquity of the ecliptic and nutation.

Parameters
[in]computeDistanceCompute distance to the Sun (in AU): 0-no, 1-yes
[in,out]positionPosition of the Sun

References Position::cosObliquity, Position::distance, Position::longitude, Position::nutationLon, Position::obliquity, Position::tJC, Position::tJC2, and TWO_PI.

Referenced by SolTrack().

◆ convertEclipticToEquatorial()

void convertEclipticToEquatorial ( double  longitude,
double  cosObliquity,
double *  rightAscension,
double *  declination 
)

Convert ecliptic coordinates to equatorial coordinates.

This function assumes that the ecliptic latitude = 0.

Parameters
[in]longitudeEcliptic longitude of the Sun (rad)
[in]cosObliquityCosine of the obliquity of the ecliptic
[out]rightAscensionRight ascension of the Sun (rad)
[out]declinationDeclination of the Sun (rad)

References STatan2().

Referenced by SolTrack().

◆ convertEquatorialToHorizontal()

void convertEquatorialToHorizontal ( struct Location  location,
struct Position position 
)

Convert equatorial to horizontal coordinates.

Also corrects for parallax and atmospheric refraction.

Parameters
[in]locationGeographic location of the observer (rad)
[in,out]positionPosition of the Sun (rad)

References Position::agst, Position::altitude, Position::altitudeRefract, Position::azimuthRefract, Location::cosLat, Position::cosObliquity, Position::declination, eq2horiz(), Location::longitude, Position::nutationLon, Location::pressure, Position::rightAscension, Location::sinLat, Location::temperature, Position::tJC2, and Position::tJD.

Referenced by SolTrack().

◆ eq2horiz()

void eq2horiz ( double  sinLat,
double  cosLat,
double  longitude,
double  rightAscension,
double  declination,
double  agst,
double *  azimuth,
double *  sinAlt 
)

Convert equatorial coordinates to horizontal coordinates.

Parameters
[in]sinLatSine of the geographic latitude of the observer
[in]cosLatCosine of the geographic latitude of the observer
[in]longitudeGeographic longitude of the observer (rad)
[in]rightAscensionRight ascension of the Sun (rad)
[in]declinationDeclination of the Sun (rad)
[in]agstApparent Greenwich sidereal time (Greenwich mean sidereal time, corrected for the equation of the equinoxes)
[out]azimuthAzimuth ("wind direction") of the Sun (rad; 0=South)
[out]sinAltSine of the altitude of the Sun above the horizon

References STatan2().

Referenced by convertEquatorialToHorizontal().

◆ convertHorizontalToEquatorial()

void convertHorizontalToEquatorial ( double  sinLat,
double  cosLat,
double  azimuth,
double  altitude,
double *  hourAngle,
double *  declination 
)

Convert (refraction-corrected) horizontal coordinates to equatorial coordinates.

Parameters
[in]sinLatSine of the geographic latitude of the observer
[in]cosLatCosine of the geographic latitude of the observer
[in]azimuthAzimuth ("wind direction") of the Sun (rad; 0=South)
[in]altitudeAltitude of the Sun above the horizon (rad)
[out]hourAngleHour angle of the Sun (rad; 0=South)
[out]declinationDeclination of the Sun (rad)

References STatan2().

Referenced by SolTrack().

◆ setNorthToZero()

void setNorthToZero ( double *  azimuth,
double *  hourAngle,
int  computeRefrEquatorial 
)

Convert the South=0 convention to North=0 convention for azimuth and hour angle.

South=0 is the default in celestial astronomy. This makes the angles compatible with the compass/wind directions.

Parameters
[in,out]azimuthAzimuth ("wind direction") of the Sun (rad)
[in,out]hourAngleHour angle of the Sun (rad)
[in]computeRefrEquatorialCompute refraction correction for equatorial coordinates

References PI, and TWO_PI.

Referenced by SolTrack().

◆ convertRadiansToDegrees()

void convertRadiansToDegrees ( double *  longitude,
double *  rightAscension,
double *  declination,
double *  altitude,
double *  azimuthRefract,
double *  altitudeRefract,
double *  hourAngle,
double *  declinationRefract,
int  computeRefrEquatorial 
)

Convert final results from radians to degrees.

Not touching intermediate results.

Parameters
[in,out]longitudeEcliptical longitude of the Sun (rad->deg)
[in,out]rightAscensionRight ascension of the Sun (rad->deg)
[in,out]declinationDeclination of the Sun (rad->deg)
[in,out]altitudeAltitude of the Sun above the horizon (rad->deg)
[in,out]azimuthRefractAzimuth ("wind direction") of the Sun (rad->deg), corrected for refraction
[in,out]altitudeRefractAltitude of the Sun above the horizon (rad->deg), corrected for refraction
[in,out]hourAngleHour angle of the Sun (rad->deg)
[in,out]declinationRefractDeclination of the Sun (rad->deg), corrected for refraction
[in]computeRefrEquatorialCompute refraction correction for equatorial coordinates

References R2D.

Referenced by SolTrack().

◆ STatan2()

double STatan2 ( double  y,
double  x 
)

My version of the atan2() function - ~39% faster than built-in (in terms of number of instructions)

Parameters
[in]yNumerator of the fraction to compute the arctangent for
[in]xDenominator of the fraction to compute the arctangent for

atan2(y,x) = atan(y/x), where the result will be put in the correct quadrant

See also
https://en.wikipedia.org/wiki/Atan2#Definition_and_computation

References PI.

Referenced by convertEclipticToEquatorial(), convertHorizontalToEquatorial(), and eq2horiz().

◆ SolTrack_RiseSet()

void SolTrack_RiseSet ( struct Time  time,
struct Location  location,
struct Position position,
struct RiseSet riseSet,
double  rsAlt,
int  useDegrees,
int  useNorthEqualsZero 
)

Compute rise, transit and set times for the Sun, as well as their azimuths/altitude.

Parameters
[in]timeStruct containing date and time to compute the position for, in UT
[in]locationStruct containing the geographic location to compute the position for
[out]positionStruct containing the position of the Sun
[out]riseSetStruct containing the Sun's rise, transit and set data
[in]rsAltAltitude to return rise/set data for (radians; 0. is actual rise/set). rsAlt>pi/2: compute transit only
[in]useDegreesUse degrees for input and output angular variables, rather than radians
[in]useNorthEqualsZeroUse the definition where azimuth=0 denotes north, rather than south

Example Usage:

SolTrack(time, location, &position, 0.0);
Note
  • if rsAlt == 0.0, actual rise and set times are computed
  • if rsAlt != 0.0, the routine calculates when alt = rsAlt is reached
  • returns times, rise/set azimuth and transit altitude in the struct position
See also
  • subroutine riset() in riset.f90 from libTheSky (libthesky.sf.net) for more info

References Position::agst, Time::day, Position::declination, Time::hour, Location::latitude, Location::longitude, Time::minute, Time::month, PI, R2D, R2H, rev(), rev2(), Position::rightAscension, RiseSet::riseAzimuth, RiseSet::riseTime, Time::second, RiseSet::setAzimuth, RiseSet::setTime, SolTrack(), RiseSet::transitAltitude, RiseSet::transitTime, and Time::year.

◆ rev()

double rev ( double  angle)

Fold an angle to take a value between 0 and 2pi.

Parameters
[in]angleAngle to fold (radians)

References MPI, and TWO_PI.

Referenced by SolTrack_RiseSet().

◆ rev2()

double rev2 ( double  angle)

Fold an angle to take a value between -pi and pi.

Parameters
[in]angleAngle to fold (radians)

References MPI, PI, and TWO_PI.

Referenced by SolTrack_RiseSet().