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