1 ;;; solar.el --- calendar functions for solar events
3 ;; Copyright (C) 1992, 1993, 1995, 1997 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 the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This collection of functions implements the features of calendar.el,
31 ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
32 ;; equinoxes/solstices.
34 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
35 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
36 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
37 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
38 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
39 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
44 ;; 1. Sunrise/sunset times will be accurate to the minute for years
45 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
47 ;; 2. Equinox/solstice times will be accurate to the minute for years
48 ;; 1951--2050. For other years the times will be within +/- 1 minute.
50 ;; Technical details of all the calendrical calculations can be found in
51 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
52 ;; Cambridge University Press (1997).
54 ;; Comments, corrections, and improvements should be sent to
55 ;; Edward M. Reingold Department of Computer Science
56 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
57 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
58 ;; Urbana, Illinois 61801
63 (require 'lisp-float-type
)
64 (error "Solar/lunar 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)'."
89 (defcustom calendar-latitude nil
90 "*Latitude of `calendar-location-name' in degrees.
92 The value can be either a decimal fraction (one place of accuracy is
93 sufficient), + north, - south, such as 40.7 for New York City, or the value
94 can be a vector [degrees minutes north/south] such as [40 50 north] for New
97 This variable should be set in `site-start'.el."
98 :type
'(choice (const nil
)
100 (vector :value
[0 0 north
]
101 (integer :tag
"Degrees")
102 (integer :tag
"Minutes")
103 (choice :tag
"Position"
109 (defcustom calendar-longitude nil
110 "*Longitude of `calendar-location-name' in degrees.
112 The value can be either a decimal fraction (one place of accuracy is
113 sufficient), + east, - west, such as -73.9 for New York City, or the value
114 can be a vector [degrees minutes east/west] such as [73 55 west] for New
117 This variable should be set in `site-start'.el."
118 :type
'(choice (const nil
)
119 (number :tag
"Exact")
120 (vector :value
[0 0 west
]
121 (integer :tag
"Degrees")
122 (integer :tag
"Minutes")
123 (choice :tag
"Position"
128 (defsubst calendar-latitude
()
129 "Convert calendar-latitude to a signed decimal fraction, if needed."
130 (if (numberp calendar-latitude
)
132 (let ((lat (+ (aref calendar-latitude
0)
133 (/ (aref calendar-latitude
1) 60.0))))
134 (if (equal (aref calendar-latitude
2) 'north
)
138 (defsubst calendar-longitude
()
139 "Convert calendar-longitude to a signed decimal fraction, if needed."
140 (if (numberp calendar-longitude
)
142 (let ((long (+ (aref calendar-longitude
0)
143 (/ (aref calendar-longitude
1) 60.0))))
144 (if (equal (aref calendar-longitude
2) 'east
)
149 (defcustom calendar-location-name
150 '(let ((float-output-format "%.1f"))
152 (if (numberp calendar-latitude
)
153 (abs calendar-latitude
)
154 (+ (aref calendar-latitude
0)
155 (/ (aref calendar-latitude
1) 60.0)))
156 (if (numberp calendar-latitude
)
157 (if (> calendar-latitude
0) "N" "S")
158 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
159 (if (numberp calendar-longitude
)
160 (abs calendar-longitude
)
161 (+ (aref calendar-longitude
0)
162 (/ (aref calendar-longitude
1) 60.0)))
163 (if (numberp calendar-longitude
)
164 (if (> calendar-longitude
0) "E" "W")
165 (if (equal (aref calendar-longitude
2) 'east
) "E" "W"))))
166 "*Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
167 For example, \"New York City\". Default value is just the latitude, longitude
170 This variable should be set in `site-start'.el."
174 (defcustom solar-error
0.5
175 "*Tolerance (in minutes) for sunrise/sunset calculations.
177 A larger value makes the calculations for sunrise/sunset faster, but less
178 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
179 times will be correct to the minute.
181 It is useless to set the value smaller than 4*delta, where delta is the
182 accuracy in the longitude of the sun (given by the function
183 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
184 delta. At present, delta = 0.01 degrees, so the value of the variable
185 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
189 (defvar solar-n-hemi-seasons
190 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
191 "List of season changes for the northern hemisphere.")
193 (defvar solar-s-hemi-seasons
194 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
195 "List of season changes for the southern hemisphere.")
197 (defvar solar-sidereal-time-greenwich-midnight
199 "Sidereal time at Greenwich at midnight (universal time).")
201 (defvar solar-northern-spring-or-summer-season nil
202 "Non-nil if northern spring or summer and nil otherwise.
203 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
205 (defun solar-setup ()
206 "Prompt user for latitude, longitude, and time zone."
208 (if (not calendar-longitude
)
209 (setq calendar-longitude
211 "Enter longitude (decimal fraction; + east, - west): ")))
212 (if (not calendar-latitude
)
213 (setq calendar-latitude
215 "Enter latitude (decimal fraction; + north, - south): ")))
216 (if (not calendar-time-zone
)
217 (setq calendar-time-zone
219 "Enter difference from Coordinated Universal Time (in minutes): "))))
221 (defun solar-get-number (prompt)
222 "Return a number from the minibuffer, prompting with PROMPT.
223 Returns nil if nothing was entered."
224 (let ((x (read-string prompt
"")))
225 (if (not (string-equal x
""))
228 ;; The condition-case stuff is needed to catch bogus arithmetic
229 ;; exceptions that occur on some machines (like Sparcs)
230 (defun solar-sin-degrees (x)
232 (sin (degrees-to-radians (mod x
360.0)))
233 (solar-sin-degrees x
)))
234 (defun solar-cosine-degrees (x)
236 (cos (degrees-to-radians (mod x
360.0)))
237 (solar-cosine-degrees x
)))
238 (defun solar-tangent-degrees (x)
240 (tan (degrees-to-radians (mod x
360.0)))
241 (solar-tangent-degrees x
)))
243 (defun solar-xy-to-quadrant (x y
)
244 "Determines the quadrant of the point X, Y."
249 (defun solar-degrees-to-quadrant (angle)
250 "Determines the quadrant of ANGLE."
251 (1+ (floor (mod angle
360) 90)))
253 (defun solar-arctan (x quad
)
254 "Arctangent of X in quadrant QUAD."
255 (let ((deg (radians-to-degrees (atan x
))))
256 (cond ((equal quad
2) (+ deg
180))
257 ((equal quad
3) (+ deg
180))
258 ((equal quad
4) (+ deg
360))
261 (defun solar-atn2 (x y
)
262 "Arctan of point X, Y."
265 (solar-arctan (/ y x
) x
)))
267 (defun solar-arccos (x)
269 (let ((y (sqrt (- 1 (* x x
)))))
272 (defun solar-arcsin (y)
274 (let ((x (sqrt (- 1 (* y y
)))))
278 (defsubst solar-degrees-to-hours
(degrees)
279 "Convert DEGREES to hours."
282 (defsubst solar-hours-to-days
(hour)
283 "Convert HOUR to decimal fraction of a day."
286 (defun solar-right-ascension (longitude obliquity
)
287 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
288 Both arguments are in degrees."
289 (solar-degrees-to-hours
291 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
292 (solar-degrees-to-quadrant longitude
))))
294 (defun solar-declination (longitude obliquity
)
295 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
296 Both arguments are in degrees."
298 (* (solar-sin-degrees obliquity
)
299 (solar-sin-degrees longitude
))))
301 (defun solar-sunrise-and-sunset (time latitude longitude height
)
302 "Sunrise, sunset and length of day.
303 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
305 TIME is a pair with the first component being the number of Julian centuries
306 elapsed at 0 Universal Time, and the second component being the universal
307 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
308 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
309 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
311 HEIGHT is the angle the center of the sun has over the horizon for the contact
312 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
313 accounting for the edge of the sun being on the horizon.
315 Coordinates are included because this function is called with latitude=1
316 degrees to find out if polar regions have 24 hours of sun or only night."
317 (let* ((rise-time (solar-moment -
1 latitude longitude time height
))
318 (set-time (solar-moment 1 latitude longitude time height
))
320 (if (not (and rise-time set-time
))
321 (if (or (and (> latitude
0)
322 solar-northern-spring-or-summer-season
)
324 (not solar-northern-spring-or-summer-season
)))
327 (setq day-length
(- set-time rise-time
)))
328 (list (if rise-time
(+ rise-time
(/ calendar-time-zone
60.0)) nil
)
329 (if set-time
(+ set-time
(/ calendar-time-zone
60.0)) nil
)
332 (defun solar-moment (direction latitude longitude time height
)
333 "Sunrise/sunset at location.
334 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
337 TIME is a pair with the first component being the number of Julian centuries
338 elapsed at 0 Universal Time, and the second component being the universal
339 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
340 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
341 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
343 HEIGHT is the angle the center of the sun has over the horizon for the contact
344 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
345 accounting for the edge of the sun being on the horizon.
348 (let* ((ut (car (cdr time
)))
349 (possible t
) ; we assume that rise or set are possible
350 (utmin (+ ut
(* direction
12.0)))
351 (utmax ut
) ; the time searched is between utmin and utmax
352 ; utmin and utmax are in hours
353 (utmoment-old 0.0) ; rise or set approximation
354 (utmoment 1.0) ; rise or set approximation
355 (hut 0) ; sun height at utmoment
358 (solar-horizontal-coordinates (list t0 utmin
)
359 latitude longitude t
))))
361 (solar-horizontal-coordinates (list t0 utmax
)
362 latitude longitude t
)))))
363 ; -0.61 degrees is the height of the middle of the sun, when it rises
367 (while ;(< i 20) ; we perform a simple dichotomy
368 ; (> (abs (- hut height)) epsilon)
369 (>= (abs (- utmoment utmoment-old
))
371 (setq utmoment-old utmoment
)
372 (setq utmoment
(/ (+ utmin utmax
) 2))
374 (solar-horizontal-coordinates
375 (list t0 utmoment
) latitude longitude t
))))
376 (if (< hut height
) (setq utmin utmoment
))
377 (if (> hut height
) (setq utmax utmoment
))
379 (setq possible nil
)) ; the sun never rises
380 (setq possible nil
)) ; the sun never sets
381 (if (not possible
) nil utmoment
)))
383 (defun solar-time-string (time time-zone
)
384 "Printable form for decimal fraction TIME in TIME-ZONE.
385 Format used is given by `calendar-time-display-form'."
386 (let* ((time (round (* 60 time
)))
387 (24-hours (/ time
60))
388 (minutes (format "%02d" (% time
60)))
389 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
390 (am-pm (if (>= 24-hours
12) "pm" "am"))
391 (24-hours (format "%02d" 24-hours
)))
392 (mapconcat 'eval calendar-time-display-form
"")))
395 (defun solar-daylight (time)
396 "Printable form for time expressed in hours."
399 (floor (* 60 (- time
(floor time
))))))
401 (defun solar-exact-local-noon (date)
402 "Date and Universal Time of local noon at *local date* date.
404 The date may be different from the one asked for, but it will be the right
405 local date. The second component of date should be an integer."
407 (ut (- 12.0 (/ (calendar-longitude) 15)))
408 (te (solar-time-equation date ut
)))
412 (setq nd
(list (car date
) (+ 1 (car (cdr date
)))
413 (car (cdr (cdr date
)))))
414 (setq ut
(- ut
24))))
417 (setq nd
(list (car date
) (- (car (cdr date
)) 1)
418 (car (cdr (cdr date
)))))
419 (setq ut
(+ ut
24))))
420 (setq nd
(calendar-gregorian-from-absolute
421 (calendar-absolute-from-gregorian nd
)))
422 ; date standardization
425 (defun solar-sunrise-sunset (date)
426 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
428 Corresponding value is nil if there is no sunrise/sunset."
429 (let* (; first, get the exact moment of local noon.
430 (exact-local-noon (solar-exact-local-noon date
))
431 ; get the time from the 2000 epoch.
432 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
433 ; store the sidereal time at Greenwich at midnight of UT time.
434 ; find if summer or winter slightly above the equator
436 (progn (setq solar-sidereal-time-greenwich-midnight
437 (solar-sidereal-time t0
))
438 (solar-sunrise-and-sunset
439 (list t0
(car (cdr exact-local-noon
)))
441 (calendar-longitude) 0)))
442 ; store the spring/summer information,
443 ; compute sunrise and sunset (two first components of rise-set).
444 ; length of day is the third component (it is only the difference
445 ; between sunset and sunrise when there is a sunset and a sunrise)
448 (setq solar-northern-spring-or-summer-season
449 (if (> (car (cdr (cdr equator-rise-set
))) 12) t nil
))
450 (solar-sunrise-and-sunset
451 (list t0
(car (cdr exact-local-noon
)))
453 (calendar-longitude) -
0.61)))
454 (rise (car rise-set
))
455 (adj-rise (if rise
(dst-adjust-time date rise
) nil
))
456 (set (car (cdr rise-set
)))
457 (adj-set (if set
(dst-adjust-time date set
) nil
))
458 (length (car (cdr (cdr rise-set
)))) )
460 (and rise
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
461 (and set
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
462 (solar-daylight length
))))
464 (defun solar-sunrise-sunset-string (date)
465 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
466 (let ((l (solar-sunrise-sunset date
)))
468 "%s, %s at %s (%s hours daylight)"
470 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
473 (concat "sunset " (apply 'solar-time-string
(car (cdr l
))))
475 (eval calendar-location-name
)
476 (car (cdr (cdr l
))))))
478 (defun solar-julian-ut-centuries (date)
479 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
480 (/ (- (calendar-absolute-from-gregorian date
)
481 (calendar-absolute-from-gregorian '(1 1.5 2000)))
484 (defun solar-ephemeris-time(time)
485 "Ephemeris Time at moment TIME.
487 TIME is a pair with the first component being the number of Julian centuries
488 elapsed at 0 Universal Time, and the second component being the universal
489 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
490 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
491 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
493 Result is in julian centuries of ephemeris time."
494 (let* ((t0 (car time
))
495 (ut (car (cdr time
)))
496 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
497 (y (+ 2000 (* 100 t1
)))
498 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
499 (+ t1
(/ (/ dt
86400) 36525))))
501 (defun solar-date-next-longitude (d l
)
502 "First moment on or after Julian day number D when sun's longitude is a
503 multiple of L degrees at calendar-location-name with that location's
504 local time (including any daylight savings rules).
506 L must be an integer divisor of 360.
508 Result is in local time expressed astronomical (Julian) day numbers.
510 The values of calendar-daylight-savings-starts,
511 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
512 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
513 calendar-time-zone are used to interpret local time."
516 (start-long (solar-longitude d
))
517 (next (mod (* l
(1+ (floor (/ start-long l
)))) 360))
518 (end (+ d
(* (/ l
360.0) 400)))
519 (end-long (solar-longitude end
)))
520 (while ;; bisection search for nearest minute
521 (< 0.00001 (- end start
))
523 ;; start-long <= next < end-long when next != 0
524 ;; when next = 0, we look for the discontinuity (start-long is near 360
525 ;; and end-long is small (less than l).
526 (setq d
(/ (+ start end
) 2.0))
527 (setq long
(solar-longitude d
))
528 (if (or (and (/= next
0) (< long next
))
529 (and (= next
0) (< l long
)))
532 (setq start-long long
))
534 (setq end-long long
)))
535 (/ (+ start end
) 2.0)))
537 (defun solar-horizontal-coordinates
538 (time latitude longitude for-sunrise-sunset
)
539 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
541 TIME is a pair with the first component being the number of Julian centuries
542 elapsed at 0 Universal Time, and the second component being the universal
543 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
544 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
545 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
547 The azimuth is given in degrees as well as the height (between -180 and 180)."
548 (let* ((ut (car (cdr time
)))
549 (ec (solar-equatorial-coordinates time for-sunrise-sunset
))
550 (st (+ solar-sidereal-time-greenwich-midnight
551 (* ut
1.00273790935)))
552 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 (calendar-longitude))))
553 ; hour angle (in degrees)
555 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah
)
556 (solar-sin-degrees latitude
))
557 (* (solar-tangent-degrees de
)
558 (solar-cosine-degrees latitude
)))
559 (solar-sin-degrees ah
)))
560 (height (solar-arcsin
561 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
562 (* (solar-cosine-degrees latitude
)
563 (solar-cosine-degrees de
)
564 (solar-cosine-degrees ah
))))))
565 (if (> height
180) (setq height
(- height
360)))
566 (list azimuth height
)))
568 (defun solar-equatorial-coordinates (time for-sunrise-sunset
)
569 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
571 TIME is a pair with the first component being the number of Julian centuries
572 elapsed at 0 Universal Time, and the second component being the universal
573 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
574 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
575 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
576 (let* ((tm (solar-ephemeris-time time
))
577 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset
)))
578 (list (solar-right-ascension (car ec
) (car (cdr ec
)))
579 (solar-declination (car ec
) (car (cdr ec
))))))
581 (defun solar-ecliptic-coordinates (time for-sunrise-sunset
)
582 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
583 equation of time (in hours) and nutation in longitude (in seconds)
584 at moment `time', expressed in julian centuries of Ephemeris Time
585 since January 1st, 2000, at 12 ET."
586 (let* ((l (+ 280.46645
588 (* 0.0003032 time time
))) ; sun mean longitude
590 (* 481267.8813 time
))) ; moon mean longitude
593 (* -
0.0001559 time time
)
594 (* -
0.00000048 time time time
))) ; sun mean anomaly
595 (i (+ 23.43929111 (* -
0.013004167 time
)
596 (* -
0.00000016389 time time
)
597 (* 0.0000005036 time time time
))); mean inclination
600 (* -
0.000014 time time
))
601 (solar-sin-degrees m
))
602 (* (+ 0.019993 (* -
0.000101 time
))
603 (solar-sin-degrees (* 2 m
)))
605 (solar-sin-degrees (* 3 m
))))) ; center equation
606 (L (+ l c
)) ; total longitude
608 (* -
1934.136 time
))) ; longitude of moon's ascending node
610 (nut (if (not for-sunrise-sunset
)
611 (+ (* -
17.20 (solar-sin-degrees omega
))
612 (* -
1.32 (solar-sin-degrees (* 2 l
)))
613 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
614 (* 0.21 (solar-sin-degrees (* 2 omega
))))
616 ; nut = nutation in longitude, measured in seconds of angle.
617 (ecc (if (not for-sunrise-sunset
)
619 (* -
0.000042037 time
)
620 (* -
0.0000001236 time time
)) ; eccentricity of earth's orbit
625 (solar-sin-degrees omega
)))) ; apparent longitude of sun
626 (y (if (not for-sunrise-sunset
)
627 (* (solar-tangent-degrees (/ i
2))
628 (solar-tangent-degrees (/ i
2)))
630 (time-eq (if (not for-sunrise-sunset
)
631 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
632 (* -
2 ecc
(solar-sin-degrees m
))
633 (* 4 ecc y
(solar-sin-degrees m
)
634 (solar-cosine-degrees (* 2 l
)))
635 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
636 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
639 ; equation of time, in hours
640 (list app i time-eq nut
)))
642 (defun solar-longitude (d)
643 "Longitude of sun on astronomical (Julian) day number D.
644 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
646 The values of calendar-daylight-savings-starts,
647 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
648 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
649 calendar-time-zone are used to interpret local time."
650 (let* ((a-d (calendar-absolute-from-astro d
))
651 ;; get Universal Time
652 (date (calendar-astro-from-absolute
654 (if (dst-in-effect a-d
)
655 (/ calendar-daylight-time-offset
24.0 60.0) 0)
656 (/ calendar-time-zone
60.0 24.0))))
657 ;; get Ephemeris Time
658 (date (+ date
(solar-ephemeris-correction
659 (extract-calendar-year
660 (calendar-gregorian-from-absolute
662 (calendar-absolute-from-astro
664 (U (/ (- date
2451545) 3652500))
674 (* (car (cdr (cdr x
))) U
))
678 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
679 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 pi
)))
680 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 pi
)))
681 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
682 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
684 (defconst solar-data-list
685 '((403406 4.721964 1.621043)
686 (195207 5.937458 62830.348067)
687 (119433 1.115589 62830.821524)
688 (112392 5.781616 62829.634302)
689 (3891 5.5474 125660.5691)
690 (2819 1.5120 125660.984)
691 (1721 4.1897 62832.4766)
693 (660 5.415 125659.31)
696 (314 5.198 777137.715)
697 (268 5.989 78604.191)
699 (234 1.423 39302.098)
701 (132 2.317 115067.698)
702 (129 3.193 15774.337)
734 (10 2.55 157208.40)))
736 (defun solar-ephemeris-correction (year)
737 "Ephemeris time minus Universal Time during Gregorian year.
740 For the years 1800-1987, the maximum error is 1.9 seconds.
741 For the other years, the maximum error is about 30 seconds."
742 (cond ((and (<= 1988 year
) (< year
2020))
743 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
744 ((and (<= 1900 year
) (< year
1988))
745 (let* ((theta (/ (- (calendar-astro-from-absolute
746 (calendar-absolute-from-gregorian
748 (calendar-astro-from-absolute
749 (calendar-absolute-from-gregorian
752 (theta2 (* theta theta
))
753 (theta3 (* theta2 theta
))
754 (theta4 (* theta2 theta2
))
755 (theta5 (* theta3 theta2
)))
762 (* 0.677066 theta3 theta3
)
763 (* -
0.212591 theta4 theta3
))))
764 ((and (<= 1800 year
) (< year
1900))
765 (let* ((theta (/ (- (calendar-astro-from-absolute
766 (calendar-absolute-from-gregorian
768 (calendar-astro-from-absolute
769 (calendar-absolute-from-gregorian
772 (theta2 (* theta theta
))
773 (theta3 (* theta2 theta
))
774 (theta4 (* theta2 theta2
))
775 (theta5 (* theta3 theta2
)))
782 (* 31.332267 theta3 theta3
)
783 (* 38.291999 theta4 theta3
)
784 (* 28.316289 theta4 theta4
)
785 (* 11.636204 theta4 theta5
)
786 (* 2.043794 theta5 theta5
))))
787 ((and (<= 1620 year
) (< year
1800))
788 (let ((x (/ (- year
1600) 10.0)))
789 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
790 (t (let* ((tmp (- (calendar-astro-from-absolute
791 (calendar-absolute-from-gregorian
794 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
795 (/ second
60.0 60.0 24.0)))))
797 (defun solar-sidereal-time (t0)
798 "Sidereal time (in hours) in Greenwich.
800 At T0=Julian centuries of universal time.
801 T0 must correspond to 0 hours UT."
802 (let* ((mean-sid-time (+ 6.6973746
804 (* 0.0000258622 t0 t0
)
805 (* -
0.0000000017222 t0 t0 t0
)))
806 (et (solar-ephemeris-time (list t0
0.0)))
807 (nut-i (solar-ecliptic-coordinates et nil
))
808 (nut (car (cdr (cdr (cdr nut-i
))))) ; nutation
809 (i (car (cdr nut-i
)))) ; inclination
810 (mod (+ (mod (+ mean-sid-time
811 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
815 (defun solar-time-equation (date ut
)
816 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
817 (let* ((et (solar-date-to-et date ut
))
818 (ec (solar-ecliptic-coordinates et nil
)))
819 (car (cdr (cdr ec
)))))
821 (defun solar-date-to-et (date ut
)
822 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
823 Expressed in julian centuries of Ephemeris Time."
824 (let ((t0 (solar-julian-ut-centuries date
)))
825 (solar-ephemeris-time (list t0 ut
))))
828 (defun sunrise-sunset (&optional arg
)
829 "Local time of sunrise and sunset for today. Accurate to a few seconds.
830 If called with an optional prefix argument, prompt for date.
832 If called with an optional double prefix argument, prompt for longitude,
833 latitude, time zone, and date, and always use standard time.
835 This function is suitable for execution in a .emacs file."
837 (or arg
(setq arg
1))
839 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
841 (let* ((calendar-longitude
842 (if (< arg
16) calendar-longitude
844 "Enter longitude (decimal fraction; + east, - west): ")))
846 (if (< arg
16) calendar-latitude
848 "Enter latitude (decimal fraction; + north, - south): ")))
850 (if (< arg
16) calendar-time-zone
852 "Enter difference from Coordinated Universal Time (in minutes): ")))
853 (calendar-location-name
854 (if (< arg
16) calendar-location-name
855 (let ((float-output-format "%.1f"))
857 (if (numberp calendar-latitude
)
858 (abs calendar-latitude
)
859 (+ (aref calendar-latitude
0)
860 (/ (aref calendar-latitude
1) 60.0)))
861 (if (numberp calendar-latitude
)
862 (if (> calendar-latitude
0) "N" "S")
863 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
864 (if (numberp calendar-longitude
)
865 (abs calendar-longitude
)
866 (+ (aref calendar-longitude
0)
867 (/ (aref calendar-longitude
1) 60.0)))
868 (if (numberp calendar-longitude
)
869 (if (> calendar-longitude
0) "E" "W")
870 (if (equal (aref calendar-longitude
2) 'east
)
872 (calendar-standard-time-zone-name
873 (if (< arg
16) calendar-standard-time-zone-name
874 (cond ((= calendar-time-zone
0) "UTC")
875 ((< calendar-time-zone
0)
876 (format "UTC%dmin" calendar-time-zone
))
877 (t (format "UTC+%dmin" calendar-time-zone
)))))
878 (calendar-daylight-savings-starts
879 (if (< arg
16) calendar-daylight-savings-starts
))
880 (calendar-daylight-savings-ends
881 (if (< arg
16) calendar-daylight-savings-ends
))
882 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
883 (date-string (calendar-date-string date t
))
884 (time-string (solar-sunrise-sunset-string date
))
885 (msg (format "%s: %s" date-string time-string
))
886 (one-window (one-window-p t
)))
887 (if (<= (length msg
) (frame-width))
889 (with-output-to-temp-buffer "*temp*"
890 (princ (concat date-string
"\n" time-string
)))
892 (substitute-command-keys
895 "Type \\[delete-other-windows] to remove temp window."
896 "Type \\[switch-to-buffer] RET to remove temp window.")
897 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
899 (defun calendar-sunrise-sunset ()
900 "Local time of sunrise and sunset for date under cursor.
901 Accurate to a few seconds."
903 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
905 (let ((date (calendar-cursor-to-date t
)))
907 (calendar-date-string date t t
)
908 (solar-sunrise-sunset-string date
))))
910 (defun diary-sunrise-sunset ()
911 "Local time of sunrise and sunset as a diary entry.
912 Accurate to a few seconds."
913 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
915 (solar-sunrise-sunset-string date
))
917 (defcustom diary-sabbath-candles-minutes
18
918 "*Number of minutes before sunset for sabbath candle lighting."
923 (defun diary-sabbath-candles ()
924 "Local time of candle lighting diary entry--applies if date is a Friday.
925 No diary entry if there is no sunset on that date."
926 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
928 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5);; Friday
929 (let* ((sunset (car (cdr (solar-sunrise-sunset date
))))
931 (cons (- (car sunset
)
932 (/ diary-sabbath-candles-minutes
60.0))
935 (format "%s Sabbath candle lighting"
936 (apply 'solar-time-string light
))))))
938 (defun solar-equinoxes/solstices
(k year
)
939 "Date of equinox/solstice K for YEAR.
940 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
941 K=3, winter solstice.
942 RESULT is a gregorian local date.
944 Accurate to less than a minute between 1951 and 2050."
945 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
946 (T (/ (- JDE0
2451545.0) 36525))
947 (W (- (* 35999.373 T
) 2.47))
948 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
949 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
950 (S (apply '+ (mapcar '(lambda(x)
951 (* (car x
) (solar-cosine-degrees
952 (+ (* (car (cdr (cdr x
))) T
)
954 solar-seasons-data
)))
955 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
956 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
957 ; ephemeris time correction
958 (JD (- JDE
(/ correction
86400)))
959 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
960 (time (- (- JD
0.5) (floor (- JD
0.5))))
962 (list (car date
) (+ (car (cdr date
)) time
963 (/ (/ calendar-time-zone
60.0) 24.0))
964 (car (cdr (cdr date
))))))
966 ; from Meeus, 1991, page 166
967 (defun solar-mean-equinoxes/solstices
(k year
)
968 "Julian day of mean equinox/solstice K for YEAR.
969 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
970 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
971 (let ((y (/ year
1000.0))
972 (z (/ (- year
2000) 1000.0)))
973 (if (< year
1000) ; actually between -1000 and 1000
974 (cond ((equal k
0) (+ 1721139.29189
978 (* -
0.00071 y y y y
)))
979 ((equal k
1) (+ 1721233.25401
983 (* 0.00025 y y y y
)))
984 ((equal k
2) (+ 1721325.70455
988 (* 0.00074 y y y y
)))
989 ((equal k
3) (+ 1721414.39987
993 (* -
0.00006 y y y y
))))
994 ; actually between 1000 and 3000
995 (cond ((equal k
0) (+ 2451623.80984
999 (* -
0.00057 z z z z
)))
1000 ((equal k
1) (+ 2451716.56767
1004 (* -
0.00030 z z z z
)))
1005 ((equal k
2) (+ 2451810.21715
1009 (* 0.00078 z z z z
)))
1010 ((equal k
3) (+ 2451900.05952
1014 (* 0.00032 z z z z
)))))))
1016 ; from Meeus, 1991, page 167
1017 (defconst solar-seasons-data
1018 '((485 324.96 1934.136)
1019 (203 337.23 32964.467)
1021 (182 27.85 445267.112)
1022 (156 73.14 45036.886)
1023 (136 171.52 22518.443)
1024 (77 222.54 65928.934)
1025 (74 296.72 3034.906)
1026 (70 243.58 9037.513)
1027 (58 119.81 33718.147)
1030 (45 247.54 29929.562)
1031 (44 325.15 31555.956)
1033 (18 155.12 67555.328)
1034 (17 288.79 4562.452)
1035 (16 198.04 62894.029)
1036 (14 199.76 31436.921)
1037 (12 95.39 14577.848)
1038 (12 287.11 31931.756)
1039 (12 320.81 34777.259)
1041 (8 15.45 16859.074)))
1044 (defun solar-equinoxes-solstices ()
1045 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
1046 Requires floating point."
1047 (let ((m displayed-month
)
1049 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
1052 (let* ((calendar-standard-time-zone-name
1053 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1054 (calendar-daylight-savings-starts
1055 (if calendar-time-zone calendar-daylight-savings-starts
))
1056 (calendar-daylight-savings-ends
1057 (if calendar-time-zone calendar-daylight-savings-ends
))
1058 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1060 (d0 (solar-equinoxes/solstices k y
))
1061 (d1 (list (car d0
) (floor (car (cdr d0
))) (car (cdr (cdr d0
)))))
1062 (h0 (* 24 (- (car (cdr d0
)) (floor (car (cdr d0
))))))
1063 (adj (dst-adjust-time d1 h0
))
1064 (d (list (car d1
) (+ (car (cdr d1
))
1065 (/ (car (cdr adj
)) 24.0))
1066 (car (cdr (cdr d1
)))))
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 ;;; solar.el ends here