1;;; lunar.el --- calendar functions for phases of the moon
2
3;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
4;;   2006, 2007  Free Software Foundation, Inc.
5
6;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7;; Maintainer: Glenn Morris <rgm@gnu.org>
8;; Keywords: calendar
9;; Human-Keywords: moon, lunar phases, calendar, diary
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., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
27
28;;; Commentary:
29
30;; This collection of functions implements lunar phases for calendar.el and
31;; diary.el.
32
33;; Based on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
34;; Willmann-Bell, Inc., 1985 and ``Astronomical Algorithms'' by Jean Meeus,
35;; Willmann-Bell, Inc., 1991.
36;;
37;; WARNING: The calculations will be accurate only to within a few minutes.
38
39;; The author would be delighted to have an astronomically more sophisticated
40;; person rewrite the code for the lunar calculations in this file!
41
42;; Technical details of all the calendrical calculations can be found in
43;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
44;; and Nachum Dershowitz, Cambridge University Press (2001).
45
46;;; Code:
47
48(defvar date)
49(defvar displayed-month)
50(defvar displayed-year)
51
52(if (fboundp 'atan)
53    (require 'lisp-float-type)
54  (error "Lunar calculations impossible since floating point is unavailable"))
55
56(require 'solar)
57
58(defun lunar-phase-list (month year)
59  "List of lunar phases for three months starting with Gregorian MONTH, YEAR."
60  (let ((end-month month)
61        (end-year year)
62        (start-month month)
63        (start-year year))
64    (increment-calendar-month end-month end-year 3)
65    (increment-calendar-month start-month start-year -1)
66    (let* ((end-date (list (list end-month 1 end-year)))
67           (start-date (list (list start-month
68                                   (calendar-last-day-of-month
69                                    start-month start-year)
70                                   start-year)))
71           (index (* 4
72                     (truncate
73                      (* 12.3685
74                         (+ year
75                            ( / (calendar-day-number (list month 1 year))
76                                366.0)
77                            -1900)))))
78           (new-moon (lunar-phase index))
79           (list))
80      (while (calendar-date-compare new-moon end-date)
81        (if (calendar-date-compare start-date new-moon)
82            (setq list (append list (list new-moon))))
83        (setq index (1+ index))
84        (setq new-moon (lunar-phase index)))
85      list)))
86
87(defun lunar-phase (index)
88  "Local date and time of lunar phase INDEX.
89Integer below INDEX/4 gives the lunation number, counting from Jan 1, 1900;
90remainder mod 4 gives the phase: 0 new moon, 1 first quarter, 2 full moon,
913 last quarter."
92  (let* ((phase (mod index 4))
93         (index (/ index 4.0))
94         (time (/ index 1236.85))
95         (date (+ (calendar-absolute-from-gregorian '(1 0.5 1900))
96                  0.75933
97                  (* 29.53058868 index)
98                  (* 0.0001178 time time)
99                  (* -0.000000155 time time time)
100                  (* 0.00033
101                     (solar-sin-degrees (+ 166.56
102                                           (* 132.87 time)
103                                           (* -0.009173 time time))))))
104         (sun-anomaly (mod
105                       (+ 359.2242
106                          (* 29.105356 index)
107                          (* -0.0000333 time time)
108                          (* -0.00000347 time time time))
109                       360.0))
110         (moon-anomaly (mod
111                        (+ 306.0253
112                           (* 385.81691806 index)
113                           (* 0.0107306 time time)
114                           (* 0.00001236 time time time))
115                        360.0))
116         (moon-lat (mod
117                    (+ 21.2964
118                       (* 390.67050646 index)
119                       (* -0.0016528 time time)
120                       (* -0.00000239 time time time))
121                    360.0))
122         (adjustment
123          (if (memq phase '(0 2))
124              (+ (* (- 0.1734 (* 0.000393 time))
125                    (solar-sin-degrees sun-anomaly))
126                 (* 0.0021 (solar-sin-degrees (* 2 sun-anomaly)))
127                 (* -0.4068 (solar-sin-degrees moon-anomaly))
128                 (* 0.0161 (solar-sin-degrees (* 2 moon-anomaly)))
129                 (* -0.0004 (solar-sin-degrees (* 3 moon-anomaly)))
130                 (* 0.0104 (solar-sin-degrees (* 2 moon-lat)))
131                 (* -0.0051 (solar-sin-degrees (+ sun-anomaly moon-anomaly)))
132                 (* -0.0074 (solar-sin-degrees (- sun-anomaly moon-anomaly)))
133                 (* 0.0004 (solar-sin-degrees (+ (* 2 moon-lat) sun-anomaly)))
134                 (* -0.0004 (solar-sin-degrees (- (* 2 moon-lat) sun-anomaly)))
135                 (* -0.0006 (solar-sin-degrees
136                             (+ (* 2 moon-lat) moon-anomaly)))
137                 (* 0.0010 (solar-sin-degrees (- (* 2 moon-lat) moon-anomaly)))
138                 (* 0.0005 (solar-sin-degrees
139                            (+ (* 2 moon-anomaly) sun-anomaly))))
140            (+ (* (- 0.1721 (* 0.0004 time))
141                  (solar-sin-degrees sun-anomaly))
142               (* 0.0021 (solar-sin-degrees (* 2 sun-anomaly)))
143               (* -0.6280 (solar-sin-degrees moon-anomaly))
144               (* 0.0089 (solar-sin-degrees (* 2 moon-anomaly)))
145               (* -0.0004 (solar-sin-degrees (* 3 moon-anomaly)))
146               (* 0.0079 (solar-sin-degrees (* 2 moon-lat)))
147               (* -0.0119 (solar-sin-degrees (+ sun-anomaly moon-anomaly)))
148               (* -0.0047 (solar-sin-degrees (- sun-anomaly moon-anomaly)))
149               (* 0.0003 (solar-sin-degrees (+ (* 2 moon-lat) sun-anomaly)))
150               (* -0.0004 (solar-sin-degrees (- (* 2 moon-lat) sun-anomaly)))
151               (* -0.0006 (solar-sin-degrees (+ (* 2 moon-lat) moon-anomaly)))
152               (* 0.0021 (solar-sin-degrees (- (* 2 moon-lat) moon-anomaly)))
153               (* 0.0003 (solar-sin-degrees
154                          (+ (* 2 moon-anomaly) sun-anomaly)))
155               (* 0.0004 (solar-sin-degrees
156                          (- sun-anomaly (* 2 moon-anomaly))))
157               (* -0.0003 (solar-sin-degrees
158                          (+ (* 2 sun-anomaly) moon-anomaly))))))
159         (adj (+ 0.0028
160                 (* -0.0004 (solar-cosine-degrees
161                             sun-anomaly))
162                 (* 0.0003 (solar-cosine-degrees
163                            moon-anomaly))))
164         (adjustment (cond ((= phase 1) (+ adjustment adj))
165                           ((= phase 2) (- adjustment adj))
166                           (t adjustment)))
167         (date (+ date adjustment))
168	 (date (+ date (/ (- calendar-time-zone
169			     (solar-ephemeris-correction
170                              (extract-calendar-year
171                               (calendar-gregorian-from-absolute
172                                (truncate date)))))
173			  60.0 24.0)))
174         (time (* 24 (- date (truncate date))))
175	 (date (calendar-gregorian-from-absolute (truncate date)))
176         (adj (dst-adjust-time date time)))
177    (list (car adj) (apply 'solar-time-string (cdr adj)) phase)))
178
179(defun lunar-phase-name (phase)
180  "Name of lunar PHASE.
1810 = new moon, 1 = first quarter, 2 = full moon, 3 = last quarter."
182  (cond ((= 0 phase) "New Moon")
183        ((= 1 phase) "First Quarter Moon")
184        ((= 2 phase) "Full Moon")
185        ((= 3 phase) "Last Quarter Moon")))
186
187(defun calendar-phases-of-moon ()
188  "Create a buffer with the lunar phases for the current calendar window."
189  (interactive)
190  (message "Computing phases of the moon...")
191  (let ((m1 displayed-month)
192        (y1 displayed-year)
193        (m2 displayed-month)
194        (y2 displayed-year))
195    (increment-calendar-month m1 y1 -1)
196    (increment-calendar-month m2 y2 1)
197    (set-buffer (get-buffer-create lunar-phases-buffer))
198    (setq buffer-read-only nil)
199    (calendar-set-mode-line
200     (if (= y1 y2)
201         (format "Phases of the Moon from %s to %s, %d%%-"
202                 (calendar-month-name m1) (calendar-month-name m2) y2)
203       (format "Phases of the Moon from %s, %d to %s, %d%%-"
204               (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
205    (erase-buffer)
206    (insert
207     (mapconcat
208      '(lambda (x)
209         (let ((date (car x))
210               (time (car (cdr x)))
211               (phase (car (cdr (cdr x)))))
212           (concat (calendar-date-string date)
213                   ": "
214                   (lunar-phase-name phase)
215                   " "
216                   time)))
217      (lunar-phase-list m1 y1) "\n"))
218    (goto-char (point-min))
219    (set-buffer-modified-p nil)
220    (setq buffer-read-only t)
221    (display-buffer lunar-phases-buffer)
222    (message "Computing phases of the moon...done")))
223
224;;;###autoload
225(defun phases-of-moon (&optional arg)
226  "Display the quarters of the moon for last month, this month, and next month.
227If called with an optional prefix argument, prompts for month and year.
228
229This function is suitable for execution in a .emacs file."
230  (interactive "P")
231  (save-excursion
232    (let* ((date (if arg
233                     (calendar-read-date t)
234                   (calendar-current-date)))
235           (displayed-month (extract-calendar-month date))
236           (displayed-year (extract-calendar-year date)))
237      (calendar-phases-of-moon))))
238
239(defun diary-phases-of-moon (&optional mark)
240"Moon phases diary entry.
241
242An optional parameter MARK specifies a face or single-character string to
243use when highlighting the day in the calendar."
244  (let* ((index (* 4
245                   (truncate
246                    (* 12.3685
247                       (+ (extract-calendar-year date)
248                          ( / (calendar-day-number date)
249                              366.0)
250                          -1900)))))
251         (phase (lunar-phase index)))
252    (while (calendar-date-compare phase (list date))
253      (setq index (1+ index))
254      (setq phase (lunar-phase index)))
255    (if (calendar-date-equal (car phase) date)
256        (cons mark (concat (lunar-phase-name (car (cdr (cdr phase)))) " "
257                (car (cdr phase)))))))
258
259
260;;  For the Chinese calendar the calculations for the new moon need to be more
261;;  accurate than those above, so we use more terms in the approximation.
262
263(defun lunar-new-moon-time (k)
264  "Astronomical (Julian) day number of K th new moon."
265  (let* ((T (/ k 1236.85))
266	 (T2 (* T T))
267	 (T3 (* T T T))
268	 (T4 (* T2 T2))
269	 (JDE (+ 2451550.09765
270		 (* 29.530588853 k)
271		 (* 0.0001337 T2)
272		 (* -0.000000150 T3)
273		 (* 0.00000000073 T4)))
274	 (E (- 1 (* 0.002516 T) (* 0.0000074 T2)))
275	 (sun-anomaly (+ 2.5534
276			 (* 29.10535669 k)
277			 (* -0.0000218 T2)
278			 (* -0.00000011 T3)))
279	 (moon-anomaly (+ 201.5643
280			  (* 385.81693528 k)
281			  (* 0.0107438 T2)
282			  (* 0.00001239 T3)
283			  (* -0.000000058 T4)))
284	 (moon-argument (+ 160.7108
285			   (* 390.67050274 k)
286			   (* -0.0016341 T2)
287			   (* -0.00000227 T3)
288			   (* 0.000000011 T4)))
289	 (omega (+ 124.7746
290		   (* -1.56375580 k)
291		   (* 0.0020691 T2)
292		   (* 0.00000215 T3)))
293	 (A1  (+ 299.77 (*  0.107408 k) (* -0.009173 T2)))
294	 (A2  (+ 251.88 (*  0.016321 k)))
295	 (A3  (+ 251.83 (* 26.641886 k)))
296	 (A4  (+ 349.42 (* 36.412478 k)))
297	 (A5  (+  84.66 (* 18.206239 k)))
298	 (A6  (+ 141.74 (* 53.303771 k)))
299	 (A7  (+ 207.14 (*  2.453732 k)))
300	 (A8  (+ 154.84 (*  7.306860 k)))
301	 (A9  (+  34.52 (* 27.261239 k)))
302	 (A10 (+ 207.19 (*  0.121824 k)))
303	 (A11 (+ 291.34 (*  1.844379 k)))
304	 (A12 (+ 161.72 (* 24.198154 k)))
305	 (A13 (+ 239.56 (* 25.513099 k)))
306	 (A14 (+ 331.55 (*  3.592518 k)))
307	 (correction
308	    (+ (* -0.40720   (solar-sin-degrees moon-anomaly))
309	       (*  0.17241 E (solar-sin-degrees sun-anomaly))
310	       (*  0.01608   (solar-sin-degrees (* 2 moon-anomaly)))
311	       (*  0.01039   (solar-sin-degrees (* 2 moon-argument)))
312	       (*  0.00739 E (solar-sin-degrees (- moon-anomaly sun-anomaly)))
313	       (* -0.00514 E (solar-sin-degrees (+ moon-anomaly sun-anomaly)))
314	       (*  0.00208 E E (solar-sin-degrees (* 2 sun-anomaly)))
315	       (* -0.00111   (solar-sin-degrees
316			       (- moon-anomaly (* 2 moon-argument))))
317	       (* -0.00057   (solar-sin-degrees
318			       (+ moon-anomaly (* 2 moon-argument))))
319	       (*  0.00056 E (solar-sin-degrees
320			       (+ (* 2 moon-anomaly) sun-anomaly)))
321	       (* -0.00042   (solar-sin-degrees (* 3 moon-anomaly)))
322	       (*  0.00042 E (solar-sin-degrees
323			       (+ sun-anomaly (* 2 moon-argument))))
324	       (*  0.00038 E (solar-sin-degrees
325			       (- sun-anomaly (* 2 moon-argument))))
326	       (* -0.00024 E (solar-sin-degrees
327			       (- (* 2 moon-anomaly) sun-anomaly)))
328	       (* -0.00017   (solar-sin-degrees omega))
329	       (* -0.00007   (solar-sin-degrees
330			       (+ moon-anomaly (* 2 sun-anomaly))))
331	       (*  0.00004   (solar-sin-degrees
332			       (- (* 2 moon-anomaly) (* 2 moon-argument))))
333	       (*  0.00004   (solar-sin-degrees (* 3 sun-anomaly)))
334	       (*  0.00003   (solar-sin-degrees (+ moon-anomaly sun-anomaly
335						   (* -2 moon-argument))))
336	       (*  0.00003   (solar-sin-degrees
337			       (+ (* 2 moon-anomaly) (* 2 moon-argument))))
338	       (* -0.00003   (solar-sin-degrees (+ moon-anomaly sun-anomaly
339						   (* 2 moon-argument))))
340	       (*  0.00003   (solar-sin-degrees (- moon-anomaly sun-anomaly
341						   (* -2 moon-argument))))
342	       (* -0.00002   (solar-sin-degrees (- moon-anomaly sun-anomaly
343						   (* 2 moon-argument))))
344	       (* -0.00002   (solar-sin-degrees
345			       (+ (* 3 moon-anomaly) sun-anomaly)))
346	       (*  0.00002   (solar-sin-degrees (* 4 moon-anomaly)))))
347	 (additional
348	    (+ (* 0.000325 (solar-sin-degrees A1))
349	       (* 0.000165 (solar-sin-degrees A2))
350	       (* 0.000164 (solar-sin-degrees A3))
351	       (* 0.000126 (solar-sin-degrees A4))
352	       (* 0.000110 (solar-sin-degrees A5))
353	       (* 0.000062 (solar-sin-degrees A6))
354	       (* 0.000060 (solar-sin-degrees A7))
355	       (* 0.000056 (solar-sin-degrees A8))
356	       (* 0.000047 (solar-sin-degrees A9))
357	       (* 0.000042 (solar-sin-degrees A10))
358	       (* 0.000040 (solar-sin-degrees A11))
359	       (* 0.000037 (solar-sin-degrees A12))
360	       (* 0.000035 (solar-sin-degrees A13))
361	       (* 0.000023 (solar-sin-degrees A14))))
362	 (newJDE (+ JDE correction additional)))
363    (+ newJDE
364       (- (solar-ephemeris-correction
365           (extract-calendar-year
366            (calendar-gregorian-from-absolute
367             (floor (calendar-absolute-from-astro newJDE))))))
368       (/ calendar-time-zone 60.0 24.0))))
369
370(defun lunar-new-moon-on-or-after (d)
371  "Astronomical (Julian) day number of first new moon on or after astronomical
372\(Julian) day number d.  The fractional part is the time of day.
373
374The date and time are local time, including any daylight saving rules,
375as governed by the values of calendar-daylight-savings-starts,
376calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
377calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
378calendar-time-zone."
379  (let* ((date (calendar-gregorian-from-absolute
380                (floor (calendar-absolute-from-astro d))))
381         (year (+ (extract-calendar-year date)
382		  (/ (calendar-day-number date) 365.25)))
383	 (k (floor (* (- year 2000.0) 12.3685)))
384         (date (lunar-new-moon-time k)))
385    (while (< date d)
386      (setq k (1+ k))
387      (setq date (lunar-new-moon-time k)))
388    (let* ((a-date (calendar-absolute-from-astro date))
389           (time (* 24 (- a-date (truncate a-date))))
390           (date (calendar-gregorian-from-absolute (truncate a-date)))
391           (adj (dst-adjust-time date time)))
392      (calendar-astro-from-absolute
393       (+ (calendar-absolute-from-gregorian (car adj))
394          (/ (car (cdr adj)) 24.0))))))
395
396(provide 'lunar)
397
398;;; arch-tag: 72f0b8a4-7bcc-4a1b-b67a-ff53c4a1d222
399;;; lunar.el ends here
400