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,
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, or (at your option)
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; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
32 ;; This collection of functions implements the features of calendar.el,
33 ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
34 ;; equinoxes/solstices.
36 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
37 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
38 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
39 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
40 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
41 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
46 ;; 1. Sunrise/sunset times will be accurate to the minute for years
47 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
49 ;; 2. Equinox/solstice times will be accurate to the minute for years
50 ;; 1951--2050. For other years the times will be within +/- 1 minute.
52 ;; Technical details of all the calendrical calculations can be found in
53 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
54 ;; and Nachum Dershowitz, Cambridge University Press (2001).
59 (defvar displayed-month
)
60 (defvar displayed-year
)
63 (require 'lisp-float-type
)
64 (error "Solar calculations impossible since floating point is unavailable"))
70 (defcustom calendar-time-display-form
71 '(12-hours ":" minutes am-pm
72 (if time-zone
" (") time-zone
(if time-zone
")"))
73 "The pseudo-pattern that governs the way a time of day is formatted.
75 A pseudo-pattern is a list of expressions that can involve the keywords
76 `12-hours', `24-hours', and `minutes', all numbers in string form,
77 and `am-pm' and `time-zone', both alphabetic strings.
81 '(24-hours \":\" minutes
82 (if time-zone \" (\") time-zone (if time-zone \")\"))
84 would give military-style times like `21:07 (UTC)'."
88 (defcustom calendar-latitude nil
89 "Latitude of `calendar-location-name' in degrees.
91 The value can be either a decimal fraction (one place of accuracy is
92 sufficient), + north, - south, such as 40.7 for New York City, or the value
93 can be a vector [degrees minutes north/south] such as [40 50 north] for New
96 This variable should be set in `site-start'.el."
97 :type
'(choice (const nil
)
99 (vector :value
[0 0 north
]
100 (integer :tag
"Degrees")
101 (integer :tag
"Minutes")
102 (choice :tag
"Position"
107 (defcustom calendar-longitude nil
108 "Longitude of `calendar-location-name' in degrees.
110 The value can be either a decimal fraction (one place of accuracy is
111 sufficient), + east, - west, such as -73.9 for New York City, or the value
112 can be a vector [degrees minutes east/west] such as [73 55 west] for New
115 This variable should be set in `site-start'.el."
116 :type
'(choice (const nil
)
117 (number :tag
"Exact")
118 (vector :value
[0 0 west
]
119 (integer :tag
"Degrees")
120 (integer :tag
"Minutes")
121 (choice :tag
"Position"
126 (defcustom calendar-location-name
127 '(let ((float-output-format "%.1f"))
129 (if (numberp calendar-latitude
)
130 (abs calendar-latitude
)
131 (+ (aref calendar-latitude
0)
132 (/ (aref calendar-latitude
1) 60.0)))
133 (if (numberp calendar-latitude
)
134 (if (> calendar-latitude
0) "N" "S")
135 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
136 (if (numberp calendar-longitude
)
137 (abs calendar-longitude
)
138 (+ (aref calendar-longitude
0)
139 (/ (aref calendar-longitude
1) 60.0)))
140 (if (numberp calendar-longitude
)
141 (if (> calendar-longitude
0) "E" "W")
142 (if (equal (aref calendar-longitude
2) 'east
) "E" "W"))))
143 "Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
144 For example, \"New York City\". Default value is just the latitude, longitude
147 This variable should be set in `site-start'.el."
151 (defcustom solar-error
0.5
152 "Tolerance (in minutes) for sunrise/sunset calculations.
154 A larger value makes the calculations for sunrise/sunset faster, but less
155 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
156 times will be correct to the minute.
158 It is useless to set the value smaller than 4*delta, where delta is the
159 accuracy in the longitude of the sun (given by the function
160 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
161 delta. At present, delta = 0.01 degrees, so the value of the variable
162 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
166 (defcustom diary-sabbath-candles-minutes
18
167 "Number of minutes before sunset for sabbath candle lighting."
173 ;;; End of user options.
176 (defvar solar-n-hemi-seasons
177 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
178 "List of season changes for the northern hemisphere.")
180 (defvar solar-s-hemi-seasons
181 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
182 "List of season changes for the southern hemisphere.")
184 (defvar solar-sidereal-time-greenwich-midnight
186 "Sidereal time at Greenwich at midnight (universal time).")
188 (defvar solar-northern-spring-or-summer-season nil
189 "Non-nil if northern spring or summer and nil otherwise.
190 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
193 (defsubst calendar-latitude
()
194 "Convert calendar-latitude to a signed decimal fraction, if needed."
195 (if (numberp calendar-latitude
)
197 (let ((lat (+ (aref calendar-latitude
0)
198 (/ (aref calendar-latitude
1) 60.0))))
199 (if (equal (aref calendar-latitude
2) 'north
)
203 (defsubst calendar-longitude
()
204 "Convert calendar-longitude to a signed decimal fraction, if needed."
205 (if (numberp calendar-longitude
)
207 (let ((long (+ (aref calendar-longitude
0)
208 (/ (aref calendar-longitude
1) 60.0))))
209 (if (equal (aref calendar-longitude
2) 'east
)
213 (defun solar-setup ()
214 "Prompt user for latitude, longitude, and 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 \
230 (defun solar-get-number (prompt)
231 "Return a number from the minibuffer, prompting with PROMPT.
232 Returns nil if nothing was entered."
233 (let ((x (read-string prompt
"")))
234 (if (not (string-equal x
""))
235 (string-to-number x
))))
237 (defun solar-sin-degrees (x)
238 (sin (degrees-to-radians (mod x
360.0))))
240 (defun solar-cosine-degrees (x)
241 (cos (degrees-to-radians (mod x
360.0))))
243 (defun solar-tangent-degrees (x)
244 (tan (degrees-to-radians (mod x
360.0))))
246 (defun solar-xy-to-quadrant (x y
)
247 "Determines the quadrant of the point X, Y."
252 (defun solar-degrees-to-quadrant (angle)
253 "Determines the quadrant of ANGLE."
254 (1+ (floor (mod angle
360) 90)))
256 (defun solar-arctan (x quad
)
257 "Arctangent of X in quadrant QUAD."
258 (let ((deg (radians-to-degrees (atan x
))))
259 (cond ((equal quad
2) (+ deg
180))
260 ((equal quad
3) (+ deg
180))
261 ((equal quad
4) (+ deg
360))
264 (defun solar-atn2 (x y
)
265 "Arctan of point X, Y."
268 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
270 (defun solar-arccos (x)
272 (let ((y (sqrt (- 1 (* x x
)))))
275 (defun solar-arcsin (y)
277 (let ((x (sqrt (- 1 (* y y
)))))
280 (defsubst solar-degrees-to-hours
(degrees)
281 "Convert DEGREES to hours."
284 (defsubst solar-hours-to-days
(hour)
285 "Convert HOUR to decimal fraction of a day."
288 (defun solar-right-ascension (longitude obliquity
)
289 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
290 Both arguments are in degrees."
291 (solar-degrees-to-hours
293 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
294 (solar-degrees-to-quadrant longitude
))))
296 (defun solar-declination (longitude obliquity
)
297 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
298 Both arguments are in degrees."
300 (* (solar-sin-degrees obliquity
)
301 (solar-sin-degrees longitude
))))
303 (defun solar-sunrise-and-sunset (time latitude longitude height
)
304 "Sunrise, sunset and length of day.
305 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
307 TIME is a pair with the first component being the number of Julian centuries
308 elapsed at 0 Universal Time, and the second component being the universal
309 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
310 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
311 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
313 HEIGHT is the angle the center of the sun has over the horizon for the contact
314 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
315 accounting for the edge of the sun being on the horizon.
317 Coordinates are included because this function is called with latitude=1
318 degrees to find out if polar regions have 24 hours of sun or only night."
319 (let* ((rise-time (solar-moment -
1 latitude longitude time height
))
320 (set-time (solar-moment 1 latitude longitude time height
))
322 (if (not (and rise-time set-time
))
323 (if (or (and (> latitude
0)
324 solar-northern-spring-or-summer-season
)
326 (not solar-northern-spring-or-summer-season
)))
329 (setq day-length
(- set-time rise-time
)))
330 (list (if rise-time
(+ rise-time
(/ calendar-time-zone
60.0)) nil
)
331 (if set-time
(+ set-time
(/ calendar-time-zone
60.0)) nil
)
334 (defun solar-moment (direction latitude longitude time height
)
335 "Sunrise/sunset at location.
336 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
339 TIME is a pair with the first component being the number of Julian centuries
340 elapsed at 0 Universal Time, and the second component being the universal
341 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
342 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
343 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
345 HEIGHT is the angle the center of the sun has over the horizon for the contact
346 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
347 accounting for the edge of the sun being on the horizon.
350 (let* ((ut (car (cdr time
)))
351 (possible t
) ; we assume that rise or set are possible
352 (utmin (+ ut
(* direction
12.0)))
353 (utmax ut
) ; the time searched is between utmin and utmax
354 ; utmin and utmax are in hours
355 (utmoment-old 0.0) ; rise or set approximation
356 (utmoment 1.0) ; rise or set approximation
357 (hut 0) ; sun height at utmoment
360 (solar-horizontal-coordinates (list t0 utmin
)
361 latitude longitude t
))))
363 (solar-horizontal-coordinates (list t0 utmax
)
364 latitude longitude t
)))))
365 ; -0.61 degrees is the height of the middle of the sun, when it rises
369 (while ;(< i 20) ; we perform a simple dichotomy
370 ; (> (abs (- hut height)) epsilon)
371 (>= (abs (- utmoment utmoment-old
))
373 (setq utmoment-old utmoment
)
374 (setq utmoment
(/ (+ utmin utmax
) 2))
376 (solar-horizontal-coordinates
377 (list t0 utmoment
) latitude longitude t
))))
378 (if (< hut height
) (setq utmin utmoment
))
379 (if (> hut height
) (setq utmax utmoment
))
381 (setq possible nil
)) ; the sun never rises
382 (setq possible nil
)) ; the sun never sets
383 (if (not possible
) nil utmoment
)))
385 (defun solar-time-string (time time-zone
)
386 "Printable form for decimal fraction TIME in TIME-ZONE.
387 Format used is given by `calendar-time-display-form'."
388 (let* ((time (round (* 60 time
)))
389 (24-hours (/ time
60))
390 (minutes (format "%02d" (% time
60)))
391 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
392 (am-pm (if (>= 24-hours
12) "pm" "am"))
393 (24-hours (format "%02d" 24-hours
)))
394 (mapconcat 'eval calendar-time-display-form
"")))
397 (defun solar-daylight (time)
398 "Printable form for time expressed in hours."
401 (floor (* 60 (- time
(floor time
))))))
403 (defun solar-exact-local-noon (date)
404 "Date and Universal Time of local noon at *local date* date.
406 The date may be different from the one asked for, but it will be the right
407 local date. The second component of date should be an integer."
409 (ut (- 12.0 (/ (calendar-longitude) 15)))
410 (te (solar-time-equation date ut
)))
414 (setq nd
(list (car date
) (+ 1 (car (cdr date
)))
415 (car (cdr (cdr date
)))))
416 (setq ut
(- ut
24))))
419 (setq nd
(list (car date
) (- (car (cdr date
)) 1)
420 (car (cdr (cdr date
)))))
421 (setq ut
(+ ut
24))))
422 (setq nd
(calendar-gregorian-from-absolute
423 (calendar-absolute-from-gregorian nd
)))
424 ; date standardization
427 (defun solar-sunrise-sunset (date)
428 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
430 Corresponding value is nil if there is no sunrise/sunset."
431 (let* (; first, get the exact moment of local noon.
432 (exact-local-noon (solar-exact-local-noon date
))
433 ; get the time from the 2000 epoch.
434 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
435 ; store the sidereal time at Greenwich at midnight of UT time.
436 ; find if summer or winter slightly above the equator
438 (progn (setq solar-sidereal-time-greenwich-midnight
439 (solar-sidereal-time t0
))
440 (solar-sunrise-and-sunset
441 (list t0
(car (cdr exact-local-noon
)))
443 (calendar-longitude) 0)))
444 ; store the spring/summer information,
445 ; compute sunrise and sunset (two first components of rise-set).
446 ; length of day is the third component (it is only the difference
447 ; between sunset and sunrise when there is a sunset and a sunrise)
450 (setq solar-northern-spring-or-summer-season
451 (if (> (car (cdr (cdr equator-rise-set
))) 12) t nil
))
452 (solar-sunrise-and-sunset
453 (list t0
(car (cdr exact-local-noon
)))
455 (calendar-longitude) -
0.61)))
456 (rise (car rise-set
))
457 (adj-rise (if rise
(dst-adjust-time date rise
) nil
))
458 (set (car (cdr rise-set
)))
459 (adj-set (if set
(dst-adjust-time date set
) nil
))
460 (length (car (cdr (cdr rise-set
)))) )
462 (and rise
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
463 (and set
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
464 (solar-daylight length
))))
466 (defun solar-sunrise-sunset-string (date)
467 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
468 (let ((l (solar-sunrise-sunset date
)))
470 "%s, %s at %s (%s hours daylight)"
472 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
475 (concat "sunset " (apply 'solar-time-string
(car (cdr l
))))
477 (eval calendar-location-name
)
478 (car (cdr (cdr l
))))))
480 (defun solar-julian-ut-centuries (date)
481 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
482 (/ (- (calendar-absolute-from-gregorian date
)
483 (calendar-absolute-from-gregorian '(1 1.5 2000)))
486 (defun solar-ephemeris-time(time)
487 "Ephemeris Time at moment TIME.
489 TIME is a pair with the first component being the number of Julian centuries
490 elapsed at 0 Universal Time, and the second component being the universal
491 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
492 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
493 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
495 Result is in julian centuries of ephemeris time."
496 (let* ((t0 (car time
))
497 (ut (car (cdr time
)))
498 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
499 (y (+ 2000 (* 100 t1
)))
500 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
501 (+ t1
(/ (/ dt
86400) 36525))))
503 (defun solar-date-next-longitude (d l
)
504 "First moment on or after Julian day number D when sun's longitude is a
505 multiple of L degrees at calendar-location-name with that location's
506 local time (including any daylight saving rules).
508 L must be an integer divisor of 360.
510 Result is in local time expressed astronomical (Julian) day numbers.
512 The values of calendar-daylight-savings-starts,
513 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
514 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
515 calendar-time-zone are used to interpret local time."
518 (start-long (solar-longitude d
))
519 (next (mod (* l
(1+ (floor (/ start-long l
)))) 360))
520 (end (+ d
(* (/ l
360.0) 400)))
521 (end-long (solar-longitude end
)))
522 (while ;; bisection search for nearest minute
523 (< 0.00001 (- end start
))
525 ;; start-long <= next < end-long when next != 0
526 ;; when next = 0, we look for the discontinuity (start-long is near 360
527 ;; and end-long is small (less than l).
528 (setq d
(/ (+ start end
) 2.0))
529 (setq long
(solar-longitude d
))
530 (if (or (and (/= next
0) (< long next
))
531 (and (= next
0) (< l long
)))
534 (setq start-long long
))
536 (setq end-long long
)))
537 (/ (+ start end
) 2.0)))
539 (defun solar-horizontal-coordinates
540 (time latitude longitude for-sunrise-sunset
)
541 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
543 TIME is a pair with the first component being the number of Julian centuries
544 elapsed at 0 Universal Time, and the second component being the universal
545 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
546 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
547 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
549 The azimuth is given in degrees as well as the height (between -180 and 180)."
550 (let* ((ut (car (cdr time
)))
551 (ec (solar-equatorial-coordinates time for-sunrise-sunset
))
552 (st (+ solar-sidereal-time-greenwich-midnight
553 (* ut
1.00273790935)))
554 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 (calendar-longitude))))
555 ; hour angle (in degrees)
557 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah
)
558 (solar-sin-degrees latitude
))
559 (* (solar-tangent-degrees de
)
560 (solar-cosine-degrees latitude
)))
561 (solar-sin-degrees ah
)))
562 (height (solar-arcsin
563 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
564 (* (solar-cosine-degrees latitude
)
565 (solar-cosine-degrees de
)
566 (solar-cosine-degrees ah
))))))
567 (if (> height
180) (setq height
(- height
360)))
568 (list azimuth height
)))
570 (defun solar-equatorial-coordinates (time for-sunrise-sunset
)
571 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
573 TIME is a pair with the first component being the number of Julian centuries
574 elapsed at 0 Universal Time, and the second component being the universal
575 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
576 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
577 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
578 (let* ((tm (solar-ephemeris-time time
))
579 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset
)))
580 (list (solar-right-ascension (car ec
) (car (cdr ec
)))
581 (solar-declination (car ec
) (car (cdr ec
))))))
583 (defun solar-ecliptic-coordinates (time for-sunrise-sunset
)
584 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
585 equation of time (in hours) and nutation in longitude (in seconds)
586 at moment `time', expressed in julian centuries of Ephemeris Time
587 since January 1st, 2000, at 12 ET."
588 (let* ((l (+ 280.46645
590 (* 0.0003032 time time
))) ; sun mean longitude
592 (* 481267.8813 time
))) ; moon mean longitude
595 (* -
0.0001559 time time
)
596 (* -
0.00000048 time time time
))) ; sun mean anomaly
597 (i (+ 23.43929111 (* -
0.013004167 time
)
598 (* -
0.00000016389 time time
)
599 (* 0.0000005036 time time time
))); mean inclination
602 (* -
0.000014 time time
))
603 (solar-sin-degrees m
))
604 (* (+ 0.019993 (* -
0.000101 time
))
605 (solar-sin-degrees (* 2 m
)))
607 (solar-sin-degrees (* 3 m
))))) ; center equation
608 (L (+ l c
)) ; total longitude
610 (* -
1934.136 time
))) ; longitude of moon's ascending node
612 (nut (if (not for-sunrise-sunset
)
613 (+ (* -
17.20 (solar-sin-degrees omega
))
614 (* -
1.32 (solar-sin-degrees (* 2 l
)))
615 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
616 (* 0.21 (solar-sin-degrees (* 2 omega
))))
618 ; nut = nutation in longitude, measured in seconds of angle.
619 (ecc (if (not for-sunrise-sunset
)
621 (* -
0.000042037 time
)
622 (* -
0.0000001236 time time
)) ; eccentricity of earth's orbit
627 (solar-sin-degrees omega
)))) ; apparent longitude of sun
628 (y (if (not for-sunrise-sunset
)
629 (* (solar-tangent-degrees (/ i
2))
630 (solar-tangent-degrees (/ i
2)))
632 (time-eq (if (not for-sunrise-sunset
)
633 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
634 (* -
2 ecc
(solar-sin-degrees m
))
635 (* 4 ecc y
(solar-sin-degrees m
)
636 (solar-cosine-degrees (* 2 l
)))
637 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
638 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
641 ; equation of time, in hours
642 (list app i time-eq nut
)))
644 (defconst solar-data-list
645 '((403406 4.721964 1.621043)
646 (195207 5.937458 62830.348067)
647 (119433 1.115589 62830.821524)
648 (112392 5.781616 62829.634302)
649 (3891 5.5474 125660.5691)
650 (2819 1.5120 125660.984)
651 (1721 4.1897 62832.4766)
653 (660 5.415 125659.31)
656 (314 5.198 777137.715)
657 (268 5.989 78604.191)
659 (234 1.423 39302.098)
661 (132 2.317 115067.698)
662 (129 3.193 15774.337)
694 (10 2.55 157208.40)))
696 (defun solar-longitude (d)
697 "Longitude of sun on astronomical (Julian) day number D.
698 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
700 The values of calendar-daylight-savings-starts,
701 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
702 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
703 calendar-time-zone are used to interpret local time."
704 (let* ((a-d (calendar-absolute-from-astro d
))
705 ;; get Universal Time
706 (date (calendar-astro-from-absolute
708 (if (dst-in-effect a-d
)
709 (/ calendar-daylight-time-offset
24.0 60.0) 0)
710 (/ calendar-time-zone
60.0 24.0))))
711 ;; get Ephemeris Time
712 (date (+ date
(solar-ephemeris-correction
713 (extract-calendar-year
714 (calendar-gregorian-from-absolute
716 (calendar-absolute-from-astro
718 (U (/ (- date
2451545) 3652500))
728 (* (car (cdr (cdr x
))) U
))
732 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
733 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 pi
)))
734 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 pi
)))
735 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
736 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
738 (defun solar-ephemeris-correction (year)
739 "Ephemeris time minus Universal Time during Gregorian year.
742 For the years 1800-1987, the maximum error is 1.9 seconds.
743 For the other years, the maximum error is about 30 seconds."
744 (cond ((and (<= 1988 year
) (< year
2020))
745 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
746 ((and (<= 1900 year
) (< year
1988))
747 (let* ((theta (/ (- (calendar-astro-from-absolute
748 (calendar-absolute-from-gregorian
750 (calendar-astro-from-absolute
751 (calendar-absolute-from-gregorian
754 (theta2 (* theta theta
))
755 (theta3 (* theta2 theta
))
756 (theta4 (* theta2 theta2
))
757 (theta5 (* theta3 theta2
)))
764 (* 0.677066 theta3 theta3
)
765 (* -
0.212591 theta4 theta3
))))
766 ((and (<= 1800 year
) (< year
1900))
767 (let* ((theta (/ (- (calendar-astro-from-absolute
768 (calendar-absolute-from-gregorian
770 (calendar-astro-from-absolute
771 (calendar-absolute-from-gregorian
774 (theta2 (* theta theta
))
775 (theta3 (* theta2 theta
))
776 (theta4 (* theta2 theta2
))
777 (theta5 (* theta3 theta2
)))
784 (* 31.332267 theta3 theta3
)
785 (* 38.291999 theta4 theta3
)
786 (* 28.316289 theta4 theta4
)
787 (* 11.636204 theta4 theta5
)
788 (* 2.043794 theta5 theta5
))))
789 ((and (<= 1620 year
) (< year
1800))
790 (let ((x (/ (- year
1600) 10.0)))
791 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
792 (t (let* ((tmp (- (calendar-astro-from-absolute
793 (calendar-absolute-from-gregorian
796 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
797 (/ second
60.0 60.0 24.0)))))
799 (defun solar-sidereal-time (t0)
800 "Sidereal time (in hours) in Greenwich.
802 At T0=Julian centuries of universal time.
803 T0 must correspond to 0 hours UT."
804 (let* ((mean-sid-time (+ 6.6973746
806 (* 0.0000258622 t0 t0
)
807 (* -
0.0000000017222 t0 t0 t0
)))
808 (et (solar-ephemeris-time (list t0
0.0)))
809 (nut-i (solar-ecliptic-coordinates et nil
))
810 (nut (car (cdr (cdr (cdr nut-i
))))) ; nutation
811 (i (car (cdr nut-i
)))) ; inclination
812 (mod (+ (mod (+ mean-sid-time
813 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
817 (defun solar-time-equation (date ut
)
818 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
819 (let* ((et (solar-date-to-et date ut
))
820 (ec (solar-ecliptic-coordinates et nil
)))
821 (car (cdr (cdr ec
)))))
823 (defun solar-date-to-et (date ut
)
824 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
825 Expressed in julian centuries of Ephemeris Time."
826 (let ((t0 (solar-julian-ut-centuries date
)))
827 (solar-ephemeris-time (list t0 ut
))))
830 (defun sunrise-sunset (&optional arg
)
831 "Local time of sunrise and sunset for today. Accurate to a few seconds.
832 If called with an optional prefix argument, prompt for date.
834 If called with an optional double prefix argument, prompt for longitude,
835 latitude, time zone, and date, and always use standard time.
837 This function is suitable for execution in a .emacs file."
839 (or arg
(setq arg
1))
841 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
843 (let* ((calendar-longitude
844 (if (< arg
16) calendar-longitude
846 "Enter longitude (decimal fraction; + east, - west): ")))
848 (if (< arg
16) calendar-latitude
850 "Enter latitude (decimal fraction; + north, - south): ")))
852 (if (< arg
16) calendar-time-zone
854 "Enter difference from Coordinated Universal Time (in minutes): ")))
855 (calendar-location-name
856 (if (< arg
16) calendar-location-name
857 (let ((float-output-format "%.1f"))
859 (if (numberp calendar-latitude
)
860 (abs calendar-latitude
)
861 (+ (aref calendar-latitude
0)
862 (/ (aref calendar-latitude
1) 60.0)))
863 (if (numberp calendar-latitude
)
864 (if (> calendar-latitude
0) "N" "S")
865 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
866 (if (numberp calendar-longitude
)
867 (abs calendar-longitude
)
868 (+ (aref calendar-longitude
0)
869 (/ (aref calendar-longitude
1) 60.0)))
870 (if (numberp calendar-longitude
)
871 (if (> calendar-longitude
0) "E" "W")
872 (if (equal (aref calendar-longitude
2) 'east
)
874 (calendar-standard-time-zone-name
875 (if (< arg
16) calendar-standard-time-zone-name
876 (cond ((= calendar-time-zone
0) "UTC")
877 ((< calendar-time-zone
0)
878 (format "UTC%dmin" calendar-time-zone
))
879 (t (format "UTC+%dmin" calendar-time-zone
)))))
880 (calendar-daylight-savings-starts
881 (if (< arg
16) calendar-daylight-savings-starts
))
882 (calendar-daylight-savings-ends
883 (if (< arg
16) calendar-daylight-savings-ends
))
884 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
885 (date-string (calendar-date-string date t
))
886 (time-string (solar-sunrise-sunset-string date
))
887 (msg (format "%s: %s" date-string time-string
))
888 (one-window (one-window-p t
)))
889 (if (<= (length msg
) (frame-width))
891 (with-output-to-temp-buffer "*temp*"
892 (princ (concat date-string
"\n" time-string
)))
894 (substitute-command-keys
897 "Type \\[delete-other-windows] to remove temp window."
898 "Type \\[switch-to-buffer] RET to remove temp window.")
899 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
901 (defun calendar-sunrise-sunset ()
902 "Local time of sunrise and sunset for date under cursor.
903 Accurate to a few seconds."
905 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
907 (let ((date (calendar-cursor-to-date t
)))
909 (calendar-date-string date t t
)
910 (solar-sunrise-sunset-string date
))))
912 (defun diary-sunrise-sunset ()
913 "Local time of sunrise and sunset as a diary entry.
914 Accurate to a few seconds."
915 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
917 (solar-sunrise-sunset-string date
))
919 (defun diary-sabbath-candles (&optional mark
)
920 "Local time of candle lighting diary entry--applies if date is a Friday.
921 No diary entry if there is no sunset on that date.
923 An optional parameter MARK specifies a face or single-character string to
924 use when highlighting the day in the calendar."
925 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
927 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5);; Friday
928 (let* ((sunset (car (cdr (solar-sunrise-sunset date
))))
930 (cons (- (car sunset
)
931 (/ diary-sabbath-candles-minutes
60.0))
935 (format "%s Sabbath candle lighting"
936 (apply 'solar-time-string light
)))))))
938 ; from Meeus, 1991, page 167
939 (defconst solar-seasons-data
940 '((485 324.96 1934.136)
941 (203 337.23 32964.467)
943 (182 27.85 445267.112)
944 (156 73.14 45036.886)
945 (136 171.52 22518.443)
946 (77 222.54 65928.934)
949 (58 119.81 33718.147)
952 (45 247.54 29929.562)
953 (44 325.15 31555.956)
955 (18 155.12 67555.328)
957 (16 198.04 62894.029)
958 (14 199.76 31436.921)
960 (12 287.11 31931.756)
961 (12 320.81 34777.259)
963 (8 15.45 16859.074)))
965 (defun solar-equinoxes/solstices
(k year
)
966 "Date of equinox/solstice K for YEAR.
967 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
968 K=3, winter solstice.
969 RESULT is a gregorian local date.
971 Accurate to less than a minute between 1951 and 2050."
972 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
973 (T (/ (- JDE0
2451545.0) 36525))
974 (W (- (* 35999.373 T
) 2.47))
975 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
976 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
977 (S (apply '+ (mapcar (lambda(x)
978 (* (car x
) (solar-cosine-degrees
979 (+ (* (car (cdr (cdr x
))) T
)
981 solar-seasons-data
)))
982 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
983 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
984 ; ephemeris time correction
985 (JD (- JDE
(/ correction
86400)))
986 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
987 (time (- (- JD
0.5) (floor (- JD
0.5))))
989 (list (car date
) (+ (car (cdr date
)) time
990 (/ (/ calendar-time-zone
60.0) 24.0))
991 (car (cdr (cdr date
))))))
993 ; from Meeus, 1991, page 166
994 (defun solar-mean-equinoxes/solstices
(k year
)
995 "Julian day of mean equinox/solstice K for YEAR.
996 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
997 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
998 (let ((y (/ year
1000.0))
999 (z (/ (- year
2000) 1000.0)))
1000 (if (< year
1000) ; actually between -1000 and 1000
1001 (cond ((equal k
0) (+ 1721139.29189
1005 (* -
0.00071 y y y y
)))
1006 ((equal k
1) (+ 1721233.25401
1010 (* 0.00025 y y y y
)))
1011 ((equal k
2) (+ 1721325.70455
1015 (* 0.00074 y y y y
)))
1016 ((equal k
3) (+ 1721414.39987
1020 (* -
0.00006 y y y y
))))
1021 ; actually between 1000 and 3000
1022 (cond ((equal k
0) (+ 2451623.80984
1026 (* -
0.00057 z z z z
)))
1027 ((equal k
1) (+ 2451716.56767
1031 (* -
0.00030 z z z z
)))
1032 ((equal k
2) (+ 2451810.21715
1036 (* 0.00078 z z z z
)))
1037 ((equal k
3) (+ 2451900.05952
1041 (* 0.00032 z z z z
)))))))
1043 (defun solar-equinoxes-solstices ()
1044 "Local date and time of equinoxes and solstices, if visible in the calendar.
1045 Requires floating point."
1046 (let ((m displayed-month
)
1048 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
1051 (let* ((calendar-standard-time-zone-name
1052 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1053 (calendar-daylight-savings-starts
1054 (if calendar-time-zone calendar-daylight-savings-starts
))
1055 (calendar-daylight-savings-ends
1056 (if calendar-time-zone calendar-daylight-savings-ends
))
1057 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1059 (d0 (solar-equinoxes/solstices k y
))
1060 (d1 (list (car d0
) (floor (car (cdr d0
))) (car (cdr (cdr d0
)))))
1061 (h0 (* 24 (- (car (cdr d0
)) (floor (car (cdr d0
))))))
1062 (adj (dst-adjust-time d1 h0
))
1063 (d (list (car (car adj
))
1064 (+ (car (cdr (car adj
)) )
1065 (/ (car (cdr adj
)) 24.0))
1066 (car (cdr (cdr (car adj
))))))
1067 ; The following is nearly as accurate, but not quite:
1068 ;(d0 (solar-date-next-longitude
1069 ; (calendar-astro-from-absolute
1070 ; (calendar-absolute-from-gregorian
1071 ; (list (+ 3 (* k 3)) 15 y)))
1073 ;(abs-day (calendar-absolute-from-astro d)))
1074 (abs-day (calendar-absolute-from-gregorian d
)))
1076 (list (calendar-gregorian-from-absolute (floor abs-day
))
1078 (nth k
(if (and calendar-latitude
1079 (< (calendar-latitude) 0))
1080 solar-s-hemi-seasons
1081 solar-n-hemi-seasons
))
1083 (* 24 (- abs-day
(floor abs-day
)))
1084 (if (dst-in-effect abs-day
)
1085 calendar-daylight-time-zone-name
1086 calendar-standard-time-zone-name
))))))))
1091 ;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1092 ;;; solar.el ends here