diff options
Diffstat (limited to '')
-rw-r--r-- | solar.c | 70 |
1 files changed, 63 insertions, 7 deletions
@@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "libred.h" +#undef libred_solar_elevation #include <math.h> #include <time.h> #include <errno.h> @@ -21,6 +22,50 @@ #endif +/* old erroneous function declarations: */ +double libred_solar_elevation(double latitude, double longitude, double *elevation); + + +#if !defined(_POSIX_TIMERS) +# ifndef CLOCK_REALTIME_COARSE +# define CLOCK_REALTIME_COARSE 0 +# endif +# ifndef CLOCK_REALTIME +# define CLOCK_REALTIME 0 +# endif +# if defined(_WIN32) +# include <windows.h> +int +clock_gettime(int clockid, struct timespec *ts) +{ + /* https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times */ + FILETIME ft; + ULARGE_INTEGER u64; + (void) clockid; + GetSystemTimePreciseAsFileTime(&ft); + u64.LowPart = ft.dwLowDateTime; + u64.HighPart = ft.dwHighDateTime; + ts->tv_sec = (time_t)(uli.QuadPart / 100000000ULL - 11644473600ULL); + ts->tv_nsec = (long)(uli.QuadPart % 100000000ULL * 10ULL); + return 0; +} +# else +# include <sys/time.h> +int +clock_gettime(int clockid, struct timespec *ts) +{ + struct timeval tv; + (void) clockid; + if (gettimeofday(&tv, NULL)) + return -1; + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; + return 0; +} +# endif +#endif + + /** * Get current Julian Centuries time (100 Julian Days since J2000) * and Julian Day time @@ -163,7 +208,7 @@ static double sun_equation_of_centre(double t) { double a = sun_geometric_mean_anomaly(t), r; - r = sin(1.0 * a) * fma(fma(-0.000014, t, 0.004817), t, 1.914602); + r = sin(1.0 * a) * fma(fma(-0.000014, t, -0.004817), t, 1.914602); r = fma(sin(2.0 * a), fma(-0.000101, t, 0.019993), r); r = fma(sin(3.0 * a), 0.000289, r); return radians(r); @@ -241,7 +286,7 @@ solar_declination(double t) * between apparent and mean solar time * * @param t The time in Julian Centuries - * @return The equation of time, in degrees + * @return The equation of time, in minutes of time */ static double equation_of_time(double t) @@ -249,13 +294,14 @@ equation_of_time(double t) double l = sun_geometric_mean_longitude(t); double e = earth_orbit_eccentricity(t); double m = sun_geometric_mean_anomaly(t); - double y = pow(tan(corrected_mean_ecliptic_obliquity(t) / 2.0), 2.0); + double y = tan(corrected_mean_ecliptic_obliquity(t) / 2.0); double r, c, s; + y *= y; s = y * sin(2.0 * l); c = y * cos(2.0 * l); r = fma(fma(4.0, c, -2.0), e * sin(m), s); - r = fma(pow(0.50 * y, 2.0), sin(-4.0 * l), r); - r = fma(pow(1.25 * e, 2.0), sin(-2.0 * m), r); + r = fma(-0.5 * y*y, sin(4.0 * l), r); + r = fma(-1.25 * e*e, sin(2.0 * m), r); return 4.0 * degrees(r); } @@ -296,8 +342,8 @@ solar_elevation_from_time(double tc, double td, double latitude, double longitud * @return 0 on success, -1 on failure * @throws Any error specified for clock_gettime(3) on error */ -double -libred_solar_elevation(double latitude, double longitude, double *elevation) +int +libred_solar_elevation__int(double latitude, double longitude, double *elevation) { double tc, td; if (julian_time(&tc, &td)) @@ -307,6 +353,16 @@ libred_solar_elevation(double latitude, double longitude, double *elevation) } /** + * Kept for binary compatibility + */ +double +libred_solar_elevation(double latitude, double longitude, double *elevation) +{ + int r = libred_solar_elevation__int(latitude, longitude, elevation); + return (double)r; +} + +/** * This function is obsolete */ int |