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>
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)
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).
58 (require 'lisp-float-type
)
59 (error "Solar calculations impossible since floating point is unavailable"))
65 (defcustom calendar-time-display-form
66 '(12-hours ":" minutes am-pm
67 (if time-zone
" (") time-zone
(if time-zone
")"))
68 "The pseudo-pattern that governs the way a time of day is formatted.
70 A pseudo-pattern is a list of expressions that can involve the keywords
71 `12-hours', `24-hours', and `minutes', all numbers in string form,
72 and `am-pm' and `time-zone', both alphabetic strings.
76 '(24-hours \":\" minutes
77 (if time-zone \" (\") time-zone (if time-zone \")\"))
79 would give military-style times like `21:07 (UTC)'."
83 (defcustom calendar-latitude nil
84 "Latitude of `calendar-location-name' in degrees.
85 The value can be either a decimal fraction (one place of accuracy is
86 sufficient), + north, - south, such as 40.7 for New York City, or the value
87 can be a vector [degrees minutes north/south] such as [40 50 north] for New
90 This variable should be set in `site-start'.el."
91 :type
'(choice (const nil
)
93 (vector :value
[0 0 north
]
94 (integer :tag
"Degrees")
95 (integer :tag
"Minutes")
96 (choice :tag
"Position"
101 (defcustom calendar-longitude nil
102 "Longitude of `calendar-location-name' in degrees.
103 The value can be either a decimal fraction (one place of accuracy is
104 sufficient), + east, - west, such as -73.9 for New York City, or the value
105 can be a vector [degrees minutes east/west] such as [73 55 west] for New
108 This variable should be set in `site-start'.el."
109 :type
'(choice (const nil
)
110 (number :tag
"Exact")
111 (vector :value
[0 0 west
]
112 (integer :tag
"Degrees")
113 (integer :tag
"Minutes")
114 (choice :tag
"Position"
119 (defcustom calendar-location-name
120 '(let ((float-output-format "%.1f"))
122 (if (numberp calendar-latitude
)
123 (abs calendar-latitude
)
124 (+ (aref calendar-latitude
0)
125 (/ (aref calendar-latitude
1) 60.0)))
126 (if (numberp calendar-latitude
)
127 (if (> calendar-latitude
0) "N" "S")
128 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
129 (if (numberp calendar-longitude
)
130 (abs calendar-longitude
)
131 (+ (aref calendar-longitude
0)
132 (/ (aref calendar-longitude
1) 60.0)))
133 (if (numberp calendar-longitude
)
134 (if (> calendar-longitude
0) "E" "W")
135 (if (equal (aref calendar-longitude
2) 'east
) "E" "W"))))
136 "Expression evaluating to the name of the calendar location.
137 For example, \"New York City\". The default value is just the
138 variable `calendar-latitude' paired with the variable `calendar-longitude'.
140 This variable should be set in `site-start'.el."
144 (defcustom solar-error
0.5
145 "Tolerance (in minutes) for sunrise/sunset calculations.
147 A larger value makes the calculations for sunrise/sunset faster, but less
148 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
149 times will be correct to the minute.
151 It is useless to set the value smaller than 4*delta, where delta is the
152 accuracy in the longitude of the sun (given by the function
153 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
154 delta. At present, delta = 0.01 degrees, so the value of the variable
155 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
159 (defcustom diary-sabbath-candles-minutes
18
160 "Number of minutes before sunset for sabbath candle lighting."
166 ;;; End of user options.
169 (defconst solar-n-hemi-seasons
170 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
171 "List of season changes for the northern hemisphere.")
173 (defconst solar-s-hemi-seasons
174 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
175 "List of season changes for the southern hemisphere.")
177 (defvar solar-sidereal-time-greenwich-midnight
179 "Sidereal time at Greenwich at midnight (universal time).")
181 (defvar solar-northern-spring-or-summer-season nil
182 "Non-nil if northern spring or summer and nil otherwise.
183 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
186 (defsubst calendar-latitude
()
187 "Ensure the variable `calendar-latitude' is a signed decimal fraction."
188 (if (numberp calendar-latitude
)
190 (let ((lat (+ (aref calendar-latitude
0)
191 (/ (aref calendar-latitude
1) 60.0))))
192 (if (equal (aref calendar-latitude
2) 'north
)
196 (defsubst calendar-longitude
()
197 "Ensure the variable `calendar-longitude' is a signed decimal fraction."
198 (if (numberp calendar-longitude
)
200 (let ((long (+ (aref calendar-longitude
0)
201 (/ (aref calendar-longitude
1) 60.0))))
202 (if (equal (aref calendar-longitude
2) 'east
)
206 (defun solar-get-number (prompt)
207 "Return a number from the minibuffer, prompting with PROMPT.
208 Returns nil if nothing was entered."
209 (let ((x (read-string prompt
"")))
210 (unless (string-equal x
"")
211 (string-to-number x
))))
213 (defun solar-setup ()
214 "Prompt for `calendar-longitude', `calendar-latitude', `calendar-time-zone'."
216 (or calendar-longitude
217 (setq calendar-longitude
219 "Enter longitude (decimal fraction; + east, - west): ")))
220 (or calendar-latitude
221 (setq calendar-latitude
223 "Enter latitude (decimal fraction; + north, - south): ")))
224 (or calendar-time-zone
225 (setq calendar-time-zone
227 "Enter difference from Coordinated Universal Time (in minutes): ")
230 (defun solar-sin-degrees (x)
231 "Return sin of X degrees."
232 (sin (degrees-to-radians (mod x
360.0))))
234 (defun solar-cosine-degrees (x)
235 "Return cosine of X degrees."
236 (cos (degrees-to-radians (mod x
360.0))))
238 (defun solar-tangent-degrees (x)
239 "Return tangent of X degrees."
240 (tan (degrees-to-radians (mod x
360.0))))
242 (defun solar-xy-to-quadrant (x y
)
243 "Determine the quadrant of the point X, Y."
248 (defun solar-degrees-to-quadrant (angle)
249 "Determine the quadrant of ANGLE degrees."
250 (1+ (floor (mod angle
360) 90)))
252 (defun solar-arctan (x quad
)
253 "Arctangent of X in quadrant QUAD."
254 (let ((deg (radians-to-degrees (atan x
))))
255 (cond ((equal quad
2) (+ deg
180))
256 ((equal quad
3) (+ deg
180))
257 ((equal quad
4) (+ deg
360))
260 (defun solar-atn2 (x y
)
261 "Arctangent of point X, Y."
264 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
266 (defun solar-arccos (x)
268 (let ((y (sqrt (- 1 (* x x
)))))
271 (defun solar-arcsin (y)
273 (let ((x (sqrt (- 1 (* y y
)))))
276 (defsubst solar-degrees-to-hours
(degrees)
277 "Convert DEGREES to hours."
280 (defsubst solar-hours-to-days
(hour)
281 "Convert HOUR to decimal fraction of a day."
284 (defun solar-right-ascension (longitude obliquity
)
285 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
286 Both arguments are in degrees."
287 (solar-degrees-to-hours
289 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
290 (solar-degrees-to-quadrant longitude
))))
292 (defun solar-declination (longitude obliquity
)
293 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
294 Both arguments are in degrees."
296 (* (solar-sin-degrees obliquity
)
297 (solar-sin-degrees longitude
))))
299 (defun solar-ecliptic-coordinates (time sunrise-flag
)
300 "Return solar longitude, ecliptic inclination, equation of time, nutation.
301 Values are for TIME in Julian centuries of Ephemeris Time since
302 January 1st, 2000, at 12 ET. Longitude and inclination are in
303 degrees, equation of time in hours, and nutation in seconds of longitude.
304 If SUNRISE-FLAG is non-nil, only calculate longitude and inclination."
305 (let* ((l (+ 280.46645
307 (* 0.0003032 time time
))) ; sun mean longitude
309 (* 481267.8813 time
))) ; moon mean longitude
312 (* -
0.0001559 time time
)
313 (* -
0.00000048 time time time
))) ; sun mean anomaly
314 (i (+ 23.43929111 (* -
0.013004167 time
)
315 (* -
0.00000016389 time time
)
316 (* 0.0000005036 time time time
))) ; mean inclination
319 (* -
0.000014 time time
))
320 (solar-sin-degrees m
))
321 (* (+ 0.019993 (* -
0.000101 time
))
322 (solar-sin-degrees (* 2 m
)))
324 (solar-sin-degrees (* 3 m
))))) ; center equation
325 (L (+ l c
)) ; total longitude
326 ;; Longitude of moon's ascending node on the ecliptic.
329 ;; nut = nutation in longitude, measured in seconds of angle.
330 (nut (unless sunrise-flag
331 (+ (* -
17.20 (solar-sin-degrees omega
))
332 (* -
1.32 (solar-sin-degrees (* 2 l
)))
333 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
334 (* 0.21 (solar-sin-degrees (* 2 omega
))))))
335 (ecc (unless sunrise-flag
; eccentricity of earth's orbit
337 (* -
0.000042037 time
)
338 (* -
0.0000001236 time time
))))
339 (app (+ L
; apparent longitude of sun
342 (solar-sin-degrees omega
))))
343 (y (unless sunrise-flag
344 (* (solar-tangent-degrees (/ i
2))
345 (solar-tangent-degrees (/ i
2)))))
346 ;; Equation of time, in hours.
347 (time-eq (unless sunrise-flag
348 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
349 (* -
2 ecc
(solar-sin-degrees m
))
350 (* 4 ecc y
(solar-sin-degrees m
)
351 (solar-cosine-degrees (* 2 l
)))
352 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
353 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
355 (list app i time-eq nut
)))
357 (defun solar-ephemeris-correction (year)
358 "Ephemeris time minus Universal Time during Gregorian YEAR.
359 Result is in days. For the years 1800-1987, the maximum error is
360 1.9 seconds. For the other years, the maximum error is about 30 seconds."
361 (cond ((and (<= 1988 year
) (< year
2020))
362 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
363 ((and (<= 1900 year
) (< year
1988))
364 (let* ((theta (/ (- (calendar-astro-from-absolute
365 (calendar-absolute-from-gregorian
367 (calendar-astro-from-absolute
368 (calendar-absolute-from-gregorian
371 (theta2 (* theta theta
))
372 (theta3 (* theta2 theta
))
373 (theta4 (* theta2 theta2
))
374 (theta5 (* theta3 theta2
)))
381 (* 0.677066 theta3 theta3
)
382 (* -
0.212591 theta4 theta3
))))
383 ((and (<= 1800 year
) (< year
1900))
384 (let* ((theta (/ (- (calendar-astro-from-absolute
385 (calendar-absolute-from-gregorian
387 (calendar-astro-from-absolute
388 (calendar-absolute-from-gregorian
391 (theta2 (* theta theta
))
392 (theta3 (* theta2 theta
))
393 (theta4 (* theta2 theta2
))
394 (theta5 (* theta3 theta2
)))
401 (* 31.332267 theta3 theta3
)
402 (* 38.291999 theta4 theta3
)
403 (* 28.316289 theta4 theta4
)
404 (* 11.636204 theta4 theta5
)
405 (* 2.043794 theta5 theta5
))))
406 ((and (<= 1620 year
) (< year
1800))
407 (let ((x (/ (- year
1600) 10.0)))
408 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
409 (t (let* ((tmp (- (calendar-astro-from-absolute
410 (calendar-absolute-from-gregorian
413 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
414 (/ second
60.0 60.0 24.0)))))
416 (defun solar-ephemeris-time (time)
417 "Ephemeris Time at moment TIME.
418 TIME is a pair with the first component being the number of Julian centuries
419 elapsed at 0 Universal Time, and the second component being the universal
420 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
421 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
422 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
424 Result is in Julian centuries of ephemeris time."
425 (let* ((t0 (car time
))
427 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
428 (y (+ 2000 (* 100 t1
)))
429 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
430 (+ t1
(/ (/ dt
86400) 36525))))
432 (defun solar-equatorial-coordinates (time sunrise-flag
)
433 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
434 TIME is a pair with the first component being the number of
435 Julian centuries elapsed at 0 Universal Time, and the second
436 component being the universal time. For instance, the pair
437 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
438 -0.040945 being the number of Julian centuries elapsed between
439 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG is passed
440 to `solar-ecliptic-coordinates'."
441 (let ((ec (solar-ecliptic-coordinates (solar-ephemeris-time time
)
443 (list (solar-right-ascension (car ec
) (cadr ec
))
444 (solar-declination (car ec
) (cadr ec
)))))
446 (defun solar-horizontal-coordinates (time latitude longitude sunrise-flag
)
447 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
448 TIME is a pair with the first component being the number of
449 Julian centuries elapsed at 0 Universal Time, and the second
450 component being the universal time. For instance, the pair
451 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
452 -0.040945 being the number of Julian centuries elapsed between
453 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG
454 is passed to `solar-ecliptic-coordinates'. Azimuth and
455 height (between -180 and 180) are both in degrees."
456 (let* ((ut (cadr time
))
457 (ec (solar-equatorial-coordinates time sunrise-flag
))
458 (st (+ solar-sidereal-time-greenwich-midnight
459 (* ut
1.00273790935)))
460 ;; Hour angle (in degrees).
461 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 (calendar-longitude))))
463 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah
)
464 (solar-sin-degrees latitude
))
465 (* (solar-tangent-degrees de
)
466 (solar-cosine-degrees latitude
)))
467 (solar-sin-degrees ah
)))
468 (height (solar-arcsin
469 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
470 (* (solar-cosine-degrees latitude
)
471 (solar-cosine-degrees de
)
472 (solar-cosine-degrees ah
))))))
473 (if (> height
180) (setq height
(- height
360)))
474 (list azimuth height
)))
476 (defun solar-moment (direction latitude longitude time height
)
477 "Sunrise/sunset at location.
478 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
481 TIME is a pair with the first component being the number of Julian centuries
482 elapsed at 0 Universal Time, and the second component being the universal
483 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
484 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
485 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
487 HEIGHT is the angle the center of the sun has over the horizon for the contact
488 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
489 accounting for the edge of the sun being on the horizon.
492 (let* ((ut (cadr time
))
493 (possible t
) ; we assume that rise or set are possible
494 (utmin (+ ut
(* direction
12.0)))
495 (utmax ut
) ; the time searched is between utmin and utmax
496 ;; utmin and utmax are in hours.
497 (utmoment-old 0.0) ; rise or set approximation
498 (utmoment 1.0) ; rise or set approximation
499 (hut 0) ; sun height at utmoment
501 (hmin (cadr (solar-horizontal-coordinates (list t0 utmin
)
502 latitude longitude t
)))
503 (hmax (cadr (solar-horizontal-coordinates (list t0 utmax
)
504 latitude longitude t
))))
505 ;; -0.61 degrees is the height of the middle of the sun, when it
509 (while ;;; (< i 20) ; we perform a simple dichotomy
510 ;;; (> (abs (- hut height)) epsilon)
511 (>= (abs (- utmoment utmoment-old
))
513 (setq utmoment-old utmoment
514 utmoment
(/ (+ utmin utmax
) 2)
515 hut
(cadr (solar-horizontal-coordinates
516 (list t0 utmoment
) latitude longitude t
)))
517 (if (< hut height
) (setq utmin utmoment
))
518 (if (> hut height
) (setq utmax utmoment
)))
519 (setq possible nil
)) ; the sun never rises
520 (setq possible nil
)) ; the sun never sets
521 (if possible utmoment
)))
523 (defun solar-sunrise-and-sunset (time latitude longitude height
)
524 "Sunrise, sunset and length of day.
525 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
527 TIME is a pair with the first component being the number of Julian centuries
528 elapsed at 0 Universal Time, and the second component being the universal
529 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
530 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
531 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
533 HEIGHT is the angle the center of the sun has over the horizon for the contact
534 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
535 accounting for the edge of the sun being on the horizon.
537 Coordinates are included because this function is called with latitude=1
538 degrees to find out if polar regions have 24 hours of sun or only night."
539 (let ((rise-time (solar-moment -
1 latitude longitude time height
))
540 (set-time (solar-moment 1 latitude longitude time height
))
542 (if (not (and rise-time set-time
))
543 (if (or (and (> latitude
0)
544 solar-northern-spring-or-summer-season
)
546 (not solar-northern-spring-or-summer-season
)))
549 (setq day-length
(- set-time rise-time
)))
550 (list (if rise-time
(+ rise-time
(/ calendar-time-zone
60.0)) nil
)
551 (if set-time
(+ set-time
(/ calendar-time-zone
60.0)) nil
)
554 (defun solar-time-string (time time-zone
)
555 "Printable form for decimal fraction TIME in TIME-ZONE.
556 Format used is given by `calendar-time-display-form'."
557 (let* ((time (round (* 60 time
)))
558 (24-hours (/ time
60))
559 (minutes (format "%02d" (% time
60)))
560 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
561 (am-pm (if (>= 24-hours
12) "pm" "am"))
562 (24-hours (format "%02d" 24-hours
)))
563 (mapconcat 'eval calendar-time-display-form
"")))
565 (defun solar-daylight (time)
566 "Printable form for TIME expressed in hours."
569 (floor (* 60 (- time
(floor time
))))))
571 (defun solar-julian-ut-centuries (date)
572 "Number of Julian centuries since 1 Jan, 2000 at noon UT for Gregorian DATE."
573 (/ (- (calendar-absolute-from-gregorian date
)
574 (calendar-absolute-from-gregorian '(1 1.5 2000)))
577 (defun solar-date-to-et (date ut
)
578 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
579 Expressed in Julian centuries of Ephemeris Time."
580 (solar-ephemeris-time (list (solar-julian-ut-centuries date
) ut
)))
582 (defun solar-time-equation (date ut
)
583 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
584 (nth 2 (solar-ecliptic-coordinates (solar-date-to-et date ut
) nil
)))
586 (defun solar-exact-local-noon (date)
587 "Date and Universal Time of local noon at *local date* DATE.
588 The date may be different from the one asked for, but it will be the right
589 local date. The second component of date should be an integer."
591 (ut (- 12.0 (/ (calendar-longitude) 15)))
592 (te (solar-time-equation date ut
)))
595 (setq nd
(list (car date
) (1+ (cadr date
))
599 (setq nd
(list (car date
) (1- (cadr date
))
602 (setq nd
(calendar-gregorian-from-absolute ; date standardization
603 (calendar-absolute-from-gregorian nd
)))
606 (defun solar-sidereal-time (t0)
607 "Sidereal time (in hours) in Greenwich at T0 Julian centuries.
608 T0 must correspond to 0 hours UT."
609 (let* ((mean-sid-time (+ 6.6973746
611 (* 0.0000258622 t0 t0
)
612 (* -
0.0000000017222 t0 t0 t0
)))
613 (et (solar-ephemeris-time (list t0
0.0)))
614 (nut-i (solar-ecliptic-coordinates et nil
))
615 (nut (nth 3 nut-i
)) ; nutation
616 (i (cadr nut-i
))) ; inclination
617 (mod (+ (mod (+ mean-sid-time
618 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
622 (defun solar-sunrise-sunset (date)
623 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
624 Corresponding value is nil if there is no sunrise/sunset."
625 ;; First, get the exact moment of local noon.
626 (let* ((exact-local-noon (solar-exact-local-noon date
))
627 ;; Get the time from the 2000 epoch.
628 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
629 ;; Store the sidereal time at Greenwich at midnight of UT time.
630 ;; Find if summer or winter slightly above the equator.
632 (progn (setq solar-sidereal-time-greenwich-midnight
633 (solar-sidereal-time t0
))
634 (solar-sunrise-and-sunset
635 (list t0
(cadr exact-local-noon
))
637 (calendar-longitude) 0)))
638 ;; Store the spring/summer information, compute sunrise and
639 ;; sunset (two first components of rise-set). Length of day
640 ;; is the third component (it is only the difference between
641 ;; sunset and sunrise when there is a sunset and a sunrise)
644 (setq solar-northern-spring-or-summer-season
645 (> (nth 2 equator-rise-set
) 12))
646 (solar-sunrise-and-sunset
647 (list t0
(cadr exact-local-noon
))
649 (calendar-longitude) -
0.61)))
650 (rise-time (car rise-set
))
651 (adj-rise (if rise-time
(dst-adjust-time date rise-time
)))
652 (set-time (cadr rise-set
))
653 (adj-set (if set-time
(dst-adjust-time date set-time
)))
654 (length (nth 2 rise-set
)))
656 (and rise-time
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
657 (and set-time
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
658 (solar-daylight length
))))
660 (defun solar-sunrise-sunset-string (date)
661 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
662 (let ((l (solar-sunrise-sunset date
)))
664 "%s, %s at %s (%s hours daylight)"
666 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
669 (concat "sunset " (apply 'solar-time-string
(cadr l
)))
671 (eval calendar-location-name
)
674 (defconst solar-data-list
675 '((403406 4.721964 1.621043)
676 (195207 5.937458 62830.348067)
677 (119433 1.115589 62830.821524)
678 (112392 5.781616 62829.634302)
679 (3891 5.5474 125660.5691)
680 (2819 1.5120 125660.984)
681 (1721 4.1897 62832.4766)
683 (660 5.415 125659.31)
686 (314 5.198 777137.715)
687 (268 5.989 78604.191)
689 (234 1.423 39302.098)
691 (132 2.317 115067.698)
692 (129 3.193 15774.337)
725 "Data used for calculation of solar longitude.")
727 (defun solar-longitude (d)
728 "Longitude of sun on astronomical (Julian) day number D.
729 Accuracy is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
730 The values of `calendar-daylight-savings-starts',
731 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
732 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
733 `calendar-time-zone' are used to interpret local time."
734 (let* ((a-d (calendar-absolute-from-astro d
))
735 ;; Get Universal Time.
736 (date (calendar-astro-from-absolute
738 (if (dst-in-effect a-d
)
739 (/ calendar-daylight-time-offset
24.0 60.0) 0)
740 (/ calendar-time-zone
60.0 24.0))))
741 ;; Get Ephemeris Time.
742 (date (+ date
(solar-ephemeris-correction
743 (extract-calendar-year
744 (calendar-gregorian-from-absolute
746 (calendar-absolute-from-astro
748 (U (/ (- date
2451545) 3652500))
762 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
763 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 pi
)))
764 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 pi
)))
765 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
766 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
768 (defun solar-date-next-longitude (d l
)
769 "First time after day D when solar longitude is a multiple of L degrees.
770 D is a Julian day number. L must be an integer divisor of 360.
771 The result is for `calendar-location-name', and is in local time
772 \(including any daylight saving rules) expressed in astronomical (Julian)
773 day numbers. The values of `calendar-daylight-savings-starts',
774 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
775 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset',
776 and `calendar-time-zone' are used to interpret local time."
779 (start-long (solar-longitude d
))
780 (next (mod (* l
(1+ (floor (/ start-long l
)))) 360))
781 (end (+ d
(* (/ l
360.0) 400)))
782 (end-long (solar-longitude end
)))
783 (while ; bisection search for nearest minute
784 (< 0.00001 (- end start
))
786 ;; start-long <= next < end-long when next != 0
787 ;; when next = 0, we look for the discontinuity (start-long is near 360
788 ;; and end-long is small (less than l).
789 (setq d
(/ (+ start end
) 2.0)
790 long
(solar-longitude d
))
791 (if (or (and (not (zerop next
)) (< long next
))
792 (and (zerop next
) (< l long
)))
797 (/ (+ start end
) 2.0)))
800 (defun sunrise-sunset (&optional arg
)
801 "Local time of sunrise and sunset for today. Accurate to a few seconds.
802 If called with an optional prefix argument ARG, prompt for date.
803 If called with an optional double prefix argument, prompt for
804 longitude, latitude, time zone, and date, and always use standard time.
806 This function is suitable for execution in a .emacs file."
808 (or arg
(setq arg
1))
810 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
812 (let* ((calendar-longitude
813 (if (< arg
16) calendar-longitude
815 "Enter longitude (decimal fraction; + east, - west): ")))
817 (if (< arg
16) calendar-latitude
819 "Enter latitude (decimal fraction; + north, - south): ")))
821 (if (< arg
16) calendar-time-zone
823 "Enter difference from Coordinated Universal Time (in minutes): ")))
824 (calendar-location-name
825 (if (< arg
16) calendar-location-name
826 (let ((float-output-format "%.1f"))
828 (if (numberp calendar-latitude
)
829 (abs calendar-latitude
)
830 (+ (aref calendar-latitude
0)
831 (/ (aref calendar-latitude
1) 60.0)))
832 (if (numberp calendar-latitude
)
833 (if (> calendar-latitude
0) "N" "S")
834 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
835 (if (numberp calendar-longitude
)
836 (abs calendar-longitude
)
837 (+ (aref calendar-longitude
0)
838 (/ (aref calendar-longitude
1) 60.0)))
839 (if (numberp calendar-longitude
)
840 (if (> calendar-longitude
0) "E" "W")
841 (if (equal (aref calendar-longitude
2) 'east
)
843 (calendar-standard-time-zone-name
844 (if (< arg
16) calendar-standard-time-zone-name
845 (cond ((zerop calendar-time-zone
) "UTC")
846 ((< calendar-time-zone
0)
847 (format "UTC%dmin" calendar-time-zone
))
848 (t (format "UTC+%dmin" calendar-time-zone
)))))
849 (calendar-daylight-savings-starts
850 (if (< arg
16) calendar-daylight-savings-starts
))
851 (calendar-daylight-savings-ends
852 (if (< arg
16) calendar-daylight-savings-ends
))
853 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
854 (date-string (calendar-date-string date t
))
855 (time-string (solar-sunrise-sunset-string date
))
856 (msg (format "%s: %s" date-string time-string
))
857 (one-window (one-window-p t
)))
858 (if (<= (length msg
) (frame-width))
860 (with-output-to-temp-buffer "*temp*"
861 (princ (concat date-string
"\n" time-string
)))
863 (substitute-command-keys
866 "Type \\[delete-other-windows] to remove temp window."
867 "Type \\[switch-to-buffer] RET to remove temp window.")
868 "Type \\[switch-to-buffer-other-window] RET to restore old \
869 contents of temp window."))))))
872 (defun calendar-sunrise-sunset ()
873 "Local time of sunrise and sunset for date under cursor.
874 Accurate to a few seconds."
876 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
878 (let ((date (calendar-cursor-to-date t
)))
880 (calendar-date-string date t t
)
881 (solar-sunrise-sunset-string date
))))
885 ;; To be called from list-sexp-diary-entries, where DATE is bound.
887 (defun diary-sunrise-sunset ()
888 "Local time of sunrise and sunset as a diary entry.
889 Accurate to a few seconds."
890 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
892 (solar-sunrise-sunset-string date
))
894 ;; To be called from list-sexp-diary-entries, where DATE is bound.
896 (defun diary-sabbath-candles (&optional mark
)
897 "Local time of candle lighting diary entry--applies if date is a Friday.
898 No diary entry if there is no sunset on that date.
900 An optional parameter MARK specifies a face or single-character string to
901 use when highlighting the day in the calendar."
902 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
904 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5) ; Friday
905 (let* ((sunset (cadr (solar-sunrise-sunset date
)))
907 (cons (- (car sunset
)
908 (/ diary-sabbath-candles-minutes
60.0))
912 (format "%s Sabbath candle lighting"
913 (apply 'solar-time-string light
)))))))
915 ;; From Meeus, 1991, page 167.
916 (defconst solar-seasons-data
917 '((485 324.96 1934.136)
918 (203 337.23 32964.467)
920 (182 27.85 445267.112)
921 (156 73.14 45036.886)
922 (136 171.52 22518.443)
923 (77 222.54 65928.934)
926 (58 119.81 33718.147)
929 (45 247.54 29929.562)
930 (44 325.15 31555.956)
932 (18 155.12 67555.328)
934 (16 198.04 62894.029)
935 (14 199.76 31436.921)
937 (12 287.11 31931.756)
938 (12 320.81 34777.259)
941 "Data for solar equinox/solstice calculations.")
943 (defun solar-equinoxes/solstices
(k year
)
944 "Date of equinox/solstice K for YEAR.
945 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
946 K=3, winter solstice. RESULT is a Gregorian local date.
947 Accurate to within a minute between 1951 and 2050."
948 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
949 (T (/ (- JDE0
2451545.0) 36525))
950 (W (- (* 35999.373 T
) 2.47))
951 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
952 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
953 (S (apply '+ (mapcar (lambda(x)
954 (* (car x
) (solar-cosine-degrees
955 (+ (* (nth 2 x
) T
) (cadr x
)))))
956 solar-seasons-data
)))
957 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
958 ;; Ephemeris time correction.
959 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
960 (JD (- JDE
(/ correction
86400)))
961 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
962 (time (- (- JD
0.5) (floor (- JD
0.5)))))
963 (list (car date
) (+ (cadr date
) time
964 (/ (/ calendar-time-zone
60.0) 24.0))
967 ;; From Meeus, 1991, page 166.
968 (defun solar-mean-equinoxes/solstices
(k year
)
969 "Julian day of mean equinox/solstice K for YEAR.
970 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
971 solstice. These formulae are only to be used between 1000 BC and 3000 AD."
972 (let ((y (/ year
1000.0))
973 (z (/ (- year
2000) 1000.0)))
974 (if (< year
1000) ; actually between -1000 and 1000
975 (cond ((equal k
0) (+ 1721139.29189
979 (* -
0.00071 y y y y
)))
980 ((equal k
1) (+ 1721233.25401
984 (* 0.00025 y y y y
)))
985 ((equal k
2) (+ 1721325.70455
989 (* 0.00074 y y y y
)))
990 ((equal k
3) (+ 1721414.39987
994 (* -
0.00006 y y y y
))))
995 ; actually between 1000 and 3000
996 (cond ((equal k
0) (+ 2451623.80984
1000 (* -
0.00057 z z z z
)))
1001 ((equal k
1) (+ 2451716.56767
1005 (* -
0.00030 z z z z
)))
1006 ((equal k
2) (+ 2451810.21715
1010 (* 0.00078 z z z z
)))
1011 ((equal k
3) (+ 2451900.05952
1015 (* 0.00032 z z z z
)))))))
1017 (defvar displayed-month
) ; from generate-calendar
1018 (defvar displayed-year
)
1020 ;;;###holiday-autoload
1021 (defun solar-equinoxes-solstices ()
1022 "Local date and time of equinoxes and solstices, if visible in the calendar.
1023 Requires floating point."
1024 (let ((m displayed-month
)
1026 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
1029 (let* ((calendar-standard-time-zone-name
1030 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1031 (calendar-daylight-savings-starts
1032 (if calendar-time-zone calendar-daylight-savings-starts
))
1033 (calendar-daylight-savings-ends
1034 (if calendar-time-zone calendar-daylight-savings-ends
))
1035 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1037 (d0 (solar-equinoxes/solstices k y
))
1038 (d1 (list (car d0
) (floor (cadr d0
)) (nth 2 d0
)))
1039 (h0 (* 24 (- (cadr d0
) (floor (cadr d0
)))))
1040 (adj (dst-adjust-time d1 h0
))
1043 (/ (cadr adj
) 24.0))
1045 ;; The following is nearly as accurate, but not quite:
1046 ;; (d0 (solar-date-next-longitude
1047 ;; (calendar-astro-from-absolute
1048 ;; (calendar-absolute-from-gregorian
1049 ;; (list (+ 3 (* k 3)) 15 y)))
1051 ;; (abs-day (calendar-absolute-from-astro d)))
1052 (abs-day (calendar-absolute-from-gregorian d
)))
1054 (list (calendar-gregorian-from-absolute (floor abs-day
))
1056 (nth k
(if (and calendar-latitude
1057 (< (calendar-latitude) 0))
1058 solar-s-hemi-seasons
1059 solar-n-hemi-seasons
))
1061 (* 24 (- abs-day
(floor abs-day
)))
1062 (if (dst-in-effect abs-day
)
1063 calendar-daylight-time-zone-name
1064 calendar-standard-time-zone-name
))))))))
1069 ;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1070 ;;; solar.el ends here