1 ;;; solar.el --- calendar functions for solar events
3 ;; Copyright (C) 1992, 1993, 1995, 1997, 2003 Free Software Foundation, Inc.
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>
9 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
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 2, or (at your option)
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.
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,
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 ;; 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
64 (defvar displayed-month)
65 (defvar displayed-year)
68 (require 'lisp-float-type)
69 (error "Solar/lunar calculations impossible since floating point is unavailable"))
75 (defcustom calendar-time-display-form
76 '(12-hours ":" minutes am-pm
77 (if time-zone " (") time-zone (if time-zone ")"))
78 "*The pseudo-pattern that governs the way a time of day is formatted.
80 A pseudo-pattern is a list of expressions that can involve the keywords
81 `12-hours', `24-hours', and `minutes', all numbers in string form,
82 and `am-pm' and `time-zone', both alphabetic strings.
86 '(24-hours \":\" minutes
87 (if time-zone \" (\") time-zone (if time-zone \")\"))
89 would give military-style times like `21:07 (UTC)'."
94 (defcustom calendar-latitude nil
95 "*Latitude of `calendar-location-name' in degrees.
97 The value can be either a decimal fraction (one place of accuracy is
98 sufficient), + north, - south, such as 40.7 for New York City, or the value
99 can be a vector [degrees minutes north/south] such as [40 50 north] for New
102 This variable should be set in `site-start'.el."
103 :type '(choice (const nil)
104 (number :tag "Exact")
105 (vector :value [0 0 north]
106 (integer :tag "Degrees")
107 (integer :tag "Minutes")
108 (choice :tag "Position"
114 (defcustom calendar-longitude nil
115 "*Longitude of `calendar-location-name' in degrees.
117 The value can be either a decimal fraction (one place of accuracy is
118 sufficient), + east, - west, such as -73.9 for New York City, or the value
119 can be a vector [degrees minutes east/west] such as [73 55 west] for New
122 This variable should be set in `site-start'.el."
123 :type '(choice (const nil)
124 (number :tag "Exact")
125 (vector :value [0 0 west]
126 (integer :tag "Degrees")
127 (integer :tag "Minutes")
128 (choice :tag "Position"
133 (defsubst calendar-latitude ()
134 "Convert calendar-latitude to a signed decimal fraction, if needed."
135 (if (numberp calendar-latitude)
137 (let ((lat (+ (aref calendar-latitude 0)
138 (/ (aref calendar-latitude 1) 60.0))))
139 (if (equal (aref calendar-latitude 2) 'north)
143 (defsubst calendar-longitude ()
144 "Convert calendar-longitude to a signed decimal fraction, if needed."
145 (if (numberp calendar-longitude)
147 (let ((long (+ (aref calendar-longitude 0)
148 (/ (aref calendar-longitude 1) 60.0))))
149 (if (equal (aref calendar-longitude 2) 'east)
154 (defcustom calendar-location-name
155 '(let ((float-output-format "%.1f"))
157 (if (numberp calendar-latitude)
158 (abs calendar-latitude)
159 (+ (aref calendar-latitude 0)
160 (/ (aref calendar-latitude 1) 60.0)))
161 (if (numberp calendar-latitude)
162 (if (> calendar-latitude 0) "N" "S")
163 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
164 (if (numberp calendar-longitude)
165 (abs calendar-longitude)
166 (+ (aref calendar-longitude 0)
167 (/ (aref calendar-longitude 1) 60.0)))
168 (if (numberp calendar-longitude)
169 (if (> calendar-longitude 0) "E" "W")
170 (if (equal (aref calendar-longitude 2) 'east) "E" "W"))))
171 "*Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
172 For example, \"New York City\". Default value is just the latitude, longitude
175 This variable should be set in `site-start'.el."
179 (defcustom solar-error 0.5
180 "*Tolerance (in minutes) for sunrise/sunset calculations.
182 A larger value makes the calculations for sunrise/sunset faster, but less
183 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
184 times will be correct to the minute.
186 It is useless to set the value smaller than 4*delta, where delta is the
187 accuracy in the longitude of the sun (given by the function
188 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
189 delta. At present, delta = 0.01 degrees, so the value of the variable
190 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
194 (defvar solar-n-hemi-seasons
195 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
196 "List of season changes for the northern hemisphere.")
198 (defvar solar-s-hemi-seasons
199 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
200 "List of season changes for the southern hemisphere.")
202 (defvar solar-sidereal-time-greenwich-midnight
204 "Sidereal time at Greenwich at midnight (universal time).")
206 (defvar solar-northern-spring-or-summer-season nil
207 "Non-nil if northern spring or summer and nil otherwise.
208 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
210 (defun solar-setup ()
211 "Prompt user for latitude, longitude, and time zone."
213 (if (not calendar-longitude)
214 (setq calendar-longitude
216 "Enter longitude (decimal fraction; + east, - west): ")))
217 (if (not calendar-latitude)
218 (setq calendar-latitude
220 "Enter latitude (decimal fraction; + north, - south): ")))
221 (if (not calendar-time-zone)
222 (setq calendar-time-zone
224 "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 ;; The condition-case stuff is needed to catch bogus arithmetic
234 ;; exceptions that occur on some machines (like Sparcs)
235 (defun solar-sin-degrees (x)
237 (sin (degrees-to-radians (mod x 360.0)))
238 (solar-sin-degrees x)))
239 (defun solar-cosine-degrees (x)
241 (cos (degrees-to-radians (mod x 360.0)))
242 (solar-cosine-degrees x)))
243 (defun solar-tangent-degrees (x)
245 (tan (degrees-to-radians (mod x 360.0)))
246 (solar-tangent-degrees x)))
248 (defun solar-xy-to-quadrant (x y)
249 "Determines the quadrant of the point X, Y."
254 (defun solar-degrees-to-quadrant (angle)
255 "Determines the quadrant of ANGLE."
256 (1+ (floor (mod angle 360) 90)))
258 (defun solar-arctan (x quad)
259 "Arctangent of X in quadrant QUAD."
260 (let ((deg (radians-to-degrees (atan x))))
261 (cond ((equal quad 2) (+ deg 180))
262 ((equal quad 3) (+ deg 180))
263 ((equal quad 4) (+ deg 360))
266 (defun solar-atn2 (x y)
267 "Arctan of point X, Y."
270 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
272 (defun solar-arccos (x)
274 (let ((y (sqrt (- 1 (* x x)))))
277 (defun solar-arcsin (y)
279 (let ((x (sqrt (- 1 (* y y)))))
283 (defsubst solar-degrees-to-hours (degrees)
284 "Convert DEGREES to hours."
287 (defsubst solar-hours-to-days (hour)
288 "Convert HOUR to decimal fraction of a day."
291 (defun solar-right-ascension (longitude obliquity)
292 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
293 Both arguments are in degrees."
294 (solar-degrees-to-hours
296 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
297 (solar-degrees-to-quadrant longitude))))
299 (defun solar-declination (longitude obliquity)
300 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
301 Both arguments are in degrees."
303 (* (solar-sin-degrees obliquity)
304 (solar-sin-degrees longitude))))
306 (defun solar-sunrise-and-sunset (time latitude longitude height)
307 "Sunrise, sunset and length of day.
308 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
310 TIME is a pair with the first component being the number of Julian centuries
311 elapsed at 0 Universal Time, and the second component being the universal
312 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
313 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
314 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
316 HEIGHT is the angle the center of the sun has over the horizon for the contact
317 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
318 accounting for the edge of the sun being on the horizon.
320 Coordinates are included because this function is called with latitude=1
321 degrees to find out if polar regions have 24 hours of sun or only night."
322 (let* ((rise-time (solar-moment -1 latitude longitude time height))
323 (set-time (solar-moment 1 latitude longitude time height))
325 (if (not (and rise-time set-time))
326 (if (or (and (> latitude 0)
327 solar-northern-spring-or-summer-season)
329 (not solar-northern-spring-or-summer-season)))
332 (setq day-length (- set-time rise-time)))
333 (list (if rise-time (+ rise-time (/ calendar-time-zone 60.0)) nil)
334 (if set-time (+ set-time (/ calendar-time-zone 60.0)) nil)
337 (defun solar-moment (direction latitude longitude time height)
338 "Sunrise/sunset at location.
339 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
342 TIME is a pair with the first component being the number of Julian centuries
343 elapsed at 0 Universal Time, and the second component being the universal
344 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
345 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
346 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
348 HEIGHT is the angle the center of the sun has over the horizon for the contact
349 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
350 accounting for the edge of the sun being on the horizon.
353 (let* ((ut (car (cdr time)))
354 (possible t) ; we assume that rise or set are possible
355 (utmin (+ ut (* direction 12.0)))
356 (utmax ut) ; the time searched is between utmin and utmax
357 ; utmin and utmax are in hours
358 (utmoment-old 0.0) ; rise or set approximation
359 (utmoment 1.0) ; rise or set approximation
360 (hut 0) ; sun height at utmoment
363 (solar-horizontal-coordinates (list t0 utmin)
364 latitude longitude t))))
366 (solar-horizontal-coordinates (list t0 utmax)
367 latitude longitude t)))))
368 ; -0.61 degrees is the height of the middle of the sun, when it rises
372 (while ;(< i 20) ; we perform a simple dichotomy
373 ; (> (abs (- hut height)) epsilon)
374 (>= (abs (- utmoment utmoment-old))
376 (setq utmoment-old utmoment)
377 (setq utmoment (/ (+ utmin utmax) 2))
379 (solar-horizontal-coordinates
380 (list t0 utmoment) latitude longitude t))))
381 (if (< hut height) (setq utmin utmoment))
382 (if (> hut height) (setq utmax utmoment))
384 (setq possible nil)) ; the sun never rises
385 (setq possible nil)) ; the sun never sets
386 (if (not possible) nil utmoment)))
388 (defun solar-time-string (time time-zone)
389 "Printable form for decimal fraction TIME in TIME-ZONE.
390 Format used is given by `calendar-time-display-form'."
391 (let* ((time (round (* 60 time)))
392 (24-hours (/ time 60))
393 (minutes (format "%02d" (% time 60)))
394 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
395 (am-pm (if (>= 24-hours 12) "pm" "am"))
396 (24-hours (format "%02d" 24-hours)))
397 (mapconcat 'eval calendar-time-display-form "")))
400 (defun solar-daylight (time)
401 "Printable form for time expressed in hours."
404 (floor (* 60 (- time (floor time))))))
406 (defun solar-exact-local-noon (date)
407 "Date and Universal Time of local noon at *local date* date.
409 The date may be different from the one asked for, but it will be the right
410 local date. The second component of date should be an integer."
412 (ut (- 12.0 (/ (calendar-longitude) 15)))
413 (te (solar-time-equation date ut)))
417 (setq nd (list (car date) (+ 1 (car (cdr date)))
418 (car (cdr (cdr date)))))
419 (setq ut (- ut 24))))
422 (setq nd (list (car date) (- (car (cdr date)) 1)
423 (car (cdr (cdr date)))))
424 (setq ut (+ ut 24))))
425 (setq nd (calendar-gregorian-from-absolute
426 (calendar-absolute-from-gregorian nd)))
427 ; date standardization
430 (defun solar-sunrise-sunset (date)
431 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
433 Corresponding value is nil if there is no sunrise/sunset."
434 (let* (; first, get the exact moment of local noon.
435 (exact-local-noon (solar-exact-local-noon date))
436 ; get the time from the 2000 epoch.
437 (t0 (solar-julian-ut-centuries (car exact-local-noon)))
438 ; store the sidereal time at Greenwich at midnight of UT time.
439 ; find if summer or winter slightly above the equator
441 (progn (setq solar-sidereal-time-greenwich-midnight
442 (solar-sidereal-time t0))
443 (solar-sunrise-and-sunset
444 (list t0 (car (cdr exact-local-noon)))
446 (calendar-longitude) 0)))
447 ; store the spring/summer information,
448 ; compute sunrise and sunset (two first components of rise-set).
449 ; length of day is the third component (it is only the difference
450 ; between sunset and sunrise when there is a sunset and a sunrise)
453 (setq solar-northern-spring-or-summer-season
454 (if (> (car (cdr (cdr equator-rise-set))) 12) t nil))
455 (solar-sunrise-and-sunset
456 (list t0 (car (cdr exact-local-noon)))
458 (calendar-longitude) -0.61)))
459 (rise (car rise-set))
460 (adj-rise (if rise (dst-adjust-time date rise) nil))
461 (set (car (cdr rise-set)))
462 (adj-set (if set (dst-adjust-time date set) nil))
463 (length (car (cdr (cdr rise-set)))) )
465 (and rise (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
466 (and set (calendar-date-equal date (car adj-set)) (cdr adj-set))
467 (solar-daylight length))))
469 (defun solar-sunrise-sunset-string (date)
470 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
471 (let ((l (solar-sunrise-sunset date)))
473 "%s, %s at %s (%s hours daylight)"
475 (concat "Sunrise " (apply 'solar-time-string (car l)))
478 (concat "sunset " (apply 'solar-time-string (car (cdr l))))
480 (eval calendar-location-name)
481 (car (cdr (cdr l))))))
483 (defun solar-julian-ut-centuries (date)
484 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
485 (/ (- (calendar-absolute-from-gregorian date)
486 (calendar-absolute-from-gregorian '(1 1.5 2000)))
489 (defun solar-ephemeris-time(time)
490 "Ephemeris Time at moment TIME.
492 TIME is a pair with the first component being the number of Julian centuries
493 elapsed at 0 Universal Time, and the second component being the universal
494 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
495 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
496 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
498 Result is in julian centuries of ephemeris time."
499 (let* ((t0 (car time))
500 (ut (car (cdr time)))
501 (t1 (+ t0 (/ (/ ut 24.0) 36525)))
502 (y (+ 2000 (* 100 t1)))
503 (dt (* 86400 (solar-ephemeris-correction (floor y)))))
504 (+ t1 (/ (/ dt 86400) 36525))))
506 (defun solar-date-next-longitude (d l)
507 "First moment on or after Julian day number D when sun's longitude is a
508 multiple of L degrees at calendar-location-name with that location's
509 local time (including any daylight savings rules).
511 L must be an integer divisor of 360.
513 Result is in local time expressed astronomical (Julian) day numbers.
515 The values of calendar-daylight-savings-starts,
516 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
517 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
518 calendar-time-zone are used to interpret local time."
521 (start-long (solar-longitude d))
522 (next (mod (* l (1+ (floor (/ start-long l)))) 360))
523 (end (+ d (* (/ l 360.0) 400)))
524 (end-long (solar-longitude end)))
525 (while ;; bisection search for nearest minute
526 (< 0.00001 (- end start))
528 ;; start-long <= next < end-long when next != 0
529 ;; when next = 0, we look for the discontinuity (start-long is near 360
530 ;; and end-long is small (less than l).
531 (setq d (/ (+ start end) 2.0))
532 (setq long (solar-longitude d))
533 (if (or (and (/= next 0) (< long next))
534 (and (= next 0) (< l long)))
537 (setq start-long long))
539 (setq end-long long)))
540 (/ (+ start end) 2.0)))
542 (defun solar-horizontal-coordinates
543 (time latitude longitude for-sunrise-sunset)
544 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
546 TIME is a pair with the first component being the number of Julian centuries
547 elapsed at 0 Universal Time, and the second component being the universal
548 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
549 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
550 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
552 The azimuth is given in degrees as well as the height (between -180 and 180)."
553 (let* ((ut (car (cdr time)))
554 (ec (solar-equatorial-coordinates time for-sunrise-sunset))
555 (st (+ solar-sidereal-time-greenwich-midnight
556 (* ut 1.00273790935)))
557 (ah (- (* st 15) (* 15 (car ec)) (* -1 (calendar-longitude))))
558 ; hour angle (in degrees)
560 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah)
561 (solar-sin-degrees latitude))
562 (* (solar-tangent-degrees de)
563 (solar-cosine-degrees latitude)))
564 (solar-sin-degrees ah)))
565 (height (solar-arcsin
566 (+ (* (solar-sin-degrees latitude) (solar-sin-degrees de))
567 (* (solar-cosine-degrees latitude)
568 (solar-cosine-degrees de)
569 (solar-cosine-degrees ah))))))
570 (if (> height 180) (setq height (- height 360)))
571 (list azimuth height)))
573 (defun solar-equatorial-coordinates (time for-sunrise-sunset)
574 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
576 TIME is a pair with the first component being the number of Julian centuries
577 elapsed at 0 Universal Time, and the second component being the universal
578 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
579 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
580 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
581 (let* ((tm (solar-ephemeris-time time))
582 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset)))
583 (list (solar-right-ascension (car ec) (car (cdr ec)))
584 (solar-declination (car ec) (car (cdr ec))))))
586 (defun solar-ecliptic-coordinates (time for-sunrise-sunset)
587 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
588 equation of time (in hours) and nutation in longitude (in seconds)
589 at moment `time', expressed in julian centuries of Ephemeris Time
590 since January 1st, 2000, at 12 ET."
591 (let* ((l (+ 280.46645
593 (* 0.0003032 time time))) ; sun mean longitude
595 (* 481267.8813 time))) ; moon mean longitude
598 (* -0.0001559 time time)
599 (* -0.00000048 time time time))) ; sun mean anomaly
600 (i (+ 23.43929111 (* -0.013004167 time)
601 (* -0.00000016389 time time)
602 (* 0.0000005036 time time time))); mean inclination
605 (* -0.000014 time time))
606 (solar-sin-degrees m))
607 (* (+ 0.019993 (* -0.000101 time))
608 (solar-sin-degrees (* 2 m)))
610 (solar-sin-degrees (* 3 m))))) ; center equation
611 (L (+ l c)) ; total longitude
613 (* -1934.136 time))) ; longitude of moon's ascending node
615 (nut (if (not for-sunrise-sunset)
616 (+ (* -17.20 (solar-sin-degrees omega))
617 (* -1.32 (solar-sin-degrees (* 2 l)))
618 (* -0.23 (solar-sin-degrees (* 2 ml)))
619 (* 0.21 (solar-sin-degrees (* 2 omega))))
621 ; nut = nutation in longitude, measured in seconds of angle.
622 (ecc (if (not for-sunrise-sunset)
624 (* -0.000042037 time)
625 (* -0.0000001236 time time)) ; eccentricity of earth's orbit
630 (solar-sin-degrees omega)))) ; apparent longitude of sun
631 (y (if (not for-sunrise-sunset)
632 (* (solar-tangent-degrees (/ i 2))
633 (solar-tangent-degrees (/ i 2)))
635 (time-eq (if (not for-sunrise-sunset)
636 (/ (* 12 (+ (* y (solar-sin-degrees (* 2 l)))
637 (* -2 ecc (solar-sin-degrees m))
638 (* 4 ecc y (solar-sin-degrees m)
639 (solar-cosine-degrees (* 2 l)))
640 (* -0.5 y y (solar-sin-degrees (* 4 l)))
641 (* -1.25 ecc ecc (solar-sin-degrees (* 2 m)))))
644 ; equation of time, in hours
645 (list app i time-eq nut)))
647 (defconst solar-data-list
648 '((403406 4.721964 1.621043)
649 (195207 5.937458 62830.348067)
650 (119433 1.115589 62830.821524)
651 (112392 5.781616 62829.634302)
652 (3891 5.5474 125660.5691)
653 (2819 1.5120 125660.984)
654 (1721 4.1897 62832.4766)
656 (660 5.415 125659.31)
659 (314 5.198 777137.715)
660 (268 5.989 78604.191)
662 (234 1.423 39302.098)
664 (132 2.317 115067.698)
665 (129 3.193 15774.337)
697 (10 2.55 157208.40)))
699 (defun solar-longitude (d)
700 "Longitude of sun on astronomical (Julian) day number D.
701 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
703 The values of calendar-daylight-savings-starts,
704 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
705 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
706 calendar-time-zone are used to interpret local time."
707 (let* ((a-d (calendar-absolute-from-astro d))
708 ;; get Universal Time
709 (date (calendar-astro-from-absolute
711 (if (dst-in-effect a-d)
712 (/ calendar-daylight-time-offset 24.0 60.0) 0)
713 (/ calendar-time-zone 60.0 24.0))))
714 ;; get Ephemeris Time
715 (date (+ date (solar-ephemeris-correction
716 (extract-calendar-year
717 (calendar-gregorian-from-absolute
719 (calendar-absolute-from-astro
721 (U (/ (- date 2451545) 3652500))
731 (* (car (cdr (cdr x))) U))
735 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U)))) 973)))
736 (A1 (mod (+ 2.18 (* U (+ -3375.70 (* 0.36 U)))) (* 2 pi)))
737 (A2 (mod (+ 3.51 (* U (+ 125666.39 (* 0.10 U)))) (* 2 pi)))
738 (nutation (* -0.0000001 (+ (* 834 (sin A1)) (* 64 (sin A2))))))
739 (mod (radians-to-degrees (+ longitude aberration nutation)) 360.0)))
741 (defun solar-ephemeris-correction (year)
742 "Ephemeris time minus Universal Time during Gregorian year.
745 For the years 1800-1987, the maximum error is 1.9 seconds.
746 For the other years, the maximum error is about 30 seconds."
747 (cond ((and (<= 1988 year) (< year 2020))
748 (/ (+ year -2000 67.0) 60.0 60.0 24.0))
749 ((and (<= 1900 year) (< year 1988))
750 (let* ((theta (/ (- (calendar-astro-from-absolute
751 (calendar-absolute-from-gregorian
753 (calendar-astro-from-absolute
754 (calendar-absolute-from-gregorian
757 (theta2 (* theta theta))
758 (theta3 (* theta2 theta))
759 (theta4 (* theta2 theta2))
760 (theta5 (* theta3 theta2)))
767 (* 0.677066 theta3 theta3)
768 (* -0.212591 theta4 theta3))))
769 ((and (<= 1800 year) (< year 1900))
770 (let* ((theta (/ (- (calendar-astro-from-absolute
771 (calendar-absolute-from-gregorian
773 (calendar-astro-from-absolute
774 (calendar-absolute-from-gregorian
777 (theta2 (* theta theta))
778 (theta3 (* theta2 theta))
779 (theta4 (* theta2 theta2))
780 (theta5 (* theta3 theta2)))
787 (* 31.332267 theta3 theta3)
788 (* 38.291999 theta4 theta3)
789 (* 28.316289 theta4 theta4)
790 (* 11.636204 theta4 theta5)
791 (* 2.043794 theta5 theta5))))
792 ((and (<= 1620 year) (< year 1800))
793 (let ((x (/ (- year 1600) 10.0)))
794 (/ (+ (* 2.19167 x x) (* -40.675 x) 196.58333) 60.0 60.0 24.0)))
795 (t (let* ((tmp (- (calendar-astro-from-absolute
796 (calendar-absolute-from-gregorian
799 (second (- (/ (* tmp tmp) 41048480.0) 15)))
800 (/ second 60.0 60.0 24.0)))))
802 (defun solar-sidereal-time (t0)
803 "Sidereal time (in hours) in Greenwich.
805 At T0=Julian centuries of universal time.
806 T0 must correspond to 0 hours UT."
807 (let* ((mean-sid-time (+ 6.6973746
809 (* 0.0000258622 t0 t0)
810 (* -0.0000000017222 t0 t0 t0)))
811 (et (solar-ephemeris-time (list t0 0.0)))
812 (nut-i (solar-ecliptic-coordinates et nil))
813 (nut (car (cdr (cdr (cdr nut-i))))) ; nutation
814 (i (car (cdr nut-i)))) ; inclination
815 (mod (+ (mod (+ mean-sid-time
816 (/ (/ (* nut (solar-cosine-degrees i)) 15) 3600)) 24.0)
820 (defun solar-time-equation (date ut)
821 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
822 (let* ((et (solar-date-to-et date ut))
823 (ec (solar-ecliptic-coordinates et nil)))
824 (car (cdr (cdr ec)))))
826 (defun solar-date-to-et (date ut)
827 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
828 Expressed in julian centuries of Ephemeris Time."
829 (let ((t0 (solar-julian-ut-centuries date)))
830 (solar-ephemeris-time (list t0 ut))))
833 (defun sunrise-sunset (&optional arg)
834 "Local time of sunrise and sunset for today. Accurate to a few seconds.
835 If called with an optional prefix argument, prompt for date.
837 If called with an optional double prefix argument, prompt for longitude,
838 latitude, time zone, and date, and always use standard time.
840 This function is suitable for execution in a .emacs file."
842 (or arg (setq arg 1))
844 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
846 (let* ((calendar-longitude
847 (if (< arg 16) calendar-longitude
849 "Enter longitude (decimal fraction; + east, - west): ")))
851 (if (< arg 16) calendar-latitude
853 "Enter latitude (decimal fraction; + north, - south): ")))
855 (if (< arg 16) calendar-time-zone
857 "Enter difference from Coordinated Universal Time (in minutes): ")))
858 (calendar-location-name
859 (if (< arg 16) calendar-location-name
860 (let ((float-output-format "%.1f"))
862 (if (numberp calendar-latitude)
863 (abs calendar-latitude)
864 (+ (aref calendar-latitude 0)
865 (/ (aref calendar-latitude 1) 60.0)))
866 (if (numberp calendar-latitude)
867 (if (> calendar-latitude 0) "N" "S")
868 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
869 (if (numberp calendar-longitude)
870 (abs calendar-longitude)
871 (+ (aref calendar-longitude 0)
872 (/ (aref calendar-longitude 1) 60.0)))
873 (if (numberp calendar-longitude)
874 (if (> calendar-longitude 0) "E" "W")
875 (if (equal (aref calendar-longitude 2) 'east)
877 (calendar-standard-time-zone-name
878 (if (< arg 16) calendar-standard-time-zone-name
879 (cond ((= calendar-time-zone 0) "UTC")
880 ((< calendar-time-zone 0)
881 (format "UTC%dmin" calendar-time-zone))
882 (t (format "UTC+%dmin" calendar-time-zone)))))
883 (calendar-daylight-savings-starts
884 (if (< arg 16) calendar-daylight-savings-starts))
885 (calendar-daylight-savings-ends
886 (if (< arg 16) calendar-daylight-savings-ends))
887 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
888 (date-string (calendar-date-string date t))
889 (time-string (solar-sunrise-sunset-string date))
890 (msg (format "%s: %s" date-string time-string))
891 (one-window (one-window-p t)))
892 (if (<= (length msg) (frame-width))
894 (with-output-to-temp-buffer "*temp*"
895 (princ (concat date-string "\n" time-string)))
897 (substitute-command-keys
900 "Type \\[delete-other-windows] to remove temp window."
901 "Type \\[switch-to-buffer] RET to remove temp window.")
902 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
904 (defun calendar-sunrise-sunset ()
905 "Local time of sunrise and sunset for date under cursor.
906 Accurate to a few seconds."
908 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
910 (let ((date (calendar-cursor-to-date t)))
912 (calendar-date-string date t t)
913 (solar-sunrise-sunset-string date))))
915 (defun diary-sunrise-sunset ()
916 "Local time of sunrise and sunset as a diary entry.
917 Accurate to a few seconds."
918 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
920 (solar-sunrise-sunset-string date))
922 (defcustom diary-sabbath-candles-minutes 18
923 "*Number of minutes before sunset for sabbath candle lighting."
928 (defun diary-sabbath-candles (&optional mark)
929 "Local time of candle lighting diary entry--applies if date is a Friday.
930 No diary entry if there is no sunset on that date.
932 An optional parameter MARK specifies a face or single-character string to
933 use when highlighting the day in the calendar."
934 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
936 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
937 (let* ((sunset (car (cdr (solar-sunrise-sunset date))))
939 (cons (- (car sunset)
940 (/ diary-sabbath-candles-minutes 60.0))
944 (format "%s Sabbath candle lighting"
945 (apply 'solar-time-string light)))))))
947 ; from Meeus, 1991, page 167
948 (defconst solar-seasons-data
949 '((485 324.96 1934.136)
950 (203 337.23 32964.467)
952 (182 27.85 445267.112)
953 (156 73.14 45036.886)
954 (136 171.52 22518.443)
955 (77 222.54 65928.934)
958 (58 119.81 33718.147)
961 (45 247.54 29929.562)
962 (44 325.15 31555.956)
964 (18 155.12 67555.328)
966 (16 198.04 62894.029)
967 (14 199.76 31436.921)
969 (12 287.11 31931.756)
970 (12 320.81 34777.259)
972 (8 15.45 16859.074)))
974 (defun solar-equinoxes/solstices (k year)
975 "Date of equinox/solstice K for YEAR.
976 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
977 K=3, winter solstice.
978 RESULT is a gregorian local date.
980 Accurate to less than a minute between 1951 and 2050."
981 (let* ((JDE0 (solar-mean-equinoxes/solstices k year))
982 (T (/ (- JDE0 2451545.0) 36525))
983 (W (- (* 35999.373 T) 2.47))
984 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W))
985 (* 0.0007 (solar-cosine-degrees (* 2 W)))))
986 (S (apply '+ (mapcar '(lambda(x)
987 (* (car x) (solar-cosine-degrees
988 (+ (* (car (cdr (cdr x))) T)
990 solar-seasons-data)))
991 (JDE (+ JDE0 (/ (* 0.00001 S) Delta-lambda)))
992 (correction (+ 102.3 (* 123.5 T) (* 32.5 T T)))
993 ; ephemeris time correction
994 (JD (- JDE (/ correction 86400)))
995 (date (calendar-gregorian-from-absolute (floor (- JD 1721424.5))))
996 (time (- (- JD 0.5) (floor (- JD 0.5))))
998 (list (car date) (+ (car (cdr date)) time
999 (/ (/ calendar-time-zone 60.0) 24.0))
1000 (car (cdr (cdr date))))))
1002 ; from Meeus, 1991, page 166
1003 (defun solar-mean-equinoxes/solstices (k year)
1004 "Julian day of mean equinox/solstice K for YEAR.
1005 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
1006 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
1007 (let ((y (/ year 1000.0))
1008 (z (/ (- year 2000) 1000.0)))
1009 (if (< year 1000) ; actually between -1000 and 1000
1010 (cond ((equal k 0) (+ 1721139.29189
1014 (* -0.00071 y y y y)))
1015 ((equal k 1) (+ 1721233.25401
1019 (* 0.00025 y y y y)))
1020 ((equal k 2) (+ 1721325.70455
1024 (* 0.00074 y y y y)))
1025 ((equal k 3) (+ 1721414.39987
1029 (* -0.00006 y y y y))))
1030 ; actually between 1000 and 3000
1031 (cond ((equal k 0) (+ 2451623.80984
1035 (* -0.00057 z z z z)))
1036 ((equal k 1) (+ 2451716.56767
1040 (* -0.00030 z z z z)))
1041 ((equal k 2) (+ 2451810.21715
1045 (* 0.00078 z z z z)))
1046 ((equal k 3) (+ 2451900.05952
1050 (* 0.00032 z z z z)))))))
1053 (defun solar-equinoxes-solstices ()
1054 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
1055 Requires floating point."
1056 (let ((m displayed-month)
1058 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
1061 (let* ((calendar-standard-time-zone-name
1062 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
1063 (calendar-daylight-savings-starts
1064 (if calendar-time-zone calendar-daylight-savings-starts))
1065 (calendar-daylight-savings-ends
1066 (if calendar-time-zone calendar-daylight-savings-ends))
1067 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
1069 (d0 (solar-equinoxes/solstices k y))
1070 (d1 (list (car d0) (floor (car (cdr d0))) (car (cdr (cdr d0)))))
1071 (h0 (* 24 (- (car (cdr d0)) (floor (car (cdr d0))))))
1072 (adj (dst-adjust-time d1 h0))
1073 (d (list (car (car adj))
1074 (+ (car (cdr (car adj)) )
1075 (/ (car (cdr adj)) 24.0))
1076 (car (cdr (cdr (car adj))))))
1077 ; The following is nearly as accurate, but not quite:
1078 ;(d0 (solar-date-next-longitude
1079 ; (calendar-astro-from-absolute
1080 ; (calendar-absolute-from-gregorian
1081 ; (list (+ 3 (* k 3)) 15 y)))
1083 ;(abs-day (calendar-absolute-from-astro d)))
1084 (abs-day (calendar-absolute-from-gregorian d)))
1086 (list (calendar-gregorian-from-absolute (floor abs-day))
1088 (nth k (if (and calendar-latitude
1089 (< (calendar-latitude) 0))
1090 solar-s-hemi-seasons
1091 solar-n-hemi-seasons))
1093 (* 24 (- abs-day (floor abs-day)))
1094 (if (dst-in-effect abs-day)
1095 calendar-daylight-time-zone-name
1096 calendar-standard-time-zone-name))))))))
1101 ;;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1102 ;;; solar.el ends here