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