1 ;;; solar.el --- calendar functions for solar events
3 ;; Copyright (C) 1992, 1993, 1995, 1997, 2003 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Denis B. Roegel <Denis.Roegel@loria.fr>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
9 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; This collection of functions implements the features of calendar.el,
32 ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
33 ;; equinoxes/solstices.
35 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
36 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
37 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
38 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
39 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
40 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
45 ;; 1. Sunrise/sunset times will be accurate to the minute for years
46 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
48 ;; 2. Equinox/solstice times will be accurate to the minute for years
49 ;; 1951--2050. For other years the times will be within +/- 1 minute.
51 ;; Technical details of all the calendrical calculations can be found in
52 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
53 ;; and Nachum Dershowitz, Cambridge University Press (2001).
55 ;; Comments, corrections, and improvements should be sent to
56 ;; Edward M. Reingold Department of Computer Science
57 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
58 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
59 ;; Urbana, Illinois 61801
63 (defvar displayed-month
)
64 (defvar displayed-year
)
67 (require 'lisp-float-type
)
68 (error "Solar/lunar calculations impossible since floating point is unavailable"))
74 (defcustom calendar-time-display-form
75 '(12-hours ":" minutes am-pm
76 (if time-zone
" (") time-zone
(if time-zone
")"))
77 "*The pseudo-pattern that governs the way a time of day is formatted.
79 A pseudo-pattern is a list of expressions that can involve the keywords
80 `12-hours', `24-hours', and `minutes', all numbers in string form,
81 and `am-pm' and `time-zone', both alphabetic strings.
85 '(24-hours \":\" minutes
86 (if time-zone \" (\") time-zone (if time-zone \")\"))
88 would give military-style times like `21:07 (UTC)'."
93 (defcustom calendar-latitude nil
94 "*Latitude of `calendar-location-name' in degrees.
96 The value can be either a decimal fraction (one place of accuracy is
97 sufficient), + north, - south, such as 40.7 for New York City, or the value
98 can be a vector [degrees minutes north/south] such as [40 50 north] for New
101 This variable should be set in `site-start'.el."
102 :type
'(choice (const nil
)
103 (number :tag
"Exact")
104 (vector :value
[0 0 north
]
105 (integer :tag
"Degrees")
106 (integer :tag
"Minutes")
107 (choice :tag
"Position"
113 (defcustom calendar-longitude nil
114 "*Longitude of `calendar-location-name' in degrees.
116 The value can be either a decimal fraction (one place of accuracy is
117 sufficient), + east, - west, such as -73.9 for New York City, or the value
118 can be a vector [degrees minutes east/west] such as [73 55 west] for New
121 This variable should be set in `site-start'.el."
122 :type
'(choice (const nil
)
123 (number :tag
"Exact")
124 (vector :value
[0 0 west
]
125 (integer :tag
"Degrees")
126 (integer :tag
"Minutes")
127 (choice :tag
"Position"
132 (defsubst calendar-latitude
()
133 "Convert calendar-latitude to a signed decimal fraction, if needed."
134 (if (numberp calendar-latitude
)
136 (let ((lat (+ (aref calendar-latitude
0)
137 (/ (aref calendar-latitude
1) 60.0))))
138 (if (equal (aref calendar-latitude
2) 'north
)
142 (defsubst calendar-longitude
()
143 "Convert calendar-longitude to a signed decimal fraction, if needed."
144 (if (numberp calendar-longitude
)
146 (let ((long (+ (aref calendar-longitude
0)
147 (/ (aref calendar-longitude
1) 60.0))))
148 (if (equal (aref calendar-longitude
2) 'east
)
153 (defcustom calendar-location-name
154 '(let ((float-output-format "%.1f"))
156 (if (numberp calendar-latitude
)
157 (abs calendar-latitude
)
158 (+ (aref calendar-latitude
0)
159 (/ (aref calendar-latitude
1) 60.0)))
160 (if (numberp calendar-latitude
)
161 (if (> calendar-latitude
0) "N" "S")
162 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
163 (if (numberp calendar-longitude
)
164 (abs calendar-longitude
)
165 (+ (aref calendar-longitude
0)
166 (/ (aref calendar-longitude
1) 60.0)))
167 (if (numberp calendar-longitude
)
168 (if (> calendar-longitude
0) "E" "W")
169 (if (equal (aref calendar-longitude
2) 'east
) "E" "W"))))
170 "*Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
171 For example, \"New York City\". Default value is just the latitude, longitude
174 This variable should be set in `site-start'.el."
178 (defcustom solar-error
0.5
179 "*Tolerance (in minutes) for sunrise/sunset calculations.
181 A larger value makes the calculations for sunrise/sunset faster, but less
182 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
183 times will be correct to the minute.
185 It is useless to set the value smaller than 4*delta, where delta is the
186 accuracy in the longitude of the sun (given by the function
187 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
188 delta. At present, delta = 0.01 degrees, so the value of the variable
189 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
193 (defvar solar-n-hemi-seasons
194 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
195 "List of season changes for the northern hemisphere.")
197 (defvar solar-s-hemi-seasons
198 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
199 "List of season changes for the southern hemisphere.")
201 (defvar solar-sidereal-time-greenwich-midnight
203 "Sidereal time at Greenwich at midnight (universal time).")
205 (defvar solar-northern-spring-or-summer-season nil
206 "Non-nil if northern spring or summer and nil otherwise.
207 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
209 (defun solar-setup ()
210 "Prompt user for latitude, longitude, and time zone."
212 (if (not calendar-longitude
)
213 (setq calendar-longitude
215 "Enter longitude (decimal fraction; + east, - west): ")))
216 (if (not calendar-latitude
)
217 (setq calendar-latitude
219 "Enter latitude (decimal fraction; + north, - south): ")))
220 (if (not calendar-time-zone
)
221 (setq calendar-time-zone
223 "Enter difference from Coordinated Universal Time (in minutes): "))))
225 (defun solar-get-number (prompt)
226 "Return a number from the minibuffer, prompting with PROMPT.
227 Returns nil if nothing was entered."
228 (let ((x (read-string prompt
"")))
229 (if (not (string-equal x
""))
232 ;; The condition-case stuff is needed to catch bogus arithmetic
233 ;; exceptions that occur on some machines (like Sparcs)
234 (defun solar-sin-degrees (x)
236 (sin (degrees-to-radians (mod x
360.0)))
237 (solar-sin-degrees x
)))
238 (defun solar-cosine-degrees (x)
240 (cos (degrees-to-radians (mod x
360.0)))
241 (solar-cosine-degrees x
)))
242 (defun solar-tangent-degrees (x)
244 (tan (degrees-to-radians (mod x
360.0)))
245 (solar-tangent-degrees x
)))
247 (defun solar-xy-to-quadrant (x y
)
248 "Determines the quadrant of the point X, Y."
253 (defun solar-degrees-to-quadrant (angle)
254 "Determines the quadrant of ANGLE."
255 (1+ (floor (mod angle
360) 90)))
257 (defun solar-arctan (x quad
)
258 "Arctangent of X in quadrant QUAD."
259 (let ((deg (radians-to-degrees (atan x
))))
260 (cond ((equal quad
2) (+ deg
180))
261 ((equal quad
3) (+ deg
180))
262 ((equal quad
4) (+ deg
360))
265 (defun solar-atn2 (x y
)
266 "Arctan of point X, Y."
269 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
271 (defun solar-arccos (x)
273 (let ((y (sqrt (- 1 (* x x
)))))
276 (defun solar-arcsin (y)
278 (let ((x (sqrt (- 1 (* y y
)))))
282 (defsubst solar-degrees-to-hours
(degrees)
283 "Convert DEGREES to hours."
286 (defsubst solar-hours-to-days
(hour)
287 "Convert HOUR to decimal fraction of a day."
290 (defun solar-right-ascension (longitude obliquity
)
291 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
292 Both arguments are in degrees."
293 (solar-degrees-to-hours
295 (* (solar-cosine-degrees obliquity
) (solar-tangent-degrees longitude
))
296 (solar-degrees-to-quadrant longitude
))))
298 (defun solar-declination (longitude obliquity
)
299 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
300 Both arguments are in degrees."
302 (* (solar-sin-degrees obliquity
)
303 (solar-sin-degrees longitude
))))
305 (defun solar-sunrise-and-sunset (time latitude longitude height
)
306 "Sunrise, sunset and length of day.
307 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
309 TIME is a pair with the first component being the number of Julian centuries
310 elapsed at 0 Universal Time, and the second component being the universal
311 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
312 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
313 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
315 HEIGHT is the angle the center of the sun has over the horizon for the contact
316 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
317 accounting for the edge of the sun being on the horizon.
319 Coordinates are included because this function is called with latitude=1
320 degrees to find out if polar regions have 24 hours of sun or only night."
321 (let* ((rise-time (solar-moment -
1 latitude longitude time height
))
322 (set-time (solar-moment 1 latitude longitude time height
))
324 (if (not (and rise-time set-time
))
325 (if (or (and (> latitude
0)
326 solar-northern-spring-or-summer-season
)
328 (not solar-northern-spring-or-summer-season
)))
331 (setq day-length
(- set-time rise-time
)))
332 (list (if rise-time
(+ rise-time
(/ calendar-time-zone
60.0)) nil
)
333 (if set-time
(+ set-time
(/ calendar-time-zone
60.0)) nil
)
336 (defun solar-moment (direction latitude longitude time height
)
337 "Sunrise/sunset at location.
338 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
341 TIME is a pair with the first component being the number of Julian centuries
342 elapsed at 0 Universal Time, and the second component being the universal
343 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
344 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
345 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
347 HEIGHT is the angle the center of the sun has over the horizon for the contact
348 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
349 accounting for the edge of the sun being on the horizon.
352 (let* ((ut (car (cdr time
)))
353 (possible t
) ; we assume that rise or set are possible
354 (utmin (+ ut
(* direction
12.0)))
355 (utmax ut
) ; the time searched is between utmin and utmax
356 ; utmin and utmax are in hours
357 (utmoment-old 0.0) ; rise or set approximation
358 (utmoment 1.0) ; rise or set approximation
359 (hut 0) ; sun height at utmoment
362 (solar-horizontal-coordinates (list t0 utmin
)
363 latitude longitude t
))))
365 (solar-horizontal-coordinates (list t0 utmax
)
366 latitude longitude t
)))))
367 ; -0.61 degrees is the height of the middle of the sun, when it rises
371 (while ;(< i 20) ; we perform a simple dichotomy
372 ; (> (abs (- hut height)) epsilon)
373 (>= (abs (- utmoment utmoment-old
))
375 (setq utmoment-old utmoment
)
376 (setq utmoment
(/ (+ utmin utmax
) 2))
378 (solar-horizontal-coordinates
379 (list t0 utmoment
) latitude longitude t
))))
380 (if (< hut height
) (setq utmin utmoment
))
381 (if (> hut height
) (setq utmax utmoment
))
383 (setq possible nil
)) ; the sun never rises
384 (setq possible nil
)) ; the sun never sets
385 (if (not possible
) nil utmoment
)))
387 (defun solar-time-string (time time-zone
)
388 "Printable form for decimal fraction TIME in TIME-ZONE.
389 Format used is given by `calendar-time-display-form'."
390 (let* ((time (round (* 60 time
)))
391 (24-hours (/ time
60))
392 (minutes (format "%02d" (% time
60)))
393 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
394 (am-pm (if (>= 24-hours
12) "pm" "am"))
395 (24-hours (format "%02d" 24-hours
)))
396 (mapconcat 'eval calendar-time-display-form
"")))
399 (defun solar-daylight (time)
400 "Printable form for time expressed in hours."
403 (floor (* 60 (- time
(floor time
))))))
405 (defun solar-exact-local-noon (date)
406 "Date and Universal Time of local noon at *local date* date.
408 The date may be different from the one asked for, but it will be the right
409 local date. The second component of date should be an integer."
411 (ut (- 12.0 (/ (calendar-longitude) 15)))
412 (te (solar-time-equation date ut
)))
416 (setq nd
(list (car date
) (+ 1 (car (cdr date
)))
417 (car (cdr (cdr date
)))))
418 (setq ut
(- ut
24))))
421 (setq nd
(list (car date
) (- (car (cdr date
)) 1)
422 (car (cdr (cdr date
)))))
423 (setq ut
(+ ut
24))))
424 (setq nd
(calendar-gregorian-from-absolute
425 (calendar-absolute-from-gregorian nd
)))
426 ; date standardization
429 (defun solar-sunrise-sunset (date)
430 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
432 Corresponding value is nil if there is no sunrise/sunset."
433 (let* (; first, get the exact moment of local noon.
434 (exact-local-noon (solar-exact-local-noon date
))
435 ; get the time from the 2000 epoch.
436 (t0 (solar-julian-ut-centuries (car exact-local-noon
)))
437 ; store the sidereal time at Greenwich at midnight of UT time.
438 ; find if summer or winter slightly above the equator
440 (progn (setq solar-sidereal-time-greenwich-midnight
441 (solar-sidereal-time t0
))
442 (solar-sunrise-and-sunset
443 (list t0
(car (cdr exact-local-noon
)))
445 (calendar-longitude) 0)))
446 ; store the spring/summer information,
447 ; compute sunrise and sunset (two first components of rise-set).
448 ; length of day is the third component (it is only the difference
449 ; between sunset and sunrise when there is a sunset and a sunrise)
452 (setq solar-northern-spring-or-summer-season
453 (if (> (car (cdr (cdr equator-rise-set
))) 12) t nil
))
454 (solar-sunrise-and-sunset
455 (list t0
(car (cdr exact-local-noon
)))
457 (calendar-longitude) -
0.61)))
458 (rise (car rise-set
))
459 (adj-rise (if rise
(dst-adjust-time date rise
) nil
))
460 (set (car (cdr rise-set
)))
461 (adj-set (if set
(dst-adjust-time date set
) nil
))
462 (length (car (cdr (cdr rise-set
)))) )
464 (and rise
(calendar-date-equal date
(car adj-rise
)) (cdr adj-rise
))
465 (and set
(calendar-date-equal date
(car adj-set
)) (cdr adj-set
))
466 (solar-daylight length
))))
468 (defun solar-sunrise-sunset-string (date)
469 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
470 (let ((l (solar-sunrise-sunset date
)))
472 "%s, %s at %s (%s hours daylight)"
474 (concat "Sunrise " (apply 'solar-time-string
(car l
)))
477 (concat "sunset " (apply 'solar-time-string
(car (cdr l
))))
479 (eval calendar-location-name
)
480 (car (cdr (cdr l
))))))
482 (defun solar-julian-ut-centuries (date)
483 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
484 (/ (- (calendar-absolute-from-gregorian date
)
485 (calendar-absolute-from-gregorian '(1 1.5 2000)))
488 (defun solar-ephemeris-time(time)
489 "Ephemeris Time at moment TIME.
491 TIME is a pair with the first component being the number of Julian centuries
492 elapsed at 0 Universal Time, and the second component being the universal
493 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
494 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
495 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
497 Result is in julian centuries of ephemeris time."
498 (let* ((t0 (car time
))
499 (ut (car (cdr time
)))
500 (t1 (+ t0
(/ (/ ut
24.0) 36525)))
501 (y (+ 2000 (* 100 t1
)))
502 (dt (* 86400 (solar-ephemeris-correction (floor y
)))))
503 (+ t1
(/ (/ dt
86400) 36525))))
505 (defun solar-date-next-longitude (d l
)
506 "First moment on or after Julian day number D when sun's longitude is a
507 multiple of L degrees at calendar-location-name with that location's
508 local time (including any daylight savings rules).
510 L must be an integer divisor of 360.
512 Result is in local time expressed astronomical (Julian) day numbers.
514 The values of calendar-daylight-savings-starts,
515 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
516 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
517 calendar-time-zone are used to interpret local time."
520 (start-long (solar-longitude d
))
521 (next (mod (* l
(1+ (floor (/ start-long l
)))) 360))
522 (end (+ d
(* (/ l
360.0) 400)))
523 (end-long (solar-longitude end
)))
524 (while ;; bisection search for nearest minute
525 (< 0.00001 (- end start
))
527 ;; start-long <= next < end-long when next != 0
528 ;; when next = 0, we look for the discontinuity (start-long is near 360
529 ;; and end-long is small (less than l).
530 (setq d
(/ (+ start end
) 2.0))
531 (setq long
(solar-longitude d
))
532 (if (or (and (/= next
0) (< long next
))
533 (and (= next
0) (< l long
)))
536 (setq start-long long
))
538 (setq end-long long
)))
539 (/ (+ start end
) 2.0)))
541 (defun solar-horizontal-coordinates
542 (time latitude longitude for-sunrise-sunset
)
543 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
545 TIME is a pair with the first component being the number of Julian centuries
546 elapsed at 0 Universal Time, and the second component being the universal
547 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
548 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
549 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
551 The azimuth is given in degrees as well as the height (between -180 and 180)."
552 (let* ((ut (car (cdr time
)))
553 (ec (solar-equatorial-coordinates time for-sunrise-sunset
))
554 (st (+ solar-sidereal-time-greenwich-midnight
555 (* ut
1.00273790935)))
556 (ah (- (* st
15) (* 15 (car ec
)) (* -
1 (calendar-longitude))))
557 ; hour angle (in degrees)
559 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah
)
560 (solar-sin-degrees latitude
))
561 (* (solar-tangent-degrees de
)
562 (solar-cosine-degrees latitude
)))
563 (solar-sin-degrees ah
)))
564 (height (solar-arcsin
565 (+ (* (solar-sin-degrees latitude
) (solar-sin-degrees de
))
566 (* (solar-cosine-degrees latitude
)
567 (solar-cosine-degrees de
)
568 (solar-cosine-degrees ah
))))))
569 (if (> height
180) (setq height
(- height
360)))
570 (list azimuth height
)))
572 (defun solar-equatorial-coordinates (time for-sunrise-sunset
)
573 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
575 TIME is a pair with the first component being the number of Julian centuries
576 elapsed at 0 Universal Time, and the second component being the universal
577 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
578 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
579 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
580 (let* ((tm (solar-ephemeris-time time
))
581 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset
)))
582 (list (solar-right-ascension (car ec
) (car (cdr ec
)))
583 (solar-declination (car ec
) (car (cdr ec
))))))
585 (defun solar-ecliptic-coordinates (time for-sunrise-sunset
)
586 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
587 equation of time (in hours) and nutation in longitude (in seconds)
588 at moment `time', expressed in julian centuries of Ephemeris Time
589 since January 1st, 2000, at 12 ET."
590 (let* ((l (+ 280.46645
592 (* 0.0003032 time time
))) ; sun mean longitude
594 (* 481267.8813 time
))) ; moon mean longitude
597 (* -
0.0001559 time time
)
598 (* -
0.00000048 time time time
))) ; sun mean anomaly
599 (i (+ 23.43929111 (* -
0.013004167 time
)
600 (* -
0.00000016389 time time
)
601 (* 0.0000005036 time time time
))); mean inclination
604 (* -
0.000014 time time
))
605 (solar-sin-degrees m
))
606 (* (+ 0.019993 (* -
0.000101 time
))
607 (solar-sin-degrees (* 2 m
)))
609 (solar-sin-degrees (* 3 m
))))) ; center equation
610 (L (+ l c
)) ; total longitude
612 (* -
1934.136 time
))) ; longitude of moon's ascending node
614 (nut (if (not for-sunrise-sunset
)
615 (+ (* -
17.20 (solar-sin-degrees omega
))
616 (* -
1.32 (solar-sin-degrees (* 2 l
)))
617 (* -
0.23 (solar-sin-degrees (* 2 ml
)))
618 (* 0.21 (solar-sin-degrees (* 2 omega
))))
620 ; nut = nutation in longitude, measured in seconds of angle.
621 (ecc (if (not for-sunrise-sunset
)
623 (* -
0.000042037 time
)
624 (* -
0.0000001236 time time
)) ; eccentricity of earth's orbit
629 (solar-sin-degrees omega
)))) ; apparent longitude of sun
630 (y (if (not for-sunrise-sunset
)
631 (* (solar-tangent-degrees (/ i
2))
632 (solar-tangent-degrees (/ i
2)))
634 (time-eq (if (not for-sunrise-sunset
)
635 (/ (* 12 (+ (* y
(solar-sin-degrees (* 2 l
)))
636 (* -
2 ecc
(solar-sin-degrees m
))
637 (* 4 ecc y
(solar-sin-degrees m
)
638 (solar-cosine-degrees (* 2 l
)))
639 (* -
0.5 y y
(solar-sin-degrees (* 4 l
)))
640 (* -
1.25 ecc ecc
(solar-sin-degrees (* 2 m
)))))
643 ; equation of time, in hours
644 (list app i time-eq nut
)))
646 (defun solar-longitude (d)
647 "Longitude of sun on astronomical (Julian) day number D.
648 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
650 The values of calendar-daylight-savings-starts,
651 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
652 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
653 calendar-time-zone are used to interpret local time."
654 (let* ((a-d (calendar-absolute-from-astro d
))
655 ;; get Universal Time
656 (date (calendar-astro-from-absolute
658 (if (dst-in-effect a-d
)
659 (/ calendar-daylight-time-offset
24.0 60.0) 0)
660 (/ calendar-time-zone
60.0 24.0))))
661 ;; get Ephemeris Time
662 (date (+ date
(solar-ephemeris-correction
663 (extract-calendar-year
664 (calendar-gregorian-from-absolute
666 (calendar-absolute-from-astro
668 (U (/ (- date
2451545) 3652500))
678 (* (car (cdr (cdr x
))) U
))
682 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U
)))) 973)))
683 (A1 (mod (+ 2.18 (* U
(+ -
3375.70 (* 0.36 U
)))) (* 2 pi
)))
684 (A2 (mod (+ 3.51 (* U
(+ 125666.39 (* 0.10 U
)))) (* 2 pi
)))
685 (nutation (* -
0.0000001 (+ (* 834 (sin A1
)) (* 64 (sin A2
))))))
686 (mod (radians-to-degrees (+ longitude aberration nutation
)) 360.0)))
688 (defconst solar-data-list
689 '((403406 4.721964 1.621043)
690 (195207 5.937458 62830.348067)
691 (119433 1.115589 62830.821524)
692 (112392 5.781616 62829.634302)
693 (3891 5.5474 125660.5691)
694 (2819 1.5120 125660.984)
695 (1721 4.1897 62832.4766)
697 (660 5.415 125659.31)
700 (314 5.198 777137.715)
701 (268 5.989 78604.191)
703 (234 1.423 39302.098)
705 (132 2.317 115067.698)
706 (129 3.193 15774.337)
738 (10 2.55 157208.40)))
740 (defun solar-ephemeris-correction (year)
741 "Ephemeris time minus Universal Time during Gregorian year.
744 For the years 1800-1987, the maximum error is 1.9 seconds.
745 For the other years, the maximum error is about 30 seconds."
746 (cond ((and (<= 1988 year
) (< year
2020))
747 (/ (+ year -
2000 67.0) 60.0 60.0 24.0))
748 ((and (<= 1900 year
) (< year
1988))
749 (let* ((theta (/ (- (calendar-astro-from-absolute
750 (calendar-absolute-from-gregorian
752 (calendar-astro-from-absolute
753 (calendar-absolute-from-gregorian
756 (theta2 (* theta theta
))
757 (theta3 (* theta2 theta
))
758 (theta4 (* theta2 theta2
))
759 (theta5 (* theta3 theta2
)))
766 (* 0.677066 theta3 theta3
)
767 (* -
0.212591 theta4 theta3
))))
768 ((and (<= 1800 year
) (< year
1900))
769 (let* ((theta (/ (- (calendar-astro-from-absolute
770 (calendar-absolute-from-gregorian
772 (calendar-astro-from-absolute
773 (calendar-absolute-from-gregorian
776 (theta2 (* theta theta
))
777 (theta3 (* theta2 theta
))
778 (theta4 (* theta2 theta2
))
779 (theta5 (* theta3 theta2
)))
786 (* 31.332267 theta3 theta3
)
787 (* 38.291999 theta4 theta3
)
788 (* 28.316289 theta4 theta4
)
789 (* 11.636204 theta4 theta5
)
790 (* 2.043794 theta5 theta5
))))
791 ((and (<= 1620 year
) (< year
1800))
792 (let ((x (/ (- year
1600) 10.0)))
793 (/ (+ (* 2.19167 x x
) (* -
40.675 x
) 196.58333) 60.0 60.0 24.0)))
794 (t (let* ((tmp (- (calendar-astro-from-absolute
795 (calendar-absolute-from-gregorian
798 (second (- (/ (* tmp tmp
) 41048480.0) 15)))
799 (/ second
60.0 60.0 24.0)))))
801 (defun solar-sidereal-time (t0)
802 "Sidereal time (in hours) in Greenwich.
804 At T0=Julian centuries of universal time.
805 T0 must correspond to 0 hours UT."
806 (let* ((mean-sid-time (+ 6.6973746
808 (* 0.0000258622 t0 t0
)
809 (* -
0.0000000017222 t0 t0 t0
)))
810 (et (solar-ephemeris-time (list t0
0.0)))
811 (nut-i (solar-ecliptic-coordinates et nil
))
812 (nut (car (cdr (cdr (cdr nut-i
))))) ; nutation
813 (i (car (cdr nut-i
)))) ; inclination
814 (mod (+ (mod (+ mean-sid-time
815 (/ (/ (* nut
(solar-cosine-degrees i
)) 15) 3600)) 24.0)
819 (defun solar-time-equation (date ut
)
820 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
821 (let* ((et (solar-date-to-et date ut
))
822 (ec (solar-ecliptic-coordinates et nil
)))
823 (car (cdr (cdr ec
)))))
825 (defun solar-date-to-et (date ut
)
826 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
827 Expressed in julian centuries of Ephemeris Time."
828 (let ((t0 (solar-julian-ut-centuries date
)))
829 (solar-ephemeris-time (list t0 ut
))))
832 (defun sunrise-sunset (&optional arg
)
833 "Local time of sunrise and sunset for today. Accurate to a few seconds.
834 If called with an optional prefix argument, prompt for date.
836 If called with an optional double prefix argument, prompt for longitude,
837 latitude, time zone, and date, and always use standard time.
839 This function is suitable for execution in a .emacs file."
841 (or arg
(setq arg
1))
843 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
845 (let* ((calendar-longitude
846 (if (< arg
16) calendar-longitude
848 "Enter longitude (decimal fraction; + east, - west): ")))
850 (if (< arg
16) calendar-latitude
852 "Enter latitude (decimal fraction; + north, - south): ")))
854 (if (< arg
16) calendar-time-zone
856 "Enter difference from Coordinated Universal Time (in minutes): ")))
857 (calendar-location-name
858 (if (< arg
16) calendar-location-name
859 (let ((float-output-format "%.1f"))
861 (if (numberp calendar-latitude
)
862 (abs calendar-latitude
)
863 (+ (aref calendar-latitude
0)
864 (/ (aref calendar-latitude
1) 60.0)))
865 (if (numberp calendar-latitude
)
866 (if (> calendar-latitude
0) "N" "S")
867 (if (equal (aref calendar-latitude
2) 'north
) "N" "S"))
868 (if (numberp calendar-longitude
)
869 (abs calendar-longitude
)
870 (+ (aref calendar-longitude
0)
871 (/ (aref calendar-longitude
1) 60.0)))
872 (if (numberp calendar-longitude
)
873 (if (> calendar-longitude
0) "E" "W")
874 (if (equal (aref calendar-longitude
2) 'east
)
876 (calendar-standard-time-zone-name
877 (if (< arg
16) calendar-standard-time-zone-name
878 (cond ((= calendar-time-zone
0) "UTC")
879 ((< calendar-time-zone
0)
880 (format "UTC%dmin" calendar-time-zone
))
881 (t (format "UTC+%dmin" calendar-time-zone
)))))
882 (calendar-daylight-savings-starts
883 (if (< arg
16) calendar-daylight-savings-starts
))
884 (calendar-daylight-savings-ends
885 (if (< arg
16) calendar-daylight-savings-ends
))
886 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
887 (date-string (calendar-date-string date t
))
888 (time-string (solar-sunrise-sunset-string date
))
889 (msg (format "%s: %s" date-string time-string
))
890 (one-window (one-window-p t
)))
891 (if (<= (length msg
) (frame-width))
893 (with-output-to-temp-buffer "*temp*"
894 (princ (concat date-string
"\n" time-string
)))
896 (substitute-command-keys
899 "Type \\[delete-other-windows] to remove temp window."
900 "Type \\[switch-to-buffer] RET to remove temp window.")
901 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
903 (defun calendar-sunrise-sunset ()
904 "Local time of sunrise and sunset for date under cursor.
905 Accurate to a few seconds."
907 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
909 (let ((date (calendar-cursor-to-date t
)))
911 (calendar-date-string date t t
)
912 (solar-sunrise-sunset-string date
))))
914 (defun diary-sunrise-sunset ()
915 "Local time of sunrise and sunset as a diary entry.
916 Accurate to a few seconds."
917 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
919 (solar-sunrise-sunset-string date
))
921 (defcustom diary-sabbath-candles-minutes
18
922 "*Number of minutes before sunset for sabbath candle lighting."
927 (defun diary-sabbath-candles (&optional mark
)
928 "Local time of candle lighting diary entry--applies if date is a Friday.
929 No diary entry if there is no sunset on that date.
931 An optional parameter MARK specifies a face or single-character string to
932 use when highlighting the day in the calendar."
933 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
935 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5);; Friday
936 (let* ((sunset (car (cdr (solar-sunrise-sunset date
))))
938 (cons (- (car sunset
)
939 (/ diary-sabbath-candles-minutes
60.0))
943 (format "%s Sabbath candle lighting"
944 (apply 'solar-time-string light
)))))))
946 ; from Meeus, 1991, page 167
947 (defconst solar-seasons-data
948 '((485 324.96 1934.136)
949 (203 337.23 32964.467)
951 (182 27.85 445267.112)
952 (156 73.14 45036.886)
953 (136 171.52 22518.443)
954 (77 222.54 65928.934)
957 (58 119.81 33718.147)
960 (45 247.54 29929.562)
961 (44 325.15 31555.956)
963 (18 155.12 67555.328)
965 (16 198.04 62894.029)
966 (14 199.76 31436.921)
968 (12 287.11 31931.756)
969 (12 320.81 34777.259)
971 (8 15.45 16859.074)))
973 (defun solar-equinoxes/solstices
(k year
)
974 "Date of equinox/solstice K for YEAR.
975 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
976 K=3, winter solstice.
977 RESULT is a gregorian local date.
979 Accurate to less than a minute between 1951 and 2050."
980 (let* ((JDE0 (solar-mean-equinoxes/solstices k year
))
981 (T (/ (- JDE0
2451545.0) 36525))
982 (W (- (* 35999.373 T
) 2.47))
983 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W
))
984 (* 0.0007 (solar-cosine-degrees (* 2 W
)))))
985 (S (apply '+ (mapcar '(lambda(x)
986 (* (car x
) (solar-cosine-degrees
987 (+ (* (car (cdr (cdr x
))) T
)
989 solar-seasons-data
)))
990 (JDE (+ JDE0
(/ (* 0.00001 S
) Delta-lambda
)))
991 (correction (+ 102.3 (* 123.5 T
) (* 32.5 T T
)))
992 ; ephemeris time correction
993 (JD (- JDE
(/ correction
86400)))
994 (date (calendar-gregorian-from-absolute (floor (- JD
1721424.5))))
995 (time (- (- JD
0.5) (floor (- JD
0.5))))
997 (list (car date
) (+ (car (cdr date
)) time
998 (/ (/ calendar-time-zone
60.0) 24.0))
999 (car (cdr (cdr date
))))))
1001 ; from Meeus, 1991, page 166
1002 (defun solar-mean-equinoxes/solstices
(k year
)
1003 "Julian day of mean equinox/solstice K for YEAR.
1004 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
1005 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
1006 (let ((y (/ year
1000.0))
1007 (z (/ (- year
2000) 1000.0)))
1008 (if (< year
1000) ; actually between -1000 and 1000
1009 (cond ((equal k
0) (+ 1721139.29189
1013 (* -
0.00071 y y y y
)))
1014 ((equal k
1) (+ 1721233.25401
1018 (* 0.00025 y y y y
)))
1019 ((equal k
2) (+ 1721325.70455
1023 (* 0.00074 y y y y
)))
1024 ((equal k
3) (+ 1721414.39987
1028 (* -
0.00006 y y y y
))))
1029 ; actually between 1000 and 3000
1030 (cond ((equal k
0) (+ 2451623.80984
1034 (* -
0.00057 z z z z
)))
1035 ((equal k
1) (+ 2451716.56767
1039 (* -
0.00030 z z z z
)))
1040 ((equal k
2) (+ 2451810.21715
1044 (* 0.00078 z z z z
)))
1045 ((equal k
3) (+ 2451900.05952
1049 (* 0.00032 z z z z
)))))))
1052 (defun solar-equinoxes-solstices ()
1053 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
1054 Requires floating point."
1055 (let ((m displayed-month
)
1057 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
1060 (let* ((calendar-standard-time-zone-name
1061 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
1062 (calendar-daylight-savings-starts
1063 (if calendar-time-zone calendar-daylight-savings-starts
))
1064 (calendar-daylight-savings-ends
1065 (if calendar-time-zone calendar-daylight-savings-ends
))
1066 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
1068 (d0 (solar-equinoxes/solstices k y
))
1069 (d1 (list (car d0
) (floor (car (cdr d0
))) (car (cdr (cdr d0
)))))
1070 (h0 (* 24 (- (car (cdr d0
)) (floor (car (cdr d0
))))))
1071 (adj (dst-adjust-time d1 h0
))
1072 (d (list (car (car adj
))
1073 (+ (car (cdr (car adj
)) )
1074 (/ (car (cdr adj
)) 24.0))
1075 (car (cdr (cdr (car adj
))))))
1076 ; The following is nearly as accurate, but not quite:
1077 ;(d0 (solar-date-next-longitude
1078 ; (calendar-astro-from-absolute
1079 ; (calendar-absolute-from-gregorian
1080 ; (list (+ 3 (* k 3)) 15 y)))
1082 ;(abs-day (calendar-absolute-from-astro d)))
1083 (abs-day (calendar-absolute-from-gregorian d
)))
1085 (list (calendar-gregorian-from-absolute (floor abs-day
))
1087 (nth k
(if (and calendar-latitude
1088 (< (calendar-latitude) 0))
1089 solar-s-hemi-seasons
1090 solar-n-hemi-seasons
))
1092 (* 24 (- abs-day
(floor abs-day
)))
1093 (if (dst-in-effect abs-day
)
1094 calendar-daylight-time-zone-name
1095 calendar-standard-time-zone-name
))))))))
1100 ;;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1101 ;;; solar.el ends here