[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/yahrzeit/bin/ -> solar.cl (source)

   1  ;;; solar.el --- calendar functions for solar events.
   2  
   3  ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
   4  
   5  ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
   6  ;; Keywords: calendar
   7  ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
   8  ;;    holidays
   9  
  10  ;; This file is part of XEmacs.
  11  
  12  ;; XEmacs is free software; you can redistribute it and/or modify it
  13  ;; under the terms of the GNU General Public License as published by
  14  ;; the Free Software Foundation; either version 2, or (at your option)
  15  ;; any later version.
  16  
  17  ;; XEmacs is distributed in the hope that it will be useful, but
  18  ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19  ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20  ;; General Public License for more details.
  21  
  22  ;; You should have received a copy of the GNU General Public License
  23  ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24  ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25  
  26  ;;; Commentary:
  27  
  28  ;; This collection of functions implements the features of calendar.el,
  29  ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
  30  ;; eqinoxes/solstices.
  31  
  32  ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
  33  ;; Almanac Office, United States Naval Observatory, Washington, 1984 and
  34  ;; on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
  35  ;; Willmann-Bell, Inc., 1985.
  36  ;;
  37  ;; WARNINGS:
  38  ;;    1. SUNRISE/SUNSET calculations will be accurate only to +/- 2 minutes.
  39  ;;       Locations should be between +/- 65 degrees of latitude.
  40  ;;       Dates should be in the latter half of the 20th century.
  41  ;;
  42  ;;    2. Equinox/solstice times will be accurate only to +/- 15 minutes.
  43  
  44  ;; The author would be delighted to have an astronomically more sophisticated
  45  ;; person rewrite the code for the solar calculations in this file!
  46  
  47  ;; Comments, corrections, and improvements should be sent to
  48  ;;  Edward M. Reingold               Department of Computer Science
  49  ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  50  ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  51  ;;                                   Urbana, Illinois 61801
  52  
  53  ;;; Code:
  54  
  55  (setq *WARN-ON-FLOATING-POINT-CONTAGION* NIL)
  56  ;(if (fboundp 'atan)
  57  ;    (require 'lisp-float-type)
  58  ;  (error "Solar/lunar calculations impossible since floating point is unavailable."))
  59  
  60  ;(defconst pi (purecopy (* 4 (atan 1))) "The value of Pi (3.1415926...)")
  61  (defconstant e (exp 1)) 
  62  
  63  ;; Careful when editing this file ... typos here will be hard to spot.
  64  ;; (defconst pi       3.14159265358979323846264338327
  65  ;;  "The value of Pi (3.14159265358979323846264338327...)")
  66  
  67  (defun quotient (m n)
  68    ;; TYPE (real non-zero-real) -> integer
  69    ;; Whole part of m/n.
  70    (floor m n))
  71  
  72  ;; XEmacs change (purecopy)
  73  ;(defconst degrees-to-radians (purecopy (/ pi 180.0))
  74  ;  "Degrees to radian conversion constant")
  75  ;(defconst radians-to-degrees (purecopy (/ 180.0 pi))
  76  ;  "Radian to degree conversion constant")
  77  
  78  ;; these expand to a single multiply by a float when byte compiled
  79  
  80  (defmacro degrees-to-radians (x)
  81    "Convert ARG from degrees to radians."
  82    (list '* (/ pi 180.0) x))
  83  (defmacro radians-to-degrees (x)
  84    "Convert ARG from radians to degrees."
  85    (list '* (/ 180.0 pi) x))
  86  
  87  
  88  
  89  
  90  ;(require 'cal-dst)
  91  
  92  ;;;###autoload
  93  (defvar calendar-time-display-form
  94    '(12-hours ":" minutes am-pm
  95      (if time-zone " (") time-zone (if time-zone ")"))
  96    "*The pseudo-pattern that governs the way a time of day is formatted.
  97  
  98  A pseudo-pattern is a list of expressions that can involve the keywords
  99  `12-hours', `24-hours', and `minutes',  all numbers in string form,
 100  and `am-pm' and `time-zone',  both alphabetic strings.
 101  
 102  For example, the form
 103  
 104    '(24-hours \":\" minutes
 105      (if time-zone \" (\") time-zone (if time-zone \")\"))
 106  
 107  would give military-style times like `21:07 (UTC)'.")
 108  
 109  ;;;###autoload
 110  (defvar calendar-latitude nil
 111    "*Latitude of `calendar-location-name' in degrees.
 112  
 113  The value can be either a decimal fraction (one place of accuracy is
 114  sufficient), + north, - south, such as 40.7 for New York City, or the value
 115  can be a vector [degrees minutes north/south] such as [40 50 north] for New
 116  York City.
 117  
 118  This variable should be set in site-local.el.")
 119  
 120  ;;;###autoload
 121  (defvar calendar-longitude nil
 122    "*Longitude of `calendar-location-name' in degrees.
 123  
 124  The value can be either a decimal fraction (one place of accuracy is
 125  sufficient), + east, - west, such as -73.9 for New York City, or the value
 126  can be a vector [degrees minutes east/west] such as [73 55 west] for New
 127  York City.
 128  
 129  This variable should be set in site-local.el.")
 130  
 131  (declaim (inline calendar-latitude))
 132  (defun calendar-latitude ()
 133    "Convert calendar-latitude to a signed decimal fraction, if needed."
 134    (if (numberp calendar-latitude)
 135        calendar-latitude
 136      (let ((lat (+ (aref calendar-latitude 0)
 137                    (/ (aref calendar-latitude 1) 60.0))))
 138        (if (equal (aref calendar-latitude 2) 'north)
 139            lat
 140          (- lat)))))
 141  (declaim (inline calendar-longitude))
 142  (defun calendar-longitude ()
 143    "Convert calendar-longitude to a signed decimal fraction, if needed."
 144    (if (numberp calendar-longitude)
 145        calendar-longitude
 146      (let ((long (+ (aref calendar-longitude 0)
 147                    (/ (aref calendar-longitude 1) 60.0))))
 148        (if (equal (aref calendar-longitude 2) 'east)
 149            long
 150          (- long)))))
 151  
 152  ;;;###autoload
 153  (defvar calendar-location-name
 154    '(let ((float-output-format "%.1f"))
 155       (format nil "~A~A, ~A~A"
 156               (if (numberp calendar-latitude)
 157                   (abs calendar-latitude)
 158                 (+ (aref calendar-latitude 0)
 159                    (/ (aref calendar-latitude 1) 60.0)))
 160               (if (numberp calendar-latitude)
 161                   (if (> calendar-latitude 0) "N" "S")
 162                 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
 163               (if (numberp calendar-longitude)
 164                   (abs calendar-longitude)
 165                 (+ (aref calendar-longitude 0)
 166                    (/ (aref calendar-longitude 1) 60.0)))
 167               (if (numberp calendar-longitude)
 168                   (if (> calendar-longitude 0) "E" "W")
 169                 (if (equal (aref calendar-latitude 2) 'east) "E" "W"))))
 170    "*Expression evaluating to name of `calendar-longitude', calendar-latitude'.
 171  For example, \"New York City\".  Default value is just the latitude, longitude
 172  pair.
 173  
 174  This variable should be set in site-local.el.")
 175  
 176  (defvar solar-n-hemi-seasons
 177    '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
 178    "List of season changes for the northern hemisphere.")
 179  
 180  (defvar solar-s-hemi-seasons
 181    '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
 182    "List of season changes for the southern hemisphere.")
 183  
 184  (defun solar-setup ()
 185    "Prompt user for latitude, longitude, and time zone."
 186    (beep)
 187    (if (not calendar-longitude)
 188        (setq calendar-longitude
 189              (solar-get-number
 190               "Enter longitude (decimal fraction; + east, - west): ")))
 191    (if (not calendar-latitude)
 192        (setq calendar-latitude
 193              (solar-get-number
 194               "Enter latitude (decimal fraction; + north, - south): ")))
 195    (if (not calendar-time-zone)
 196        (setq calendar-time-zone
 197              (solar-get-number
 198               "Enter difference from Coordinated Universal Time (in minutes): "))))
 199  
 200  (defun solar-get-number (prompt)
 201    "Return a number from the minibuffer, prompting with PROMPT.
 202  Returns nil if nothing was entered."
 203    (let ((x (read-string prompt "")))
 204      (if (not (string-equal x ""))
 205          (string-to-int x))))
 206  
 207  (declaim (inline solar-sin-degrees))
 208  (defun solar-sin-degrees (x)
 209    (sin (degrees-to-radians x)))
 210  
 211  (declaim (inline solar-cosine-degrees))
 212  (defun solar-cosine-degrees (x)
 213    (cos (degrees-to-radians x)))
 214  
 215  (defun solar-tangent-degrees (x)
 216    (tan (degrees-to-radians x)))
 217  
 218  (defun solar-xy-to-quadrant (x y)
 219    "Determines the quadrant of the point X, Y."
 220    (if (> x 0)
 221        (if (> y 0) 1 4)
 222        (if (> y 0) 2 3)))
 223  
 224  (defun solar-degrees-to-quadrant (angle)
 225    "Determines the quadrant of ANGLE."
 226    (1+ (floor (mod angle 360) 90)))
 227  
 228  (defun solar-arctan (x quad)
 229    "Arctangent of X in quadrant QUAD."
 230    (let ((deg (radians-to-degrees (atan x))))
 231      (cond ((equal quad 2)   (+ deg 180))
 232        ((equal quad 3)   (+ deg 180))
 233        ((equal quad 4)   (+ deg 360))
 234        (t                deg))))
 235  
 236  (defun solar-arccos (x)
 237    (let ((y (sqrt (- 1 (* x x)))))
 238      (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
 239  
 240  (defun solar-arcsin (y)
 241    (let ((x (sqrt (- 1 (* y y)))))
 242      (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
 243  
 244  (defconstant solar-earth-inclination 23.441884 
 245    "Inclination of earth's equator to its solar orbit in degrees.")
 246  
 247  (defconstant solar-cos-inclination (solar-cosine-degrees solar-earth-inclination) 
 248    "Cosine of earth's inclination.")
 249  
 250  (defconstant solar-sin-inclination (solar-sin-degrees solar-earth-inclination)
 251    "Sine of earth's inclination.")
 252  
 253  (defconstant solar-earth-orbit-eccentricity 0.016718d0
 254    "Eccentricity of orbit of the earth around the sun.")
 255  
 256  (declaim (inline solar-degrees-to-hours))
 257  (defun solar-degrees-to-hours (deg)
 258    (/ deg 15.d0))
 259  
 260  (declaim (inline solar-hours-to-days))
 261  (defun solar-hours-to-days (hour)
 262    (/ hour 24d0))
 263  
 264  (defun solar-longitude-of-sun (day)
 265    "Longitude of the sun at DAY in the year."
 266    (let ((mean-anomaly (coerce (- (* 0.9856d0 day) 3.289d0) 'double-float)))
 267      (mod  (coerce ( + mean-anomaly 
 268          (* 1.916d0 (solar-sin-degrees mean-anomaly))
 269          (* 0.020d0 (solar-sin-degrees (* 2d0 mean-anomaly)))
 270          282.634d0) 'double-float)
 271       360.0d0)))
 272  
 273  (defun solar-moment (date latitude longitude rise-or-set)
 274    ;; TYPE (fixed-date angle angle real) -> moment
 275    ;; Local time (fraction of day) of sunrise/sunset at
 276    ;; latitude, longitude (in nonpolar regions) for fixed
 277    ;; date.  rise-or-set is -0.25 for sunrise and +0.25 for
 278    ;; sunset.
 279    (let* ((approx  ; Approximate time of event.
 280            (+ (day-number (gregorian-from-fixed date))
 281               0.5 rise-or-set (/ longitude -360.0d0)))
 282           (anomaly ; Anomaly of sun.
 283            (- (* 0.9856d0 approx) 3.289d0))
 284           (sun     ; Longitude of sun.
 285            (degrees (+ anomaly (* 1.916d0
 286                                   (sin-degrees anomaly))
 287                        282.634d0
 288                        (* 0.020d0
 289                           (sin-degrees (* 2 anomaly))))))
 290           (right-ascension ; Right ascension of sun.
 291            (arctan-degrees
 292             (* (cosine-degrees 23.441884d0)
 293                (tangent-degrees sun))
 294             (1+ (quotient sun 90)))) ; Quadrant.
 295           (declination ; Declination of sun.
 296            (arcsin-degrees (* (sin-degrees 23.441884d0)
 297                               (sin-degrees sun))))
 298           (local (* (signum rise-or-set)
 299                     (arccos-degrees
 300                      (/ (- (cosine-degrees 90.833333d0)
 301                            (* (sin-degrees declination)
 302                               (sin-degrees latitude)))
 303                         (cosine-degrees declination)
 304                         (cosine-degrees latitude))))))
 305      (mod (- (/ (+ local right-ascension) 360)
 306              0.27592d0 (* 0.00273792d0 approx))
 307           1)))
 308  
 309  
 310  
 311  (defun solar-right-ascension (longitude)
 312    "Right ascension of the sun, given its LONGITUDE."
 313    (solar-degrees-to-hours
 314     (solar-arctan
 315      (* solar-cos-inclination (solar-tangent-degrees longitude))
 316      (solar-degrees-to-quadrant longitude))))
 317  
 318  (defun solar-declination (longitude)
 319    "Declination of the sun, given its LONGITUDE."
 320    (solar-arcsin
 321     (* solar-sin-inclination
 322        (solar-sin-degrees longitude))))
 323  
 324  (defun solar-sunrise (date)
 325    "Calculates the *standard* time of sunrise for Gregorian DATE.
 326  Calculation is for location given by `calendar-latitude' and
 327  `calendar-longitude'.
 328  
 329  Returns a decimal fraction of hours.  Returns nil if the sun does not rise at
 330  that location on that day."
 331    (let* ((day-of-year (calendar-day-number date))
 332       (approx-sunrise
 333            (+ day-of-year
 334               (solar-hours-to-days
 335                (-  6 (solar-degrees-to-hours (calendar-longitude))))))
 336       (solar-longitude-of-sun-at-sunrise
 337            (solar-longitude-of-sun approx-sunrise))
 338       (solar-right-ascension-at-sunrise
 339            (solar-right-ascension solar-longitude-of-sun-at-sunrise))
 340       (solar-declination-at-sunrise
 341            (solar-declination solar-longitude-of-sun-at-sunrise))
 342       (cos-local-sunrise
 343            (/ (- (solar-cosine-degrees (+ 90 (/ 50d0 60d0)))
 344                  (* (solar-sin-degrees solar-declination-at-sunrise)
 345                     (solar-sin-degrees (calendar-latitude))))
 346               (* (solar-cosine-degrees solar-declination-at-sunrise)
 347                  (solar-cosine-degrees (calendar-latitude))))))
 348      (if (<= (abs cos-local-sunrise) 1);; otherwise, no sunrise that day
 349        (let* ((local-sunrise (solar-degrees-to-hours
 350                               (- 360 (solar-arccos cos-local-sunrise))))
 351               (local-mean-sunrise
 352            (mod (- (+ local-sunrise solar-right-ascension-at-sunrise)
 353                (+ (* 0.065710d0 approx-sunrise)
 354               6.622d0))
 355             24)))
 356      (+ (- local-mean-sunrise (solar-degrees-to-hours (calendar-longitude)))
 357         (/ calendar-time-zone 60d0))))))
 358  
 359  (defun solar-sunset (date)
 360    "Calculates the *standard* time of sunset for Gregorian DATE.
 361  Calculation is for location given by `calendar-latitude' and
 362  `calendar-longitude'.
 363  
 364  Returns a decimal fractions of hours.  Returns nil if the sun does not set at
 365  that location on that day."
 366    (let* ((day-of-year (calendar-day-number date))
 367       (approx-sunset
 368            (+ day-of-year
 369               (solar-hours-to-days
 370                (- 18 (solar-degrees-to-hours (calendar-longitude))))))
 371       (solar-longitude-of-sun-at-sunset
 372            (solar-longitude-of-sun approx-sunset))
 373       (solar-right-ascension-at-sunset
 374            (solar-right-ascension solar-longitude-of-sun-at-sunset))
 375       (solar-declination-at-sunset
 376            (solar-declination solar-longitude-of-sun-at-sunset))
 377       (cos-local-sunset
 378            (/ (- (solar-cosine-degrees (+ 90 (/ 50d0 60d0)))
 379                  (* (solar-sin-degrees solar-declination-at-sunset)
 380                     (solar-sin-degrees (calendar-latitude))))
 381               (* (solar-cosine-degrees solar-declination-at-sunset)
 382                  (solar-cosine-degrees (calendar-latitude))))))
 383      (if (<= (abs cos-local-sunset) 1);; otherwise, no sunset that day
 384        (let* ((local-sunset (solar-degrees-to-hours
 385                              (solar-arccos cos-local-sunset)))
 386               (local-mean-sunset
 387            (mod (- (+ local-sunset solar-right-ascension-at-sunset)
 388                (+ (* 0.065710d0 approx-sunset) 6.622d0))
 389             24)))
 390      (+ (- local-mean-sunset (solar-degrees-to-hours (calendar-longitude)))
 391         (/ calendar-time-zone 60d0))))))
 392  
 393  (defun solar-adj-time-for-dst (date time &optional style)
 394    "Adjust decimal fraction standard TIME on DATE to account for dst.
 395  Returns a list (date adj-time zone) where `date' and `time' are the values
 396  adjusted for `zone'; here `date' is a list (month day year), `time' is a
 397  decimal fraction time, and `zone' is a string.
 398  
 399  Optional parameter STYLE forces the result time to be standard time when its
 400  value is 'standard and daylight savings time (if available) when its value is
 401  'daylight.
 402  
 403  Conversion to daylight savings time is done according to
 404  `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
 405  `calendar-daylight-savings-starts-time',
 406  `calendar-daylight-savings-ends-time', and
 407  `calendar-daylight-savings-offset'."
 408  
 409    (let*  ((year (extract-calendar-year date))
 410       (rounded-abs-date (+ (calendar-absolute-from-gregorian date)
 411                    (/ (round (* 60d0 time)) 60d0 24d0)))
 412           (dst-starts (and calendar-daylight-savings-starts
 413                            (+ (calendar-absolute-from-gregorian
 414                                (eval calendar-daylight-savings-starts))
 415                   (/ calendar-daylight-savings-starts-time
 416                  60d0 24d0))))
 417           (dst-ends (and calendar-daylight-savings-ends
 418                          (+ (calendar-absolute-from-gregorian
 419                              (eval calendar-daylight-savings-ends))
 420                 (/ (- calendar-daylight-savings-ends-time
 421                   calendar-daylight-time-offset)
 422                    60d0 24d0))))
 423       (dst (and (not (eq style 'standard))
 424                     (or (eq style 'daylight)
 425                         (and dst-starts dst-ends
 426                              (or (and (< dst-starts dst-ends);; northern hemi.
 427                                       (<= dst-starts rounded-abs-date)
 428                                       (< rounded-abs-date dst-ends))
 429                                  (and (< dst-ends dst-starts);; southern hemi.
 430                                       (or (< rounded-abs-date dst-ends)
 431                                           (<= dst-starts rounded-abs-date)))))
 432                         (and dst-starts (not dst-ends)
 433                              (<= dst-starts rounded-abs-date))
 434                         (and dst-ends (not dst-starts)
 435                              (< rounded-abs-date dst-ends)))))
 436       (time-zone (if dst
 437              calendar-daylight-time-zone-name
 438              calendar-standard-time-zone-name))
 439       (time (+ rounded-abs-date
 440                    (if dst (/ calendar-daylight-time-offset 24d0 60d0) 0d0))))
 441      (declare (type double-float rounded-abs-date)
 442           (type double-float dst-starts)
 443           (type double-float dst-ends))
 444      (list (calendar-gregorian-from-absolute (truncate time))
 445            (* 24d0 (- time (truncate time)))
 446            time-zone)))
 447  
 448  
 449  
 450  (defun solar-time-string (time time-zone)
 451    (declare (special time-zone ))
 452    
 453    "Printable form for decimal fraction TIME on DATE.
 454  Format used is given by `calendar-time-display-form'."
 455    (let* ((time (round (* 60 time)))
 456       (24-hours (quotient time 60))
 457       (minutes (format nil "~02d" (% time 60)))
 458       (12-hours (format nil "~d" (1+ (% (+ 24-hours 11) 12))))
 459       (am-pm (if (>= 24-hours 12) "pm" "am"))
 460       (24-hours (format nil "~02d" 24-hours )))
 461      (declare (special 12-hours minutes am-pm time-zone ))
 462      (format nil "~A"  (eval `(concatenate 'string ,@calendar-time-display-form)))))
 463  
 464  (defun mincha-time-string (time time-zone)
 465    (declare (special time-zone ))
 466    
 467    "Printable form for decimal fraction TIME on DATE.
 468  Format used is given by `calendar-time-display-form'."
 469    (let* ((t1 (round (* 60 time)))
 470       (time (floor (* 5 (floor (- t1 15) 5))))
 471       (24-hours (quotient time 60))
 472       (minutes (format nil "~02d" (% time 60)))
 473       (12-hours (format nil "~d" (1+ (% (+ 24-hours 11) 12))))
 474       (am-pm (if (>= 24-hours 12) " P.M." " A.M."))
 475       (24-hours (format nil "~02d" 24-hours )))
 476  
 477      (declare (special 12-hours minutes am-pm time-zone ))
 478      (setq time-zone nil)
 479      (format nil "~A"  (eval `(concatenate 'string ,@calendar-time-display-form)))))
 480  
 481  
 482  (defun solar-sunrise-sunset (date)
 483    "String giving local times of sunrise and sunset on Gregorian DATE."
 484    (let* ((rise (solar-sunrise date))
 485           (adj-rise (if rise (solar-adj-time-for-dst date rise)))
 486           (set (solar-sunset date))
 487           (adj-set (if set (solar-adj-time-for-dst date set))))
 488      (format nil "~A, ~A at ~A"
 489          (if (and rise (calendar-date-equal date (car adj-rise)))
 490          (concatenate 'string "Sunrise " (apply 'solar-time-string (cdr adj-rise)))
 491            "No sunrise")
 492          (if (and set (calendar-date-equal date (car adj-set)))
 493          (concatenate 'string "sunset " (apply 'solar-time-string (cdr adj-set)))
 494            "no sunset")
 495          (eval calendar-location-name))))
 496  
 497  (defun solar-apparent-longitude-of-sun (date)
 498    "Apparent longitude of the sun on Gregorian DATE."
 499    (let* ((time (quotient (- (calendar-absolute-from-gregorian date) 
 500               (calendar-absolute-from-gregorian '(1 0.5 1900)))
 501            36525))
 502       (l (+ 279.69668d0
 503             (* 36000.76892d0 time)
 504             (* 0.0003025d0 time time)))
 505       (m (+ 358.47583d0
 506             (* 35999.04975d0 time)
 507             (* -0.000150d0 time time)
 508             (* -0.0000033d0 time time time)))
 509       (c (+ (* (+ 1.919460d0
 510               (* -0.004789d0 time)
 511               (* -0.000014d0 time time))
 512            (solar-sin-degrees m))
 513             (* (+ 0.020094d0
 514               (* -0.000100d0 time))
 515            (solar-sin-degrees (* 2 m)))
 516             (* 0.000293d0
 517            (solar-sin-degrees (* 3 m)))))
 518       (L (+ l c))
 519       (omega (+ 259.18d0
 520             (* -1934.142d0 time)))
 521       (app (+ L
 522           -0.00569d0
 523           (* -0.00479d0 
 524              (solar-sin-degrees omega)))))
 525      app))
 526  
 527  (defun solar-ephemeris-correction (year)
 528    "Difference in minutes between Ephemeris time and UTC in YEAR.
 529  Value is only an approximation."
 530    (let ((T (/ (- year 1900) 100.0)))
 531      (+ 0.41d0 (* 1.2053d0 T) (* 0.4992d0 T T))))
 532  
 533  (defun solar-equinoxes/solstices (k year)
 534    "Date of equinox/solstice K for YEAR.  K=0, spring equinox; K=1, summer
 535  solstice; K=2, fall equinox; K=3, winter solstice.  Accurate to within
 536  several minutes."
 537    (let ((date (list (+ 3 (* k 3)) 21 year))
 538          app
 539      (correction 1000))
 540      (while (> correction 0.00001d0)
 541        (setq app (mod (solar-apparent-longitude-of-sun date) 360))
 542        (setq correction (* 58 (solar-sin-degrees (- (* k 90) app))))
 543        (setq date (list (extract-calendar-month date)
 544                 (+ (extract-calendar-day date) correction)
 545                 year)))
 546      (list (extract-calendar-month date)
 547            (+ (extract-calendar-day date) (/ calendar-time-zone 60.0d0 24.0d0)
 548               (- (/ (solar-ephemeris-correction year) 60.0d0 24.0d0)))
 549            year)))
 550  
 551  ;;;###autoload
 552  (defun sunrise-sunset (&optional arg)
 553    "Local time of sunrise and sunset for today.  Accurate to +/- 2 minutes.
 554  If called with an optional prefix argument, prompt for date.
 555  
 556  If called with an optional double prefix argument, prompt for longitude,
 557  latitude, time zone, and date, and always use standard time.
 558  
 559  This function is suitable for execution in a .emacs file."
 560   (or arg (setq arg 1))
 561   (if (and (< arg 16)
 562            (not (and calendar-latitude calendar-longitude calendar-time-zone)))
 563       (solar-setup))
 564   (let* ((calendar-longitude
 565           (if (< arg 16) calendar-longitude
 566             (solar-get-number
 567              "Enter longitude (decimal fraction; + east, - west): ")))
 568          (calendar-latitude
 569           (if (< arg 16) calendar-latitude
 570             (solar-get-number
 571              "Enter latitude (decimal fraction; + north, - south): ")))
 572          (calendar-time-zone
 573           (if (< arg 16) calendar-time-zone
 574             (solar-get-number
 575              "Enter difference from Coordinated Universal Time (in minutes): ")))
 576          (calendar-location-name
 577           (if (< arg 16) calendar-location-name
 578             (let ((float-output-format "~.1f"))
 579               (format nil "~A~A, ~A~A"
 580                       (if (numberp calendar-latitude)
 581                           (abs calendar-latitude)
 582                         (+ (aref calendar-latitude 0)
 583                            (/ (aref calendar-latitude 1) 60.0)))
 584                       (if (numberp calendar-latitude)
 585                           (if (> calendar-latitude 0) "N" "S")
 586                         (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
 587                       (if (numberp calendar-longitude)
 588                           (abs calendar-longitude)
 589                         (+ (aref calendar-longitude 0)
 590                            (/ (aref calendar-longitude 1) 60.0)))
 591                       (if (numberp calendar-longitude)
 592                           (if (> calendar-longitude 0) "E" "W")
 593                         (if (equal (aref calendar-latitude 2) 'east)
 594                             "E" "W"))))))
 595          (calendar-standard-time-zone-name
 596           (if (< arg 16) calendar-standard-time-zone-name
 597             (cond ((= calendar-time-zone 0) "UTC")
 598                   ((< calendar-time-zone 0)
 599                       (format nil "UTC~dmin" calendar-time-zone))
 600                   (t  (format nil "UTC+~dmin" calendar-time-zone)))))
 601          (calendar-daylight-savings-starts
 602           (if (< arg 16) calendar-daylight-savings-starts))
 603          (calendar-daylight-savings-ends
 604           (if (< arg 16) calendar-daylight-savings-ends))
 605          (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
 606          (date-string (calendar-date-string date t))
 607          (time-string (solar-sunrise-sunset date))
 608          (format t "~A: ~A" date-string time-string)
 609          (one-window (one-window-p t)))
 610     (if (<= (length msg) (frame-width))
 611         (message msg)
 612       (with-output-to-temp-buffer "*temp*"
 613         (print(concatenate 'string date-string "\n" time-string)))
 614       (message (substitute-command-keys
 615                 (if one-window
 616                     (if pop-up-windows
 617                         "Type \\[delete-other-windows] to remove temp window."
 618                       "Type \\[switch-to-buffer] RET to remove temp window.")
 619                   "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
 620   
 621  (defun calendar-sunrise-sunset ()
 622    "Local time of sunrise and sunset for date under cursor.
 623  Accurate to +/- 2 minutes."
 624    (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
 625        (solar-setup))
 626    (let ((date (calendar-cursor-to-date t)))
 627      (message "%s: %s"
 628               (calendar-date-string date t t)
 629               (solar-sunrise-sunset date))))
 630  
 631  (defun diary-sunrise-sunset ()
 632    "Local time of sunrise and sunset as a diary entry.
 633  Accurate to +/- 2 minutes."
 634    (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
 635        (solar-setup))
 636    (solar-sunrise-sunset date))
 637  
 638  (defun diary-sabbath-candles ()
 639    "Local time of candle lighting diary entry--applies if date is a Friday.
 640  No diary entry if there is no sunset on that date."
 641    (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
 642        (solar-setup))
 643    (if (= (% (calendar-absolute-from-gregorian date) 7) 5);;  Friday
 644        (let* ((sunset (solar-sunset date))
 645           (light (if sunset
 646                          (solar-adj-time-for-dst
 647                           date
 648                           (- sunset (/ 18.0d0 60.0d0))))))
 649          (if (and light (calendar-date-equal date (car light)))
 650              (format nil "~A Candle lighting"
 651                      (apply 'solar-time-string (cdr light)))))))
 652  
 653  ;;;###autoload
 654  (defun solar-equinoxes-solstices ()
 655    "Date and time of equinoxes and solstices, if visible in the calendar window.
 656  Requires floating point."
 657    (let ((m displayed-month)
 658          (y displayed-year))
 659      (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
 660                      ((= 2 (% m 3))  1)
 661                      (t              0)))
 662      (let* ((calendar-standard-time-zone-name
 663              (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
 664             (calendar-daylight-savings-starts
 665              (if calendar-time-zone calendar-daylight-savings-starts))
 666             (calendar-daylight-savings-ends
 667              (if calendar-time-zone calendar-daylight-savings-ends))
 668             (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
 669             (k (1- (/ m 3)))
 670         (date (solar-equinoxes/solstices k y))
 671         (s-hemi (and calendar-latitude (< (calendar-latitude) 0)))
 672         (day (extract-calendar-day date))
 673             (adj (solar-adj-time-for-dst
 674                   (list (extract-calendar-month date)
 675                 (truncate day)
 676                 (extract-calendar-year date))
 677                   (* 24 (- day (truncate day))))))
 678        (list (list (car adj)
 679            (format nil "~A ~A"
 680                (nth k (if s-hemi solar-s-hemi-seasons
 681                                     solar-n-hemi-seasons))
 682                (apply 'solar-time-string (cdr adj))))))))
 683  
 684  ;(provide 'solar)
 685  
 686  ;;; solar.el ends here


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7