]> code.delx.au - gnu-emacs/blob - lisp/calendar/solar.el
(calendar-location-name): Doc fix.
[gnu-emacs] / lisp / calendar / solar.el
1 ;;; solar.el --- calendar functions for solar events.
2
3 ;; Copyright (C) 1992, 1993, 1995, 1997 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 the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
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.
33
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,
40 ;; Inc., 1986.
41
42 ;;
43 ;; Accuracy:
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.
46 ;;
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.
49
50 ;; Comments, corrections, and improvements should be sent to
51 ;; Edward M. Reingold Department of Computer Science
52 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
53 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
54 ;; Urbana, Illinois 61801
55
56 ;;; Code:
57
58 (if (fboundp 'atan)
59 (require 'lisp-float-type)
60 (error "Solar/lunar calculations impossible since floating point is unavailable."))
61
62 (require 'cal-dst)
63 (require 'cal-julian)
64
65 ;;;###autoload
66 (defcustom calendar-time-display-form
67 '(12-hours ":" minutes am-pm
68 (if time-zone " (") time-zone (if time-zone ")"))
69 "*The pseudo-pattern that governs the way a time of day is formatted.
70
71 A pseudo-pattern is a list of expressions that can involve the keywords
72 `12-hours', `24-hours', and `minutes', all numbers in string form,
73 and `am-pm' and `time-zone', both alphabetic strings.
74
75 For example, the form
76
77 '(24-hours \":\" minutes
78 (if time-zone \" (\") time-zone (if time-zone \")\"))
79
80 would give military-style times like `21:07 (UTC)'."
81 :type 'sexp
82 :group 'calendar)
83
84 ;;;###autoload
85 (defcustom calendar-latitude nil
86 "*Latitude of `calendar-location-name' in degrees.
87
88 The value can be either a decimal fraction (one place of accuracy is
89 sufficient), + north, - south, such as 40.7 for New York City, or the value
90 can be a vector [degrees minutes north/south] such as [40 50 north] for New
91 York City.
92
93 This variable should be set in `site-start'.el."
94 :type '(choice (const nil)
95 (number :tag "Exact")
96 (vector :value [0 0 north]
97 (integer :tag "Degrees")
98 (integer :tag "Minutes")
99 (choice :tag "Position"
100 (const north)
101 (const south))))
102 :group 'calendar)
103
104 ;;;###autoload
105 (defcustom calendar-longitude nil
106 "*Longitude of `calendar-location-name' in degrees.
107
108 The value can be either a decimal fraction (one place of accuracy is
109 sufficient), + east, - west, such as -73.9 for New York City, or the value
110 can be a vector [degrees minutes east/west] such as [73 55 west] for New
111 York City.
112
113 This variable should be set in `site-start'.el."
114 :type '(choice (const nil)
115 (number :tag "Exact")
116 (vector :value [0 0 west]
117 (integer :tag "Degrees")
118 (integer :tag "Minutes")
119 (choice :tag "Position"
120 (const east)
121 (const west))))
122 :group 'calendar)
123
124 (defsubst calendar-latitude ()
125 "Convert calendar-latitude to a signed decimal fraction, if needed."
126 (if (numberp calendar-latitude)
127 calendar-latitude
128 (let ((lat (+ (aref calendar-latitude 0)
129 (/ (aref calendar-latitude 1) 60.0))))
130 (if (equal (aref calendar-latitude 2) 'north)
131 lat
132 (- lat)))))
133
134 (defsubst calendar-longitude ()
135 "Convert calendar-longitude to a signed decimal fraction, if needed."
136 (if (numberp calendar-longitude)
137 calendar-longitude
138 (let ((long (+ (aref calendar-longitude 0)
139 (/ (aref calendar-longitude 1) 60.0))))
140 (if (equal (aref calendar-longitude 2) 'east)
141 long
142 (- long)))))
143
144 ;;;###autoload
145 (defcustom calendar-location-name
146 '(let ((float-output-format "%.1f"))
147 (format "%s%s, %s%s"
148 (if (numberp calendar-latitude)
149 (abs calendar-latitude)
150 (+ (aref calendar-latitude 0)
151 (/ (aref calendar-latitude 1) 60.0)))
152 (if (numberp calendar-latitude)
153 (if (> calendar-latitude 0) "N" "S")
154 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
155 (if (numberp calendar-longitude)
156 (abs calendar-longitude)
157 (+ (aref calendar-longitude 0)
158 (/ (aref calendar-longitude 1) 60.0)))
159 (if (numberp calendar-longitude)
160 (if (> calendar-longitude 0) "E" "W")
161 (if (equal (aref calendar-longitude 2) 'east) "E" "W"))))
162 "*Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
163 For example, \"New York City\". Default value is just the latitude, longitude
164 pair.
165
166 This variable should be set in `site-start'.el."
167 :type 'sexp
168 :group 'calendar)
169
170 (defcustom solar-error 0.5
171 "*Tolerance (in minutes) for sunrise/sunset calculations.
172
173 A larger value makes the calculations for sunrise/sunset faster, but less
174 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
175 times will be correct to the minute.
176
177 It is useless to set the value smaller than 4*delta, where delta is the
178 accuracy in the longitude of the sun (given by the function
179 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
180 delta. At present, delta = 0.01 degrees, so the value of the variable
181 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
182 :type 'number
183 :group 'calendar)
184
185 (defvar solar-n-hemi-seasons
186 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
187 "List of season changes for the northern hemisphere.")
188
189 (defvar solar-s-hemi-seasons
190 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
191 "List of season changes for the southern hemisphere.")
192
193 (defvar solar-sidereal-time-greenwich-midnight
194 nil
195 "Sidereal time at Greenwich at midnight (universal time).")
196
197 (defvar solar-spring-or-summer-season nil
198 "T if spring or summer and nil otherwise.
199 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
200
201 (defun solar-setup ()
202 "Prompt user for latitude, longitude, and time zone."
203 (beep)
204 (if (not calendar-longitude)
205 (setq calendar-longitude
206 (solar-get-number
207 "Enter longitude (decimal fraction; + east, - west): ")))
208 (if (not calendar-latitude)
209 (setq calendar-latitude
210 (solar-get-number
211 "Enter latitude (decimal fraction; + north, - south): ")))
212 (if (not calendar-time-zone)
213 (setq calendar-time-zone
214 (solar-get-number
215 "Enter difference from Coordinated Universal Time (in minutes): "))))
216
217 (defun solar-get-number (prompt)
218 "Return a number from the minibuffer, prompting with PROMPT.
219 Returns nil if nothing was entered."
220 (let ((x (read-string prompt "")))
221 (if (not (string-equal x ""))
222 (string-to-int x))))
223
224 ;; The condition-case stuff is needed to catch bogus arithmetic
225 ;; exceptions that occur on some machines (like Sparcs)
226 (defun solar-sin-degrees (x)
227 (condition-case nil
228 (sin (degrees-to-radians (mod x 360.0)))
229 (solar-sin-degrees x)))
230 (defun solar-cosine-degrees (x)
231 (condition-case nil
232 (cos (degrees-to-radians (mod x 360.0)))
233 (solar-cosine-degrees x)))
234 (defun solar-tangent-degrees (x)
235 (condition-case nil
236 (tan (degrees-to-radians (mod x 360.0)))
237 (solar-tangent-degrees x)))
238
239 (defun solar-xy-to-quadrant (x y)
240 "Determines the quadrant of the point X, Y."
241 (if (> x 0)
242 (if (> y 0) 1 4)
243 (if (> y 0) 2 3)))
244
245 (defun solar-degrees-to-quadrant (angle)
246 "Determines the quadrant of ANGLE."
247 (1+ (floor (mod angle 360) 90)))
248
249 (defun solar-arctan (x quad)
250 "Arctangent of X in quadrant QUAD."
251 (let ((deg (radians-to-degrees (atan x))))
252 (cond ((equal quad 2) (+ deg 180))
253 ((equal quad 3) (+ deg 180))
254 ((equal quad 4) (+ deg 360))
255 (t deg))))
256
257 (defun solar-atn2 (x y)
258 "Arctan of point X, Y."
259 (if (= x 0)
260 (if (> y 0) 90 270)
261 (solar-arctan (/ y x) x)))
262
263 (defun solar-arccos (x)
264 "Arcos of X."
265 (let ((y (sqrt (- 1 (* x x)))))
266 (solar-atn2 x y)))
267
268 (defun solar-arcsin (y)
269 "Arcsin of Y."
270 (let ((x (sqrt (- 1 (* y y)))))
271 (solar-atn2 x y)
272 ))
273
274 (defsubst solar-degrees-to-hours (degrees)
275 "Convert DEGREES to hours."
276 (/ degrees 15.0))
277
278 (defsubst solar-hours-to-days (hour)
279 "Convert HOUR to decimal fraction of a day."
280 (/ hour 24.0))
281
282 (defun solar-right-ascension (longitude obliquity)
283 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
284 Both arguments are in degrees."
285 (solar-degrees-to-hours
286 (solar-arctan
287 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
288 (solar-degrees-to-quadrant longitude))))
289
290 (defun solar-declination (longitude obliquity)
291 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
292 Both arguments are in degrees."
293 (solar-arcsin
294 (* (solar-sin-degrees obliquity)
295 (solar-sin-degrees longitude))))
296
297 (defun solar-sunrise-and-sunset (time latitude longitude)
298 "Sunrise, sunset and length of day.
299 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
300
301 TIME is a pair with the first component being the number of Julian centuries
302 elapsed at 0 Universal Time, and the second component being the universal
303 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
304 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
305 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
306
307 Coordinates are included because this function is called with latitude=10
308 degrees to find out if polar regions have 24 hours of sun or only night."
309 (let* ((rise-time (solar-moment -1 latitude longitude time))
310 (set-time (solar-moment 1 latitude longitude time))
311 (day-length))
312 (if (not (and rise-time set-time))
313 (if (or (and (> latitude 0) solar-spring-or-summer-season)
314 (and (< latitude 0) (not solar-spring-or-summer-season)))
315 (setq day-length 24)
316 (setq day-length 0))
317 (setq day-length (- set-time rise-time)))
318 (list (if rise-time (+ rise-time (/ calendar-time-zone 60.0)) nil)
319 (if set-time (+ set-time (/ calendar-time-zone 60.0)) nil)
320 day-length)))
321
322 (defun solar-moment (direction latitude longitude time)
323 "Sunrise/sunset at location.
324 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
325 being TIME.
326
327 TIME is a pair with the first component being the number of Julian centuries
328 elapsed at 0 Universal Time, and the second component being the universal
329 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
330 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
331 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
332
333 Uses binary search."
334 (let* ((ut (car (cdr time)))
335 (possible 1) ; we assume that rise or set are possible
336 (utmin (+ ut (* direction 12.0)))
337 (utmax ut) ; the time searched is between utmin and utmax
338 ; utmin and utmax are in hours
339 (utmoment-old 0.0) ; rise or set approximation
340 (utmoment 1.0) ; rise or set approximation
341 (hut 0) ; sun height at utmoment
342 (t0 (car time))
343 (hmin (car (cdr
344 (solar-horizontal-coordinates (list t0 utmin)
345 latitude longitude t))))
346 (hmax (car (cdr
347 (solar-horizontal-coordinates (list t0 utmax)
348 latitude longitude t)))))
349 ; -0.61 degrees is the height of the middle of the sun, when it rises
350 ; or sets.
351 (if (< hmin -0.61)
352 (if (> hmax -0.61)
353 (while ;(< i 20) ; we perform a simple dichotomy
354 ; (> (abs (+ hut 0.61)) epsilon)
355 (>= (abs (- utmoment utmoment-old))
356 (/ solar-error 60))
357 (setq utmoment-old utmoment)
358 (setq utmoment (/ (+ utmin utmax) 2))
359 (setq hut (car (cdr
360 (solar-horizontal-coordinates
361 (list t0 utmoment) latitude longitude t))))
362 (if (< hut -0.61) (setq utmin utmoment))
363 (if (> hut -0.61) (setq utmax utmoment))
364 )
365 (setq possible 0)) ; the sun never rises
366 (setq possible 0)) ; the sun never sets
367 (if (equal possible 0) nil utmoment)))
368
369 (defun solar-time-string (time time-zone)
370 "Printable form for decimal fraction TIME in TIME-ZONE.
371 Format used is given by `calendar-time-display-form'."
372 (let* ((time (round (* 60 time)))
373 (24-hours (/ time 60))
374 (minutes (format "%02d" (% time 60)))
375 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
376 (am-pm (if (>= 24-hours 12) "pm" "am"))
377 (24-hours (format "%02d" 24-hours)))
378 (mapconcat 'eval calendar-time-display-form "")))
379
380
381 (defun solar-daylight (time)
382 "Printable form for time expressed in hours."
383 (format "%d:%02d"
384 (floor time)
385 (floor (* 60 (- time (floor time))))))
386
387 (defun solar-exact-local-noon (date)
388 "Date and Universal Time of local noon at *local date* date.
389
390 The date may be different from the one asked for, but it will be the right
391 local date. The second component of date should be an integer."
392 (let* ((nd date)
393 (ut (- 12.0 (/ (calendar-longitude) 15)))
394 (te (solar-time-equation date ut)))
395 (setq ut (- ut te))
396 (if (>= ut 24)
397 (progn
398 (setq nd (list (car date) (+ 1 (car (cdr date)))
399 (car (cdr (cdr date)))))
400 (setq ut (- ut 24))))
401 (if (< ut 0)
402 (progn
403 (setq nd (list (car date) (- (car (cdr date)) 1)
404 (car (cdr (cdr date)))))
405 (setq ut (+ ut 24))))
406 (setq nd (calendar-gregorian-from-absolute
407 (calendar-absolute-from-gregorian nd)))
408 ; date standardization
409 (list nd ut)))
410
411 (defun solar-sunrise-sunset (date)
412 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
413
414 Corresponding value is nil if there is no sunrise/sunset."
415 (let* (; first, get the exact moment of local noon.
416 (exact-local-noon (solar-exact-local-noon date))
417 ; get the the time from the 2000 epoch.
418 (t0 (solar-julian-ut-centuries (car exact-local-noon)))
419 ; store the sidereal time at Greenwich at midnight of UT time.
420 ; find if summer or winter slightly above the equator
421 (equator-rise-set
422 (progn (setq solar-sidereal-time-greenwich-midnight
423 (solar-sidereal-time t0))
424 (solar-sunrise-and-sunset
425 (list t0 (car (cdr exact-local-noon)))
426 10.0
427 (calendar-longitude))))
428 ; store the spring/summer information,
429 ; compute sunrise and sunset (two first components of rise-set).
430 ; length of day is the third component (it is only the difference
431 ; between sunset and sunrise when there is a sunset and a sunrise)
432 (rise-set
433 (progn
434 (setq solar-spring-or-summer-season
435 (if (> (car (cdr (cdr equator-rise-set))) 12) 1 0))
436 (solar-sunrise-and-sunset
437 (list t0 (car (cdr exact-local-noon)))
438 (calendar-latitude)
439 (calendar-longitude))))
440 (rise (car rise-set))
441 (adj-rise (if rise (dst-adjust-time date rise) nil))
442 (set (car (cdr rise-set)))
443 (adj-set (if set (dst-adjust-time date set) nil))
444 (length (car (cdr (cdr rise-set)))) )
445 (list
446 (and rise (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
447 (and set (calendar-date-equal date (car adj-set)) (cdr adj-set))
448 (solar-daylight length))))
449
450 (defun solar-sunrise-sunset-string (date)
451 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
452 (let ((l (solar-sunrise-sunset date)))
453 (format
454 "%s, %s at %s (%s hours daylight)"
455 (if (car l)
456 (concat "Sunrise " (apply 'solar-time-string (car l)))
457 "No sunrise")
458 (if (car (cdr l))
459 (concat "sunset " (apply 'solar-time-string (car (cdr l))))
460 "no sunset")
461 (eval calendar-location-name)
462 (car (cdr (cdr l))))))
463
464 (defun solar-julian-ut-centuries (date)
465 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
466 (/ (- (calendar-absolute-from-gregorian date)
467 (calendar-absolute-from-gregorian '(1 1.5 2000)))
468 36525.0))
469
470 (defun solar-ephemeris-time(time)
471 "Ephemeris Time at moment TIME.
472
473 TIME is a pair with the first component being the number of Julian centuries
474 elapsed at 0 Universal Time, and the second component being the universal
475 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
476 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
477 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
478
479 Result is in julian centuries of ephemeris time."
480 (let* ((t0 (car time))
481 (ut (car (cdr time)))
482 (t1 (+ t0 (/ (/ ut 24.0) 36525)))
483 (y (+ 2000 (* 100 t1)))
484 (dt (* 86400 (solar-ephemeris-correction (floor y)))))
485 (+ t1 (/ (/ dt 86400) 36525))))
486
487 (defun solar-date-next-longitude (d l)
488 "First moment on or after Julian day number D when sun's longitude is a
489 multiple of L degrees at calendar-location-name with that location's
490 local time (including any daylight savings rules).
491
492 L must be an integer divisor of 360.
493
494 Result is in local time expressed astronomical (Julian) day numbers.
495
496 The values of calendar-daylight-savings-starts,
497 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
498 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
499 calendar-time-zone are used to interpret local time."
500 (let* ((long)
501 (start d)
502 (start-long (solar-longitude d))
503 (next (mod (* l (1+ (floor (/ start-long l)))) 360))
504 (end (+ d (* (/ l 360.0) 400)))
505 (end-long (solar-longitude end)))
506 (while ;; bisection search for nearest minute
507 (< 0.00001 (- end start))
508 ;; start <= d < end
509 ;; start-long <= next < end-long when next != 0
510 ;; when next = 0, we look for the discontinuity (start-long is near 360
511 ;; and end-long is small (less than l).
512 (setq d (/ (+ start end) 2.0))
513 (setq long (solar-longitude d))
514 (if (or (and (/= next 0) (< long next))
515 (and (= next 0) (< l long)))
516 (progn
517 (setq start d)
518 (setq start-long long))
519 (setq end d)
520 (setq end-long long)))
521 (/ (+ start end) 2.0)))
522
523 (defun solar-horizontal-coordinates
524 (time latitude longitude for-sunrise-sunset)
525 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
526
527 TIME is a pair with the first component being the number of Julian centuries
528 elapsed at 0 Universal Time, and the second component being the universal
529 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
530 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
531 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
532
533 The azimuth is given in degrees as well as the height (between -180 and 180)."
534 (let* ((ut (car (cdr time)))
535 (ec (solar-equatorial-coordinates time for-sunrise-sunset))
536 (st (+ solar-sidereal-time-greenwich-midnight
537 (* ut 1.00273790935)))
538 (ah (- (* st 15) (* 15 (car ec)) (* -1 (calendar-longitude))))
539 ; hour angle (in degrees)
540 (de (car (cdr ec)))
541 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah)
542 (solar-sin-degrees latitude))
543 (* (solar-tangent-degrees de)
544 (solar-cosine-degrees latitude)))
545 (solar-sin-degrees ah)))
546 (height (solar-arcsin
547 (+ (* (solar-sin-degrees latitude) (solar-sin-degrees de))
548 (* (solar-cosine-degrees latitude)
549 (solar-cosine-degrees de)
550 (solar-cosine-degrees ah))))))
551 (if (> height 180) (setq height (- height 360)))
552 (list azimuth height)))
553
554 (defun solar-equatorial-coordinates (time for-sunrise-sunset)
555 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
556
557 TIME is a pair with the first component being the number of Julian centuries
558 elapsed at 0 Universal Time, and the second component being the universal
559 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
560 \(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
561 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
562 (let* ((tm (solar-ephemeris-time time))
563 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset)))
564 (list (solar-right-ascension (car ec) (car (cdr ec)))
565 (solar-declination (car ec) (car (cdr ec))))))
566
567 (defun solar-ecliptic-coordinates (time for-sunrise-sunset)
568 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
569 equation of time (in hours) and nutation in longitude (in seconds)
570 at moment `time', expressed in julian centuries of Ephemeris Time
571 since January 1st, 2000, at 12 ET."
572 (let* ((l (+ 280.46645
573 (* 36000.76983 time)
574 (* 0.0003032 time time))) ; sun mean longitude
575 (ml (+ 218.3165
576 (* 481267.8813 time))) ; moon mean longitude
577 (m (+ 357.52910
578 (* 35999.05030 time)
579 (* -0.0001559 time time)
580 (* -0.00000048 time time time))) ; sun mean anomaly
581 (i (+ 23.43929111 (* -0.013004167 time)
582 (* -0.00000016389 time time)
583 (* 0.0000005036 time time time))); mean inclination
584 (c (+ (* (+ 1.914600
585 (* -0.004817 time)
586 (* -0.000014 time time))
587 (solar-sin-degrees m))
588 (* (+ 0.019993 (* -0.000101 time))
589 (solar-sin-degrees (* 2 m)))
590 (* 0.000290
591 (solar-sin-degrees (* 3 m))))) ; center equation
592 (L (+ l c)) ; total longitude
593 (omega (+ 125.04
594 (* -1934.136 time))) ; longitude of moon's ascending node
595 ; on the ecliptic
596 (nut (if (not for-sunrise-sunset)
597 (+ (* -17.20 (solar-sin-degrees omega))
598 (* -1.32 (solar-sin-degrees (* 2 l)))
599 (* -0.23 (solar-sin-degrees (* 2 ml)))
600 (* 0.21 (solar-sin-degrees (* 2 omega))))
601 nil))
602 ; nut = nutation in longitude, measured in seconds of angle.
603 (ecc (if (not for-sunrise-sunset)
604 (+ 0.016708617
605 (* -0.000042037 time)
606 (* -0.0000001236 time time)) ; eccentricity of earth's orbit
607 nil))
608 (app (+ L
609 -0.00569
610 (* -0.00478
611 (solar-sin-degrees omega)))) ; apparent longitude of sun
612 (y (if (not for-sunrise-sunset)
613 (* (solar-tangent-degrees (/ i 2))
614 (solar-tangent-degrees (/ i 2)))
615 nil))
616 (time-eq (if (not for-sunrise-sunset)
617 (/ (* 12 (+ (* y (solar-sin-degrees (* 2 l)))
618 (* -2 ecc (solar-sin-degrees m))
619 (* 4 ecc y (solar-sin-degrees m)
620 (solar-cosine-degrees (* 2 l)))
621 (* -0.5 y y (solar-sin-degrees (* 4 l)))
622 (* -1.25 ecc ecc (solar-sin-degrees (* 2 m)))))
623 3.1415926535)
624 nil)))
625 ; equation of time, in hours
626 (list app i time-eq nut)))
627
628 (defun solar-longitude (d)
629 "Longitude of sun on astronomical (Julian) day number D.
630 Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
631
632 The values of calendar-daylight-savings-starts,
633 calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
634 calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
635 calendar-time-zone are used to interpret local time."
636 (let* ((a-d (calendar-absolute-from-astro d))
637 ;; get Universal Time
638 (date (calendar-astro-from-absolute
639 (- a-d
640 (if (dst-in-effect a-d)
641 (/ calendar-daylight-time-offset 24.0 60.0) 0)
642 (/ calendar-time-zone 60.0 24.0))))
643 ;; get Ephemeris Time
644 (date (+ date (solar-ephemeris-correction
645 (extract-calendar-year
646 (calendar-gregorian-from-absolute
647 (floor
648 (calendar-absolute-from-astro
649 date)))))))
650 (U (/ (- date 2451545) 3652500))
651 (longitude
652 (+ 4.9353929
653 (* 62833.1961680 U)
654 (* 0.0000001
655 (apply '+
656 (mapcar '(lambda (x)
657 (* (car x)
658 (sin (mod
659 (+ (car (cdr x))
660 (* (car (cdr (cdr x))) U))
661 (* 2 pi)))))
662 solar-data-list)))))
663 (aberration
664 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U)))) 973)))
665 (A1 (mod (+ 2.18 (* U (+ -3375.70 (* 0.36 U)))) (* 2 pi)))
666 (A2 (mod (+ 3.51 (* U (+ 125666.39 (* 0.10 U)))) (* 2 pi)))
667 (nutation (* -0.0000001 (+ (* 834 (sin A1)) (* 64 (sin A2))))))
668 (mod (radians-to-degrees (+ longitude aberration nutation)) 360.0)))
669
670 (defconst solar-data-list
671 '((403406 4.721964 1.621043)
672 (195207 5.937458 62830.348067)
673 (119433 1.115589 62830.821524)
674 (112392 5.781616 62829.634302)
675 (3891 5.5474 125660.5691)
676 (2819 1.5120 125660.984)
677 (1721 4.1897 62832.4766)
678 (0 1.163 0.813)
679 (660 5.415 125659.31)
680 (350 4.315 57533.85)
681 (334 4.553 -33.931)
682 (314 5.198 777137.715)
683 (268 5.989 78604.191)
684 (242 2.911 5.412)
685 (234 1.423 39302.098)
686 (158 0.061 -34.861)
687 (132 2.317 115067.698)
688 (129 3.193 15774.337)
689 (114 2.828 5296.670)
690 (99 0.52 58849.27)
691 (93 4.65 5296.11)
692 (86 4.35 -3980.70)
693 (78 2.75 52237.69)
694 (72 4.50 55076.47)
695 (68 3.23 261.08)
696 (64 1.22 15773.85)
697 (46 0.14 188491.03)
698 (38 3.44 -7756.55)
699 (37 4.37 264.89)
700 (32 1.14 117906.27)
701 (29 2.84 55075.75)
702 (28 5.96 -7961.39)
703 (27 5.09 188489.81)
704 (27 1.72 2132.19)
705 (25 2.56 109771.03)
706 (24 1.92 54868.56)
707 (21 0.09 25443.93)
708 (21 5.98 -55731.43)
709 (20 4.03 60697.74)
710 (18 4.47 2132.79)
711 (17 0.79 109771.63)
712 (14 4.24 -7752.82)
713 (13 2.01 188491.91)
714 (13 2.65 207.81)
715 (13 4.98 29424.63)
716 (12 0.93 -7.99)
717 (10 2.21 46941.14)
718 (10 3.59 -68.29)
719 (10 1.50 21463.25)
720 (10 2.55 157208.40)))
721
722 (defun solar-ephemeris-correction (year)
723 "Ephemeris time minus Universal Time during Gregorian year.
724 Result is in days.
725
726 For the years 1800-1987, the maximum error is 1.9 seconds.
727 For the other years, the maximum error is about 30 seconds."
728 (cond ((and (<= 1988 year) (< year 2020))
729 (/ (+ year -2000 67.0) 60.0 60.0 24.0))
730 ((and (<= 1900 year) (< year 1988))
731 (let* ((theta (/ (- (calendar-astro-from-absolute
732 (calendar-absolute-from-gregorian
733 (list 7 1 year)))
734 (calendar-astro-from-absolute
735 (calendar-absolute-from-gregorian
736 '(1 1 1900))))
737 36525.0))
738 (theta2 (* theta theta))
739 (theta3 (* theta2 theta))
740 (theta4 (* theta2 theta2))
741 (theta5 (* theta3 theta2)))
742 (+ -0.00002
743 (* 0.000297 theta)
744 (* 0.025184 theta2)
745 (* -0.181133 theta3)
746 (* 0.553040 theta4)
747 (* -0.861938 theta5)
748 (* 0.677066 theta3 theta3)
749 (* -0.212591 theta4 theta3))))
750 ((and (<= 1800 year) (< year 1900))
751 (let* ((theta (/ (- (calendar-astro-from-absolute
752 (calendar-absolute-from-gregorian
753 (list 7 1 year)))
754 (calendar-astro-from-absolute
755 (calendar-absolute-from-gregorian
756 '(1 1 1900))))
757 36525.0))
758 (theta2 (* theta theta))
759 (theta3 (* theta2 theta))
760 (theta4 (* theta2 theta2))
761 (theta5 (* theta3 theta2)))
762 (+ -0.000009
763 (* 0.003844 theta)
764 (* 0.083563 theta2)
765 (* 0.865736 theta3)
766 (* 4.867575 theta4)
767 (* 15.845535 theta5)
768 (* 31.332267 theta3 theta3)
769 (* 38.291999 theta4 theta3)
770 (* 28.316289 theta4 theta4)
771 (* 11.636204 theta4 theta5)
772 (* 2.043794 theta5 theta5))))
773 ((and (<= 1620 year) (< year 1800))
774 (let ((x (/ (- year 1600) 10.0)))
775 (/ (+ (* 2.19167 x x) (* -40.675 x) 196.58333) 60.0 60.0 24.0)))
776 (t (let* ((tmp (- (calendar-astro-from-absolute
777 (calendar-absolute-from-gregorian
778 (list 1 1 year)))
779 2382148))
780 (second (- (/ (* tmp tmp) 41048480.0) 15)))
781 (/ second 60.0 60.0 24.0)))))
782
783 (defun solar-sidereal-time (t0)
784 "Sidereal time (in hours) in Greenwich.
785
786 At T0=Julian centuries of universal time.
787 T0 must correspond to 0 hours UT."
788 (let* ((mean-sid-time (+ 6.6973746
789 (* 2400.051337 t0)
790 (* 0.0000258622 t0 t0)
791 (* -0.0000000017222 t0 t0 t0)))
792 (et (solar-ephemeris-time (list t0 0.0)))
793 (nut-i (solar-ecliptic-coordinates et nil))
794 (nut (car (cdr (cdr (cdr nut-i))))) ; nutation
795 (i (car (cdr nut-i)))) ; inclination
796 (mod (+ (mod (+ mean-sid-time
797 (/ (/ (* nut (solar-cosine-degrees i)) 15) 3600)) 24.0)
798 24.0)
799 24.0)))
800
801 (defun solar-time-equation (date ut)
802 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
803 (let* ((et (solar-date-to-et date ut))
804 (ec (solar-ecliptic-coordinates et nil)))
805 (car (cdr (cdr ec)))))
806
807 (defun solar-date-to-et (date ut)
808 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
809 Expressed in julian centuries of Ephemeris Time."
810 (let ((t0 (solar-julian-ut-centuries date)))
811 (solar-ephemeris-time (list t0 ut))))
812
813 ;;;###autoload
814 (defun sunrise-sunset (&optional arg)
815 "Local time of sunrise and sunset for today. Accurate to a few seconds.
816 If called with an optional prefix argument, prompt for date.
817
818 If called with an optional double prefix argument, prompt for longitude,
819 latitude, time zone, and date, and always use standard time.
820
821 This function is suitable for execution in a .emacs file."
822 (interactive "p")
823 (or arg (setq arg 1))
824 (if (and (< arg 16)
825 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
826 (solar-setup))
827 (let* ((calendar-longitude
828 (if (< arg 16) calendar-longitude
829 (solar-get-number
830 "Enter longitude (decimal fraction; + east, - west): ")))
831 (calendar-latitude
832 (if (< arg 16) calendar-latitude
833 (solar-get-number
834 "Enter latitude (decimal fraction; + north, - south): ")))
835 (calendar-time-zone
836 (if (< arg 16) calendar-time-zone
837 (solar-get-number
838 "Enter difference from Coordinated Universal Time (in minutes): ")))
839 (calendar-location-name
840 (if (< arg 16) calendar-location-name
841 (let ((float-output-format "%.1f"))
842 (format "%s%s, %s%s"
843 (if (numberp calendar-latitude)
844 (abs calendar-latitude)
845 (+ (aref calendar-latitude 0)
846 (/ (aref calendar-latitude 1) 60.0)))
847 (if (numberp calendar-latitude)
848 (if (> calendar-latitude 0) "N" "S")
849 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
850 (if (numberp calendar-longitude)
851 (abs calendar-longitude)
852 (+ (aref calendar-longitude 0)
853 (/ (aref calendar-longitude 1) 60.0)))
854 (if (numberp calendar-longitude)
855 (if (> calendar-longitude 0) "E" "W")
856 (if (equal (aref calendar-longitude 2) 'east)
857 "E" "W"))))))
858 (calendar-standard-time-zone-name
859 (if (< arg 16) calendar-standard-time-zone-name
860 (cond ((= calendar-time-zone 0) "UTC")
861 ((< calendar-time-zone 0)
862 (format "UTC%dmin" calendar-time-zone))
863 (t (format "UTC+%dmin" calendar-time-zone)))))
864 (calendar-daylight-savings-starts
865 (if (< arg 16) calendar-daylight-savings-starts))
866 (calendar-daylight-savings-ends
867 (if (< arg 16) calendar-daylight-savings-ends))
868 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
869 (date-string (calendar-date-string date t))
870 (time-string (solar-sunrise-sunset-string date))
871 (msg (format "%s: %s" date-string time-string))
872 (one-window (one-window-p t)))
873 (if (<= (length msg) (frame-width))
874 (message "%s" msg)
875 (with-output-to-temp-buffer "*temp*"
876 (princ (concat date-string "\n" time-string)))
877 (message "%s"
878 (substitute-command-keys
879 (if one-window
880 (if pop-up-windows
881 "Type \\[delete-other-windows] to remove temp window."
882 "Type \\[switch-to-buffer] RET to remove temp window.")
883 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
884
885 (defun calendar-sunrise-sunset ()
886 "Local time of sunrise and sunset for date under cursor.
887 Accurate to a few seconds."
888 (interactive)
889 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
890 (solar-setup))
891 (let ((date (calendar-cursor-to-date t)))
892 (message "%s: %s"
893 (calendar-date-string date t t)
894 (solar-sunrise-sunset-string date))))
895
896 (defun diary-sunrise-sunset ()
897 "Local time of sunrise and sunset as a diary entry.
898 Accurate to a few seconds."
899 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
900 (solar-setup))
901 (solar-sunrise-sunset-string date))
902
903 (defun diary-sabbath-candles ()
904 "Local time of candle lighting diary entry--applies if date is a Friday.
905 No diary entry if there is no sunset on that date."
906 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
907 (solar-setup))
908 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
909 (let* ((sunset (car (cdr (solar-sunrise-sunset date))))
910 (light (if sunset
911 (cons (- (car sunset) (/ 18.0 60.0)) (cdr sunset)))))
912 (if sunset
913 (format "%s Sabbath candle lighting"
914 (apply 'solar-time-string light))))))
915
916 (defun solar-equinoxes/solstices (k year)
917 "Date of equinox/solstice K for YEAR.
918 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
919 K=3, winter solstice.
920 RESULT is a gregorian local date.
921
922 Accurate to less than a minute between 1951 and 2050."
923 (let* ((JDE0 (solar-mean-equinoxes/solstices k year))
924 (T (/ (- JDE0 2451545.0) 36525))
925 (W (- (* 35999.373 T) 2.47))
926 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W))
927 (* 0.0007 (solar-cosine-degrees (* 2 W)))))
928 (S (apply '+ (mapcar '(lambda(x)
929 (* (car x) (solar-cosine-degrees
930 (+ (* (car (cdr (cdr x))) T)
931 (car (cdr x))))))
932 solar-seasons-data)))
933 (JDE (+ JDE0 (/ (* 0.00001 S) Delta-lambda)))
934 (correction (+ 102.3 (* 123.5 T) (* 32.5 T T)))
935 ; ephemeris time correction
936 (JD (- JDE (/ correction 86400)))
937 (date (calendar-gregorian-from-absolute (floor (- JD 1721424.5))))
938 (time (- (- JD 0.5) (floor (- JD 0.5))))
939 )
940 (list (car date) (+ (car (cdr date)) time
941 (/ (/ calendar-time-zone 60.0) 24.0))
942 (car (cdr (cdr date))))))
943
944 ; from Meeus, 1991, page 166
945 (defun solar-mean-equinoxes/solstices (k year)
946 "Julian day of mean equinox/solstice K for YEAR.
947 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
948 solstice. These formulas are only to be used between 1000 BC and 3000 AD."
949 (let ((y (/ year 1000.0))
950 (z (/ (- year 2000) 1000.0)))
951 (if (< year 1000) ; actually between -1000 and 1000
952 (cond ((equal k 0) (+ 1721139.29189
953 (* 365242.13740 y)
954 (* 0.06134 y y)
955 (* 0.00111 y y y)
956 (* -0.00071 y y y y)))
957 ((equal k 1) (+ 1721233.25401
958 (* 365241.72562 y)
959 (* -0.05323 y y)
960 (* 0.00907 y y y)
961 (* 0.00025 y y y y)))
962 ((equal k 2) (+ 1721325.70455
963 (* 365242.49558 y)
964 (* -0.11677 y y)
965 (* -0.00297 y y y)
966 (* 0.00074 y y y y)))
967 ((equal k 3) (+ 1721414.39987
968 (* 365242.88257 y)
969 (* -0.00769 y y)
970 (* -0.00933 y y y)
971 (* -0.00006 y y y y))))
972 ; actually between 1000 and 3000
973 (cond ((equal k 0) (+ 2451623.80984
974 (* 365242.37404 z)
975 (* 0.05169 z z)
976 (* -0.00411 z z z)
977 (* -0.00057 z z z z)))
978 ((equal k 1) (+ 2451716.56767
979 (* 365241.62603 z)
980 (* 0.00325 z z)
981 (* 0.00888 z z z)
982 (* -0.00030 z z z z)))
983 ((equal k 2) (+ 2451810.21715
984 (* 365242.01767 z)
985 (* -0.11575 z z)
986 (* 0.00337 z z z)
987 (* 0.00078 z z z z)))
988 ((equal k 3) (+ 2451900.05952
989 (* 365242.74049 z)
990 (* -0.06223 z z)
991 (* -0.00823 z z z)
992 (* 0.00032 z z z z)))))))
993
994 ; from Meeus, 1991, page 167
995 (defconst solar-seasons-data
996 '((485 324.96 1934.136)
997 (203 337.23 32964.467)
998 (199 342.08 20.186)
999 (182 27.85 445267.112)
1000 (156 73.14 45036.886)
1001 (136 171.52 22518.443)
1002 (77 222.54 65928.934)
1003 (74 296.72 3034.906)
1004 (70 243.58 9037.513)
1005 (58 119.81 33718.147)
1006 (52 297.17 150.678)
1007 (50 21.02 2281.226)
1008 (45 247.54 29929.562)
1009 (44 325.15 31555.956)
1010 (29 60.93 4443.417)
1011 (18 155.12 67555.328)
1012 (17 288.79 4562.452)
1013 (16 198.04 62894.029)
1014 (14 199.76 31436.921)
1015 (12 95.39 14577.848)
1016 (12 287.11 31931.756)
1017 (12 320.81 34777.259)
1018 (9 227.73 1222.114)
1019 (8 15.45 16859.074)))
1020
1021 ;;;###autoload
1022 (defun solar-equinoxes-solstices ()
1023 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
1024 Requires floating point."
1025 (let ((m displayed-month)
1026 (y displayed-year))
1027 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
1028 ((= 2 (% m 3)) 1)
1029 (t 0)))
1030 (let* ((calendar-standard-time-zone-name
1031 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
1032 (calendar-daylight-savings-starts
1033 (if calendar-time-zone calendar-daylight-savings-starts))
1034 (calendar-daylight-savings-ends
1035 (if calendar-time-zone calendar-daylight-savings-ends))
1036 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
1037 (k (1- (/ m 3)))
1038 (d0 (solar-equinoxes/solstices k y))
1039 (d1 (list (car d0) (floor (car (cdr d0))) (car (cdr (cdr d0)))))
1040 (h0 (* 24 (- (car (cdr d0)) (floor (car (cdr d0))))))
1041 (adj (dst-adjust-time d1 h0))
1042 (d (list (car d1) (+ (car (cdr d1))
1043 (/ (car (cdr adj)) 24.0))
1044 (car (cdr (cdr d1)))))
1045 ; The following is nearly as accurate, but not quite:
1046 ;(d0 (solar-date-next-longitude
1047 ; (calendar-astro-from-absolute
1048 ; (calendar-absolute-from-gregorian
1049 ; (list (+ 3 (* k 3)) 15 y)))
1050 ; 90))
1051 ;(abs-day (calendar-absolute-from-astro d)))
1052 (abs-day (calendar-absolute-from-gregorian d)))
1053 (list
1054 (list (calendar-gregorian-from-absolute (floor abs-day))
1055 (format "%s %s"
1056 (nth k (if (and calendar-latitude
1057 (< (calendar-latitude) 0))
1058 solar-s-hemi-seasons
1059 solar-n-hemi-seasons))
1060 (solar-time-string
1061 (* 24 (- abs-day (floor abs-day)))
1062 (if (dst-in-effect abs-day)
1063 calendar-daylight-time-zone-name
1064 calendar-standard-time-zone-name))))))))
1065
1066
1067 (provide 'solar)
1068
1069 ;;; solar.el ends here