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