(ada-case-keyword, ada-auto-case, ada-krunch-args,
[bpt/emacs.git] / lisp / calendar / solar.el
1 ;;; solar.el --- calendar functions for solar events.
2
3 ;; Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Denis B. Roegel <Denis.Roegel@loria.fr>
7 ;; Keywords: calendar
8 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
9 ;; holidays
10
11 ;; This file is part of GNU Emacs.
12
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)
16 ;; any later version.
17
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.
22
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.
26
27 ;;; Commentary:
28
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.
32
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,
39 ;; Inc., 1986.
40
41 ;;
42 ;; Accuracy:
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.
45 ;;
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.
48
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
54
55 ;;; Code:
56
57 (if (fboundp 'atan)
58 (require 'lisp-float-type)
59 (error "Solar/lunar calculations impossible since floating point is unavailable."))
60
61 (require 'cal-dst)
62 (require 'cal-julian)
63
64 ;;;###autoload
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.
69
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.
73
74 For example, the form
75
76 '(24-hours \":\" minutes
77 (if time-zone \" (\") time-zone (if time-zone \")\"))
78
79 would give military-style times like `21:07 (UTC)'.")
80
81 ;;;###autoload
82 (defvar calendar-latitude nil
83 "*Latitude of `calendar-location-name' in degrees.
84
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
88 York City.
89
90 This variable should be set in site-local.el.")
91
92 ;;;###autoload
93 (defvar calendar-longitude nil
94 "*Longitude of `calendar-location-name' in degrees.
95
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
99 York City.
100
101 This variable should be set in site-start.el.")
102
103 (defsubst calendar-latitude ()
104 "Convert calendar-latitude to a signed decimal fraction, if needed."
105 (if (numberp calendar-latitude)
106 calendar-latitude
107 (let ((lat (+ (aref calendar-latitude 0)
108 (/ (aref calendar-latitude 1) 60.0))))
109 (if (equal (aref calendar-latitude 2) 'north)
110 lat
111 (- lat)))))
112
113 (defsubst calendar-longitude ()
114 "Convert calendar-longitude to a signed decimal fraction, if needed."
115 (if (numberp calendar-longitude)
116 calendar-longitude
117 (let ((long (+ (aref calendar-longitude 0)
118 (/ (aref calendar-longitude 1) 60.0))))
119 (if (equal (aref calendar-longitude 2) 'east)
120 long
121 (- long)))))
122
123 ;;;###autoload
124 (defvar calendar-location-name
125 '(let ((float-output-format "%.1f"))
126 (format "%s%s, %s%s"
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
143 pair.
144
145 This variable should be set in site-start.el.")
146
147 (defvar solar-error 0.5
148 "*Tolerance (in minutes) for sunrise/sunset calculations.
149
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.
153
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).")
159
160 (defvar solar-n-hemi-seasons
161 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
162 "List of season changes for the northern hemisphere.")
163
164 (defvar solar-s-hemi-seasons
165 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
166 "List of season changes for the southern hemisphere.")
167
168 (defvar solar-sidereal-time-greenwich-midnight
169 nil
170 "Sidereal time at Greenwich at midnight (universal time).")
171
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.")
175
176 (defun solar-setup ()
177 "Prompt user for latitude, longitude, and time zone."
178 (beep)
179 (if (not calendar-longitude)
180 (setq calendar-longitude
181 (solar-get-number
182 "Enter longitude (decimal fraction; + east, - west): ")))
183 (if (not calendar-latitude)
184 (setq calendar-latitude
185 (solar-get-number
186 "Enter latitude (decimal fraction; + north, - south): ")))
187 (if (not calendar-time-zone)
188 (setq calendar-time-zone
189 (solar-get-number
190 "Enter difference from Coordinated Universal Time (in minutes): "))))
191
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 ""))
197 (string-to-int x))))
198
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)
202 (condition-case nil
203 (sin (degrees-to-radians (mod x 360.0)))
204 (solar-sin-degrees x)))
205 (defun solar-cosine-degrees (x)
206 (condition-case nil
207 (cos (degrees-to-radians (mod x 360.0)))
208 (solar-cosine-degrees x)))
209 (defun solar-tangent-degrees (x)
210 (condition-case nil
211 (tan (degrees-to-radians (mod x 360.0)))
212 (solar-tangent-degrees x)))
213
214 (defun solar-xy-to-quadrant (x y)
215 "Determines the quadrant of the point X, Y."
216 (if (> x 0)
217 (if (> y 0) 1 4)
218 (if (> y 0) 2 3)))
219
220 (defun solar-degrees-to-quadrant (angle)
221 "Determines the quadrant of ANGLE."
222 (1+ (floor (mod angle 360) 90)))
223
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))
230 (t deg))))
231
232 (defun solar-atn2 (x y)
233 "Arctan of point X, Y."
234 (if (= y 0)
235 (if (> x 0) 90 270)
236 (solar-arctan (/ x y) y)))
237
238 (defun solar-arccos (x)
239 "Arcos of X."
240 (let ((y (sqrt (- 1 (* x x)))))
241 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
242
243 (defun solar-arcsin (y)
244 "Arcsin of Y."
245 (let ((x (sqrt (- 1 (* y y)))))
246 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
247
248 (defsubst solar-degrees-to-hours (degrees)
249 "Convert DEGREES to hours."
250 (/ degrees 15.0))
251
252 (defsubst solar-hours-to-days (hour)
253 "Convert HOUR to decimal fraction of a day."
254 (/ hour 24.0))
255
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
260 (solar-arctan
261 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
262 (solar-degrees-to-quadrant longitude))))
263
264 (defun solar-declination (longitude obliquity)
265 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
266 Both arguments are in degrees."
267 (solar-arcsin
268 (* (solar-sin-degrees obliquity)
269 (solar-sin-degrees longitude))))
270
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.
274
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.
280
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))
285 (day-length))
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)))
289 (setq day-length 24)
290 (setq day-length 0))
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)))
294
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
298 being TIME.
299
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.
305
306 Uses binary search."
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
315 (t0 (car time))
316 (hmin (car (cdr
317 (solar-horizontal-coordinates (list t0 utmin)
318 latitude longitude t))))
319 (hmax (car (cdr
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
323 ; or sets.
324 (if (< hmin -0.61)
325 (if (> hmax -0.61)
326 (while ;(< i 20) ; we perform a simple dichotomy
327 ; (> (abs (+ hut 0.61)) epsilon)
328 (>= (abs (- utmoment utmoment-old))
329 (/ solar-error 60))
330 (setq utmoment-old utmoment)
331 (setq utmoment (/ (+ utmin utmax) 2))
332 (setq hut (car (cdr
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))
337 )
338 (setq possible 0)) the sun never rises
339 (setq possible 0)) ; the sun never sets
340 (if (equal possible 0) nil utmoment)))
341
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 "")))
352
353
354 (defun solar-daylight (time)
355 "Printable form for time expressed in hours."
356 (format "%d:%02d"
357 (floor time)
358 (floor (* 60 (- time (floor time))))))
359
360 (defun solar-exact-local-noon (date)
361 "Date and Universal Time of local noon at *local date* date.
362
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."
365 (let* ((nd date)
366 (ut (- 12.0 (/ calendar-longitude 15)))
367 (te (solar-time-equation date ut)))
368 (setq ut (- ut te))
369 (if (>= ut 24)
370 (progn
371 (setq nd (list (car date) (+ 1 (car (cdr date)))
372 (car (cdr (cdr date)))))
373 (setq ut (- ut 24))))
374 (if (< ut 0)
375 (progn
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
382 (list nd ut)))
383
384 (defun solar-sunrise-sunset (date)
385 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
386
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
394 (equator-rise-set
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)))
399 10.0
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)
405 (rise-set
406 (progn
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)))
411 calendar-latitude
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)))) )
418 (list
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))))
422
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)))
426 (format
427 "%s, %s at %s (%s hours daylight)"
428 (if (car l)
429 (concat "Sunrise " (apply 'solar-time-string (car l)))
430 "No sunrise")
431 (if (car (cdr l))
432 (concat "sunset " (apply 'solar-time-string (car (cdr l))))
433 "no sunset")
434 (eval calendar-location-name)
435 (car (cdr (cdr l))))))
436
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)))
441 36525.0))
442
443 (defun solar-ephemeris-time(time)
444 "Ephemeris Time at moment TIME.
445
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.
451
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))))
459
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).
464
465 L must be an integer divisor of 360.
466
467 Result is in local time expressed astronomical (Julian) day numbers.
468
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."
473 (let* ((long)
474 (start d)
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))
481 ;; start <= d < end
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)))
489 (progn
490 (setq start d)
491 (setq start-long long))
492 (setq end d)
493 (setq end-long long)))
494 (/ (+ start end) 2.0)))
495
496 (defun solar-horizontal-coordinates
497 (time latitude longitude for-sunrise-sunset)
498 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
499
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.
505
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)
513 (de (car (cdr ec)))
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)))
526
527 (defun solar-equatorial-coordinates (time for-sunrise-sunset)
528 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
529
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))))))
539
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
546 (* 36000.76983 time)
547 (* 0.0003032 time time))) ; sun mean longitude
548 (ml (+ 218.3165
549 (* 481267.8813 time))) ; moon mean longitude
550 (m (+ 357.52910
551 (* 35999.05030 time)
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
557 (c (+ (* (+ 1.914600
558 (* -0.004817 time)
559 (* -0.000014 time time))
560 (solar-sin-degrees m))
561 (* (+ 0.019993 (* -0.000101 time))
562 (solar-sin-degrees (* 2 m)))
563 (* 0.000290
564 (solar-sin-degrees (* 3 m))))) ; center equation
565 (L (+ l c)) ; total longitude
566 (omega (+ 125.04
567 (* -1934.136 time))) ; longitude of moon's ascending node
568 ; on the ecliptic
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))))
574 nil))
575 ; nut = nutation in longitude, measured in seconds of angle.
576 (ecc (if (not for-sunrise-sunset)
577 (+ 0.016708617
578 (* -0.000042037 time)
579 (* -0.0000001236 time time)) ; eccentricity of earth's orbit
580 nil))
581 (app (+ L
582 -0.00569
583 (* -0.00478
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)))
588 nil))
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)))))
596 3.1415926535)
597 nil)))
598 ; equation of time, in hours
599 (list app i time-eq nut)))
600
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).
604
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
612 (- a-d
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
620 (floor
621 (calendar-absolute-from-astro
622 date)))))))
623 (U (/ (- date 2451545) 3652500))
624 (longitude
625 (+ 4.9353929
626 (* 62833.1961680 U)
627 (* 0.0000001
628 (apply '+
629 (mapcar '(lambda (x)
630 (* (car x)
631 (sin (mod
632 (+ (car (cdr x))
633 (* (car (cdr (cdr x))) U))
634 (* 2 pi)))))
635 solar-data-list)))))
636 (aberration
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)))
642
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)
651 (0 1.163 0.813)
652 (660 5.415 125659.31)
653 (350 4.315 57533.85)
654 (334 4.553 -33.931)
655 (314 5.198 777137.715)
656 (268 5.989 78604.191)
657 (242 2.911 5.412)
658 (234 1.423 39302.098)
659 (158 0.061 -34.861)
660 (132 2.317 115067.698)
661 (129 3.193 15774.337)
662 (114 2.828 5296.670)
663 (99 0.52 58849.27)
664 (93 4.65 5296.11)
665 (86 4.35 -3980.70)
666 (78 2.75 52237.69)
667 (72 4.50 55076.47)
668 (68 3.23 261.08)
669 (64 1.22 15773.85)
670 (46 0.14 188491.03)
671 (38 3.44 -7756.55)
672 (37 4.37 264.89)
673 (32 1.14 117906.27)
674 (29 2.84 55075.75)
675 (28 5.96 -7961.39)
676 (27 5.09 188489.81)
677 (27 1.72 2132.19)
678 (25 2.56 109771.03)
679 (24 1.92 54868.56)
680 (21 0.09 25443.93)
681 (21 5.98 -55731.43)
682 (20 4.03 60697.74)
683 (18 4.47 2132.79)
684 (17 0.79 109771.63)
685 (14 4.24 -7752.82)
686 (13 2.01 188491.91)
687 (13 2.65 207.81)
688 (13 4.98 29424.63)
689 (12 0.93 -7.99)
690 (10 2.21 46941.14)
691 (10 3.59 -68.29)
692 (10 1.50 21463.25)
693 (10 2.55 157208.40)))
694
695 (defun solar-ephemeris-correction (year)
696 "Ephemeris time minus Universal Time during Gregorian year.
697 Result is in days.
698
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
706 (list 7 1 year)))
707 (calendar-astro-from-absolute
708 (calendar-absolute-from-gregorian
709 '(1 1 1900))))
710 36525.0))
711 (theta2 (* theta theta))
712 (theta3 (* theta2 theta))
713 (theta4 (* theta2 theta2))
714 (theta5 (* theta3 theta2)))
715 (+ -0.00002
716 (* 0.000297 theta)
717 (* 0.025184 theta2)
718 (* -0.181133 theta3)
719 (* 0.553040 theta4)
720 (* -0.861938 theta5)
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
726 (list 7 1 year)))
727 (calendar-astro-from-absolute
728 (calendar-absolute-from-gregorian
729 '(1 1 1900))))
730 36525.0))
731 (theta2 (* theta theta))
732 (theta3 (* theta2 theta))
733 (theta4 (* theta2 theta2))
734 (theta5 (* theta3 theta2)))
735 (+ -0.000009
736 (* 0.003844 theta)
737 (* 0.083563 theta2)
738 (* 0.865736 theta3)
739 (* 4.867575 theta4)
740 (* 15.845535 theta5)
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
751 (list 1 1 year)))
752 2382148))
753 (second (- (/ (* tmp tmp) 41048480.0) 15)))
754 (/ second 60.0 60.0 24.0)))))
755
756 (defun solar-sidereal-time (t0)
757 "Sidereal time (in hours) in Greenwich.
758
759 At T0=Julian centuries of universal time.
760 T0 must correspond to 0 hours UT."
761 (let* ((mean-sid-time (+ 6.6973746
762 (* 2400.051337 t0)
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)
771 24.0)
772 24.0)))
773
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)))))
779
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))))
785
786 ;;;###autoload
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.
790
791 If called with an optional double prefix argument, prompt for longitude,
792 latitude, time zone, and date, and always use standard time.
793
794 This function is suitable for execution in a .emacs file."
795 (interactive "p")
796 (or arg (setq arg 1))
797 (if (and (< arg 16)
798 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
799 (solar-setup))
800 (let* ((calendar-longitude
801 (if (< arg 16) calendar-longitude
802 (solar-get-number
803 "Enter longitude (decimal fraction; + east, - west): ")))
804 (calendar-latitude
805 (if (< arg 16) calendar-latitude
806 (solar-get-number
807 "Enter latitude (decimal fraction; + north, - south): ")))
808 (calendar-time-zone
809 (if (< arg 16) calendar-time-zone
810 (solar-get-number
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"))
815 (format "%s%s, %s%s"
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)
830 "E" "W"))))))
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))
847 (message msg)
848 (with-output-to-temp-buffer "*temp*"
849 (princ (concat date-string "\n" time-string)))
850 (message (substitute-command-keys
851 (if one-window
852 (if pop-up-windows
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."))))))
856
857 (defun calendar-sunrise-sunset ()
858 "Local time of sunrise and sunset for date under cursor.
859 Accurate to a few seconds."
860 (interactive)
861 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
862 (solar-setup))
863 (let ((date (calendar-cursor-to-date t)))
864 (message "%s: %s"
865 (calendar-date-string date t t)
866 (solar-sunrise-sunset-string date))))
867
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))
872 (solar-setup))
873 (solar-sunrise-sunset-string date))
874
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))
879 (solar-setup))
880 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
881 (let* ((sunset (car (cdr (solar-sunrise-sunset date))))
882 (light (if sunset
883 (dst-adjust-time
884 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)))))))
889
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.
895
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)
905 (car (cdr x))))))
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))))
913 )
914 (list (car date) (+ (car (cdr date)) time
915 (/ (/ calendar-time-zone 60.0) 24.0))
916 (car (cdr (cdr date))))))
917
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
927 (* 365242.13740 y)
928 (* 0.06134 y y)
929 (* 0.00111 y y y)
930 (* -0.00071 y y y y)))
931 ((equal k 1) (+ 1721233.25401
932 (* 365241.72562 y)
933 (* -0.05323 y y)
934 (* 0.00907 y y y)
935 (* 0.00025 y y y y)))
936 ((equal k 2) (+ 1721325.70455
937 (* 365242.49558 y)
938 (* -0.11677 y y)
939 (* -0.00297 y y y)
940 (* 0.00074 y y y y)))
941 ((equal k 3) (+ 1721414.39987
942 (* 365242.88257 y)
943 (* -0.00769 y y)
944 (* -0.00933 y y y)
945 (* -0.00006 y y y y))))
946 ; actually between 1000 and 3000
947 (cond ((equal k 0) (+ 2451623.80984
948 (* 365242.37404 z)
949 (* 0.05169 z z)
950 (* -0.00411 z z z)
951 (* -0.00057 z z z z)))
952 ((equal k 1) (+ 2451716.56767
953 (* 365241.62603 z)
954 (* 0.00325 z z)
955 (* 0.00888 z z z)
956 (* -0.00030 z z z z)))
957 ((equal k 2) (+ 2451810.21715
958 (* 365242.01767 z)
959 (* -0.11575 z z)
960 (* 0.00337 z z z)
961 (* 0.00078 z z z z)))
962 ((equal k 3) (+ 2451900.05952
963 (* 365242.74049 z)
964 (* -0.06223 z z)
965 (* -0.00823 z z z)
966 (* 0.00032 z z z z)))))))
967
968 ; from Meeus, 1991, page 167
969 (defconst solar-seasons-data
970 '((485 324.96 1934.136)
971 (203 337.23 32964.467)
972 (199 342.08 20.186)
973 (182 27.85 445267.112)
974 (156 73.14 45036.886)
975 (136 171.52 22518.443)
976 (77 222.54 65928.934)
977 (74 296.72 3034.906)
978 (70 243.58 9037.513)
979 (58 119.81 33718.147)
980 (52 297.17 150.678)
981 (50 21.02 2281.226)
982 (45 247.54 29929.562)
983 (44 325.15 31555.956)
984 (29 60.93 4443.417)
985 (18 155.12 67555.328)
986 (17 288.79 4562.452)
987 (16 198.04 62894.029)
988 (14 199.76 31436.921)
989 (12 95.39 14577.848)
990 (12 287.11 31931.756)
991 (12 320.81 34777.259)
992 (9 227.73 1222.114)
993 (8 15.45 16859.074)))
994
995 ;;;###autoload
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)
1000 (y displayed-year))
1001 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
1002 ((= 2 (% m 3)) 1)
1003 (t 0)))
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))
1011 (k (1- (/ m 3)))
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)))
1024 ; 90))
1025 ;(abs-day (calendar-absolute-from-astro d)))
1026 (abs-day (calendar-absolute-from-gregorian d)))
1027 (list
1028 (list (calendar-gregorian-from-absolute (floor abs-day))
1029 (format "%s %s"
1030 (nth k (if (and calendar-latitude
1031 (< (calendar-latitude) 0))
1032 solar-s-hemi-seasons
1033 solar-n-hemi-seasons))
1034 (solar-time-string
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))))))))
1039
1040
1041 (provide 'solar)
1042
1043 ;;; solar.el ends here