1 ;;; solar.el --- calendar functions for solar events
3 ;; Copyright (C) 1992-1993, 1995, 1997, 2001-2017 Free Software
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
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
30 ;; See calendar.el. This file implements features that deal with
31 ;; times of day, sunrise/sunset, and equinoxes/solstices.
33 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
34 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
35 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
36 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
37 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
38 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
43 ;; 1. Sunrise/sunset times will be accurate to the minute for years
44 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
46 ;; 2. Equinox/solstice times will be accurate to the minute for years
47 ;; 1951--2050. For other years the times will be within +/- 1 minute.
53 ;; calendar-astro-to-absolute and v versa are cal-autoloads.
54 ;;;(require 'cal-julian)
57 (defcustom calendar-time-display-form
58 '(12-hours ":" minutes am-pm
59 (if time-zone
" (") time-zone
(if time-zone
")"))
60 "The pseudo-pattern that governs the way a time of day is formatted.
62 A pseudo-pattern is a list of expressions that can involve the keywords
63 `12-hours', `24-hours', and `minutes', all numbers in string form,
64 and `am-pm' and `time-zone', both alphabetic strings.
68 (24-hours \":\" minutes
69 (if time-zone \" (\") time-zone (if time-zone \")\"))
71 would give military-style times like \"21:07 (UTC)\"."
76 (defcustom calendar-latitude nil
77 "Latitude of `calendar-location-name' in degrees.
78 The value can be either a decimal fraction (one place of accuracy is
79 sufficient), + north, - south, such as 40.7 for New York City, or the value
80 can be a vector [degrees minutes north/south] such as [40 50 north] for New
83 This variable should be set in `site-start'.el."
84 :type
'(choice (const nil
)
86 (vector :value
[0 0 north
]
87 (integer :tag
"Degrees")
88 (integer :tag
"Minutes")
89 (choice :tag
"Position"
94 (defcustom calendar-longitude nil
95 "Longitude of `calendar-location-name' in degrees.
96 The value can be either a decimal fraction (one place of accuracy is
97 sufficient), + east, - west, such as -73.9 for New York City, or the value
98 can be a vector [degrees minutes east/west] such as [73 55 west] for New
101 This variable should be set in `site-start'.el."
102 :type
'(choice (const nil
)
103 (number :tag
"Exact")
104 (vector :value
[0 0 west
]
105 (integer :tag
"Degrees")
106 (integer :tag
"Minutes")
107 (choice :tag
"Position"
112 (defcustom calendar-location-name
113 '(let ((float-output-format "%.1f"))
115 (if (numberp calendar-latitude
)
116 (abs calendar-latitude
)
117 (+ (aref calendar-latitude
0)
118 (/ (aref calendar-latitude
1) 60.0)))
119 (if (numberp calendar-latitude
)
120 (if (> calendar-latitude
0) "N" "S")
121 (if (eq (aref calendar-latitude
2) 'north
) "N" "S"))
122 (if (numberp calendar-longitude
)
123 (abs calendar-longitude
)
124 (+ (aref calendar-longitude
0)
125 (/ (aref calendar-longitude
1) 60.0)))
126 (if (numberp calendar-longitude
)
127 (if (> calendar-longitude
0) "E" "W")
128 (if (eq (aref calendar-longitude
2) 'east
) "E" "W"))))
129 "Expression evaluating to the name of the calendar location.
130 For example, \"New York City\". The default value is just the
131 variable `calendar-latitude' paired with the variable `calendar-longitude'.
133 This variable should be set in `site-start'.el."
138 (defcustom solar-error
0.5
139 "Tolerance (in minutes) for sunrise/sunset calculations.
141 A larger value makes the calculations for sunrise/sunset faster, but less
142 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
143 times will be correct to the minute.
145 It is useless to set the value smaller than 4*delta, where delta is the
146 accuracy in the longitude of the sun (given by the function
147 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
148 delta. At present, delta = 0.01 degrees, so the value of the variable
149 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
153 (defcustom solar-n-hemi-seasons
154 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
155 "List of season changes for the northern hemisphere."
157 (string :tag
"Vernal Equinox")
158 (string :tag
"Summer Solstice")
159 (string :tag
"Autumnal Equinox")
160 (string :tag
"Winter Solstice"))
163 (defcustom solar-s-hemi-seasons
164 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
165 "List of season changes for the southern hemisphere."
167 (string :tag
"Autumnal Equinox")
168 (string :tag
"Winter Solstice")
169 (string :tag
"Vernal Equinox")
170 (string :tag
"Summer Solstice"))
173 ;;; End of user options.
175 (defvar solar-sidereal-time-greenwich-midnight nil
176 "Sidereal time at Greenwich at midnight (Universal Time).")
178 (defvar solar-northern-spring-or-summer-season nil
179 "Non-nil if northern spring or summer and nil otherwise.
180 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
183 (defsubst calendar-latitude
()
184 "Ensure the variable `calendar-latitude' is a signed decimal fraction."
185 (if (numberp calendar-latitude
)
187 (let ((lat (+ (aref calendar-latitude
0)
188 (/ (aref calendar-latitude
1) 60.0))))
189 (if (eq (aref calendar-latitude
2) 'north
)
193 (defsubst calendar-longitude
()
194 "Ensure the variable `calendar-longitude' is a signed decimal fraction."
195 (if (numberp calendar-longitude
)
197 (let ((long (+ (aref calendar-longitude
0)
198 (/ (aref calendar-longitude
1) 60.0))))
199 (if (eq (aref calendar-longitude
2) 'east
)
203 (defun solar-get-number (prompt)
204 "Return a number from the minibuffer, prompting with PROMPT.
205 Returns nil if nothing was entered."
206 (let ((x (read-string prompt
"")))
207 (unless (string-equal x
"")
208 (string-to-number x
))))
210 (defun solar-setup ()
211 "Prompt for `calendar-longitude', `calendar-latitude', `calendar-time-zone'."
213 (or calendar-longitude
214 (setq calendar-longitude
216 "Enter longitude (decimal fraction; + east, - west): ")))
217 (or calendar-latitude
218 (setq calendar-latitude
220 "Enter latitude (decimal fraction; + north, - south): ")))
221 (or calendar-time-zone
222 (setq calendar-time-zone
224 "Enter difference from Coordinated Universal Time (in minutes): ")
227 (defun solar-sin-degrees (x)
228 "Return sin of X degrees."
229 (sin (degrees-to-radians (mod x
360.0))))
231 (defun solar-cosine-degrees (x)
232 "Return cosine of X degrees."
233 (cos (degrees-to-radians (mod x
360.0))))
235 (defun solar-tangent-degrees (x)
236 "Return tangent of X degrees."
237 (tan (degrees-to-radians (mod x
360.0))))
239 (defun solar-xy-to-quadrant (x y
)
240 "Determine the quadrant of the point X, Y."
245 (defun solar-degrees-to-quadrant (angle)
246 "Determine the quadrant of ANGLE degrees."
247 (1+ (floor (mod angle
360) 90)))
249 (defun solar-arctan (x quad
)
250 "Arctangent of X in quadrant QUAD."
251 (let ((deg (radians-to-degrees (atan x
))))
252 (cond ((= quad
2) (+ deg
180))
253 ((= quad
3) (+ deg
180))
254 ((= quad
4) (+ deg
360))
257 (defun solar-atn2 (x y
)
258 "Arctangent of point X, Y."
261 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
263 (defun solar-arccos (x)
265 (let ((y (sqrt (- 1 (* x x
)))))
268 (defun solar-arcsin (y)
270 (let ((x (sqrt (- 1 (* y y
)))))
273 (defsubst solar-degrees-to-hours
(degrees)
274 "Convert DEGREES to hours."
277 (defsubst solar-hours-to-days
(hour)
278 "Convert HOUR to decimal fraction of a day."
281 (defun solar-right-ascension (longitude obliquity
)
282 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
283 Both arguments are in degrees."
284 (solar-degrees-to-hours
286 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
287 (solar-degrees-to-quadrant longitude
))))
289 (defun solar-declination (longitude obliquity
)
290 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
291 Both arguments are in degrees."
293 (* (solar-sin-degrees obliquity
)
294 (solar-sin-degrees longitude
))))
296 (defun solar-ecliptic-coordinates (time sunrise-flag
)
297 "Return solar longitude, ecliptic inclination, equation of time, nutation.
298 Values are for TIME in Julian centuries of Ephemeris Time since
299 January 1st, 2000, at 12 ET. Longitude and inclination are in
300 degrees, equation of time in hours, and nutation in seconds of longitude.
301 If SUNRISE-FLAG is non-nil, only calculate longitude and inclination."
302 (let* ((l (+ 280.46645
304 (* 0.0003032 time time
))) ; sun mean longitude
306 (* 481267.8813 time
))) ; moon mean longitude
309 (* -
0.0001559 time time
)
310 (* -
0.00000048 time time time
))) ; sun mean anomaly
311 (i (+ 23.43929111 (* -
0.013004167 time
)
312 (* -
0.00000016389 time time
)
313 (* 0.0000005036 time time time
))) ; mean inclination
316 (* -
0.000014 time time
))
317 (solar-sin-degrees m
))
318 (* (+ 0.019993 (* -
0.000101 time
))
319 (solar-sin-degrees (* 2 m
)))
321 (solar-sin-degrees (* 3 m
))))) ; center equation
322 (L (+ l c
)) ; total longitude
323 ;; Longitude of moon's ascending node on the ecliptic.
326 ;; nut = nutation in longitude, measured in seconds of angle.
327 (nut (unless sunrise-flag
328 (+ (* -
17.20 (solar-sin-degrees omega
))
329 (* -
1.32 (solar-sin-degrees (* 2 l
)))
330 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
331 (* 0.21 (solar-sin-degrees (* 2 omega
))))))
332 (ecc (unless sunrise-flag
; eccentricity of earth's orbit
334 (* -
0.000042037 time
)
335 (* -
0.0000001236 time time
))))
336 (app (+ L
; apparent longitude of sun
339 (solar-sin-degrees omega
))))
340 (y (unless sunrise-flag
341 (* (solar-tangent-degrees (/ i
2))
342 (solar-tangent-degrees (/ i
2)))))
343 ;; Equation of time, in hours.
344 (time-eq (unless sunrise-flag
345 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
346 (* -
2 ecc
(solar-sin-degrees m
))
347 (* 4 ecc y
(solar-sin-degrees m
)
348 (solar-cosine-degrees (* 2 l
)))
349 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
350 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
352 (list app i time-eq nut
)))
354 (defun solar-ephemeris-correction (year)
355 "Ephemeris time minus Universal Time during Gregorian YEAR.
356 Result is in days. For the years 1800-1987, the maximum error is
357 1.9 seconds. For the other years, the maximum error is about 30 seconds."
358 (cond ((and (<= 1988 year
) (< year
2020))
359 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
360 ((and (<= 1900 year
) (< year
1988))
361 (let* ((theta (/ (- (calendar-astro-from-absolute
362 (calendar-absolute-from-gregorian
364 (calendar-astro-from-absolute
365 (calendar-absolute-from-gregorian
368 (theta2 (* theta theta
))
369 (theta3 (* theta2 theta
))
370 (theta4 (* theta2 theta2
))
371 (theta5 (* theta3 theta2
)))
378 (* 0.677066 theta3 theta3
)
379 (* -
0.212591 theta4 theta3
))))
380 ((and (<= 1800 year
) (< year
1900))
381 (let* ((theta (/ (- (calendar-astro-from-absolute
382 (calendar-absolute-from-gregorian
384 (calendar-astro-from-absolute
385 (calendar-absolute-from-gregorian
388 (theta2 (* theta theta
))
389 (theta3 (* theta2 theta
))
390 (theta4 (* theta2 theta2
))
391 (theta5 (* theta3 theta2
)))
398 (* 31.332267 theta3 theta3
)
399 (* 38.291999 theta4 theta3
)
400 (* 28.316289 theta4 theta4
)
401 (* 11.636204 theta4 theta5
)
402 (* 2.043794 theta5 theta5
))))
403 ((and (<= 1620 year
) (< year
1800))
404 (let ((x (/ (- year
1600) 10.0)))
405 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
406 (t (let* ((tmp (- (calendar-astro-from-absolute
407 (calendar-absolute-from-gregorian
410 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
411 (/ second
60.0 60.0 24.0)))))
413 (defun solar-ephemeris-time (time)
414 "Ephemeris Time at moment TIME.
415 TIME is a pair with the first component being the number of Julian centuries
416 elapsed at 0 Universal Time, and the second component counting Universal Time
417 hours. For instance, the pair corresponding to November 28, 1995 at 16 UT is
418 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
419 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
421 Result is in Julian centuries of ephemeris time."
422 (let* ((t0 (car time
))
424 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
425 (y (+ 2000 (* 100 t1
)))
426 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
427 (+ t1
(/ (/ dt
86400) 36525))))
429 (defun solar-equatorial-coordinates (time sunrise-flag
)
430 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
431 TIME is a pair with the first component being the number of
432 Julian centuries elapsed at 0 Universal Time, and the second
433 component counting Universal Time hours. For instance, the pair
434 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
435 -0.040945 being the number of Julian centuries elapsed between
436 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG is passed
437 to `solar-ecliptic-coordinates'."
438 (let ((ec (solar-ecliptic-coordinates (solar-ephemeris-time time
)
440 (list (solar-right-ascension (car ec
) (cadr ec
))
441 (solar-declination (car ec
) (cadr ec
)))))
443 (defun solar-horizontal-coordinates (time latitude longitude sunrise-flag
)
444 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
445 TIME is a pair with the first component being the number of
446 Julian centuries elapsed at 0 Universal Time, and the second
447 component counting Universal Time hours. For instance, the pair
448 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
449 -0.040945 being the number of Julian centuries elapsed between
450 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG
451 is passed to `solar-ecliptic-coordinates'. Azimuth and
452 height (between -180 and 180) are both in degrees."
453 (let* ((ut (cadr time
))
454 (ec (solar-equatorial-coordinates time sunrise-flag
))
455 (st (+ solar-sidereal-time-greenwich-midnight
456 (* ut
1.00273790935)))
457 ;; Hour angle (in degrees).
458 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 longitude
)))
460 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah
)
461 (solar-sin-degrees latitude
))
462 (* (solar-tangent-degrees de
)
463 (solar-cosine-degrees latitude
)))
464 (solar-sin-degrees ah
)))
465 (height (solar-arcsin
466 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
467 (* (solar-cosine-degrees latitude
)
468 (solar-cosine-degrees de
)
469 (solar-cosine-degrees ah
))))))
470 (if (> height
180) (setq height
(- height
360)))
471 (list azimuth height
)))
473 (defun solar-moment (direction latitude longitude time height
)
474 "Sunrise/sunset at location.
475 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
478 TIME is a pair with the first component being the number of Julian centuries
479 elapsed at 0 Universal Time, and the second component counting Universal Time
480 hours. For instance, the pair corresponding to November 28, 1995 at 16 UT is
481 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
482 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
484 HEIGHT is the angle the center of the sun has over the horizon for the contact
485 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
486 accounting for the edge of the sun being on the horizon.
489 (let* ((ut (cadr time
))
490 (possible t
) ; we assume that rise or set are possible
491 (utmin (+ ut
(* direction
12.0)))
492 (utmax ut
) ; the time searched is between utmin and utmax
493 ;; utmin and utmax are in hours.
494 (utmoment-old 0.0) ; rise or set approximation
495 (utmoment 1.0) ; rise or set approximation
496 (hut 0) ; sun height at utmoment
498 (hmin (cadr (solar-horizontal-coordinates (list t0 utmin
)
499 latitude longitude t
)))
500 (hmax (cadr (solar-horizontal-coordinates (list t0 utmax
)
501 latitude longitude t
))))
502 ;; -0.61 degrees is the height of the middle of the sun, when it
506 (while ;;; (< i 20) ; we perform a simple dichotomy
507 ;;; (> (abs (- hut height)) epsilon)
508 (>= (abs (- utmoment utmoment-old
))
510 (setq utmoment-old utmoment
511 utmoment
(/ (+ utmin utmax
) 2)
512 hut
(cadr (solar-horizontal-coordinates
513 (list t0 utmoment
) latitude longitude t
)))
514 (if (< hut height
) (setq utmin utmoment
))
515 (if (> hut height
) (setq utmax utmoment
)))
516 (setq possible nil
)) ; the sun never rises
517 (setq possible nil
)) ; the sun never sets
518 (if possible utmoment
)))
520 (defun solar-sunrise-and-sunset (time latitude longitude height
)
521 "Sunrise, sunset and length of day.
522 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
524 TIME is a pair with the first component being the number of Julian centuries
525 elapsed at 0 Universal Time, and the second component counting Universal Time
526 hours. For instance, the pair corresponding to November 28, 1995 at 16 UT is
527 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
528 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
530 HEIGHT is the angle the center of the sun has over the horizon for the contact
531 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
532 accounting for the edge of the sun being on the horizon.
534 Coordinates are included because this function is called with latitude=1
535 degrees to find out if polar regions have 24 hours of sun or only night."
536 (let ((rise-time (solar-moment -
1 latitude longitude time height
))
537 (set-time (solar-moment 1 latitude longitude time height
))
539 (if (not (and rise-time set-time
))
540 (if (or (and (> latitude
0)
541 solar-northern-spring-or-summer-season
)
543 (not solar-northern-spring-or-summer-season
)))
546 (setq day-length
(- set-time rise-time
)))
547 (list (if rise-time
(+ rise-time
(/ calendar-time-zone
60.0)) nil
)
548 (if set-time
(+ set-time
(/ calendar-time-zone
60.0)) nil
)
551 (defun solar-time-string (time time-zone
)
552 "Printable form for decimal fraction TIME in TIME-ZONE.
553 Format used is given by `calendar-time-display-form'."
554 (let* ((time (round (* 60 time
)))
555 (24-hours (/ time
60))
556 (minutes (format "%02d" (% time
60)))
557 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
558 (am-pm (if (>= 24-hours
12) "pm" "am"))
559 (24-hours (format "%02d" 24-hours
)))
560 (mapconcat 'eval calendar-time-display-form
"")))
562 (defun solar-daylight (time)
563 "Printable form for TIME expressed in hours."
566 (floor (* 60 (- time
(floor time
))))))
568 (defun solar-julian-ut-centuries (date)
569 "Number of Julian centuries since 1 Jan, 2000 at noon UT for Gregorian DATE."
570 (/ (- (calendar-absolute-from-gregorian date
)
571 (calendar-absolute-from-gregorian '(1 1.5 2000)))
574 (defun solar-date-to-et (date ut
)
575 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
576 Expressed in Julian centuries of Ephemeris Time."
577 (solar-ephemeris-time (list (solar-julian-ut-centuries date
) ut
)))
579 (defun solar-time-equation (date ut
)
580 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
581 (nth 2 (solar-ecliptic-coordinates (solar-date-to-et date ut
) nil
)))
583 (defun solar-exact-local-noon (date)
584 "Date and Universal Time of local noon at *local date* DATE.
585 The date may be different from the one asked for, but it will be the right
586 local date. The second component of date should be an integer."
588 (ut (- 12.0 (/ (calendar-longitude) 15)))
589 (te (solar-time-equation date ut
)))
592 (setq nd
(list (car date
) (1+ (cadr date
))
596 (setq nd
(list (car date
) (1- (cadr date
))
599 (setq nd
(calendar-gregorian-from-absolute ; date standardization
600 (calendar-absolute-from-gregorian nd
)))
603 (defun solar-sidereal-time (t0)
604 "Sidereal time (in hours) in Greenwich at T0 Julian centuries.
605 T0 must correspond to 0 hours UT."
606 (let* ((mean-sid-time (+ 6.6973746
608 (* 0.0000258622 t0 t0
)
609 (* -
0.0000000017222 t0 t0 t0
)))
610 (et (solar-ephemeris-time (list t0
0.0)))
611 (nut-i (solar-ecliptic-coordinates et nil
))
612 (nut (nth 3 nut-i
)) ; nutation
613 (i (cadr nut-i
))) ; inclination
614 (mod (+ (mod (+ mean-sid-time
615 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
619 (defun solar-sunrise-sunset (date)
620 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
621 Corresponding value is nil if there is no sunrise/sunset."
622 ;; First, get the exact moment of local noon.
623 (let* ((exact-local-noon (solar-exact-local-noon date
))
624 ;; Get the time from the 2000 epoch.
625 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
626 ;; Store the sidereal time at Greenwich at midnight of UT time.
627 ;; Find if summer or winter slightly above the equator.
629 (progn (setq solar-sidereal-time-greenwich-midnight
630 (solar-sidereal-time t0
))
631 (solar-sunrise-and-sunset
632 (list t0
(cadr exact-local-noon
))
634 (calendar-longitude) 0)))
635 ;; Store the spring/summer information, compute sunrise and
636 ;; sunset (two first components of rise-set). Length of day
637 ;; is the third component (it is only the difference between
638 ;; sunset and sunrise when there is a sunset and a sunrise)
641 (setq solar-northern-spring-or-summer-season
642 (> (nth 2 equator-rise-set
) 12))
643 (solar-sunrise-and-sunset
644 (list t0
(cadr exact-local-noon
))
646 (calendar-longitude) -
0.61)))
647 (rise-time (car rise-set
))
648 (adj-rise (if rise-time
(dst-adjust-time date rise-time
)))
649 (set-time (cadr rise-set
))
650 (adj-set (if set-time
(dst-adjust-time date set-time
)))
651 (length (nth 2 rise-set
)))
653 (and rise-time
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
654 (and set-time
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
655 (solar-daylight length
))))
657 (defun solar-sunrise-sunset-string (date &optional nolocation
)
658 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
659 Optional NOLOCATION non-nil means do not print the location."
660 (let ((l (solar-sunrise-sunset date
)))
662 "%s, %s%s (%s hrs daylight)"
664 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
667 (concat "sunset " (apply 'solar-time-string
(cadr l
)))
670 (format " at %s" (eval calendar-location-name
)))
673 (defconst solar-data-list
674 '((403406 4.721964 1.621043)
675 (195207 5.937458 62830.348067)
676 (119433 1.115589 62830.821524)
677 (112392 5.781616 62829.634302)
678 (3891 5.5474 125660.5691)
679 (2819 1.5120 125660.984)
680 (1721 4.1897 62832.4766)
682 (660 5.415 125659.31)
685 (314 5.198 777137.715)
686 (268 5.989 78604.191)
688 (234 1.423 39302.098)
690 (132 2.317 115067.698)
691 (129 3.193 15774.337)
724 "Data used for calculation of solar longitude.")
726 (defun solar-longitude (d)
727 "Longitude of sun on astronomical (Julian) day number D.
728 Accuracy is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
729 The values of `calendar-daylight-savings-starts',
730 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
731 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
732 `calendar-time-zone' are used to interpret local time."
733 (let* ((a-d (calendar-astro-to-absolute d
))
734 ;; Get Universal Time.
735 (date (calendar-astro-from-absolute
737 (if (dst-in-effect a-d
)
738 (/ calendar-daylight-time-offset
24.0 60.0) 0)
739 (/ calendar-time-zone
60.0 24.0))))
740 ;; Get Ephemeris Time.
741 (date (+ date
(solar-ephemeris-correction
742 (calendar-extract-year
743 (calendar-gregorian-from-absolute
745 (calendar-astro-to-absolute
747 (U (/ (- date
2451545) 3652500))
761 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
762 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 float-pi
)))
763 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 float-pi
)))
764 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
765 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
767 (defun solar-date-next-longitude (d l
)
768 "First time after day D when solar longitude is a multiple of L degrees.
769 D is a Julian day number. L must be an integer divisor of 360.
770 The result is for `calendar-location-name', and is in local time
771 \(including any daylight saving rules) expressed in astronomical (Julian)
772 day numbers. The values of `calendar-daylight-savings-starts',
773 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
774 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset',
775 and `calendar-time-zone' are used to interpret local time."
777 (next (mod (* l
(1+ (floor (/ (solar-longitude d
) l
)))) 360))
778 (end (+ d
(* (/ l
360.0) 400)))
780 ;; Bisection search for nearest minute.
781 (while (< 0.00001 (- end start
))
783 ;; start-long <= next < end-long when next != 0
784 ;; when next = 0, look for the discontinuity (start-long is near 360
785 ;; and end-long is small (less than l)).
786 (setq d
(/ (+ start end
) 2.0)
787 long
(solar-longitude d
))
788 (if (or (and (not (zerop next
)) (< long next
))
789 (and (zerop next
) (< l long
)))
792 (/ (+ start end
) 2.0)))
794 ;; FIXME but there already is solar-sunrise-sunset.
796 (defun sunrise-sunset (&optional arg
)
797 "Local time of sunrise and sunset for today. Accurate to a few seconds.
798 If called with an optional prefix argument ARG, prompt for date.
799 If called with an optional double prefix argument, prompt for
800 longitude, latitude, time zone, and date, and always use standard time.
802 This function is suitable for execution in an init file."
804 (or arg
(setq arg
1))
806 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
808 (let* ((calendar-longitude
809 (if (< arg
16) calendar-longitude
811 "Enter longitude (decimal fraction; + east, - west): ")))
813 (if (< arg
16) calendar-latitude
815 "Enter latitude (decimal fraction; + north, - south): ")))
817 (if (< arg
16) calendar-time-zone
819 "Enter difference from Coordinated Universal Time (in minutes): ")))
820 (calendar-location-name
821 (if (< arg
16) calendar-location-name
822 (let ((float-output-format "%.1f"))
824 (if (numberp calendar-latitude
)
825 (abs calendar-latitude
)
826 (+ (aref calendar-latitude
0)
827 (/ (aref calendar-latitude
1) 60.0)))
828 (if (numberp calendar-latitude
)
829 (if (> calendar-latitude
0) "N" "S")
830 (if (eq (aref calendar-latitude
2) 'north
) "N" "S"))
831 (if (numberp calendar-longitude
)
832 (abs calendar-longitude
)
833 (+ (aref calendar-longitude
0)
834 (/ (aref calendar-longitude
1) 60.0)))
835 (if (numberp calendar-longitude
)
836 (if (> calendar-longitude
0) "E" "W")
837 (if (eq (aref calendar-longitude
2) 'east
)
839 (calendar-standard-time-zone-name
840 (if (< arg
16) calendar-standard-time-zone-name
841 (cond ((zerop calendar-time-zone
) "UTC")
842 ((< calendar-time-zone
0)
843 (format "UTC%dmin" calendar-time-zone
))
844 (t (format "UTC+%dmin" calendar-time-zone
)))))
845 (calendar-daylight-savings-starts
846 (if (< arg
16) calendar-daylight-savings-starts
))
847 (calendar-daylight-savings-ends
848 (if (< arg
16) calendar-daylight-savings-ends
))
849 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
850 (date-string (calendar-date-string date t
))
851 (time-string (solar-sunrise-sunset-string date
))
853 (if (< arg
4) "" ; don't print date if it's today's
854 (format "%s: " date-string
))
860 (defun calendar-sunrise-sunset (&optional event
)
861 "Local time of sunrise and sunset for date under cursor.
862 Accurate to a few seconds."
863 (interactive (list last-nonmenu-event
))
864 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
866 (let ((date (calendar-cursor-to-date t event
)))
868 (calendar-date-string date t t
)
869 (solar-sunrise-sunset-string date
))))
872 (defun calendar-sunrise-sunset-month (&optional event
)
873 "Local time of sunrise and sunset for month under cursor or at EVENT."
874 (interactive (list last-nonmenu-event
))
875 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
877 (let* ((date (calendar-cursor-to-date t event
))
880 (last (calendar-last-day-of-month month year
))
881 (title (format "Sunrise/sunset times for %s %d at %s"
882 (calendar-month-name month
) year
883 (eval calendar-location-name
))))
884 (calendar-in-read-only-buffer solar-sunrises-buffer
885 (calendar-set-mode-line title
)
886 (insert title
":\n\n")
888 (setq date
(list month
(1+ i
) year
))
889 (insert (format "%s %2d: " (calendar-month-name month t
) (1+ i
))
890 (solar-sunrise-sunset-string date t
) "\n")))))
894 ;; To be called from diary-list-sexp-entries, where DATE is bound.
896 (defun diary-sunrise-sunset ()
897 "Local time of sunrise and sunset as a diary entry.
898 Accurate to a few seconds."
899 (or (and calendar-latitude calendar-longitude calendar-time-zone
)
901 (solar-sunrise-sunset-string date
))
903 ;; From Meeus, 1991, page 167.
904 (defconst solar-seasons-data
905 '((485 324.96 1934.136)
906 (203 337.23 32964.467)
908 (182 27.85 445267.112)
909 (156 73.14 45036.886)
910 (136 171.52 22518.443)
911 (77 222.54 65928.934)
914 (58 119.81 33718.147)
917 (45 247.54 29929.562)
918 (44 325.15 31555.956)
920 (18 155.12 67555.328)
922 (16 198.04 62894.029)
923 (14 199.76 31436.921)
925 (12 287.11 31931.756)
926 (12 320.81 34777.259)
929 "Data for solar equinox/solstice calculations.")
931 (defun solar-equinoxes/solstices
(k year
)
932 "Date of equinox/solstice K for YEAR.
933 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
934 K=3, winter solstice. RESULT is a Gregorian local date.
935 Accurate to within a minute between 1951 and 2050."
936 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
937 (T (/ (- JDE0
2451545.0) 36525))
938 (W (- (* 35999.373 T
) 2.47))
939 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
940 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
941 (S (apply '+ (mapcar (lambda(x)
942 (* (car x
) (solar-cosine-degrees
943 (+ (* (nth 2 x
) T
) (cadr x
)))))
944 solar-seasons-data
)))
945 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
946 ;; Ephemeris time correction.
947 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
948 (JD (- JDE
(/ correction
86400)))
949 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
950 (time (- (- JD
0.5) (floor (- JD
0.5)))))
951 (list (car date
) (+ (cadr date
) time
952 (/ (/ calendar-time-zone
60.0) 24.0))
955 ;; From Meeus, 1991, page 166.
956 (defun solar-mean-equinoxes/solstices
(k year
)
957 "Julian day of mean equinox/solstice K for YEAR.
958 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
959 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
960 (let ((y (/ year
1000.0))
961 (z (/ (- year
2000) 1000.0)))
962 (if (< year
1000) ; actually between -1000 and 1000
963 (cond ((= k
0) (+ 1721139.29189
967 (* -
0.00071 y y y y
)))
968 ((= k
1) (+ 1721233.25401
972 (* 0.00025 y y y y
)))
973 ((= k
2) (+ 1721325.70455
977 (* 0.00074 y y y y
)))
978 ((= k
3) (+ 1721414.39987
982 (* -
0.00006 y y y y
))))
983 ; actually between 1000 and 3000
984 (cond ((= k
0) (+ 2451623.80984
988 (* -
0.00057 z z z z
)))
989 ((= k
1) (+ 2451716.56767
993 (* -
0.00030 z z z z
)))
994 ((= k
2) (+ 2451810.21715
998 (* 0.00078 z z z z
)))
999 ((= k
3) (+ 2451900.05952
1003 (* 0.00032 z z z z
)))))))
1005 (defvar displayed-month
) ; from calendar-generate
1006 (defvar displayed-year
)
1008 ;;;###holiday-autoload
1009 (defun solar-equinoxes-solstices ()
1010 "Local date and time of equinoxes and solstices, if visible in the calendar.
1011 Requires floating point."
1012 (let* ((m displayed-month
)
1014 (calendar-standard-time-zone-name
1015 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1016 (calendar-daylight-savings-starts
1017 (if calendar-time-zone calendar-daylight-savings-starts
))
1018 (calendar-daylight-savings-ends
1019 (if calendar-time-zone calendar-daylight-savings-ends
))
1020 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1022 (calendar-increment-month m y
(cond ((= 1 (% m
3)) -
1)
1026 (d0 (solar-equinoxes/solstices k y
))
1027 (d1 (list (car d0
) (floor (cadr d0
)) (nth 2 d0
)))
1028 (h0 (* 24 (- (cadr d0
) (floor (cadr d0
)))))
1029 (adj (dst-adjust-time d1 h0
))
1032 (/ (cadr adj
) 24.0))
1034 ;; The following is nearly as accurate, but not quite:
1035 ;; (d0 (solar-date-next-longitude
1036 ;; (calendar-astro-from-absolute
1037 ;; (calendar-absolute-from-gregorian
1038 ;; (list (+ 3 (* k 3)) 15 y)))
1040 ;; (abs-day (calendar-astro-to-absolute d)))
1041 (abs-day (calendar-absolute-from-gregorian d
)))
1043 (list (calendar-gregorian-from-absolute (floor abs-day
))
1045 (nth k
(if (and calendar-latitude
1046 (< (calendar-latitude) 0))
1047 solar-s-hemi-seasons
1048 solar-n-hemi-seasons
))
1050 (* 24 (- abs-day
(floor abs-day
)))
1051 (if (dst-in-effect abs-day
)
1052 calendar-daylight-time-zone-name
1053 calendar-standard-time-zone-name
)))))))
1058 ;;; solar.el ends here