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