]> code.delx.au - gnu-emacs/blob - lisp/calendar/solar.el
Doc fixes.
[gnu-emacs] / lisp / calendar / solar.el
1 ;;; solar.el --- calendar functions for solar events.
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: calendar
7 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
8 ;; holidays
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY. No author or distributor
14 ;; accepts responsibility to anyone for the consequences of using it
15 ;; or for whether it serves any particular purpose or works at all,
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public
17 ;; License for full details.
18
19 ;; Everyone is granted permission to copy, modify and redistribute
20 ;; GNU Emacs, but only under the conditions described in the
21 ;; GNU Emacs General Public License. A copy of this license is
22 ;; supposed to have been given to you along with GNU Emacs so you
23 ;; can know your rights and responsibilities. It should be in a
24 ;; file named COPYING. Among other things, the copyright notice
25 ;; and this notice must be preserved on all copies.
26
27 ;;; Commentary:
28
29 ;; This collection of functions implements the features of calendar.el and
30 ;; diary.el that deal with times of day, sunrise/sunset, and
31 ;; eqinoxes/solstices.
32
33 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
34 ;; Almanac Office, United States Naval Observatory, Washington, 1984 and
35 ;; on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
36 ;; Willmann-Bell, Inc., 1985.
37 ;;
38 ;; WARNINGS:
39 ;; 1. SUNRISE/SUNSET calculations will be accurate only to +/- 2 minutes.
40 ;; Locations should be between +/- 65 degrees of latitude.
41 ;; Dates should be in the latter half of the 20th century.
42 ;;
43 ;; 2. Equinox/solstice times will be accurate only to +/- 15 minutes.
44
45 ;; The author would be delighted to have an astronomically more sophisticated
46 ;; person rewrite the code for the solar calculations in this file!
47
48 ;; Comments, corrections, and improvements should be sent to
49 ;; Edward M. Reingold Department of Computer Science
50 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
51 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
52 ;; Urbana, Illinois 61801
53
54 ;;; Code:
55
56 (if (fboundp 'atan)
57 (require 'lisp-float-type)
58 (error "Solar calculations impossible since floating point is unavailable."))
59
60 (require 'cal-dst)
61
62 ;;;###autoload
63 (defvar calendar-time-display-form
64 '(12-hours ":" minutes am-pm
65 (if time-zone " (") time-zone (if time-zone ")"))
66 "*The pseudo-pattern that governs the way a time of day is formatted.
67
68 A pseudo-pattern is a list of expressions that can involve the keywords
69 `12-hours', `24-hours', and `minutes', all numbers in string form,
70 and `am-pm' and `time-zone', both alphabetic strings.
71
72 For example, the form
73
74 '(24-hours \":\" minutes
75 (if time-zone \" (\") time-zone (if time-zone \")\"))
76
77 would give military-style times like `21:07 (UTC)'.")
78
79 ;;;###autoload
80 (defvar calendar-latitude nil
81 "*Latitude of `calendar-location-name' in degrees, + north, - south.
82 For example, 40.7 for New York City.
83 It may not be a good idea to set this in advance for your site;
84 if there may be users running Emacs at your site
85 who are physically located elsewhere, they would get the wrong
86 value and might not know how to override it.")
87
88 ;;;###autoload
89 (defvar calendar-longitude nil
90 "*Longitude of `calendar-location-name' in degrees, + east, - west.
91 For example, -74.0 for New York City.
92 It may not be a good idea to set this in advance for your site;
93 if there may be users running Emacs at your site
94 who are physically located elsewhere, they would get the wrong
95 value and might not know how to override it.")
96
97 ;;;###autoload
98 (defvar calendar-location-name
99 '(let ((float-output-format "%.1f"))
100 (format "%s%s, %s%s"
101 (abs calendar-latitude)
102 (if (> calendar-latitude 0) "N" "S")
103 (abs calendar-longitude)
104 (if (> calendar-longitude 0) "E" "W")))
105 "*Expression evaluating to name of `calendar-longitude', calendar-latitude'.
106 Default value is just the latitude, longitude pair.")
107
108 (defun solar-setup ()
109 "Prompt user for latitude, longitude, and time zone."
110 (beep)
111 (if (not calendar-longitude)
112 (setq calendar-longitude
113 (solar-get-number
114 "Enter longitude (decimal fraction; + east, - west): ")))
115 (if (not calendar-latitude)
116 (setq calendar-latitude
117 (solar-get-number
118 "Enter latitude (decimal fraction; + north, - south): ")))
119 (if (not calendar-time-zone)
120 (setq calendar-time-zone
121 (solar-get-number
122 "Enter difference from Coordinated Universal Time (in minutes): "))))
123
124 (defun solar-get-number (prompt)
125 "Return a number from the minibuffer, prompting with PROMPT.
126 Returns nil if nothing was entered."
127 (let ((x (read-string prompt "")))
128 (if (not (string-equal x ""))
129 (string-to-int x))))
130
131 (defun solar-sin-degrees (x)
132 (sin (degrees-to-radians x)))
133
134 (defun solar-cosine-degrees (x)
135 (cos (degrees-to-radians x)))
136
137 (defun solar-tangent-degrees (x)
138 (tan (degrees-to-radians x)))
139
140 (defun solar-xy-to-quadrant (x y)
141 "Determines the quadrant of the point X, Y."
142 (if (> x 0)
143 (if (> y 0) 1 4)
144 (if (> y 0) 2 3)))
145
146 (defun solar-degrees-to-quadrant (angle)
147 "Determines the quadrant of ANGLE."
148 (1+ (truncate (/ (solar-mod angle 360.0) 90.0))))
149
150 (defun solar-arctan (x quad)
151 "Arctangent of X in quadrant QUAD."
152 (let ((deg (radians-to-degrees (atan x))))
153 (cond ((equal quad 2) (+ deg 180))
154 ((equal quad 3) (+ deg 180))
155 ((equal quad 4) (+ deg 360))
156 (t deg))))
157
158 (defun solar-arccos (x)
159 (let ((y (sqrt (- 1 (* x x)))))
160 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
161
162 (defun solar-arcsin (y)
163 (let ((x (sqrt (- 1 (* y y)))))
164 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
165
166 (defun solar-mod (x y)
167 "Returns X mod Y; value is *always* non-negative."
168 (let ((v (% x y)))
169 (if (> 0 v)
170 (+ v y)
171 v)))
172
173 (defconst solar-earth-inclination 23.441884
174 "Inclination of earth's equator to its solar orbit in degrees.")
175
176 (defconst solar-cos-inclination (solar-cosine-degrees solar-earth-inclination)
177 "Cosine of earth's inclination.")
178
179 (defconst solar-sin-inclination (solar-sin-degrees solar-earth-inclination)
180 "Sine of earth's inclination.")
181
182 (defconst solar-earth-orbit-eccentricity 0.016718
183 "Eccentricity of orbit of the earth around the sun.")
184
185 (defmacro solar-degrees-to-hours (deg)
186 (list '/ deg 15))
187
188 (defmacro solar-hours-to-days (hour)
189 (list '/ hour 24))
190
191 (defun solar-longitude-of-sun (day)
192 "Longitude of the sun at DAY in the year."
193 (let ((mean-anomaly (- (* 0.9856 day) 3.289)))
194 (solar-mod (+ mean-anomaly
195 (* 1.916 (solar-sin-degrees mean-anomaly))
196 (* 0.020 (solar-sin-degrees (* 2 mean-anomaly)))
197 282.634)
198 360)))
199
200 (defun solar-right-ascension (longitude)
201 "Right ascension of the sun, given its LONGITUDE."
202 (solar-degrees-to-hours
203 (solar-arctan
204 (* solar-cos-inclination (solar-tangent-degrees longitude))
205 (solar-degrees-to-quadrant longitude))))
206
207 (defun solar-declination (longitude)
208 "Declination of the sun, given its LONGITUDE."
209 (solar-arcsin
210 (* solar-sin-inclination
211 (solar-sin-degrees longitude))))
212
213 (defun solar-sunrise (date)
214 "Calculates the *standard* time of sunrise for Gregorian DATE for location
215 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fraction
216 of hours. Returns nil if the sun does not rise at that location on that day."
217 (let* ((day-of-year (calendar-day-number date))
218 (approx-sunrise
219 (+ day-of-year
220 (solar-hours-to-days
221 (- 6 (solar-degrees-to-hours calendar-longitude)))))
222 (solar-longitude-of-sun-at-sunrise
223 (solar-longitude-of-sun approx-sunrise))
224 (solar-right-ascension-at-sunrise
225 (solar-right-ascension solar-longitude-of-sun-at-sunrise))
226 (solar-declination-at-sunrise
227 (solar-declination solar-longitude-of-sun-at-sunrise))
228 (cos-local-sunrise
229 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
230 (* (solar-sin-degrees solar-declination-at-sunrise)
231 (solar-sin-degrees calendar-latitude)))
232 (* (solar-cosine-degrees solar-declination-at-sunrise)
233 (solar-cosine-degrees calendar-latitude)))))
234 (if (<= (abs cos-local-sunrise) 1);; otherwise, no sunrise that day
235 (let* ((local-sunrise (solar-degrees-to-hours
236 (- 360 (solar-arccos cos-local-sunrise))))
237 (local-mean-sunrise
238 (solar-mod (- (+ local-sunrise solar-right-ascension-at-sunrise)
239 (+ (* 0.065710 approx-sunrise)
240 6.622))
241 24)))
242 (+ (- local-mean-sunrise (solar-degrees-to-hours calendar-longitude))
243 (/ calendar-time-zone 60.0))))))
244
245 (defun solar-sunset (date)
246 "Calculates the *standard* time of sunset for Gregorian DATE for location
247 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fractions
248 of hours. Returns nil if the sun does not set at that location on that day."
249 (let* ((day-of-year (calendar-day-number date))
250 (approx-sunset
251 (+ day-of-year
252 (solar-hours-to-days
253 (- 18 (solar-degrees-to-hours calendar-longitude)))))
254 (solar-longitude-of-sun-at-sunset
255 (solar-longitude-of-sun approx-sunset))
256 (solar-right-ascension-at-sunset
257 (solar-right-ascension solar-longitude-of-sun-at-sunset))
258 (solar-declination-at-sunset
259 (solar-declination solar-longitude-of-sun-at-sunset))
260 (cos-local-sunset
261 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
262 (* (solar-sin-degrees solar-declination-at-sunset)
263 (solar-sin-degrees calendar-latitude)))
264 (* (solar-cosine-degrees solar-declination-at-sunset)
265 (solar-cosine-degrees calendar-latitude)))))
266 (if (<= (abs cos-local-sunset) 1);; otherwise, no sunset that day
267 (let* ((local-sunset (solar-degrees-to-hours
268 (solar-arccos cos-local-sunset)))
269 (local-mean-sunset
270 (solar-mod (- (+ local-sunset solar-right-ascension-at-sunset)
271 (+ (* 0.065710 approx-sunset) 6.622))
272 24)))
273 (+ (- local-mean-sunset (solar-degrees-to-hours calendar-longitude))
274 (/ calendar-time-zone 60.0))))))
275
276 (defun solar-time-string (time date &optional style)
277 "Printable form for decimal fraction *standard* TIME on DATE.
278 Optional parameter STYLE forces the time to be standard time when its value
279 is 'standard and daylight savings time (if available) when its value is
280 'daylight.
281
282 Format used is given by `calendar-time-display-form'. Converted to daylight
283 savings time according to `calendar-daylight-savings-starts',
284 `calendar-daylight-savings-ends', `calendar-daylight-switchover-time', and
285 `calendar-daylight-savings-offset'."
286 (let* ((year (extract-calendar-year date))
287 (time (round (* 60 time)))
288 (rounded-abs-date (+ (calendar-absolute-from-gregorian date)
289 (/ time 60.0 24.0)))
290 (dst-starts (and calendar-daylight-savings-starts
291 (+ (calendar-absolute-from-gregorian
292 (eval calendar-daylight-savings-starts))
293 (/ calendar-daylight-savings-switchover-time
294 60.0 24.0))))
295 (dst-ends (and calendar-daylight-savings-ends
296 (+ (calendar-absolute-from-gregorian
297 (eval calendar-daylight-savings-ends))
298 (/ (- calendar-daylight-savings-switchover-time
299 calendar-daylight-time-offset)
300 60.0 24.0))))
301 (dst (and (not (eq style 'standard))
302 (or (eq style 'daylight)
303 (and dst-starts dst-ends
304 (or (and (< dst-starts dst-ends);; northern hemi.
305 (<= dst-starts rounded-abs-date)
306 (< rounded-abs-date dst-ends))
307 (and (< dst-ends dst-starts);; southern hemi.
308 (or (< rounded-abs-date dst-ends)
309 (<= dst-starts rounded-abs-date)))))
310 (and dst-starts (not dst-ends)
311 (<= dst-starts rounded-abs-date))
312 (and dst-ends (not dst-starts)
313 (< rounded-abs-date dst-ends)))))
314 (time-zone (if dst
315 calendar-daylight-time-zone-name
316 calendar-standard-time-zone-name))
317 (time (+ time (if dst calendar-daylight-time-offset 0)))
318 (24-hours (/ time 60))
319 (minutes (format "%02d" (% time 60)))
320 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
321 (am-pm (if (>= 24-hours 12) "pm" "am"))
322 (24-hours (format "%02d" 24-hours)))
323 (mapconcat 'eval calendar-time-display-form "")))
324
325 (defun solar-sunrise-sunset (date)
326 "String giving local times of sunrise and sunset on Gregorian DATE."
327 (let ((rise (solar-sunrise date))
328 (set (solar-sunset date)))
329 (format "%s, %s at %s"
330 (if rise
331 (concat "Sunrise " (solar-time-string rise date))
332 "No sunrise")
333 (if set
334 (concat "sunset " (solar-time-string set date))
335 "no sunset")
336 (eval calendar-location-name))))
337
338 (defun solar-apparent-longitude-of-sun (date)
339 "Apparent longitude of the sun on Gregorian DATE."
340 (let* ((time (/ (- (calendar-absolute-from-gregorian date)
341 (calendar-absolute-from-gregorian '(1 0.5 1900)))
342 36525))
343 (l (+ 279.69668
344 (* 36000.76892 time)
345 (* 0.0003025 time time)))
346 (m (+ 358.47583
347 (* 35999.04975 time)
348 (* -0.000150 time time)
349 (* -0.0000033 time time time)))
350 (c (+ (* (+ 1.919460
351 (* -0.004789 time)
352 (* -0.000014 time time))
353 (solar-sin-degrees m))
354 (* (+ 0.020094
355 (* -0.000100 time))
356 (solar-sin-degrees (* 2 m)))
357 (* 0.000293
358 (solar-sin-degrees (* 3 m)))))
359 (L (+ l c))
360 (omega (+ 259.18
361 (* -1934.142 time)))
362 (app (+ L
363 -0.00569
364 (* -0.00479
365 (solar-sin-degrees omega)))))
366 app))
367
368 (defun solar-ephemeris-correction (year)
369 "Difference in minutes between Ephemeris time and UTC in YEAR.
370 Value is only an approximation."
371 (let ((T (/ (- year 1900) 100.0)))
372 (+ 0.41 (* 1.2053 T) (* 0.4992 T T))))
373
374 (defun solar-equinoxes/solstices (k year)
375 "Date of equinox/solstice K for YEAR. K=0, spring equinox; K=1, summer
376 solstice; K=2, fall equinox; K=3, winter solstice. Accurate to within
377 several minutes."
378 (let ((date (list (+ 3 (* k 3)) 21 year))
379 app
380 (correction 1000))
381 (while (> correction 0.00001)
382 (setq app (solar-mod (solar-apparent-longitude-of-sun date) 360.0))
383 (setq correction (* 58 (solar-sin-degrees (- (* k 90) app))))
384 (setq date (list (extract-calendar-month date)
385 (+ (extract-calendar-day date) correction)
386 year)))
387 (list (extract-calendar-month date)
388 (+ (extract-calendar-day date) (/ calendar-time-zone 60.0 24.0)
389 (- (/ (solar-ephemeris-correction year) 60.0 24.0)))
390 year)))
391
392 ;;;###autoload
393 (defun sunrise-sunset (&optional arg)
394 "Local time of sunrise and sunset for today. Accurate to +/- 2 minutes.
395 If called with an optional prefix argument, prompts for date.
396
397 If called with an optional double prefix argument, prompts for longitude,
398 latitude, time zone, and date.
399
400 This function is suitable for execution in a .emacs file."
401 (interactive "p")
402 (if (and (< arg 16)
403 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
404 (solar-setup))
405 (let* ((calendar-longitude
406 (if (< arg 16) calendar-longitude
407 (solar-get-number
408 "Enter longitude (decimal fraction; + east, - west): ")))
409 (calendar-latitude
410 (if (< arg 16) calendar-latitude
411 (solar-get-number
412 "Enter latitude (decimal fraction; + north, - south): ")))
413 (calendar-time-zone
414 (if (< arg 16) calendar-time-zone
415 (solar-get-number
416 "Enter difference from Coordinated Universal Time (in minutes): ")))
417 (calendar-location-name
418 (if (< arg 16) calendar-location-name
419 (let ((float-output-format "%.1f"))
420 (format "%s%s, %s%s"
421 (abs calendar-latitude)
422 (if (> calendar-latitude 0) "N" "S")
423 (abs calendar-longitude)
424 (if (> calendar-longitude 0) "E" "W")))))
425 (calendar-standard-time-zone-name
426 (if (< arg 16) calendar-standard-time-zone-name
427 (cond ((= calendar-time-zone 0) "UTC")
428 ((< calendar-time-zone 0)
429 (format "UTC%dmin" calendar-time-zone))
430 (t (format "UTC+%dmin" calendar-time-zone)))))
431 (calendar-daylight-savings-starts
432 (if (< arg 16) calendar-daylight-savings-starts))
433 (calendar-daylight-savings-ends
434 (if (< arg 16) calendar-daylight-savings-ends))
435 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
436 (date-string (calendar-date-string date t))
437 (time-string (solar-sunrise-sunset date))
438 (msg (format "%s: %s" date-string time-string))
439 (one-window (one-window-p t)))
440 (if (<= (length msg) (screen-width))
441 (message msg)
442 (with-output-to-temp-buffer "*temp*"
443 (princ (concat date-string "\n" time-string)))
444 (message (substitute-command-keys
445 (if one-window
446 (if pop-up-windows
447 "Type \\[delete-other-windows] to remove temp window."
448 "Type \\[switch-to-buffer] RET to remove temp window.")
449 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
450
451 (defun calendar-sunrise-sunset ()
452 "Local time of sunrise and sunset for date under cursor.
453 Accurate to +/- 2 minutes."
454 (interactive)
455 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
456 (solar-setup))
457 (message
458 (solar-sunrise-sunset
459 (or (calendar-cursor-to-date)
460 (error "Cursor is not on a date!")))))
461
462 (defun diary-sunrise-sunset ()
463 "Local time of sunrise and sunset as a diary entry.
464 Accurate to +/- 2 minutes."
465 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
466 (solar-setup))
467 (solar-sunrise-sunset date))
468
469 (defun diary-sabbath-candles ()
470 "Local time of candle lighting diary entry--applies if date is a Friday.
471 No diary entry if there is no sunset on that date."
472 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
473 (solar-setup))
474 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
475 (let* ((sunset (solar-sunset date))
476 (light (if sunset (- sunset (/ 18.0 60.0)))))
477 (if light (format "%s Sabbath candle lighting"
478 (solar-time-string light date))))))
479
480 (defun solar-equinoxes-solstices ()
481 "Date and time of equinoxes and solstices, if visible in the calendar window.
482 Requires floating point."
483 (let ((m displayed-month)
484 (y displayed-year))
485 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
486 ((= 2 (% m 3)) 1)
487 (t 0)))
488 (let* ((calendar-standard-time-zone-name
489 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
490 (calendar-daylight-savings-starts
491 (if calendar-time-zone calendar-daylight-savings-starts))
492 (calendar-daylight-savings-ends
493 (if calendar-time-zone calendar-daylight-savings-ends))
494 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
495 (k (1- (/ m 3)))
496 (date (solar-equinoxes/solstices k y))
497 (day (extract-calendar-day date))
498 (time (* 24 (- day (truncate day))))
499 ;; Time zone/DST can't move the date out of range,
500 ;; so let solar-time-string do the conversion.
501 (date (list (extract-calendar-month date)
502 (truncate day)
503 (extract-calendar-year date))))
504 (list (list date
505 (format "%s %s"
506 (cond ((= k 0) "Vernal Equinox")
507 ((= k 1) "Summer Solstice")
508 ((= k 2) "Fall Equinox")
509 ((= k 3) "Winter Solstice"))
510 (solar-time-string time date)))))))
511
512 (provide 'solar)
513
514 ;;; solar.el ends here