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