]> code.delx.au - gnu-emacs/blob - lisp/calendar/solar.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / calendar / solar.el
1 ;;; solar.el --- calendar functions for solar events
2
3 ;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Denis B. Roegel <Denis.Roegel@loria.fr>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: calendar
10 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary, holidays
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; See calendar.el. This file implements features that deal with
30 ;; times of day, sunrise/sunset, and equinoxes/solstices.
31
32 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
33 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
34 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
35 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
36 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
37 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
38 ;; Inc., 1986.
39
40 ;;
41 ;; Accuracy:
42 ;; 1. Sunrise/sunset times will be accurate to the minute for years
43 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
44 ;;
45 ;; 2. Equinox/solstice times will be accurate to the minute for years
46 ;; 1951--2050. For other years the times will be within +/- 1 minute.
47
48 ;;; Code:
49
50 (require 'calendar)
51 (require 'cal-dst)
52 ;; calendar-astro-to-absolute and v versa are cal-autoloads.
53 ;;;(require 'cal-julian)
54
55
56 (defcustom calendar-time-display-form
57 '(12-hours ":" minutes am-pm
58 (if time-zone " (") time-zone (if time-zone ")"))
59 "The pseudo-pattern that governs the way a time of day is formatted.
60
61 A pseudo-pattern is a list of expressions that can involve the keywords
62 `12-hours', `24-hours', and `minutes', all numbers in string form,
63 and `am-pm' and `time-zone', both alphabetic strings.
64
65 For example, the form
66
67 '(24-hours \":\" minutes
68 (if time-zone \" (\") time-zone (if time-zone \")\"))
69
70 would give military-style times like `21:07 (UTC)'."
71 :type 'sexp
72 :group 'calendar)
73
74 (defcustom calendar-latitude nil
75 "Latitude of `calendar-location-name' in degrees.
76 The value can be either a decimal fraction (one place of accuracy is
77 sufficient), + north, - south, such as 40.7 for New York City, or the value
78 can be a vector [degrees minutes north/south] such as [40 50 north] for New
79 York City.
80
81 This variable should be set in `site-start'.el."
82 :type '(choice (const nil)
83 (number :tag "Exact")
84 (vector :value [0 0 north]
85 (integer :tag "Degrees")
86 (integer :tag "Minutes")
87 (choice :tag "Position"
88 (const north)
89 (const south))))
90 :group 'calendar)
91
92 (defcustom calendar-longitude nil
93 "Longitude of `calendar-location-name' in degrees.
94 The value can be either a decimal fraction (one place of accuracy is
95 sufficient), + east, - west, such as -73.9 for New York City, or the value
96 can be a vector [degrees minutes east/west] such as [73 55 west] for New
97 York City.
98
99 This variable should be set in `site-start'.el."
100 :type '(choice (const nil)
101 (number :tag "Exact")
102 (vector :value [0 0 west]
103 (integer :tag "Degrees")
104 (integer :tag "Minutes")
105 (choice :tag "Position"
106 (const east)
107 (const west))))
108 :group 'calendar)
109
110 (defcustom calendar-location-name
111 '(let ((float-output-format "%.1f"))
112 (format "%s%s, %s%s"
113 (if (numberp calendar-latitude)
114 (abs calendar-latitude)
115 (+ (aref calendar-latitude 0)
116 (/ (aref calendar-latitude 1) 60.0)))
117 (if (numberp calendar-latitude)
118 (if (> calendar-latitude 0) "N" "S")
119 (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
120 (if (numberp calendar-longitude)
121 (abs calendar-longitude)
122 (+ (aref calendar-longitude 0)
123 (/ (aref calendar-longitude 1) 60.0)))
124 (if (numberp calendar-longitude)
125 (if (> calendar-longitude 0) "E" "W")
126 (if (eq (aref calendar-longitude 2) 'east) "E" "W"))))
127 "Expression evaluating to the name of the calendar location.
128 For example, \"New York City\". The default value is just the
129 variable `calendar-latitude' paired with the variable `calendar-longitude'.
130
131 This variable should be set in `site-start'.el."
132 :type 'sexp
133 :group 'calendar)
134
135 (defcustom solar-error 0.5
136 "Tolerance (in minutes) for sunrise/sunset calculations.
137
138 A larger value makes the calculations for sunrise/sunset faster, but less
139 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
140 times will be correct to the minute.
141
142 It is useless to set the value smaller than 4*delta, where delta is the
143 accuracy in the longitude of the sun (given by the function
144 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
145 delta. At present, delta = 0.01 degrees, so the value of the variable
146 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
147 :type 'number
148 :group 'calendar)
149
150 ;;; End of user options.
151
152
153 (defconst solar-n-hemi-seasons
154 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
155 "List of season changes for the northern hemisphere.")
156
157 (defconst solar-s-hemi-seasons
158 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
159 "List of season changes for the southern hemisphere.")
160
161 (defvar solar-sidereal-time-greenwich-midnight nil
162 "Sidereal time at Greenwich at midnight (universal time).")
163
164 (defvar solar-northern-spring-or-summer-season nil
165 "Non-nil if northern spring or summer and nil otherwise.
166 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
167
168
169 (defsubst calendar-latitude ()
170 "Ensure the variable `calendar-latitude' is a signed decimal fraction."
171 (if (numberp calendar-latitude)
172 calendar-latitude
173 (let ((lat (+ (aref calendar-latitude 0)
174 (/ (aref calendar-latitude 1) 60.0))))
175 (if (eq (aref calendar-latitude 2) 'north)
176 lat
177 (- lat)))))
178
179 (defsubst calendar-longitude ()
180 "Ensure the variable `calendar-longitude' is a signed decimal fraction."
181 (if (numberp calendar-longitude)
182 calendar-longitude
183 (let ((long (+ (aref calendar-longitude 0)
184 (/ (aref calendar-longitude 1) 60.0))))
185 (if (eq (aref calendar-longitude 2) 'east)
186 long
187 (- long)))))
188
189 (defun solar-get-number (prompt)
190 "Return a number from the minibuffer, prompting with PROMPT.
191 Returns nil if nothing was entered."
192 (let ((x (read-string prompt "")))
193 (unless (string-equal x "")
194 (string-to-number x))))
195
196 (defun solar-setup ()
197 "Prompt for `calendar-longitude', `calendar-latitude', `calendar-time-zone'."
198 (beep)
199 (or calendar-longitude
200 (setq calendar-longitude
201 (solar-get-number
202 "Enter longitude (decimal fraction; + east, - west): ")))
203 (or calendar-latitude
204 (setq calendar-latitude
205 (solar-get-number
206 "Enter latitude (decimal fraction; + north, - south): ")))
207 (or calendar-time-zone
208 (setq calendar-time-zone
209 (solar-get-number
210 "Enter difference from Coordinated Universal Time (in minutes): ")
211 )))
212
213 (defun solar-sin-degrees (x)
214 "Return sin of X degrees."
215 (sin (degrees-to-radians (mod x 360.0))))
216
217 (defun solar-cosine-degrees (x)
218 "Return cosine of X degrees."
219 (cos (degrees-to-radians (mod x 360.0))))
220
221 (defun solar-tangent-degrees (x)
222 "Return tangent of X degrees."
223 (tan (degrees-to-radians (mod x 360.0))))
224
225 (defun solar-xy-to-quadrant (x y)
226 "Determine the quadrant of the point X, Y."
227 (if (> x 0)
228 (if (> y 0) 1 4)
229 (if (> y 0) 2 3)))
230
231 (defun solar-degrees-to-quadrant (angle)
232 "Determine the quadrant of ANGLE degrees."
233 (1+ (floor (mod angle 360) 90)))
234
235 (defun solar-arctan (x quad)
236 "Arctangent of X in quadrant QUAD."
237 (let ((deg (radians-to-degrees (atan x))))
238 (cond ((= quad 2) (+ deg 180))
239 ((= quad 3) (+ deg 180))
240 ((= quad 4) (+ deg 360))
241 (t deg))))
242
243 (defun solar-atn2 (x y)
244 "Arctangent of point X, Y."
245 (if (zerop x)
246 (if (> y 0) 90 270)
247 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
248
249 (defun solar-arccos (x)
250 "Arccosine of X."
251 (let ((y (sqrt (- 1 (* x x)))))
252 (solar-atn2 x y)))
253
254 (defun solar-arcsin (y)
255 "Arcsin of Y."
256 (let ((x (sqrt (- 1 (* y y)))))
257 (solar-atn2 x y)))
258
259 (defsubst solar-degrees-to-hours (degrees)
260 "Convert DEGREES to hours."
261 (/ degrees 15.0))
262
263 (defsubst solar-hours-to-days (hour)
264 "Convert HOUR to decimal fraction of a day."
265 (/ hour 24.0))
266
267 (defun solar-right-ascension (longitude obliquity)
268 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
269 Both arguments are in degrees."
270 (solar-degrees-to-hours
271 (solar-arctan
272 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
273 (solar-degrees-to-quadrant longitude))))
274
275 (defun solar-declination (longitude obliquity)
276 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
277 Both arguments are in degrees."
278 (solar-arcsin
279 (* (solar-sin-degrees obliquity)
280 (solar-sin-degrees longitude))))
281
282 (defun solar-ecliptic-coordinates (time sunrise-flag)
283 "Return solar longitude, ecliptic inclination, equation of time, nutation.
284 Values are for TIME in Julian centuries of Ephemeris Time since
285 January 1st, 2000, at 12 ET. Longitude and inclination are in
286 degrees, equation of time in hours, and nutation in seconds of longitude.
287 If SUNRISE-FLAG is non-nil, only calculate longitude and inclination."
288 (let* ((l (+ 280.46645
289 (* 36000.76983 time)
290 (* 0.0003032 time time))) ; sun mean longitude
291 (ml (+ 218.3165
292 (* 481267.8813 time))) ; moon mean longitude
293 (m (+ 357.52910
294 (* 35999.05030 time)
295 (* -0.0001559 time time)
296 (* -0.00000048 time time time))) ; sun mean anomaly
297 (i (+ 23.43929111 (* -0.013004167 time)
298 (* -0.00000016389 time time)
299 (* 0.0000005036 time time time))) ; mean inclination
300 (c (+ (* (+ 1.914600
301 (* -0.004817 time)
302 (* -0.000014 time time))
303 (solar-sin-degrees m))
304 (* (+ 0.019993 (* -0.000101 time))
305 (solar-sin-degrees (* 2 m)))
306 (* 0.000290
307 (solar-sin-degrees (* 3 m))))) ; center equation
308 (L (+ l c)) ; total longitude
309 ;; Longitude of moon's ascending node on the ecliptic.
310 (omega (+ 125.04
311 (* -1934.136 time)))
312 ;; nut = nutation in longitude, measured in seconds of angle.
313 (nut (unless sunrise-flag
314 (+ (* -17.20 (solar-sin-degrees omega))
315 (* -1.32 (solar-sin-degrees (* 2 l)))
316 (* -0.23 (solar-sin-degrees (* 2 ml)))
317 (* 0.21 (solar-sin-degrees (* 2 omega))))))
318 (ecc (unless sunrise-flag ; eccentricity of earth's orbit
319 (+ 0.016708617
320 (* -0.000042037 time)
321 (* -0.0000001236 time time))))
322 (app (+ L ; apparent longitude of sun
323 -0.00569
324 (* -0.00478
325 (solar-sin-degrees omega))))
326 (y (unless sunrise-flag
327 (* (solar-tangent-degrees (/ i 2))
328 (solar-tangent-degrees (/ i 2)))))
329 ;; Equation of time, in hours.
330 (time-eq (unless sunrise-flag
331 (/ (* 12 (+ (* y (solar-sin-degrees (* 2 l)))
332 (* -2 ecc (solar-sin-degrees m))
333 (* 4 ecc y (solar-sin-degrees m)
334 (solar-cosine-degrees (* 2 l)))
335 (* -0.5 y y (solar-sin-degrees (* 4 l)))
336 (* -1.25 ecc ecc (solar-sin-degrees (* 2 m)))))
337 3.1415926535))))
338 (list app i time-eq nut)))
339
340 (defun solar-ephemeris-correction (year)
341 "Ephemeris time minus Universal Time during Gregorian YEAR.
342 Result is in days. For the years 1800-1987, the maximum error is
343 1.9 seconds. For the other years, the maximum error is about 30 seconds."
344 (cond ((and (<= 1988 year) (< year 2020))
345 (/ (+ year -2000 67.0) 60.0 60.0 24.0))
346 ((and (<= 1900 year) (< year 1988))
347 (let* ((theta (/ (- (calendar-astro-from-absolute
348 (calendar-absolute-from-gregorian
349 (list 7 1 year)))
350 (calendar-astro-from-absolute
351 (calendar-absolute-from-gregorian
352 '(1 1 1900))))
353 36525.0))
354 (theta2 (* theta theta))
355 (theta3 (* theta2 theta))
356 (theta4 (* theta2 theta2))
357 (theta5 (* theta3 theta2)))
358 (+ -0.00002
359 (* 0.000297 theta)
360 (* 0.025184 theta2)
361 (* -0.181133 theta3)
362 (* 0.553040 theta4)
363 (* -0.861938 theta5)
364 (* 0.677066 theta3 theta3)
365 (* -0.212591 theta4 theta3))))
366 ((and (<= 1800 year) (< year 1900))
367 (let* ((theta (/ (- (calendar-astro-from-absolute
368 (calendar-absolute-from-gregorian
369 (list 7 1 year)))
370 (calendar-astro-from-absolute
371 (calendar-absolute-from-gregorian
372 '(1 1 1900))))
373 36525.0))
374 (theta2 (* theta theta))
375 (theta3 (* theta2 theta))
376 (theta4 (* theta2 theta2))
377 (theta5 (* theta3 theta2)))
378 (+ -0.000009
379 (* 0.003844 theta)
380 (* 0.083563 theta2)
381 (* 0.865736 theta3)
382 (* 4.867575 theta4)
383 (* 15.845535 theta5)
384 (* 31.332267 theta3 theta3)
385 (* 38.291999 theta4 theta3)
386 (* 28.316289 theta4 theta4)
387 (* 11.636204 theta4 theta5)
388 (* 2.043794 theta5 theta5))))
389 ((and (<= 1620 year) (< year 1800))
390 (let ((x (/ (- year 1600) 10.0)))
391 (/ (+ (* 2.19167 x x) (* -40.675 x) 196.58333) 60.0 60.0 24.0)))
392 (t (let* ((tmp (- (calendar-astro-from-absolute
393 (calendar-absolute-from-gregorian
394 (list 1 1 year)))
395 2382148))
396 (second (- (/ (* tmp tmp) 41048480.0) 15)))
397 (/ second 60.0 60.0 24.0)))))
398
399 (defun solar-ephemeris-time (time)
400 "Ephemeris Time at moment TIME.
401 TIME is a pair with the first component being the number of Julian centuries
402 elapsed at 0 Universal Time, and the second component being the universal
403 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
404 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
405 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
406
407 Result is in Julian centuries of ephemeris time."
408 (let* ((t0 (car time))
409 (ut (cadr time))
410 (t1 (+ t0 (/ (/ ut 24.0) 36525)))
411 (y (+ 2000 (* 100 t1)))
412 (dt (* 86400 (solar-ephemeris-correction (floor y)))))
413 (+ t1 (/ (/ dt 86400) 36525))))
414
415 (defun solar-equatorial-coordinates (time sunrise-flag)
416 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
417 TIME is a pair with the first component being the number of
418 Julian centuries elapsed at 0 Universal Time, and the second
419 component being the universal time. For instance, the pair
420 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
421 -0.040945 being the number of Julian centuries elapsed between
422 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG is passed
423 to `solar-ecliptic-coordinates'."
424 (let ((ec (solar-ecliptic-coordinates (solar-ephemeris-time time)
425 sunrise-flag)))
426 (list (solar-right-ascension (car ec) (cadr ec))
427 (solar-declination (car ec) (cadr ec)))))
428
429 (defun solar-horizontal-coordinates (time latitude longitude sunrise-flag)
430 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
431 TIME is a pair with the first component being the number of
432 Julian centuries elapsed at 0 Universal Time, and the second
433 component being the universal time. For instance, the pair
434 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
435 -0.040945 being the number of Julian centuries elapsed between
436 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG
437 is passed to `solar-ecliptic-coordinates'. Azimuth and
438 height (between -180 and 180) are both in degrees."
439 (let* ((ut (cadr time))
440 (ec (solar-equatorial-coordinates time sunrise-flag))
441 (st (+ solar-sidereal-time-greenwich-midnight
442 (* ut 1.00273790935)))
443 ;; Hour angle (in degrees).
444 (ah (- (* st 15) (* 15 (car ec)) (* -1 (calendar-longitude))))
445 (de (cadr ec))
446 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah)
447 (solar-sin-degrees latitude))
448 (* (solar-tangent-degrees de)
449 (solar-cosine-degrees latitude)))
450 (solar-sin-degrees ah)))
451 (height (solar-arcsin
452 (+ (* (solar-sin-degrees latitude) (solar-sin-degrees de))
453 (* (solar-cosine-degrees latitude)
454 (solar-cosine-degrees de)
455 (solar-cosine-degrees ah))))))
456 (if (> height 180) (setq height (- height 360)))
457 (list azimuth height)))
458
459 (defun solar-moment (direction latitude longitude time height)
460 "Sunrise/sunset at location.
461 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
462 being TIME.
463
464 TIME is a pair with the first component being the number of Julian centuries
465 elapsed at 0 Universal Time, and the second component being the universal
466 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
467 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
468 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
469
470 HEIGHT is the angle the center of the sun has over the horizon for the contact
471 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
472 accounting for the edge of the sun being on the horizon.
473
474 Uses binary search."
475 (let* ((ut (cadr time))
476 (possible t) ; we assume that rise or set are possible
477 (utmin (+ ut (* direction 12.0)))
478 (utmax ut) ; the time searched is between utmin and utmax
479 ;; utmin and utmax are in hours.
480 (utmoment-old 0.0) ; rise or set approximation
481 (utmoment 1.0) ; rise or set approximation
482 (hut 0) ; sun height at utmoment
483 (t0 (car time))
484 (hmin (cadr (solar-horizontal-coordinates (list t0 utmin)
485 latitude longitude t)))
486 (hmax (cadr (solar-horizontal-coordinates (list t0 utmax)
487 latitude longitude t))))
488 ;; -0.61 degrees is the height of the middle of the sun, when it
489 ;; rises or sets.
490 (if (< hmin height)
491 (if (> hmax height)
492 (while ;;; (< i 20) ; we perform a simple dichotomy
493 ;;; (> (abs (- hut height)) epsilon)
494 (>= (abs (- utmoment utmoment-old))
495 (/ solar-error 60))
496 (setq utmoment-old utmoment
497 utmoment (/ (+ utmin utmax) 2)
498 hut (cadr (solar-horizontal-coordinates
499 (list t0 utmoment) latitude longitude t)))
500 (if (< hut height) (setq utmin utmoment))
501 (if (> hut height) (setq utmax utmoment)))
502 (setq possible nil)) ; the sun never rises
503 (setq possible nil)) ; the sun never sets
504 (if possible utmoment)))
505
506 (defun solar-sunrise-and-sunset (time latitude longitude height)
507 "Sunrise, sunset and length of day.
508 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
509
510 TIME is a pair with the first component being the number of Julian centuries
511 elapsed at 0 Universal Time, and the second component being the universal
512 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
513 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
514 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
515
516 HEIGHT is the angle the center of the sun has over the horizon for the contact
517 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
518 accounting for the edge of the sun being on the horizon.
519
520 Coordinates are included because this function is called with latitude=1
521 degrees to find out if polar regions have 24 hours of sun or only night."
522 (let ((rise-time (solar-moment -1 latitude longitude time height))
523 (set-time (solar-moment 1 latitude longitude time height))
524 day-length)
525 (if (not (and rise-time set-time))
526 (if (or (and (> latitude 0)
527 solar-northern-spring-or-summer-season)
528 (and (< latitude 0)
529 (not solar-northern-spring-or-summer-season)))
530 (setq day-length 24)
531 (setq day-length 0))
532 (setq day-length (- set-time rise-time)))
533 (list (if rise-time (+ rise-time (/ calendar-time-zone 60.0)) nil)
534 (if set-time (+ set-time (/ calendar-time-zone 60.0)) nil)
535 day-length)))
536
537 (defun solar-time-string (time time-zone)
538 "Printable form for decimal fraction TIME in TIME-ZONE.
539 Format used is given by `calendar-time-display-form'."
540 (let* ((time (round (* 60 time)))
541 (24-hours (/ time 60))
542 (minutes (format "%02d" (% time 60)))
543 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
544 (am-pm (if (>= 24-hours 12) "pm" "am"))
545 (24-hours (format "%02d" 24-hours)))
546 (mapconcat 'eval calendar-time-display-form "")))
547
548 (defun solar-daylight (time)
549 "Printable form for TIME expressed in hours."
550 (format "%d:%02d"
551 (floor time)
552 (floor (* 60 (- time (floor time))))))
553
554 (defun solar-julian-ut-centuries (date)
555 "Number of Julian centuries since 1 Jan, 2000 at noon UT for Gregorian DATE."
556 (/ (- (calendar-absolute-from-gregorian date)
557 (calendar-absolute-from-gregorian '(1 1.5 2000)))
558 36525.0))
559
560 (defun solar-date-to-et (date ut)
561 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
562 Expressed in Julian centuries of Ephemeris Time."
563 (solar-ephemeris-time (list (solar-julian-ut-centuries date) ut)))
564
565 (defun solar-time-equation (date ut)
566 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
567 (nth 2 (solar-ecliptic-coordinates (solar-date-to-et date ut) nil)))
568
569 (defun solar-exact-local-noon (date)
570 "Date and Universal Time of local noon at *local date* DATE.
571 The date may be different from the one asked for, but it will be the right
572 local date. The second component of date should be an integer."
573 (let* ((nd date)
574 (ut (- 12.0 (/ (calendar-longitude) 15)))
575 (te (solar-time-equation date ut)))
576 (setq ut (- ut te))
577 (if (>= ut 24)
578 (setq nd (list (car date) (1+ (cadr date))
579 (nth 2 date))
580 ut (- ut 24)))
581 (if (< ut 0)
582 (setq nd (list (car date) (1- (cadr date))
583 (nth 2 date))
584 ut (+ ut 24)))
585 (setq nd (calendar-gregorian-from-absolute ; date standardization
586 (calendar-absolute-from-gregorian nd)))
587 (list nd ut)))
588
589 (defun solar-sidereal-time (t0)
590 "Sidereal time (in hours) in Greenwich at T0 Julian centuries.
591 T0 must correspond to 0 hours UT."
592 (let* ((mean-sid-time (+ 6.6973746
593 (* 2400.051337 t0)
594 (* 0.0000258622 t0 t0)
595 (* -0.0000000017222 t0 t0 t0)))
596 (et (solar-ephemeris-time (list t0 0.0)))
597 (nut-i (solar-ecliptic-coordinates et nil))
598 (nut (nth 3 nut-i)) ; nutation
599 (i (cadr nut-i))) ; inclination
600 (mod (+ (mod (+ mean-sid-time
601 (/ (/ (* nut (solar-cosine-degrees i)) 15) 3600)) 24.0)
602 24.0)
603 24.0)))
604
605 (defun solar-sunrise-sunset (date)
606 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
607 Corresponding value is nil if there is no sunrise/sunset."
608 ;; First, get the exact moment of local noon.
609 (let* ((exact-local-noon (solar-exact-local-noon date))
610 ;; Get the time from the 2000 epoch.
611 (t0 (solar-julian-ut-centuries (car exact-local-noon)))
612 ;; Store the sidereal time at Greenwich at midnight of UT time.
613 ;; Find if summer or winter slightly above the equator.
614 (equator-rise-set
615 (progn (setq solar-sidereal-time-greenwich-midnight
616 (solar-sidereal-time t0))
617 (solar-sunrise-and-sunset
618 (list t0 (cadr exact-local-noon))
619 1.0
620 (calendar-longitude) 0)))
621 ;; Store the spring/summer information, compute sunrise and
622 ;; sunset (two first components of rise-set). Length of day
623 ;; is the third component (it is only the difference between
624 ;; sunset and sunrise when there is a sunset and a sunrise)
625 (rise-set
626 (progn
627 (setq solar-northern-spring-or-summer-season
628 (> (nth 2 equator-rise-set) 12))
629 (solar-sunrise-and-sunset
630 (list t0 (cadr exact-local-noon))
631 (calendar-latitude)
632 (calendar-longitude) -0.61)))
633 (rise-time (car rise-set))
634 (adj-rise (if rise-time (dst-adjust-time date rise-time)))
635 (set-time (cadr rise-set))
636 (adj-set (if set-time (dst-adjust-time date set-time)))
637 (length (nth 2 rise-set)))
638 (list
639 (and rise-time (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
640 (and set-time (calendar-date-equal date (car adj-set)) (cdr adj-set))
641 (solar-daylight length))))
642
643 (defun solar-sunrise-sunset-string (date)
644 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
645 (let ((l (solar-sunrise-sunset date)))
646 (format
647 "%s, %s at %s (%s hours daylight)"
648 (if (car l)
649 (concat "Sunrise " (apply 'solar-time-string (car l)))
650 "No sunrise")
651 (if (cadr l)
652 (concat "sunset " (apply 'solar-time-string (cadr l)))
653 "no sunset")
654 (eval calendar-location-name)
655 (nth 2 l))))
656
657 (defconst solar-data-list
658 '((403406 4.721964 1.621043)
659 (195207 5.937458 62830.348067)
660 (119433 1.115589 62830.821524)
661 (112392 5.781616 62829.634302)
662 (3891 5.5474 125660.5691)
663 (2819 1.5120 125660.984)
664 (1721 4.1897 62832.4766)
665 (0 1.163 0.813)
666 (660 5.415 125659.31)
667 (350 4.315 57533.85)
668 (334 4.553 -33.931)
669 (314 5.198 777137.715)
670 (268 5.989 78604.191)
671 (242 2.911 5.412)
672 (234 1.423 39302.098)
673 (158 0.061 -34.861)
674 (132 2.317 115067.698)
675 (129 3.193 15774.337)
676 (114 2.828 5296.670)
677 (99 0.52 58849.27)
678 (93 4.65 5296.11)
679 (86 4.35 -3980.70)
680 (78 2.75 52237.69)
681 (72 4.50 55076.47)
682 (68 3.23 261.08)
683 (64 1.22 15773.85)
684 (46 0.14 188491.03)
685 (38 3.44 -7756.55)
686 (37 4.37 264.89)
687 (32 1.14 117906.27)
688 (29 2.84 55075.75)
689 (28 5.96 -7961.39)
690 (27 5.09 188489.81)
691 (27 1.72 2132.19)
692 (25 2.56 109771.03)
693 (24 1.92 54868.56)
694 (21 0.09 25443.93)
695 (21 5.98 -55731.43)
696 (20 4.03 60697.74)
697 (18 4.47 2132.79)
698 (17 0.79 109771.63)
699 (14 4.24 -7752.82)
700 (13 2.01 188491.91)
701 (13 2.65 207.81)
702 (13 4.98 29424.63)
703 (12 0.93 -7.99)
704 (10 2.21 46941.14)
705 (10 3.59 -68.29)
706 (10 1.50 21463.25)
707 (10 2.55 157208.40))
708 "Data used for calculation of solar longitude.")
709
710 (defun solar-longitude (d)
711 "Longitude of sun on astronomical (Julian) day number D.
712 Accuracy is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
713 The values of `calendar-daylight-savings-starts',
714 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
715 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
716 `calendar-time-zone' are used to interpret local time."
717 (let* ((a-d (calendar-astro-to-absolute d))
718 ;; Get Universal Time.
719 (date (calendar-astro-from-absolute
720 (- a-d
721 (if (dst-in-effect a-d)
722 (/ calendar-daylight-time-offset 24.0 60.0) 0)
723 (/ calendar-time-zone 60.0 24.0))))
724 ;; Get Ephemeris Time.
725 (date (+ date (solar-ephemeris-correction
726 (calendar-extract-year
727 (calendar-gregorian-from-absolute
728 (floor
729 (calendar-astro-to-absolute
730 date)))))))
731 (U (/ (- date 2451545) 3652500))
732 (longitude
733 (+ 4.9353929
734 (* 62833.1961680 U)
735 (* 0.0000001
736 (apply '+
737 (mapcar (lambda (x)
738 (* (car x)
739 (sin (mod
740 (+ (cadr x)
741 (* (nth 2 x) U))
742 (* 2 pi)))))
743 solar-data-list)))))
744 (aberration
745 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U)))) 973)))
746 (A1 (mod (+ 2.18 (* U (+ -3375.70 (* 0.36 U)))) (* 2 pi)))
747 (A2 (mod (+ 3.51 (* U (+ 125666.39 (* 0.10 U)))) (* 2 pi)))
748 (nutation (* -0.0000001 (+ (* 834 (sin A1)) (* 64 (sin A2))))))
749 (mod (radians-to-degrees (+ longitude aberration nutation)) 360.0)))
750
751 (defun solar-date-next-longitude (d l)
752 "First time after day D when solar longitude is a multiple of L degrees.
753 D is a Julian day number. L must be an integer divisor of 360.
754 The result is for `calendar-location-name', and is in local time
755 \(including any daylight saving rules) expressed in astronomical (Julian)
756 day numbers. The values of `calendar-daylight-savings-starts',
757 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
758 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset',
759 and `calendar-time-zone' are used to interpret local time."
760 (let* ((long)
761 (start d)
762 (start-long (solar-longitude d))
763 (next (mod (* l (1+ (floor (/ start-long l)))) 360))
764 (end (+ d (* (/ l 360.0) 400)))
765 (end-long (solar-longitude end)))
766 (while ; bisection search for nearest minute
767 (< 0.00001 (- end start))
768 ;; start <= d < end
769 ;; start-long <= next < end-long when next != 0
770 ;; when next = 0, we look for the discontinuity (start-long is near 360
771 ;; and end-long is small (less than l).
772 (setq d (/ (+ start end) 2.0)
773 long (solar-longitude d))
774 (if (or (and (not (zerop next)) (< long next))
775 (and (zerop next) (< l long)))
776 (setq start d
777 start-long long)
778 (setq end d
779 end-long long)))
780 (/ (+ start end) 2.0)))
781
782 ;; FIXME but there already is solar-sunrise-sunset.
783 ;;;###autoload
784 (defun sunrise-sunset (&optional arg)
785 "Local time of sunrise and sunset for today. Accurate to a few seconds.
786 If called with an optional prefix argument ARG, prompt for date.
787 If called with an optional double prefix argument, prompt for
788 longitude, latitude, time zone, and date, and always use standard time.
789
790 This function is suitable for execution in a .emacs file."
791 (interactive "p")
792 (or arg (setq arg 1))
793 (if (and (< arg 16)
794 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
795 (solar-setup))
796 (let* ((calendar-longitude
797 (if (< arg 16) calendar-longitude
798 (solar-get-number
799 "Enter longitude (decimal fraction; + east, - west): ")))
800 (calendar-latitude
801 (if (< arg 16) calendar-latitude
802 (solar-get-number
803 "Enter latitude (decimal fraction; + north, - south): ")))
804 (calendar-time-zone
805 (if (< arg 16) calendar-time-zone
806 (solar-get-number
807 "Enter difference from Coordinated Universal Time (in minutes): ")))
808 (calendar-location-name
809 (if (< arg 16) calendar-location-name
810 (let ((float-output-format "%.1f"))
811 (format "%s%s, %s%s"
812 (if (numberp calendar-latitude)
813 (abs calendar-latitude)
814 (+ (aref calendar-latitude 0)
815 (/ (aref calendar-latitude 1) 60.0)))
816 (if (numberp calendar-latitude)
817 (if (> calendar-latitude 0) "N" "S")
818 (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
819 (if (numberp calendar-longitude)
820 (abs calendar-longitude)
821 (+ (aref calendar-longitude 0)
822 (/ (aref calendar-longitude 1) 60.0)))
823 (if (numberp calendar-longitude)
824 (if (> calendar-longitude 0) "E" "W")
825 (if (eq (aref calendar-longitude 2) 'east)
826 "E" "W"))))))
827 (calendar-standard-time-zone-name
828 (if (< arg 16) calendar-standard-time-zone-name
829 (cond ((zerop calendar-time-zone) "UTC")
830 ((< calendar-time-zone 0)
831 (format "UTC%dmin" calendar-time-zone))
832 (t (format "UTC+%dmin" calendar-time-zone)))))
833 (calendar-daylight-savings-starts
834 (if (< arg 16) calendar-daylight-savings-starts))
835 (calendar-daylight-savings-ends
836 (if (< arg 16) calendar-daylight-savings-ends))
837 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
838 (date-string (calendar-date-string date t))
839 (time-string (solar-sunrise-sunset-string date))
840 (msg (format "%s: %s" date-string time-string))
841 (one-window (one-window-p t)))
842 (if (<= (length msg) (frame-width))
843 (message "%s" msg)
844 (with-output-to-temp-buffer "*temp*"
845 (princ (concat date-string "\n" time-string)))
846 (message "%s"
847 (substitute-command-keys
848 (if one-window
849 (if pop-up-windows
850 "Type \\[delete-other-windows] to remove temp window."
851 "Type \\[switch-to-buffer] RET to remove temp window.")
852 "Type \\[switch-to-buffer-other-window] RET to restore old \
853 contents of temp window."))))))
854
855 ;;;###cal-autoload
856 (defun calendar-sunrise-sunset ()
857 "Local time of sunrise and sunset for date under cursor.
858 Accurate to a few seconds."
859 (interactive)
860 (or (and calendar-latitude calendar-longitude calendar-time-zone)
861 (solar-setup))
862 (let ((date (calendar-cursor-to-date t)))
863 (message "%s: %s"
864 (calendar-date-string date t t)
865 (solar-sunrise-sunset-string date))))
866
867 (defvar date)
868
869 ;; To be called from diary-list-sexp-entries, where DATE is bound.
870 ;;;###diary-autoload
871 (defun diary-sunrise-sunset ()
872 "Local time of sunrise and sunset as a diary entry.
873 Accurate to a few seconds."
874 (or (and calendar-latitude calendar-longitude calendar-time-zone)
875 (solar-setup))
876 (solar-sunrise-sunset-string date))
877
878 ;; From Meeus, 1991, page 167.
879 (defconst solar-seasons-data
880 '((485 324.96 1934.136)
881 (203 337.23 32964.467)
882 (199 342.08 20.186)
883 (182 27.85 445267.112)
884 (156 73.14 45036.886)
885 (136 171.52 22518.443)
886 (77 222.54 65928.934)
887 (74 296.72 3034.906)
888 (70 243.58 9037.513)
889 (58 119.81 33718.147)
890 (52 297.17 150.678)
891 (50 21.02 2281.226)
892 (45 247.54 29929.562)
893 (44 325.15 31555.956)
894 (29 60.93 4443.417)
895 (18 155.12 67555.328)
896 (17 288.79 4562.452)
897 (16 198.04 62894.029)
898 (14 199.76 31436.921)
899 (12 95.39 14577.848)
900 (12 287.11 31931.756)
901 (12 320.81 34777.259)
902 (9 227.73 1222.114)
903 (8 15.45 16859.074))
904 "Data for solar equinox/solstice calculations.")
905
906 (defun solar-equinoxes/solstices (k year)
907 "Date of equinox/solstice K for YEAR.
908 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
909 K=3, winter solstice. RESULT is a Gregorian local date.
910 Accurate to within a minute between 1951 and 2050."
911 (let* ((JDE0 (solar-mean-equinoxes/solstices k year))
912 (T (/ (- JDE0 2451545.0) 36525))
913 (W (- (* 35999.373 T) 2.47))
914 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W))
915 (* 0.0007 (solar-cosine-degrees (* 2 W)))))
916 (S (apply '+ (mapcar (lambda(x)
917 (* (car x) (solar-cosine-degrees
918 (+ (* (nth 2 x) T) (cadr x)))))
919 solar-seasons-data)))
920 (JDE (+ JDE0 (/ (* 0.00001 S) Delta-lambda)))
921 ;; Ephemeris time correction.
922 (correction (+ 102.3 (* 123.5 T) (* 32.5 T T)))
923 (JD (- JDE (/ correction 86400)))
924 (date (calendar-gregorian-from-absolute (floor (- JD 1721424.5))))
925 (time (- (- JD 0.5) (floor (- JD 0.5)))))
926 (list (car date) (+ (cadr date) time
927 (/ (/ calendar-time-zone 60.0) 24.0))
928 (nth 2 date))))
929
930 ;; From Meeus, 1991, page 166.
931 (defun solar-mean-equinoxes/solstices (k year)
932 "Julian day of mean equinox/solstice K for YEAR.
933 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
934 solstice. These formulae are only to be used between 1000 BC and 3000 AD."
935 (let ((y (/ year 1000.0))
936 (z (/ (- year 2000) 1000.0)))
937 (if (< year 1000) ; actually between -1000 and 1000
938 (cond ((= k 0) (+ 1721139.29189
939 (* 365242.13740 y)
940 (* 0.06134 y y)
941 (* 0.00111 y y y)
942 (* -0.00071 y y y y)))
943 ((= k 1) (+ 1721233.25401
944 (* 365241.72562 y)
945 (* -0.05323 y y)
946 (* 0.00907 y y y)
947 (* 0.00025 y y y y)))
948 ((= k 2) (+ 1721325.70455
949 (* 365242.49558 y)
950 (* -0.11677 y y)
951 (* -0.00297 y y y)
952 (* 0.00074 y y y y)))
953 ((= k 3) (+ 1721414.39987
954 (* 365242.88257 y)
955 (* -0.00769 y y)
956 (* -0.00933 y y y)
957 (* -0.00006 y y y y))))
958 ; actually between 1000 and 3000
959 (cond ((= k 0) (+ 2451623.80984
960 (* 365242.37404 z)
961 (* 0.05169 z z)
962 (* -0.00411 z z z)
963 (* -0.00057 z z z z)))
964 ((= k 1) (+ 2451716.56767
965 (* 365241.62603 z)
966 (* 0.00325 z z)
967 (* 0.00888 z z z)
968 (* -0.00030 z z z z)))
969 ((= k 2) (+ 2451810.21715
970 (* 365242.01767 z)
971 (* -0.11575 z z)
972 (* 0.00337 z z z)
973 (* 0.00078 z z z z)))
974 ((= k 3) (+ 2451900.05952
975 (* 365242.74049 z)
976 (* -0.06223 z z)
977 (* -0.00823 z z z)
978 (* 0.00032 z z z z)))))))
979
980 (defvar displayed-month) ; from calendar-generate
981 (defvar displayed-year)
982
983 ;;;###holiday-autoload
984 (defun solar-equinoxes-solstices ()
985 "Local date and time of equinoxes and solstices, if visible in the calendar.
986 Requires floating point."
987 (let* ((m displayed-month)
988 (y displayed-year)
989 (calendar-standard-time-zone-name
990 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
991 (calendar-daylight-savings-starts
992 (if calendar-time-zone calendar-daylight-savings-starts))
993 (calendar-daylight-savings-ends
994 (if calendar-time-zone calendar-daylight-savings-ends))
995 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
996 (k (progn
997 (calendar-increment-month m y (cond ((= 1 (% m 3)) -1)
998 ((= 2 (% m 3)) 1)
999 (t 0)))
1000 (1- (/ m 3))))
1001 (d0 (solar-equinoxes/solstices k y))
1002 (d1 (list (car d0) (floor (cadr d0)) (nth 2 d0)))
1003 (h0 (* 24 (- (cadr d0) (floor (cadr d0)))))
1004 (adj (dst-adjust-time d1 h0))
1005 (d (list (caar adj)
1006 (+ (car (cdar adj))
1007 (/ (cadr adj) 24.0))
1008 (cadr (cdar adj))))
1009 ;; The following is nearly as accurate, but not quite:
1010 ;; (d0 (solar-date-next-longitude
1011 ;; (calendar-astro-from-absolute
1012 ;; (calendar-absolute-from-gregorian
1013 ;; (list (+ 3 (* k 3)) 15 y)))
1014 ;; 90))
1015 ;; (abs-day (calendar-astro-to-absolute d)))
1016 (abs-day (calendar-absolute-from-gregorian d)))
1017 (list
1018 (list (calendar-gregorian-from-absolute (floor abs-day))
1019 (format "%s %s"
1020 (nth k (if (and calendar-latitude
1021 (< (calendar-latitude) 0))
1022 solar-s-hemi-seasons
1023 solar-n-hemi-seasons))
1024 (solar-time-string
1025 (* 24 (- abs-day (floor abs-day)))
1026 (if (dst-in-effect abs-day)
1027 calendar-daylight-time-zone-name
1028 calendar-standard-time-zone-name)))))))
1029
1030
1031 (provide 'solar)
1032
1033 ;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1034 ;;; solar.el ends here