aboutsummaryrefslogtreecommitdiffstats
path: root/solar.c
blob: 398001475e243af73c2848b62330b16f3f77ef27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* See LICENSE file for copyright and license details. */
#include "libred.h"
#include <math.h>
#include <time.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#if __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunsuffixed-float-constants"
#endif

/* Select clock. */
#if defined(DO_NOT_USE_COARSEC_CLOCK) || !defined(CLOCK_REALTIME_COARSE)
# ifdef CLOCK_REALTIME_COARSE
#  undef CLOCK_REALTIME_COARSE
# endif
# define CLOCK_REALTIME_COARSE  CLOCK_REALTIME
#endif


/**
 * Get current Julian Centuries time (100 Julian Days since J2000)
 * and Julian Day time
 * 
 * @param   tc_out  Output parameter for the current Julian Centuries time
 * @param   td_out  Output parameter for the current Julian Day time
 * @return          0 on success, -1 on failure
 * @throws          Any error specified for clock_gettime(3) on error
 */
static int
julian_time(double *tc_out, double *td_out)
{
	struct timespec now;
	double tu;
	if (clock_gettime(CLOCK_REALTIME_COARSE, &now))
		return -1;
	tu = fma((double)now.tv_nsec, 0.000000001, (double)now.tv_sec);
	*td_out = tu / 86400.0 + 2440587.5;
	*tc_out = (*td_out - 2451545.0) / 36525.0;
	return 0;
}


/**
 * Convert an angle (or otherwise) from degrees to radians
 * 
 * @param  deg  The angle in degrees
 * @param       The angle in radians
 */
static double
radians(double deg)
{
	return (double)M_PI / 180.0 * deg;
}

/**
 * Convert an angle (or otherwise) from radians to degrees
 * 
 * @param  rad  The angle in radians
 * @param       The angle in degrees
 */
static double
degrees(double rad)
{
	return 180.0 / (double)M_PI * rad;
}

/**
 * Convert an angle (or otherwise) from degrees to radians
 * and, using fused multply–add, add some number of degrees
 * 
 * @param  deg  The angle in degrees
 * @param  aug  The number of radians to add
 * @param       The angle in radians, plus `aug`
 */
static double
radians_plus(double deg, double aug)
{
	return fma((double)M_PI / 180.0, deg, aug);
}

/**
 * Convert an angle (or otherwise) from radians to degrees
 * and, using fused multply–add, add some number of degrees
 * 
 * @param  rad  The angle in radians
 * @param  aug  The number of degrees to add
 * @param       The angle in degrees, plus `aug`
 */
static double
degrees_plus(double rad, double aug)
{
	return fma(180.0 / (double)M_PI, rad, aug);
}


/**
 * Calculates the Sun's elevation from the solar hour angle
 * 
 * @param   latitude     The latitude in degrees northwards from 
 *                       the equator, negative for southwards
 * @param   declination  The declination, in radians
 * @param   hour_angle   The solar hour angle, in radians
 * @return               The Sun's elevation, in radians
 */
static double
elevation_from_hour_angle(double latitude, double declination, double hour_angle)
{
	double c, s;
	latitude = radians(latitude);
	c = cos(latitude) * cos(declination);
	s = sin(latitude) * sin(declination);
	return asin(fma(c, cos(hour_angle), s));
}

/**
 * Calculates the Sun's geometric mean longitude
 * 
 * @param   t  The time in Julian Centuries
 * @return     The Sun's geometric mean longitude in radians
 */
static double
sun_geometric_mean_longitude(double t)
{
	return radians(fmod(fma(fma(0.0003032, t, 36000.76983), t, 280.46646), 360.0));
}

/**
 * Calculates the Sun's geometric mean anomaly
 * 
 * @param   t  The time in Julian Centuries
 * @return     The Sun's geometric mean anomaly in radians
 */
static double
sun_geometric_mean_anomaly(double t)
{
	return radians(fmod(fma(fma(-0.0001537, t, 35999.05029), t, 357.52911), 360.0));
}

/**
 * Calculates the Earth's orbit eccentricity
 * 
 * @param   t  The time in Julian Centuries
 * @return     The Earth's orbit eccentricity
 */
static double
earth_orbit_eccentricity(double t)
{
	return fma(fma(-0.0000001267, t, -0.000042037), t, 0.016708634);
}

/**
 * Calculates the Sun's equation of the centre, the difference
 * between the true anomaly and the mean anomaly
 * 
 * @param   t  The time in Julian Centuries
 * @return     The Sun's equation of the centre, in radians
 */
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 = fma(sin(2.0 * a), fma(-0.000101, t, 0.019993), r);
	r = fma(sin(3.0 * a), 0.000289, r);
	return radians(r);
}

/**
 * Calculates the Sun's real longitudinal position
 * 
 * @param   t  The time in Julian Centuries
 * @return     The longitude, in radians
 */
static double
sun_real_longitude(double t)
{
	return sun_geometric_mean_longitude(t) + sun_equation_of_centre(t);
}

/**
 * Calculates the Sun's apparent longitudinal position
 * 
 * @param   t  The time in Julian Centuries
 * @return     The longitude, in radians
 */
static double
sun_apparent_longitude(double t)
{
	double r = degrees_plus(sun_real_longitude(t), -0.00569);
	double a = radians(fma(-1934.136, t, 125.04));
	return radians(fma(-0.00478, sin(a), r));
}

/**
 * Calculates the mean ecliptic obliquity of the Sun's
 * apparent motion without variation correction
 * 
 * @param   t  The time in Julian Centuries
 * @return     The uncorrected mean obliquity, in radians
 */
static double
mean_ecliptic_obliquity(double t)
{
	double r = fma(fma(fma(0.001813, t, -0.00059), t, -46.815), t, 21.448);
	return radians(23.0 + (26.0 + r / 60.0) / 60.0);
}

/**
 * Calculates the mean ecliptic obliquity of the Sun's
 * parent motion with variation correction
 * 
 * @param   t  The time in Julian Centuries
 * @return     The mean obliquity, in radians
 */
static double
corrected_mean_ecliptic_obliquity(double t)
{
	double r = cos(radians(fma(-1934.136, t, 125.04)));
	return radians_plus(0.00256 * r, mean_ecliptic_obliquity(t));
}

/**
 * Calculates the Sun's declination
 * 
 * @param   t  The time in Julian Centuries
 * @return     The Sun's declination, in radian
 */
static double
solar_declination(double t)
{
	double r = sin(corrected_mean_ecliptic_obliquity(t));
	return asin(r * sin(sun_apparent_longitude(t)));
}

/**
 * Calculates the equation of time, the discrepancy
 * between apparent and mean solar time
 * 
 * @param   t  The time in Julian Centuries
 * @return     The equation of time, in degrees
 */
static double
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 r, c, s;
	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);
	return 4.0 * degrees(r);
}

/**
 * Calculates the Sun's elevation as apparent
 * from a geographical position
 * 
 * @param   tc         The time in Julian Centuries
 * @param   td         The time in Julian Days
 * @param   latitude   The latitude in degrees northwards from 
 *                     the equator, negative for southwards
 * @param   longitude  The longitude in degrees eastwards from
 *                     Greenwich, negative for westwards
 * @return             The Sun's apparent elevation at the specified time as seen
 *                     from the specified position, measured in radians
 */
static double
solar_elevation_from_time(double tc, double td, double latitude, double longitude)
{
	double r;
	td = td - round(td);
	r = fma(1440, td - 1, -equation_of_time(tc));
	r = radians(fma(0.25, r, -longitude));
	return elevation_from_hour_angle(latitude, solar_declination(tc), r);
}

/**
 * Calculates the Sun's elevation as apparent
 * from a geographical position
 * 
 * @param   latitude   The latitude in degrees northwards from 
 *                     the equator, negative for southwards
 * @param   longitude  The longitude in degrees eastwards from
 *                     Greenwich, negative for westwards
 * @param   elevation  Output parameter for the Sun's apparent elevation
 *                     as seen, right now, from the specified position,
 *                     measured in degrees
 * @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)
{
	double tc, td;
	if (julian_time(&tc, &td))
		return -1;
	*elevation = degrees(solar_elevation_from_time(tc, td, latitude, longitude));
	return 0;
}

/**
 * This function is obsolete
 */
int
libred_check_timetravel(void)
{
	return 0;
}