diff options
author | Mattias Andrée <maandree@kth.se> | 2019-10-06 09:36:23 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-10-06 09:36:23 +0200 |
commit | 1be48c64b4e5df9e9b1ad69b3bb30b867b0abf9e (patch) | |
tree | 697f6c2fb5f3f91ab64e6ed32c45e483bf31e442 /solar.c | |
parent | Add golden hour and blue hour (diff) | |
download | libred-1be48c64b4e5df9e9b1ad69b3bb30b867b0abf9e.tar.gz libred-1be48c64b4e5df9e9b1ad69b3bb30b867b0abf9e.tar.bz2 libred-1be48c64b4e5df9e9b1ad69b3bb30b867b0abf9e.tar.xz |
Clean up
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | solar.c (renamed from src/solar.c) | 194 |
1 files changed, 96 insertions, 98 deletions
@@ -1,21 +1,4 @@ -/** - * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - * This file is a stripped down port of <https://github.com/maandree/solar-python>. - */ +/* See LICENSE file for copyright and license details. */ #include "libred.h" #include <math.h> #include <time.h> @@ -48,14 +31,16 @@ * @throws 0 On success. * @throws Any error specified for clock_gettime(3) on error. */ -static double julian_centuries() +static double +julian_centuries() { - struct timespec now; - double tm; - if (clock_gettime(CLOCK_REALTIME_COARSE, &now)) return 0.0; - tm = (double)(now.tv_nsec) / 1000000000.0 + (double)(now.tv_sec); - tm = (tm / 86400.0 + 2440587.5 - 2451545.0) / 36525.0; - return errno = 0, tm; + struct timespec now; + double tm; + if (clock_gettime(CLOCK_REALTIME_COARSE, &now)) + return 0.0; + tm = (double)(now.tv_nsec) / 1000000000.0 + (double)(now.tv_sec); + tm = (tm / 86400.0 + 2440587.5 - 2451545.0) / 36525.0; + return errno = 0, tm; } /** @@ -64,9 +49,10 @@ static double julian_centuries() * @param tm The time in Julian Centuries * @return The time in Julian Days */ -static inline double julian_centuries_to_julian_day(double tm) +static inline double +julian_centuries_to_julian_day(double tm) { - return tm * 36525.0 + 2451545.0; + return tm * 36525.0 + 2451545.0; } @@ -76,9 +62,10 @@ static inline double julian_centuries_to_julian_day(double tm) * @param deg The angle in degrees. * @param The angle in radians. */ -static inline double radians(double deg) +static inline double +radians(double deg) { - return deg * (double)M_PI / 180.0; + return deg * (double)M_PI / 180.0; } /** @@ -87,9 +74,10 @@ static inline double radians(double deg) * @param rad The angle in radians. * @param The angle in degrees. */ -static inline double degrees(double rad) +static inline double +degrees(double rad) { - return rad * 180.0 / (double)M_PI; + return rad * 180.0 / (double)M_PI; } @@ -102,12 +90,13 @@ static inline double degrees(double rad) * @param hour_angle The solar hour angle, in radians. * @return The Sun's elevation, in radians. */ -static inline double elevation_from_hour_angle(double latitude, double declination, double hour_angle) +static inline double +elevation_from_hour_angle(double latitude, double declination, double hour_angle) { - double rc = cos(radians(latitude)); - rc *= cos(hour_angle) * cos(declination); - rc += sin(radians(latitude)) * sin(declination); - return asin(rc); + double rc = cos(radians(latitude)); + rc *= cos(hour_angle) * cos(declination); + rc += sin(radians(latitude)) * sin(declination); + return asin(rc); } /** @@ -116,13 +105,14 @@ static inline double elevation_from_hour_angle(double latitude, double declinati * @param tm The time in Julian Centuries. * @return The Sun's geometric mean longitude in radians. */ -static inline double sun_geometric_mean_longitude(double tm) +static inline double +sun_geometric_mean_longitude(double tm) { - double rc = fmod(pow(0.0003032 * tm, 2.0) + 36000.76983 * tm + 280.46646, 360.0); + double rc = fmod(pow(0.0003032 * tm, 2.0) + 36000.76983 * tm + 280.46646, 360.0); #if defined(TIMETRAVELLER) - rc = rc < 0.0 ? (rc + 360.0) : rc; + rc = rc < 0.0 ? (rc + 360.0) : rc; #endif - return radians(rc); + return radians(rc); } /** @@ -131,9 +121,10 @@ static inline double sun_geometric_mean_longitude(double tm) * @param tm The time in Julian Centuries. * @return The Sun's geometric mean anomaly in radians. */ -static inline double sun_geometric_mean_anomaly(double tm) +static inline double +sun_geometric_mean_anomaly(double tm) { - return radians(pow(-0.0001537 * tm, 2.0) + 35999.05029 * tm + 357.52911); + return radians(pow(-0.0001537 * tm, 2.0) + 35999.05029 * tm + 357.52911); } /** @@ -142,9 +133,10 @@ static inline double sun_geometric_mean_anomaly(double tm) * @param tm The time in Julian Centuries. * @return The Earth's orbit eccentricity. */ -static inline double earth_orbit_eccentricity(double tm) +static inline double +earth_orbit_eccentricity(double tm) { - return pow(-0.0000001267 * tm, 2.0) - 0.000042037 * tm + 0.016708634; + return pow(-0.0000001267 * tm, 2.0) - 0.000042037 * tm + 0.016708634; } /** @@ -154,13 +146,14 @@ static inline double earth_orbit_eccentricity(double tm) * @param tm The time in Julian Centuries. * @return The Sun's equation of the centre, in radians. */ -static inline double sun_equation_of_centre(double tm) +static inline double +sun_equation_of_centre(double tm) { - double a = sun_geometric_mean_anomaly(tm), rc; - rc = sin(1.0 * a) * (pow(-0.000014 * tm, 2.0) - 0.004817 * tm + 1.914602); - rc += sin(2.0 * a) * (-0.000101 * tm + 0.019993); - rc += sin(3.0 * a) * 0.000289; - return radians(rc); + double a = sun_geometric_mean_anomaly(tm), rc; + rc = sin(1.0 * a) * (pow(-0.000014 * tm, 2.0) - 0.004817 * tm + 1.914602); + rc += sin(2.0 * a) * (-0.000101 * tm + 0.019993); + rc += sin(3.0 * a) * 0.000289; + return radians(rc); } /** @@ -169,9 +162,10 @@ static inline double sun_equation_of_centre(double tm) * @param tm The time in Julian Centuries. * @return The longitude, in radians. */ -static inline double sun_real_longitude(double tm) +static inline double +sun_real_longitude(double tm) { - return sun_geometric_mean_longitude(tm) + sun_equation_of_centre(tm); + return sun_geometric_mean_longitude(tm) + sun_equation_of_centre(tm); } /** @@ -180,10 +174,11 @@ static inline double sun_real_longitude(double tm) * @param tm The time in Julian Centuries. * @return The longitude, in radians. */ -static inline double sun_apparent_longitude(double tm) +static inline double +sun_apparent_longitude(double tm) { - double rc = degrees(sun_real_longitude(tm)) - 0.00569; - return radians(rc - 0.00478 * sin(radians(-1934.136 * tm + 125.04))); + double rc = degrees(sun_real_longitude(tm)) - 0.00569; + return radians(rc - 0.00478 * sin(radians(-1934.136 * tm + 125.04))); } /** @@ -193,10 +188,11 @@ static inline double sun_apparent_longitude(double tm) * @param tm The time in Julian Centuries. * @return The uncorrected mean obliquity, in radians. */ -static double mean_ecliptic_obliquity(double tm) +static double +mean_ecliptic_obliquity(double tm) { - double rc = pow(0.001813 * tm, 3.0) - pow(0.00059 * tm, 2.0) - 46.815 * tm + 21.448; - return radians(23.0 + (26.0 + rc / 60.0) / 60.0); + double rc = pow(0.001813 * tm, 3.0) - pow(0.00059 * tm, 2.0) - 46.815 * tm + 21.448; + return radians(23.0 + (26.0 + rc / 60.0) / 60.0); } /** @@ -206,10 +202,11 @@ static double mean_ecliptic_obliquity(double tm) * @param tm The time in Julian Centuries. * @return The mean obliquity, in radians. */ -static double corrected_mean_ecliptic_obliquity(double tm) +static double +corrected_mean_ecliptic_obliquity(double tm) { - double rc = 0.00256 * cos(radians(-1934.136 * tm + 125.04)); - return radians(rc + degrees(mean_ecliptic_obliquity(tm))); + double rc = 0.00256 * cos(radians(-1934.136 * tm + 125.04)); + return radians(rc + degrees(mean_ecliptic_obliquity(tm))); } /** @@ -218,10 +215,11 @@ static double corrected_mean_ecliptic_obliquity(double tm) * @param tm The time in Julian Centuries. * @return The Sun's declination, in radian. */ -static inline double solar_declination(double tm) +static inline double +solar_declination(double tm) { - double rc = sin(corrected_mean_ecliptic_obliquity(tm)); - return asin(rc * sin(sun_apparent_longitude(tm))); + double rc = sin(corrected_mean_ecliptic_obliquity(tm)); + return asin(rc * sin(sun_apparent_longitude(tm))); } /** @@ -231,17 +229,18 @@ static inline double solar_declination(double tm) * @param tm The time in Julian Centuries. * @return The equation of time, in degrees. */ -static inline double equation_of_time(double tm) +static inline double +equation_of_time(double tm) { - double l = sun_geometric_mean_longitude(tm); - double e = earth_orbit_eccentricity(tm); - double m = sun_geometric_mean_anomaly(tm); - double y = pow(tan(corrected_mean_ecliptic_obliquity(tm) / 2.0), 2.0); - double rc = y * sin(2.0 * l); - rc += (4.0 * y * cos(2.0 * l) - 2.0) * e * sin(m); - rc -= pow(0.5 * y, 2.0) * sin(4.0 * l); - rc -= pow(1.25 * e, 2.0) * sin(2.0 * m); - return 4.0 * degrees(rc); + double l = sun_geometric_mean_longitude(tm); + double e = earth_orbit_eccentricity(tm); + double m = sun_geometric_mean_anomaly(tm); + double y = pow(tan(corrected_mean_ecliptic_obliquity(tm) / 2.0), 2.0); + double rc = y * sin(2.0 * l); + rc += (4.0 * y * cos(2.0 * l) - 2.0) * e * sin(m); + rc -= pow(0.5 * y, 2.0) * sin(4.0 * l); + rc -= pow(1.25 * e, 2.0) * sin(2.0 * m); + return 4.0 * degrees(rc); } /** @@ -256,13 +255,14 @@ static inline double equation_of_time(double tm) * @return The Sun's apparent elevation at the specified time as seen * from the specified position, measured in radians. */ -static inline double solar_elevation_from_time(double tm, double latitude, double longitude) +static inline double +solar_elevation_from_time(double tm, double latitude, double longitude) { - double rc = julian_centuries_to_julian_day(tm); - rc = (rc - round(rc) - 0.5) * 1440; - rc = 720.0 - rc - equation_of_time(tm); - rc = radians(rc / 4.0 - longitude); - return elevation_from_hour_angle(latitude, solar_declination(tm), rc); + double rc = julian_centuries_to_julian_day(tm); + rc = (rc - round(rc) - 0.5) * 1440; + rc = 720.0 - rc - equation_of_time(tm); + rc = radians(rc / 4.0 - longitude); + return elevation_from_hour_angle(latitude, solar_declination(tm), rc); } @@ -280,10 +280,11 @@ static inline double solar_elevation_from_time(double tm, double latitude, doubl * @throws 0 On success. * @throws Any error specified for clock_gettime(3) on error. */ -double libred_solar_elevation(double latitude, double longitude) +double +libred_solar_elevation(double latitude, double longitude) { - double tm = julian_centuries(); - return errno ? -1 : degrees(solar_elevation_from_time(tm, latitude, longitude)); + double tm = julian_centuries(); + return errno ? -1 : degrees(solar_elevation_from_time(tm, latitude, longitude)); } @@ -295,20 +296,17 @@ double libred_solar_elevation(double latitude, double longitude) int libred_check_timetravel(void) { #if !defined(TIMETRAVELLER) - struct timespec now; - if (clock_gettime(CLOCK_REALTIME, &now)) return -1; - if (now.tv_nsec < (time_t)946728000L) - fprintf(stderr, - "We have detected that you are a time-traveller" - "(or your clock is not configured correctly.)" - "Please recompile libred with -DTIMETRAVELLER" - "(or correct your clock.)"), exit(1); + struct timespec now; + if (clock_gettime(CLOCK_REALTIME, &now)) + return -1; + if (now.tv_sec < (time_t)946728000L) { + fprintf(stderr, + "We have detected that you are a time-traveller" + "(or your clock is not configured correctly.)" + "Please recompile libred with -DTIMETRAVELLER" + "(or correct your clock.)"); + exit(1); + } #endif - return 0; + return 0; } - - -#if __GNUC__ -# pragma GCC diagnostic pop -#endif - |