home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / calendar / solar.el < prev    next >
Encoding:
Text File  |  1993-03-06  |  17.0 KB  |  447 lines

  1. ;;; solar.el --- calendar functions for solar events.
  2.  
  3. ;; Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Keywords: sunrise, sunset, equinox, solstice, calendar, diary, holidays
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  12. ;; accepts responsibility to anyone for the consequences of using it
  13. ;; or for whether it serves any particular purpose or works at all,
  14. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  15. ;; License for full details.
  16.  
  17. ;; Everyone is granted permission to copy, modify and redistribute
  18. ;; GNU Emacs, but only under the conditions described in the
  19. ;; GNU Emacs General Public License.   A copy of this license is
  20. ;; supposed to have been given to you along with GNU Emacs so you
  21. ;; can know your rights and responsibilities.  It should be in a
  22. ;; file named COPYING.  Among other things, the copyright notice
  23. ;; and this notice must be preserved on all copies.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This collection of functions implements the features of calendar.el and
  28. ;; diary.el that deal with sunrise/sunset and eqinoxes/solstices.
  29.  
  30. ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
  31. ;; Almanac Office, United States Naval Observatory, Washington, 1984 and
  32. ;; on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
  33. ;; Willmann-Bell, Inc., 1985.
  34. ;;
  35. ;; WARNINGS:
  36. ;;    1. SUNRISE/SUNSET calculations will be accurate only to +/- 2 minutes.
  37. ;;       Locations should be between +/- 65 degrees of latitude.
  38. ;;       Dates should be in the latter half of the 20th century.
  39. ;;
  40. ;;    2. Equinox/solstice times will be accurate only to +/- 15 minutes.
  41.  
  42. ;; The author would be delighted to have an astronomically more sophisticated
  43. ;; person rewrite the code for the solar calculations in this file!
  44.  
  45. ;; Comments, corrections, and improvements should be sent to
  46. ;;  Edward M. Reingold               Department of Computer Science
  47. ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  48. ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  49. ;;                                   Urbana, Illinois 61801
  50.  
  51. ;;; Code:
  52.  
  53. (if (fboundp 'atan)
  54.     (require 'lisp-float-type)
  55.   (error "Solar calculations impossible since floating point is unavailable."))
  56.  
  57. (require 'calendar)
  58.  
  59. (defun solar-setup ()
  60.   "Prompt user for latitude, longitude, and time zone."
  61.   (beep)
  62.   (if (not calendar-longitude)
  63.       (setq calendar-longitude
  64.             (solar-get-number
  65.              "Enter longitude (decimal fraction; + east, - west): ")))
  66.   (if (not calendar-latitude)
  67.       (setq calendar-latitude
  68.             (solar-get-number
  69.              "Enter latitude (decimal fraction; + north, - south): ")))
  70.   (if (not calendar-time-zone)
  71.       (setq calendar-time-zone
  72.             (solar-get-number
  73.              "Enter difference from Universal Time (in minutes): "))))
  74.  
  75. (defun solar-get-number (prompt)
  76.   "Return a number from the minibuffer, prompting with PROMPT.
  77. Returns nil if nothing was entered."
  78.   (let ((x (read-string prompt "")))
  79.     (if (not (string-equal x ""))
  80.         (string-to-int x))))
  81.  
  82. (defun solar-sin-degrees (x)
  83.   (sin (degrees-to-radians x)))
  84.  
  85. (defun solar-cosine-degrees (x)
  86.   (cos (degrees-to-radians x)))
  87.  
  88. (defun solar-tangent-degrees (x)
  89.   (tan (degrees-to-radians x)))
  90.  
  91. (defun solar-xy-to-quadrant (x y)
  92.   "Determines the quadrant of the point X, Y."
  93.   (if (> x 0)
  94.       (if (> y 0) 1 4)
  95.       (if (> y 0) 2 3)))
  96.  
  97. (defun solar-degrees-to-quadrant (angle)
  98.   "Determines the quadrant of ANGLE."
  99.   (1+ (truncate (/ (solar-mod angle 360.0) 90.0))))
  100.  
  101. (defun solar-arctan (x quad)
  102.   "Arctangent of X in quadrant QUAD."
  103.   (let ((deg (radians-to-degrees (atan x))))
  104.     (cond ((equal quad 2)   (+ deg 180))
  105.       ((equal quad 3)   (+ deg 180))
  106.       ((equal quad 4)   (+ deg 360))
  107.       (t                deg))))
  108.  
  109. (defun solar-arccos (x)
  110.   (let ((y (sqrt (- 1 (* x x)))))
  111.     (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
  112.  
  113. (defun solar-arcsin (y)
  114.   (let ((x (sqrt (- 1 (* y y)))))
  115.     (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
  116.  
  117. (defun solar-mod (x y)
  118.   "Returns X mod Y; value is *always* non-negative."
  119.   (let ((v (% x y)))
  120.     (if (> 0 v)
  121.     (+ v y)
  122.       v)))
  123.  
  124. (defconst solar-earth-inclination 23.441884 
  125.   "Inclination of earth's equator to its solar orbit in degrees.")
  126.  
  127. (defconst solar-cos-inclination (solar-cosine-degrees solar-earth-inclination) 
  128.   "Cosine of earth's inclination.")
  129.  
  130. (defconst solar-sin-inclination (solar-sin-degrees solar-earth-inclination)
  131.   "Sine of earth's inclination.")
  132.  
  133. (defconst solar-earth-orbit-eccentricity 0.016718
  134.   "Eccentricity of orbit of the earth around the sun.")
  135.  
  136. (defmacro solar-degrees-to-hours (deg)
  137.   (list '/ deg 15))
  138.  
  139. (defmacro solar-hours-to-days (hour)
  140.   (list '/ hour 24))
  141.  
  142. (defun solar-longitude-of-sun (day)
  143.   "Longitude of the sun at DAY in the year."
  144.   (let ((mean-anomaly (- (* 0.9856 day) 3.289)))
  145.     (solar-mod (+ mean-anomaly 
  146.           (* 1.916 (solar-sin-degrees mean-anomaly))
  147.           (* 0.020 (solar-sin-degrees (* 2 mean-anomaly)))
  148.           282.634)
  149.            360)))
  150.  
  151. (defun solar-right-ascension (longitude)
  152.   "Right ascension of the sun, given its LONGITUDE."
  153.   (solar-degrees-to-hours
  154.    (solar-arctan
  155.     (* solar-cos-inclination (solar-tangent-degrees longitude))
  156.     (solar-degrees-to-quadrant longitude))))
  157.  
  158. (defun solar-declination (longitude)
  159.   "Declination of the sun, given its LONGITUDE."
  160.   (solar-arcsin
  161.    (* solar-sin-inclination
  162.       (solar-sin-degrees longitude))))
  163.  
  164. (defun solar-sunrise (date)
  165.   "Calculates the *standard* time of sunrise for Gregorian DATE for location
  166. given by `calendar-latitude' and `calendar-longitude'.  Returns a decimal fraction
  167. of hours.  Returns nil if the sun does not rise at that location on that day."
  168.   (let* ((day-of-year (calendar-day-number date))
  169.      (approx-sunrise
  170.           (+ day-of-year
  171.              (solar-hours-to-days
  172.               (-  6 (solar-degrees-to-hours calendar-longitude)))))
  173.      (solar-longitude-of-sun-at-sunrise
  174.           (solar-longitude-of-sun approx-sunrise))
  175.      (solar-right-ascension-at-sunrise
  176.           (solar-right-ascension solar-longitude-of-sun-at-sunrise))
  177.      (solar-declination-at-sunrise
  178.           (solar-declination solar-longitude-of-sun-at-sunrise))
  179.      (cos-local-sunrise
  180.           (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
  181.                 (* (solar-sin-degrees solar-declination-at-sunrise)
  182.                    (solar-sin-degrees calendar-latitude)))
  183.              (* (solar-cosine-degrees solar-declination-at-sunrise)
  184.                 (solar-cosine-degrees calendar-latitude)))))
  185.     (if (<= (abs cos-local-sunrise) 1);; otherwise, no sunrise that day
  186.       (let* ((local-sunrise (solar-degrees-to-hours
  187.                              (- 360 (solar-arccos cos-local-sunrise))))
  188.              (local-mean-sunrise
  189.           (solar-mod (- (+ local-sunrise solar-right-ascension-at-sunrise)
  190.                 (+ (* 0.065710 approx-sunrise)
  191.                    6.622))
  192.              24)))
  193.     (+ (- local-mean-sunrise (solar-degrees-to-hours calendar-longitude))
  194.        (/ calendar-time-zone 60.0))))))
  195.  
  196. (defun solar-sunset (date)
  197.   "Calculates the *standard* time of sunset for Gregorian DATE for location
  198. given by `calendar-latitude' and `calendar-longitude'.  Returns a decimal fractions
  199. of hours.  Returns nil if the sun does not set at that location on that day."
  200.   (let* ((day-of-year (calendar-day-number date))
  201.      (approx-sunset
  202.           (+ day-of-year
  203.              (solar-hours-to-days
  204.               (- 18 (solar-degrees-to-hours calendar-longitude)))))
  205.      (solar-longitude-of-sun-at-sunset
  206.           (solar-longitude-of-sun approx-sunset))
  207.      (solar-right-ascension-at-sunset
  208.           (solar-right-ascension solar-longitude-of-sun-at-sunset))
  209.      (solar-declination-at-sunset
  210.           (solar-declination solar-longitude-of-sun-at-sunset))
  211.      (cos-local-sunset
  212.           (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
  213.                 (* (solar-sin-degrees solar-declination-at-sunset)
  214.                    (solar-sin-degrees calendar-latitude)))
  215.              (* (solar-cosine-degrees solar-declination-at-sunset)
  216.                 (solar-cosine-degrees calendar-latitude)))))
  217.     (if (<= (abs cos-local-sunset) 1);; otherwise, no sunset that day
  218.       (let* ((local-sunset (solar-degrees-to-hours
  219.                             (solar-arccos cos-local-sunset)))
  220.              (local-mean-sunset
  221.           (solar-mod (- (+ local-sunset solar-right-ascension-at-sunset)
  222.                 (+ (* 0.065710 approx-sunset) 6.622))
  223.              24)))
  224.     (+ (- local-mean-sunset (solar-degrees-to-hours calendar-longitude))
  225.        (/ calendar-time-zone 60.0))))))
  226.  
  227. (defun solar-time-string (time date)
  228.   "Printable form for decimal fraction standard TIME on DATE.
  229. Format used is given by `calendar-time-display-form'.  Converted to daylight
  230. savings time according to `calendar-daylight-savings-starts' and
  231. `calendar-daylight-savings-ends', if those variables are not nil."
  232.   (let* ((year (extract-calendar-year date))
  233.      (abs-date (calendar-absolute-from-gregorian date))
  234.      (dst (and calendar-daylight-savings-starts
  235.            calendar-daylight-savings-ends
  236.            (<= (calendar-absolute-from-gregorian
  237.             (eval calendar-daylight-savings-starts))
  238.                abs-date)
  239.            (< abs-date
  240.               (calendar-absolute-from-gregorian
  241.                (eval calendar-daylight-savings-ends)))))
  242.      (time (if dst (1+ time) time))
  243.      (time-zone (if dst
  244.             calendar-daylight-time-zone-name
  245.             calendar-standard-time-zone-name))
  246.      (24-hours (truncate time))
  247.      (minutes (round (* 60 (- time 24-hours))))
  248.          (24-hours (if (= minutes 60) (1+ 24-hours) 24-hours))
  249.          (minutes (if (= minutes 60) 0 minutes))
  250.          (minutes (format "%02d" minutes))
  251.      (12-hours (format "%d" (if (> 24-hours 12)
  252.                     (- 24-hours 12)
  253.                   (if (= 24-hours 0) 12 24-hours))))
  254.      (am-pm (if (>= 24-hours 12) "pm" "am"))
  255.      (24-hours (format "%02d" 24-hours)))
  256.     (mapconcat 'eval calendar-time-display-form "")))
  257.  
  258. (defun solar-sunrise-sunset (date)
  259.   "String giving local times of sunrise and sunset on Gregorian DATE."
  260.   (let ((rise (solar-sunrise date))
  261.     (set (solar-sunset date)))
  262.     (format "%s, %s at %s"
  263.         (if rise
  264.         (concat "Sunrise " (solar-time-string rise date))
  265.           "No sunrise")
  266.         (if set
  267.         (concat "sunset " (solar-time-string set date))
  268.           "no sunset")
  269.         (eval calendar-location-name))))
  270.  
  271. (defun solar-apparent-longitude-of-sun (date)
  272.   "Apparent longitude of the sun on Gregorian DATE."
  273.   (let* ((time (/ (- (calendar-absolute-from-gregorian date) 
  274.              (calendar-absolute-from-gregorian '(1 0.5 1900)))
  275.           36525))
  276.      (l (+ 279.69668
  277.            (* 36000.76892 time)
  278.            (* 0.0003025 time time)))
  279.      (m (+ 358.47583
  280.            (* 35999.04975 time)
  281.            (* -0.000150 time time)
  282.            (* -0.0000033 time time time)))
  283.      (c (+ (* (+ 1.919460
  284.              (* -0.004789 time)
  285.              (* -0.000014 time time))
  286.           (solar-sin-degrees m))
  287.            (* (+ 0.020094
  288.              (* -0.000100 time))
  289.           (solar-sin-degrees (* 2 m)))
  290.            (* 0.000293
  291.           (solar-sin-degrees (* 3 m)))))
  292.      (L (+ l c))
  293.      (omega (+ 259.18
  294.            (* -1934.142 time)))
  295.      (app (+ L
  296.          -0.00569
  297.          (* -0.00479
  298.             (solar-sin-degrees omega)))))
  299.     app))
  300.  
  301. (defun solar-ephemeris-correction (year)
  302.   "Difference in minutes between Ephemeris time an Universal time in YEAR.
  303. Value is only an approximation."
  304.   (let ((T (/ (- year 1900) 100.0)))
  305.     (+ 0.41 (* 1.2053 T) (* 0.4992 T T))))
  306.  
  307. (defun solar-equinoxes/solstices (k year)
  308.   "Date of equinox/solstice K for YEAR.  K=0, spring equinox; K=1, summer
  309. solstice; K=2, fall equinox; K=3, winter solstice.  Accurate to within
  310. several minutes."
  311.   (let ((date (list (+ 3 (* k 3)) 21 year))
  312.     (correction 1000))
  313.     (while (> correction 0.00001)
  314.       (setq app (solar-mod (solar-apparent-longitude-of-sun date) 360.0))
  315.       (setq correction (* 58 (solar-sin-degrees (- (* k 90) app))))
  316.       (setq date (list (extract-calendar-month date)
  317.                (+ (extract-calendar-day date) correction)
  318.                year)))
  319.     (list (extract-calendar-month date)
  320.           (+ (extract-calendar-day date) (/ calendar-time-zone 60.0 24.0)
  321.              (- (/ (solar-ephemeris-correction year) 60.0 24.0)))
  322.           year)))
  323.  
  324. ;;;###autoload
  325. (defun sunrise-sunset (&optional arg)
  326.   "Local time of sunrise and sunset for today.  Accurate to +/- 2 minutes.
  327. If called with an optional prefix argument, prompts for date.
  328.  
  329. If called with an optional double prefix argument, prompts for longitude,
  330. latitude, time zone, and date.
  331.  
  332. This function is suitable for execution in a .emacs file."
  333.  (interactive "p")
  334.  (if (and (< arg 16)
  335.           (not (and calendar-latitude calendar-longitude calendar-time-zone)))
  336.      (solar-setup))
  337.  (let* ((calendar-longitude
  338.          (if (< arg 16) calendar-longitude
  339.            (solar-get-number
  340.             "Enter longitude (decimal fraction; + east, - west): ")))
  341.         (calendar-latitude
  342.          (if (< arg 16) calendar-latitude
  343.            (solar-get-number
  344.             "Enter latitude (decimal fraction; + north, - south): ")))
  345.         (calendar-time-zone
  346.          (if (< arg 16) calendar-time-zone
  347.            (solar-get-number
  348.             "Enter difference from Universal Time (in minutes): ")))
  349.         (calendar-location-name
  350.          (if (< arg 16) calendar-location-name
  351.            (let ((float-output-format "%.1f"))
  352.              (format "%s%s, %s%s"
  353.                      (abs calendar-latitude)
  354.                      (if (> calendar-latitude 0) "N" "S")
  355.                      (abs calendar-longitude)
  356.                      (if (> calendar-longitude 0) "E" "W")))))
  357.         (calendar-standard-time-zone-name
  358.          (if (< arg 16) calendar-standard-time-zone-name
  359.            (cond ((= calendar-time-zone 0) "UT")
  360.                  ((< calendar-time-zone 0)
  361.                      (format "UT%dmin" calendar-time-zone))
  362.                  (t  (format "UT+%dmin" calendar-time-zone)))))
  363.         (calendar-daylight-savings-starts
  364.          (if (< arg 16) calendar-daylight-savings-starts))
  365.         (calendar-daylight-savings-ends
  366.          (if (< arg 16) calendar-daylight-savings-ends))
  367.         (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
  368.         (date-string (calendar-date-string date t))
  369.         (time-string (solar-sunrise-sunset date))
  370.         (msg (format "%s: %s" date-string time-string))
  371.         (one-window (one-window-p t)))
  372.    (if (<= (length msg) (screen-width))
  373.        (message msg)
  374.      (with-output-to-temp-buffer "*temp*"
  375.        (princ (concat date-string "\n" time-string)))
  376.      (message (substitute-command-keys
  377.                (if one-window
  378.                    (if pop-up-windows
  379.                        "Type \\[delete-other-windows] to remove temp window."
  380.                      "Type \\[switch-to-buffer] RET to remove temp window.")
  381.                  "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
  382.  
  383. (defun calendar-sunrise-sunset ()
  384.   "Local time of sunrise and sunset for date under cursor.
  385. Accurate to +/- 2 minutes."
  386.   (interactive)
  387.   (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
  388.       (solar-setup))
  389.   (message
  390.    (solar-sunrise-sunset
  391.     (or (calendar-cursor-to-date)
  392.     (error "Cursor is not on a date!")))))
  393.  
  394. (defun diary-sunrise-sunset ()
  395.   "Local time of sunrise and sunset as a diary entry.
  396. Accurate to +/- 2 minutes."
  397.   (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
  398.       (solar-setup))
  399.   (solar-sunrise-sunset date))
  400.  
  401. (defun diary-sabbath-candles ()
  402.   "Local time of candle lighting diary entry--applies if date is a Friday.
  403. No diary entry if there is no sunset on that date."
  404.   (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
  405.       (solar-setup))
  406.   (if (= (% (calendar-absolute-from-gregorian date) 7) 5);;  Friday
  407.       (let* ((sunset (solar-sunset date))
  408.          (light (if sunset (- sunset (/ 18.0 60.0)))))
  409.         (if light (format "%s Sabbath candle lighting"
  410.                           (solar-time-string light date))))))
  411.  
  412. (defun calendar-holiday-function-solar-equinoxes-solstices ()
  413.   "Date and time of equinoxes and solstices, if visible in the calendar window.
  414. Requires floating point."
  415.   (let* ((m displayed-month)
  416.      (y displayed-year))
  417.     (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
  418.                     ((= 2 (% m 3))  1)
  419.                     (t              0)))
  420.     (let* ((calendar-standard-time-zone-name
  421.             (if calendar-time-zone calendar-standard-time-zone-name "UT"))
  422.            (calendar-daylight-savings-starts
  423.             (if calendar-time-zone calendar-daylight-savings-starts))
  424.            (calendar-daylight-savings-ends
  425.             (if calendar-time-zone calendar-daylight-savings-ends))
  426.            (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
  427.            (k (1- (/ m 3)))
  428.        (date (solar-equinoxes/solstices k y))
  429.        (day (extract-calendar-day date))
  430.        (time (* 24 (- day (truncate day))))
  431.            ;; Time zone/DST can't move the date out of range,
  432.            ;; so let solar-time-string do the conversion.
  433.        (date (list (extract-calendar-month date)
  434.                (truncate day)
  435.                (extract-calendar-year date))))
  436.       (list (list date
  437.           (format "%s %s"
  438.               (cond ((= k 0)  "Vernal Equinox")
  439.                 ((= k 1)  "Summer Solstice")
  440.                 ((= k 2)  "Fall Equinox")
  441.                 ((= k 3)  "Winter Solstice"))
  442.               (solar-time-string time date)))))))
  443.  
  444. (provide 'solar)
  445.  
  446. ;;; solar.el ends here
  447.