![]() |
ALMaSS
1.2 (after EcoStack, March 2024)
The Animal, Landscape and Man Simulation System
|
#include <sunset.h>
Public Member Functions | |
SunSet () | |
SunSet (double, double, int) | |
SunSet (double, double, double) | |
~SunSet () | |
void | setPosition (double, double, int) |
void | setPosition (double, double, double) |
void | setTZOffset (int) |
void | setTZOffset (double) |
double | setCurrentDate (int, int, int) |
double | calcNauticalSunrise () const |
double | calcNauticalSunset () const |
double | calcCivilSunrise () const |
double | calcCivilSunset () const |
double | calcAstronomicalSunrise () const |
double | calcAstronomicalSunset () const |
double | calcCustomSunrise (double) const |
double | calcCustomSunset (double) const |
double | calcSunriseUTC () |
double | calcSunsetUTC () |
double | calcSunrise () const |
double | calcSunset () const |
int | moonPhase (int) const |
int | moonPhase () const |
Static Public Attributes | |
static constexpr double | SUNSET_OFFICIAL = 90.833 |
static constexpr double | SUNSET_NAUTICAL = 102.0 |
static constexpr double | SUNSET_CIVIL = 96.0 |
static constexpr double | SUNSET_ASTONOMICAL = 108.0 |
Private Member Functions | |
double | degToRad (double) const |
double | radToDeg (double) const |
double | calcMeanObliquityOfEcliptic (double) const |
double | calcGeomMeanLongSun (double) const |
double | calcObliquityCorrection (double) const |
double | calcEccentricityEarthOrbit (double) const |
double | calcGeomMeanAnomalySun (double) const |
double | calcEquationOfTime (double) const |
double | calcTimeJulianCent (double) const |
double | calcSunTrueLong (double) const |
double | calcSunApparentLong (double) const |
double | calcSunDeclination (double) const |
double | calcHourAngleSunrise (double, double, double) const |
double | calcHourAngleSunset (double, double, double) const |
double | calcJD (int, int, int) const |
double | calcJDFromJulianCent (double) const |
double | calcSunEqOfCenter (double) const |
double | calcAbsSunrise (double) const |
double | calcAbsSunset (double) const |
Private Attributes | |
double | m_latitude |
double | m_longitude |
double | m_julianDate |
double | m_tzOffset |
int | m_year |
int | m_month |
int | m_day |
This class controls all aspects of the operations. The math is done in private functions, and the public API's allow for returning a sunrise/sunset value for the given coordinates and timezone.
The resulting calculations are relative to midnight of the day you set in the setCurrentDate() function. It does not return a time_t value for delta from the current epoch as that would not make sense as the sunrise/sunset can be calculated thousands of years in the past. The library acts on a day timeframe, and doesn't try to assume anything about any other unit of measurement other than the current set day.
You can instantiate this class a few different ways, depending on your needs. It's possible to set your location one time and forget about doing that again if you don't plan to move. Then you only need to change the date and timezone to get correct data. Or, you can simply create an object with no location or time data and then do that later. This is a good mechanism for the setup()/loop() construct.
The most important thing to remember is to make sure the library knows the exact date and timezone for the sunrise/sunset you are trying to calculate. Not doing so means you are going to have very odd results. It's reasonably easy to know when you've forgotten one or the other by looking at the time the sun would rise and noticing that it is X hours earlier or later. That is, if you get a return of 128 minutes (2:08 AM) past midnight when the sun should rise at 308 (6:08 AM), then you probably forgot to set your EST timezone.
The library also has no idea about daylight savings time. If your timezone changes during the year to account for savings time, you must update your timezone accordingly.
SunSet::SunSet | ( | ) |
Default constructor taking no arguments. It will default all values to zero. It is possible to call calcSunrise() on this type of initialization and it will not fail, but it is unlikely you are at 0,0, TZ=0. This also will not include an initialized date to work from.
SunSet::SunSet | ( | double | lat, |
double | lon, | ||
int | tz | ||
) |
lat | Double Latitude for this object |
lon | Double Longitude for this object |
tz | Integer based timezone for this object |
This will create an object for a location with an integer based timezone value. This constructor is a relic of the original design. It is not deprecated, as this is a valid construction, but the double is preferred for correctness.
SunSet::SunSet | ( | double | lat, |
double | lon, | ||
double | tz | ||
) |
lat | Double Latitude for this object |
lon | Double Longitude for this object |
tz | Double based timezone for this object |
This will create an object for a location with a double based timezone value.
|
private |
offset | Double The specific angle to use when calculating sunrise |
This does a bunch of work to get to an accurate angle. Note that it does it 2x, once to get a rough position, and then it doubles back and redoes the calculations to refine the value. The first time through, it will be off by as much as 2 minutes, but the second time through, it will be nearly perfect.
Note that this is the base calculation for all sunrise calls. The others just modify the offset angle to account for the different needs.
References calcEquationOfTime(), calcHourAngleSunrise(), calcJDFromJulianCent(), calcSunDeclination(), calcTimeJulianCent(), m_julianDate, m_latitude, m_longitude, and radToDeg().
Referenced by calcCustomSunrise(), and calcSunriseUTC().
|
private |
offset | Double The specific angle to use when calculating sunset |
This does a bunch of work to get to an accurate angle. Note that it does it 2x, once to get a rough position, and then it doubles back and redoes the calculations to refine the value. The first time through, it will be off by as much as 2 minutes, but the second time through, it will be nearly perfect.
Note that this is the base calculation for all sunset calls. The others just modify the offset angle to account for the different needs.
References calcEquationOfTime(), calcHourAngleSunset(), calcJDFromJulianCent(), calcSunDeclination(), calcTimeJulianCent(), m_julianDate, m_latitude, m_longitude, and radToDeg().
Referenced by calcCustomSunset(), and calcSunsetUTC().
double SunSet::calcAstronomicalSunrise | ( | ) | const |
This function will return the Astronomical sunrise in local time for your location
References calcCustomSunrise(), and SUNSET_ASTONOMICAL.
double SunSet::calcAstronomicalSunset | ( | ) | const |
This function will return the Astronomical sunset in local time for your location
References calcCustomSunset(), and SUNSET_ASTONOMICAL.
double SunSet::calcCivilSunrise | ( | ) | const |
This function will return the Civil sunrise in local time for your location
References calcCustomSunrise(), and SUNSET_CIVIL.
double SunSet::calcCivilSunset | ( | ) | const |
This function will return the Civil sunset in local time for your location
References calcCustomSunset(), and SUNSET_CIVIL.
double SunSet::calcCustomSunrise | ( | double | angle | ) | const |
angle | The angle in degrees over the horizon at which to calculate the sunset time |
This function will return the sunrise in local time for your location for any angle over the horizon, where < 90 would be above the horizon, and > 90 would be at or below.
References calcAbsSunrise(), and m_tzOffset.
Referenced by calcAstronomicalSunrise(), calcCivilSunrise(), calcNauticalSunrise(), and calcSunrise().
double SunSet::calcCustomSunset | ( | double | angle | ) | const |
angle | The angle in degrees over the horizon at which to calculate the sunset time |
This function will return the sunset in local time for your location for any angle over the horizon, where < 90 would be above the horizon, and > 90 would be at or below.
References calcAbsSunset(), and m_tzOffset.
Referenced by calcAstronomicalSunset(), calcCivilSunset(), calcNauticalSunset(), and calcSunset().
|
private |
Referenced by calcEquationOfTime().
|
private |
References calcEccentricityEarthOrbit(), calcGeomMeanAnomalySun(), calcGeomMeanLongSun(), calcObliquityCorrection(), degToRad(), and radToDeg().
Referenced by calcAbsSunrise(), and calcAbsSunset().
|
private |
Referenced by calcEquationOfTime(), and calcSunEqOfCenter().
|
private |
Referenced by calcEquationOfTime(), and calcSunTrueLong().
|
private |
|
private |
|
private |
y | Integer year as a 4 digit value |
m | Integer month, not 0 based |
d | Integer day, not 0 based |
A well known JD calculator
Referenced by setCurrentDate().
|
private |
Referenced by calcAbsSunrise(), and calcAbsSunset().
|
private |
Referenced by calcObliquityCorrection().
double SunSet::calcNauticalSunrise | ( | ) | const |
This function will return the Nautical sunrise in local time for your location
References calcCustomSunrise(), and SUNSET_NAUTICAL.
double SunSet::calcNauticalSunset | ( | ) | const |
This function will return the Nautical sunset in local time for your location
References calcCustomSunset(), and SUNSET_NAUTICAL.
|
private |
References calcMeanObliquityOfEcliptic(), and degToRad().
Referenced by calcEquationOfTime(), and calcSunDeclination().
|
private |
|
private |
References calcObliquityCorrection(), calcSunApparentLong(), degToRad(), and radToDeg().
Referenced by calcAbsSunrise(), and calcAbsSunset().
|
private |
References calcGeomMeanAnomalySun(), and degToRad().
Referenced by calcSunTrueLong().
double SunSet::calcSunrise | ( | ) | const |
This function will return the Official sunrise in local time for your location
References calcCustomSunrise(), and SUNSET_OFFICIAL.
Referenced by Calendar::CreateDaylength().
double SunSet::calcSunriseUTC | ( | ) |
This is a holdover from the original implementation and to me doesn't seem to be very useful, it's just confusing. This function is deprecated but won't be removed unless that becomes necessary.
References calcAbsSunrise(), and SUNSET_OFFICIAL.
double SunSet::calcSunset | ( | ) | const |
This function will return the Official sunset in local time for your location
References calcCustomSunset(), and SUNSET_OFFICIAL.
Referenced by Calendar::CreateDaylength().
double SunSet::calcSunsetUTC | ( | ) |
This is a holdover from the original implementation and to me doesn't seem to be very useful, it's just confusing. This function is deprecated but won't be removed unless that becomes necessary.
References calcAbsSunset(), and SUNSET_OFFICIAL.
|
private |
|
private |
Referenced by calcAbsSunrise(), and calcAbsSunset().
|
private |
References M_PI.
Referenced by calcEquationOfTime(), calcHourAngleSunrise(), calcHourAngleSunset(), calcObliquityCorrection(), calcSunApparentLong(), calcSunDeclination(), and calcSunEqOfCenter().
int SunSet::moonPhase | ( | ) | const |
Overload to set the moonphase for right now
int SunSet::moonPhase | ( | int | fromepoch | ) | const |
fromepoch | time_t seconds from epoch to calculate the moonphase for |
This is a simple calculation to tell us roughly what the moon phase is locally. It does not give position. It's roughly accurate, but not great.
The return value is 0 to 29, with 0 and 29 being hidden and 14 being full.
|
private |
References M_PI.
Referenced by calcAbsSunrise(), calcAbsSunset(), calcEquationOfTime(), and calcSunDeclination().
double SunSet::setCurrentDate | ( | int | y, |
int | m, | ||
int | d | ||
) |
double SunSet::setCurrentDate(int y, int m, int d)
y | Integer year, must be 4 digits |
m | Integer month, not zero based (Jan = 1) |
d | Integer day of month, not zero based (month starts on day 1) |
Since these calculations are done based on the Julian Calendar, we must convert our year month day into Julian before we use it. You get the Julian value for free if you want it.
References calcJD(), m_day, m_julianDate, m_month, and m_year.
Referenced by Calendar::CreateDaylength().
void SunSet::setPosition | ( | double | lat, |
double | lon, | ||
double | tz | ||
) |
lat | Double Latitude value |
lon | Double Longitude value |
tz | Double Timezone offset |
This will set the location the library uses for it's math. The timezone is included in this as it's not valid to call any of the calc functions until you have set a timezone. It is possible to simply call setPosition one time, with a timezone and not use the setTZOffset() function ever, if you never change timezone values.
References m_latitude, m_longitude, and m_tzOffset.
void SunSet::setPosition | ( | double | lat, |
double | lon, | ||
int | tz | ||
) |
lat | Double Latitude value |
lon | Double Longitude value |
tz | Integer Timezone offset |
This will set the location the library uses for it's math. The timezone is included in this as it's not valid to call any of the calc functions until you have set a timezone. It is possible to simply call setPosition one time, with a timezone and not use the setTZOffset() function ever, if you never change timezone values.
This is the old version of the setPosition using an integer timezone, and will not be deprecated. However, it is preferred to use the double version going forward.
References m_latitude, m_longitude, and m_tzOffset.
Referenced by Calendar::CreateDaylength().
void SunSet::setTZOffset | ( | double | tz | ) |
tz | Double timezone, may be positive or negative |
Critical to set your timezone so results are accurate for your time and date. This function is critical to make sure the system works correctly. If you do not set the timezone correctly, the return value will not be correct for your location. Forgetting this will result in return values that may actually be negative in some cases.
References m_tzOffset.
void SunSet::setTZOffset | ( | int | tz | ) |
tz | Integer timezone, may be positive or negative |
Critical to set your timezone so results are accurate for your time and date. This function is critical to make sure the system works correctly. If you do not set the timezone correctly, the return value will not be correct for your location. Forgetting this will result in return values that may actually be negative in some cases.
This function is a holdover from the previous design using an integer timezone and will not be deprecated. It is preferred to use the setTZOffset(doubble).
References m_tzOffset.
|
private |
Referenced by setCurrentDate().
|
private |
Referenced by calcAbsSunrise(), calcAbsSunset(), and setCurrentDate().
|
private |
Referenced by calcAbsSunrise(), calcAbsSunset(), and setPosition().
|
private |
Referenced by calcAbsSunrise(), calcAbsSunset(), and setPosition().
|
private |
Referenced by setCurrentDate().
|
private |
Referenced by calcCustomSunrise(), calcCustomSunset(), setPosition(), and setTZOffset().
|
private |
Referenced by setCurrentDate().
|
staticconstexpr |
Astronomical sun angle for sunset
Referenced by calcAstronomicalSunrise(), and calcAstronomicalSunset().
|
staticconstexpr |
Civil sun angle for sunset
Referenced by calcCivilSunrise(), and calcCivilSunset().
|
staticconstexpr |
Nautical sun angle for sunset
Referenced by calcNauticalSunrise(), and calcNauticalSunset().
|
staticconstexpr |
Standard sun angle for sunset
Referenced by calcSunrise(), calcSunriseUTC(), calcSunset(), and calcSunsetUTC().