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