1 ;;; solar.el --- calendar functions for solar events.
3 ;; Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Denis B. Roegel <Denis.Roegel@loria.fr>
8 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; This collection of functions implements the features of calendar.el,
30 ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
31 ;; 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.
49 ;; Comments, corrections, and improvements should be sent to
50 ;; Edward M. Reingold Department of Computer Science
51 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
52 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
53 ;; Urbana, Illinois 61801
58 (require 'lisp-float-type
)
59 (error "Solar/lunar calculations impossible since floating point is unavailable."))
65 (defvar 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)'.")
82 (defvar calendar-latitude nil
83 "*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-local.el.")
93 (defvar calendar-longitude nil
94 "*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.")
103 (defsubst calendar-latitude
()
104 "Convert calendar-latitude to a signed decimal fraction, if needed."
105 (if (numberp calendar-latitude
)
107 (let ((lat (+ (aref calendar-latitude
0)
108 (/ (aref calendar-latitude
1) 60.0))))
109 (if (equal (aref calendar-latitude
2) 'north
)
113 (defsubst calendar-longitude
()
114 "Convert calendar-longitude to a signed decimal fraction, if needed."
115 (if (numberp calendar-longitude
)
117 (let ((long (+ (aref calendar-longitude
0)
118 (/ (aref calendar-longitude
1) 60.0))))
119 (if (equal (aref calendar-longitude
2) 'east
)
124 (defvar calendar-location-name
125 '(let ((float-output-format "%.1f"))
127 (if (numberp calendar-latitude
)
128 (abs calendar-latitude
)
129 (+ (aref calendar-latitude
0)
130 (/ (aref calendar-latitude
1) 60.0)))
131 (if (numberp calendar-latitude
)
132 (if (> calendar-latitude
0) "N" "S")
133 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
134 (if (numberp calendar-longitude
)
135 (abs calendar-longitude
)
136 (+ (aref calendar-longitude
0)
137 (/ (aref calendar-longitude
1) 60.0)))
138 (if (numberp calendar-longitude
)
139 (if (> calendar-longitude
0) "E" "W")
140 (if (equal (aref calendar-longitude
2) 'east
) "E" "W"))))
141 "*Expression evaluating to name of `calendar-longitude', calendar-latitude'.
142 For example, \"New York City\". Default value is just the latitude, longitude
145 This variable should be set in site-start.el.")
147 (defvar solar-error
0.5
148 "*Tolerance (in minutes) for sunrise/sunset calculations.
150 A larger value makes the calculations for sunrise/sunset faster, but less
151 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
152 times will be correct to the minute.
154 It is useless to set the value smaller than 4*delta, where delta is the
155 accuracy in the longitude of the sun (given by the function
156 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
157 delta. At present, delta = 0.01 degrees, so the value of the variable
158 `solar-error' should be at least 0.04 minutes (about 2.5 seconds).")
160 (defvar solar-n-hemi-seasons
161 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
162 "List of season changes for the northern hemisphere.")
164 (defvar solar-s-hemi-seasons
165 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
166 "List of season changes for the southern hemisphere.")
168 (defvar solar-sidereal-time-greenwich-midnight
170 "Sidereal time at Greenwich at midnight (universal time).")
172 (defvar solar-spring-or-summer-season nil
173 "T if spring or summer and nil otherwise.
174 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
176 (defun solar-setup ()
177 "Prompt user for latitude, longitude, and time zone."
179 (if (not calendar-longitude
)
180 (setq calendar-longitude
182 "Enter longitude (decimal fraction; + east, - west): ")))
183 (if (not calendar-latitude
)
184 (setq calendar-latitude
186 "Enter latitude (decimal fraction; + north, - south): ")))
187 (if (not calendar-time-zone
)
188 (setq calendar-time-zone
190 "Enter difference from Coordinated Universal Time (in minutes): "))))
192 (defun solar-get-number (prompt)
193 "Return a number from the minibuffer, prompting with PROMPT.
194 Returns nil if nothing was entered."
195 (let ((x (read-string prompt
"")))
196 (if (not (string-equal x
""))
199 ;; The condition-case stuff is needed to catch bogus arithmetic
200 ;; exceptions that occur on some machines (like Sparcs)
201 (defun solar-sin-degrees (x)
203 (sin (degrees-to-radians (mod x
360.0)))
204 (solar-sin-degrees x
)))
205 (defun solar-cosine-degrees (x)
207 (cos (degrees-to-radians (mod x
360.0)))
208 (solar-cosine-degrees x
)))
209 (defun solar-tangent-degrees (x)
211 (tan (degrees-to-radians (mod x
360.0)))
212 (solar-tangent-degrees x
)))
214 (defun solar-xy-to-quadrant (x y
)
215 "Determines the quadrant of the point X, Y."
220 (defun solar-degrees-to-quadrant (angle)
221 "Determines the quadrant of ANGLE."
222 (1+ (floor (mod angle
360) 90)))
224 (defun solar-arctan (x quad
)
225 "Arctangent of X in quadrant QUAD."
226 (let ((deg (radians-to-degrees (atan x
))))
227 (cond ((equal quad
2) (+ deg
180))
228 ((equal quad
3) (+ deg
180))
229 ((equal quad
4) (+ deg
360))
232 (defun solar-atn2 (x y
)
233 "Arctan of point X, Y."
236 (solar-arctan (/ x y
) y
)))
238 (defun solar-arccos (x)
240 (let ((y (sqrt (- 1 (* x x
)))))
241 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
243 (defun solar-arcsin (y)
245 (let ((x (sqrt (- 1 (* y y
)))))
246 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
248 (defsubst solar-degrees-to-hours
(degrees)
249 "Convert DEGREES to hours."
252 (defsubst solar-hours-to-days
(hour)
253 "Convert HOUR to decimal fraction of a day."
256 (defun solar-right-ascension (longitude obliquity
)
257 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
258 Both arguments are in degrees."
259 (solar-degrees-to-hours
261 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
262 (solar-degrees-to-quadrant longitude
))))
264 (defun solar-declination (longitude obliquity
)
265 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
266 Both arguments are in degrees."
268 (* (solar-sin-degrees obliquity
)
269 (solar-sin-degrees longitude
))))
271 (defun solar-sunrise-and-sunset (time latitude longitude
)
272 "Sunrise, sunset and length of day.
273 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
275 TIME is a pair with the first component being the number of Julian centuries
276 elapsed at 0 Universal Time, and the second component being the universal
277 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
278 (-0.040945 16), -0.040945 being the number of julian centuries elapsed between
279 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
281 Coordinates are included because this function is called with latitude=10
282 degrees to find out if polar regions have 24 hours of sun or only night."
283 (let* ((rise-time (solar-moment -
1 latitude longitude time
))
284 (set-time (solar-moment 1 latitude longitude time
))
286 (if (not (and rise-time set-time
))
287 (if (or (and (> latitude
0) solar-spring-or-summer-season
)
288 (and (< latitude
0) (not solar-spring-or-summer-season
)))
291 (setq day-length
(- set-time rise-time
)))
292 (list (+ rise-time
(/ calendar-time-zone
60.0))
293 (+ set-time
(/ calendar-time-zone
60.0)) day-length
)))
295 (defun solar-moment (direction latitude longitude time
)
296 "Sunrise/sunset at location.
297 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
300 TIME is a pair with the first component being the number of Julian centuries
301 elapsed at 0 Universal Time, and the second component being the universal
302 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
303 (-0.040945 16), -0.040945 being the number of julian centuries elapsed between
304 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
307 (let* ((ut (car (cdr time
)))
308 (possible 1) ; we assume that rise or set are possible
309 (utmin (+ ut
(* direction
12.0)))
310 (utmax ut
) ; the time searched is between utmin and utmax
311 ; utmin and utmax are in hours
312 (utmoment-old 0.0) ; rise or set approximation
313 (utmoment 1.0) ; rise or set approximation
314 (hut 0) ; sun height at utmoment
317 (solar-horizontal-coordinates (list t0 utmin
)
318 latitude longitude t
))))
320 (solar-horizontal-coordinates (list t0 utmax
)
321 latitude longitude t
)))))
322 ; -0.61 degrees is the height of the middle of the sun, when it rises
326 (while ;(< i 20) ; we perform a simple dichotomy
327 ; (> (abs (+ hut 0.61)) epsilon)
328 (>= (abs (- utmoment utmoment-old
))
330 (setq utmoment-old utmoment
)
331 (setq utmoment
(/ (+ utmin utmax
) 2))
333 (solar-horizontal-coordinates
334 (list t0 utmoment
) latitude longitude t
))))
335 (if (< hut -
0.61) (setq utmin utmoment
))
336 (if (> hut -
0.61) (setq utmax utmoment
))
338 (setq possible
0)) the sun never rises
339 (setq possible
0)) ; the sun never sets
340 (if (equal possible
0) nil utmoment
)))
342 (defun solar-time-string (time time-zone
)
343 "Printable form for decimal fraction TIME in TIME-ZONE.
344 Format used is given by `calendar-time-display-form'."
345 (let* ((time (round (* 60 time
)))
346 (24-hours (/ time
60))
347 (minutes (format "%02d" (% time
60)))
348 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
349 (am-pm (if (>= 24-hours
12) "pm" "am"))
350 (24-hours (format "%02d" 24-hours
)))
351 (mapconcat 'eval calendar-time-display-form
"")))
354 (defun solar-daylight (time)
355 "Printable form for time expressed in hours."
358 (floor (* 60 (- time
(floor time
))))))
360 (defun solar-exact-local-noon (date)
361 "Date and Universal Time of local noon at *local date* date.
363 The date may be different from the one asked for, but it will be the right
364 local date. The second component of date should be an integer."
366 (ut (- 12.0 (/ calendar-longitude
15)))
367 (te (solar-time-equation date ut
)))
371 (setq nd
(list (car date
) (+ 1 (car (cdr date
)))
372 (car (cdr (cdr date
)))))
373 (setq ut
(- ut
24))))
376 (setq nd
(list (car date
) (- (car (cdr date
)) 1)
377 (car (cdr (cdr date
)))))
378 (setq ut
(+ ut
24))))
379 (setq nd
(calendar-gregorian-from-absolute
380 (calendar-absolute-from-gregorian nd
)))
381 ; date standardization
384 (defun solar-sunrise-sunset (date)
385 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
387 Corresponding value is nil if there is no sunrise/sunset."
388 (let* (; first, get the exact moment of local noon.
389 (exact-local-noon (solar-exact-local-noon date
))
390 ; get the the time from the 2000 epoch.
391 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
392 ; store the sidereal time at Greenwich at midnight of UT time.
393 ; find if summer or winter slightly above the equator
395 (progn (setq solar-sidereal-time-greenwich-midnight
396 (solar-sidereal-time t0
))
397 (solar-sunrise-and-sunset
398 (list t0
(car (cdr exact-local-noon
)))
400 calendar-longitude
)))
401 ; store the spring/summer information,
402 ; compute sunrise and sunset (two first components of rise-set).
403 ; length of day is the third component (it is only the difference
404 ; between sunset and sunrise when there is a sunset and a sunrise)
407 (setq solar-spring-or-summer-season
408 (if (> (car (cdr (cdr equator-rise-set
))) 12) 1 0))
409 (solar-sunrise-and-sunset
410 (list t0
(car (cdr exact-local-noon
)))
412 calendar-longitude
)))
413 (rise (car rise-set
))
414 (adj-rise (if rise
(dst-adjust-time date rise
) nil
))
415 (set (car (cdr rise-set
)))
416 (adj-set (if set
(dst-adjust-time date set
) nil
))
417 (length (car (cdr (cdr rise-set
)))) )
419 (and rise
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
420 (and set
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
421 (solar-daylight length
))))
423 (defun solar-sunrise-sunset-string (date)
424 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
425 (let ((l (solar-sunrise-sunset date
)))
427 "%s, %s at %s (%s hours daylight)"
429 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
432 (concat "sunset " (apply 'solar-time-string
(car (cdr l
))))
434 (eval calendar-location-name
)
435 (car (cdr (cdr l
))))))
437 (defun solar-julian-ut-centuries (date)
438 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
439 (/ (- (calendar-absolute-from-gregorian date
)
440 (calendar-absolute-from-gregorian '(1 1.5 2000)))
443 (defun solar-ephemeris-time(time)
444 "Ephemeris Time at moment TIME.
446 TIME is a pair with the first component being the number of Julian centuries
447 elapsed at 0 Universal Time, and the second component being the universal
448 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
449 (-0.040945 16), -0.040945 being the number of julian centuries elapsed between
450 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
452 Result is in julian centuries of ephemeris time."
453 (let* ((t0 (car time
))
454 (ut (car (cdr time
)))
455 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
456 (y (+ 2000 (* 100 t1
)))
457 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
458 (+ t1
(/ (/ dt
86400) 36525))))
460 (defun solar-date-next-longitude (d l
)
461 "First moment on or after Julian day number D when sun's longitude is a
462 multiple of L degrees at calendar-location-name with that location's
463 local time (including any daylight savings rules).
465 L must be an integer divisor of 360.
467 Result is in local time expressed astronomical (Julian) day numbers.
469 The values of calendar-daylight-savings-starts,
470 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
471 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
472 calendar-time-zone are used to interpret local time."
475 (start-long (solar-longitude d
))
476 (next (mod (* l
(1+ (floor (/ start-long l
)))) 360))
477 (end (+ d
(* (/ l
360.0) 400)))
478 (end-long (solar-longitude end
)))
479 (while ;; bisection search for nearest minute
480 (< 0.00001 (- end start
))
482 ;; start-long <= next < end-long when next != 0
483 ;; when next = 0, we look for the discontinuity (start-long is near 360
484 ;; and end-long is small (less than l).
485 (setq d
(/ (+ start end
) 2.0))
486 (setq long
(solar-longitude d
))
487 (if (or (and (/= next
0) (< long next
))
488 (and (= next
0) (< l long
)))
491 (setq start-long long
))
493 (setq end-long long
)))
494 (/ (+ start end
) 2.0)))
496 (defun solar-horizontal-coordinates
497 (time latitude longitude for-sunrise-sunset
)
498 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
500 TIME is a pair with the first component being the number of Julian centuries
501 elapsed at 0 Universal Time, and the second component being the universal
502 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
503 (-0.040945 16), -0.040945 being the number of julian centuries elapsed between
504 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
506 The azimuth is given in degrees as well as the height (between -180 and 180)."
507 (let* ((ut (car (cdr time
)))
508 (ec (solar-equatorial-coordinates time for-sunrise-sunset
))
509 (st (+ solar-sidereal-time-greenwich-midnight
510 (* ut
1.00273790935)))
511 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 calendar-longitude
)))
512 ; hour angle (in degrees)
514 (azimuth (solar-atn2 (solar-sin-degrees ah
)
515 (- (* (solar-cosine-degrees ah
)
516 (solar-sin-degrees latitude
))
517 (* (solar-tangent-degrees de
)
518 (solar-cosine-degrees latitude
)))))
519 (height (solar-arcsin
520 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
521 (* (solar-cosine-degrees latitude
)
522 (solar-cosine-degrees de
)
523 (solar-cosine-degrees ah
))))))
524 (if (> height
180) (setq height
(- height
360)))
525 (list azimuth height
)))
527 (defun solar-equatorial-coordinates (time for-sunrise-sunset
)
528 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
530 TIME is a pair with the first component being the number of Julian centuries
531 elapsed at 0 Universal Time, and the second component being the universal
532 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
533 (-0.040945 16), -0.040945 being the number of julian centuries elapsed between
534 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
535 (let* ((tm (solar-ephemeris-time time
))
536 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset
)))
537 (list (solar-right-ascension (car ec
) (car (cdr ec
)))
538 (solar-declination (car ec
) (car (cdr ec
))))))
540 (defun solar-ecliptic-coordinates (time for-sunrise-sunset
)
541 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
542 equation of time (in hours) and nutation in longitude (in seconds)
543 at moment `time', expressed in julian centuries of Ephemeris Time
544 since January 1st, 2000, at 12 ET."
545 (let* ((l (+ 280.46645
547 (* 0.0003032 time time
))) ; sun mean longitude
549 (* 481267.8813 time
))) ; moon mean longitude
552 (* -
0.0001559 time time
)
553 (* -
0.00000048 time time time
))) ; sun mean anomaly
554 (i (+ 23.43929111 (* -
0.013004167 time
)
555 (* -
0.00000016389 time time
)
556 (* 0.0000005036 time time time
))); mean inclination
559 (* -
0.000014 time time
))
560 (solar-sin-degrees m
))
561 (* (+ 0.019993 (* -
0.000101 time
))
562 (solar-sin-degrees (* 2 m
)))
564 (solar-sin-degrees (* 3 m
))))) ; center equation
565 (L (+ l c
)) ; total longitude
567 (* -
1934.136 time
))) ; longitude of moon's ascending node
569 (nut (if (not for-sunrise-sunset
)
570 (+ (* -
17.20 (solar-sin-degrees omega
))
571 (* -
1.32 (solar-sin-degrees (* 2 l
)))
572 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
573 (* 0.21 (solar-sin-degrees (* 2 omega
))))
575 ; nut = nutation in longitude, measured in seconds of angle.
576 (ecc (if (not for-sunrise-sunset
)
578 (* -
0.000042037 time
)
579 (* -
0.0000001236 time time
)) ; eccentricity of earth's orbit
584 (solar-sin-degrees omega
)))) ; apparent longitude of sun
585 (y (if (not for-sunrise-sunset
)
586 (* (solar-tangent-degrees (/ i
2))
587 (solar-tangent-degrees (/ i
2)))
589 (time-eq (if (not for-sunrise-sunset
)
590 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
591 (* -
2 ecc
(solar-sin-degrees m
))
592 (* 4 ecc y
(solar-sin-degrees m
)
593 (solar-cosine-degrees (* 2 l
)))
594 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
595 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
598 ; equation of time, in hours
599 (list app i time-eq nut
)))
601 (defun solar-longitude (d)
602 "Longitude of sun on astronomical (Julian) day number D.
603 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
605 The values of calendar-daylight-savings-starts,
606 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
607 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
608 calendar-time-zone are used to interpret local time."
609 (let* ((a-d (calendar-absolute-from-astro d
))
610 ;; get Universal Time
611 (date (calendar-astro-from-absolute
613 (if (dst-in-effect a-d
)
614 (/ calendar-daylight-time-offset
24.0 60.0) 0)
615 (/ calendar-time-zone
60.0 24.0))))
616 ;; get Ephemeris Time
617 (date (+ date
(solar-ephemeris-correction
618 (extract-calendar-year
619 (calendar-gregorian-from-absolute
621 (calendar-absolute-from-astro
623 (U (/ (- date
2451545) 3652500))
633 (* (car (cdr (cdr x
))) U
))
637 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
638 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 pi
)))
639 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 pi
)))
640 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
641 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
643 (defconst solar-data-list
644 '((403406 4.721964 1.621043)
645 (195207 5.937458 62830.348067)
646 (119433 1.115589 62830.821524)
647 (112392 5.781616 62829.634302)
648 (3891 5.5474 125660.5691)
649 (2819 1.5120 125660.984)
650 (1721 4.1897 62832.4766)
652 (660 5.415 125659.31)
655 (314 5.198 777137.715)
656 (268 5.989 78604.191)
658 (234 1.423 39302.098)
660 (132 2.317 115067.698)
661 (129 3.193 15774.337)
693 (10 2.55 157208.40)))
695 (defun solar-ephemeris-correction (year)
696 "Ephemeris time minus Universal Time during Gregorian year.
699 For the years 1800-1987, the maximum error is 1.9 seconds.
700 For the other years, the maximum error is about 30 seconds."
701 (cond ((and (<= 1988 year
) (< year
2020))
702 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
703 ((and (<= 1900 year
) (< year
1988))
704 (let* ((theta (/ (- (calendar-astro-from-absolute
705 (calendar-absolute-from-gregorian
707 (calendar-astro-from-absolute
708 (calendar-absolute-from-gregorian
711 (theta2 (* theta theta
))
712 (theta3 (* theta2 theta
))
713 (theta4 (* theta2 theta2
))
714 (theta5 (* theta3 theta2
)))
721 (* 0.677066 theta3 theta3
)
722 (* -
0.212591 theta4 theta3
))))
723 ((and (<= 1800 year
) (< year
1900))
724 (let* ((theta (/ (- (calendar-astro-from-absolute
725 (calendar-absolute-from-gregorian
727 (calendar-astro-from-absolute
728 (calendar-absolute-from-gregorian
731 (theta2 (* theta theta
))
732 (theta3 (* theta2 theta
))
733 (theta4 (* theta2 theta2
))
734 (theta5 (* theta3 theta2
)))
741 (* 31.332267 theta3 theta3
)
742 (* 38.291999 theta4 theta3
)
743 (* 28.316289 theta4 theta4
)
744 (* 11.636204 theta4 theta5
)
745 (* 2.043794 theta5 theta5
))))
746 ((and (<= 1620 year
) (< year
1800))
747 (let ((x (/ (- year
1600) 10.0)))
748 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
749 (t (let* ((tmp (- (calendar-astro-from-absolute
750 (calendar-absolute-from-gregorian
753 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
754 (/ second
60.0 60.0 24.0)))))
756 (defun solar-sidereal-time (t0)
757 "Sidereal time (in hours) in Greenwich.
759 At T0=Julian centuries of universal time.
760 T0 must correspond to 0 hours UT."
761 (let* ((mean-sid-time (+ 6.6973746
763 (* 0.0000258622 t0 t0
)
764 (* -
0.0000000017222 t0 t0 t0
)))
765 (et (solar-ephemeris-time (list t0
0.0)))
766 (nut-i (solar-ecliptic-coordinates et nil
))
767 (nut (car (cdr (cdr (cdr nut-i
))))) ; nutation
768 (i (car (cdr nut-i
)))) ; inclination
769 (mod (+ (mod (+ mean-sid-time
770 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
774 (defun solar-time-equation (date ut
)
775 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
776 (let* ((et (solar-date-to-et date ut
))
777 (ec (solar-ecliptic-coordinates et nil
)))
778 (car (cdr (cdr ec
)))))
780 (defun solar-date-to-et (date ut
)
781 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
782 Expressed in julian centuries of Ephemeris Time."
783 (let ((t0 (solar-julian-ut-centuries date
)))
784 (solar-ephemeris-time (list t0 ut
))))
787 (defun sunrise-sunset (&optional arg
)
788 "Local time of sunrise and sunset for today. Accurate to a few seconds.
789 If called with an optional prefix argument, prompt for date.
791 If called with an optional double prefix argument, prompt for longitude,
792 latitude, time zone, and date, and always use standard time.
794 This function is suitable for execution in a .emacs file."
796 (or arg
(setq arg
1))
798 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
800 (let* ((calendar-longitude
801 (if (< arg
16) calendar-longitude
803 "Enter longitude (decimal fraction; + east, - west): ")))
805 (if (< arg
16) calendar-latitude
807 "Enter latitude (decimal fraction; + north, - south): ")))
809 (if (< arg
16) calendar-time-zone
811 "Enter difference from Coordinated Universal Time (in minutes): ")))
812 (calendar-location-name
813 (if (< arg
16) calendar-location-name
814 (let ((float-output-format "%.1f"))
816 (if (numberp calendar-latitude
)
817 (abs calendar-latitude
)
818 (+ (aref calendar-latitude
0)
819 (/ (aref calendar-latitude
1) 60.0)))
820 (if (numberp calendar-latitude
)
821 (if (> calendar-latitude
0) "N" "S")
822 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
823 (if (numberp calendar-longitude
)
824 (abs calendar-longitude
)
825 (+ (aref calendar-longitude
0)
826 (/ (aref calendar-longitude
1) 60.0)))
827 (if (numberp calendar-longitude
)
828 (if (> calendar-longitude
0) "E" "W")
829 (if (equal (aref calendar-longitude
2) 'east
)
831 (calendar-standard-time-zone-name
832 (if (< arg
16) calendar-standard-time-zone-name
833 (cond ((= calendar-time-zone
0) "UTC")
834 ((< calendar-time-zone
0)
835 (format "UTC%dmin" calendar-time-zone
))
836 (t (format "UTC+%dmin" calendar-time-zone
)))))
837 (calendar-daylight-savings-starts
838 (if (< arg
16) calendar-daylight-savings-starts
))
839 (calendar-daylight-savings-ends
840 (if (< arg
16) calendar-daylight-savings-ends
))
841 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
842 (date-string (calendar-date-string date t
))
843 (time-string (solar-sunrise-sunset-string date
))
844 (msg (format "%s: %s" date-string time-string
))
845 (one-window (one-window-p t
)))
846 (if (<= (length msg
) (frame-width))
848 (with-output-to-temp-buffer "*temp*"
849 (princ (concat date-string
"\n" time-string
)))
850 (message (substitute-command-keys
853 "Type \\[delete-other-windows] to remove temp window."
854 "Type \\[switch-to-buffer] RET to remove temp window.")
855 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
857 (defun calendar-sunrise-sunset ()
858 "Local time of sunrise and sunset for date under cursor.
859 Accurate to a few seconds."
861 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
863 (let ((date (calendar-cursor-to-date t
)))
865 (calendar-date-string date t t
)
866 (solar-sunrise-sunset-string date
))))
868 (defun diary-sunrise-sunset ()
869 "Local time of sunrise and sunset as a diary entry.
870 Accurate to a few seconds."
871 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
873 (solar-sunrise-sunset-string date
))
875 (defun diary-sabbath-candles ()
876 "Local time of candle lighting diary entry--applies if date is a Friday.
877 No diary entry if there is no sunset on that date."
878 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
880 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5);; Friday
881 (let* ((sunset (car (cdr (solar-sunrise-sunset date
))))
885 (- (car sunset
) (/ 18.0 60.0))))))
886 (if (and light
(calendar-date-equal date
(car light
)))
887 (format "%s Sabbath candle lighting"
888 (apply 'solar-time-string
(cdr light
)))))))
890 (defun solar-equinoxes/solstices
(k year
)
891 "Date of equinox/solstice K for YEAR.
892 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
893 K=3, winter solstice.
894 RESULT is a gregorian local date.
896 Accurate to less than a minute between 1951 and 2050."
897 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
898 (T (/ (- JDE0
2451545.0) 36525))
899 (W (- (* 35999.373 T
) 2.47))
900 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
901 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
902 (S (apply '+ (mapcar '(lambda(x)
903 (* (car x
) (solar-cosine-degrees
904 (+ (* (car (cdr (cdr x
))) T
)
906 solar-seasons-data
)))
907 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
908 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
909 ; ephemeris time correction
910 (JD (- JDE
(/ correction
86400)))
911 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
912 (time (- (- JD
0.5) (floor (- JD
0.5))))
914 (list (car date
) (+ (car (cdr date
)) time
915 (/ (/ calendar-time-zone
60.0) 24.0))
916 (car (cdr (cdr date
))))))
918 ; from Meeus, 1991, page 166
919 (defun solar-mean-equinoxes/solstices
(k year
)
920 "Julian day of mean equinox/solstice K for YEAR.
921 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
922 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
923 (let ((y (/ year
1000.0))
924 (z (/ (- year
2000) 1000.0)))
925 (if (< year
1000) ; actually between -1000 and 1000
926 (cond ((equal k
0) (+ 1721139.29189
930 (* -
0.00071 y y y y
)))
931 ((equal k
1) (+ 1721233.25401
935 (* 0.00025 y y y y
)))
936 ((equal k
2) (+ 1721325.70455
940 (* 0.00074 y y y y
)))
941 ((equal k
3) (+ 1721414.39987
945 (* -
0.00006 y y y y
))))
946 ; actually between 1000 and 3000
947 (cond ((equal k
0) (+ 2451623.80984
951 (* -
0.00057 z z z z
)))
952 ((equal k
1) (+ 2451716.56767
956 (* -
0.00030 z z z z
)))
957 ((equal k
2) (+ 2451810.21715
961 (* 0.00078 z z z z
)))
962 ((equal k
3) (+ 2451900.05952
966 (* 0.00032 z z z z
)))))))
968 ; from Meeus, 1991, page 167
969 (defconst solar-seasons-data
970 '((485 324.96 1934.136)
971 (203 337.23 32964.467)
973 (182 27.85 445267.112)
974 (156 73.14 45036.886)
975 (136 171.52 22518.443)
976 (77 222.54 65928.934)
979 (58 119.81 33718.147)
982 (45 247.54 29929.562)
983 (44 325.15 31555.956)
985 (18 155.12 67555.328)
987 (16 198.04 62894.029)
988 (14 199.76 31436.921)
990 (12 287.11 31931.756)
991 (12 320.81 34777.259)
993 (8 15.45 16859.074)))
996 (defun solar-equinoxes-solstices ()
997 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
998 Requires floating point."
999 (let ((m displayed-month
)
1001 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
1004 (let* ((calendar-standard-time-zone-name
1005 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1006 (calendar-daylight-savings-starts
1007 (if calendar-time-zone calendar-daylight-savings-starts
))
1008 (calendar-daylight-savings-ends
1009 (if calendar-time-zone calendar-daylight-savings-ends
))
1010 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1012 (d0 (solar-equinoxes/solstices k y
))
1013 (d1 (list (car d0
) (floor (car (cdr d0
))) (car (cdr (cdr d0
)))))
1014 (h0 (* 24 (- (car (cdr d0
)) (floor (car (cdr d0
))))))
1015 (adj (dst-adjust-time d1 h0
))
1016 (d (list (car d1
) (+ (car (cdr d1
))
1017 (/ (car (cdr adj
)) 24.0))
1018 (car (cdr (cdr d1
)))))
1019 ; The following is nearly as accurate, but not quite:
1020 ;(d0 (solar-date-next-longitude
1021 ; (calendar-astro-from-absolute
1022 ; (calendar-absolute-from-gregorian
1023 ; (list (+ 3 (* k 3)) 15 y)))
1025 ;(abs-day (calendar-absolute-from-astro d)))
1026 (abs-day (calendar-absolute-from-gregorian d
)))
1028 (list (calendar-gregorian-from-absolute (floor abs-day
))
1030 (nth k
(if (and calendar-latitude
1031 (< (calendar-latitude) 0))
1032 solar-s-hemi-seasons
1033 solar-n-hemi-seasons
))
1035 (* 24 (- abs-day
(floor abs-day
)))
1036 (if (dst-in-effect abs-day
)
1037 calendar-daylight-time-zone-name
1038 calendar-standard-time-zone-name
))))))))
1043 ;;; solar.el ends here