lunar.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ;;; lunar.el --- calendar functions for phases of the moon
  2. ;; Copyright (C) 1992-1993, 1995, 1997, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  5. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  6. ;; Keywords: calendar
  7. ;; Human-Keywords: moon, lunar phases, calendar, diary
  8. ;; Package: calendar
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; See calendar.el.
  22. ;; Based on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
  23. ;; Willmann-Bell, Inc., 1985 and ``Astronomical Algorithms'' by Jean Meeus,
  24. ;; Willmann-Bell, Inc., 1991.
  25. ;;
  26. ;; WARNING: The calculations will be accurate only to within a few minutes.
  27. ;; The author would be delighted to have an astronomically more sophisticated
  28. ;; person rewrite the code for the lunar calculations in this file!
  29. ;;; Code:
  30. (require 'calendar)
  31. (require 'solar)
  32. (require 'cal-dst)
  33. ;; calendar-astro-to-absolute and v versa are cal-autoloads.
  34. ;;;(require 'cal-julian)
  35. (defcustom lunar-phase-names
  36. '("New Moon" "First Quarter Moon" "Full Moon" "Last Quarter Moon")
  37. "List of names for the lunar phases."
  38. :type '(list
  39. (string :tag "New Moon")
  40. (string :tag "First Quarter Moon")
  41. (string :tag "Full Moon")
  42. (string :tag "Last Quarter Moon"))
  43. :group 'calendar
  44. :version "23.2")
  45. (defun lunar-phase (index)
  46. "Local date and time of lunar phase INDEX.
  47. Integer below INDEX/4 gives the lunation number, counting from Jan 1, 1900;
  48. remainder mod 4 gives the phase: 0 new moon, 1 first quarter, 2 full moon,
  49. 3 last quarter. Returns a list (DATE TIME PHASE)."
  50. (let* ((phase (mod index 4))
  51. (index (/ index 4.0))
  52. (time (/ index 1236.85))
  53. (date (+ (calendar-absolute-from-gregorian '(1 0.5 1900))
  54. 0.75933
  55. (* 29.53058868 index) ; FIXME 29.530588853?
  56. (* 0.0001178 time time)
  57. (* -0.000000155 time time time)
  58. (* 0.00033
  59. (solar-sin-degrees (+ 166.56
  60. (* 132.87 time)
  61. (* -0.009173 time time))))))
  62. (sun-anomaly (mod
  63. (+ 359.2242
  64. (* 29.105356 index)
  65. (* -0.0000333 time time)
  66. (* -0.00000347 time time time))
  67. 360.0))
  68. (moon-anomaly (mod
  69. (+ 306.0253
  70. (* 385.81691806 index)
  71. (* 0.0107306 time time)
  72. (* 0.00001236 time time time))
  73. 360.0))
  74. (moon-lat (mod
  75. (+ 21.2964
  76. (* 390.67050646 index)
  77. (* -0.0016528 time time)
  78. (* -0.00000239 time time time))
  79. 360.0))
  80. (adjustment
  81. (if (memq phase '(0 2))
  82. (+ (* (- 0.1734 (* 0.000393 time))
  83. (solar-sin-degrees sun-anomaly))
  84. (* 0.0021 (solar-sin-degrees (* 2 sun-anomaly)))
  85. (* -0.4068 (solar-sin-degrees moon-anomaly))
  86. (* 0.0161 (solar-sin-degrees (* 2 moon-anomaly)))
  87. (* -0.0004 (solar-sin-degrees (* 3 moon-anomaly)))
  88. (* 0.0104 (solar-sin-degrees (* 2 moon-lat)))
  89. (* -0.0051 (solar-sin-degrees (+ sun-anomaly moon-anomaly)))
  90. (* -0.0074 (solar-sin-degrees (- sun-anomaly moon-anomaly)))
  91. (* 0.0004 (solar-sin-degrees (+ (* 2 moon-lat) sun-anomaly)))
  92. (* -0.0004 (solar-sin-degrees (- (* 2 moon-lat) sun-anomaly)))
  93. (* -0.0006 (solar-sin-degrees
  94. (+ (* 2 moon-lat) moon-anomaly)))
  95. (* 0.0010 (solar-sin-degrees (- (* 2 moon-lat) moon-anomaly)))
  96. (* 0.0005 (solar-sin-degrees
  97. (+ (* 2 moon-anomaly) sun-anomaly))))
  98. (+ (* (- 0.1721 (* 0.0004 time))
  99. (solar-sin-degrees sun-anomaly))
  100. (* 0.0021 (solar-sin-degrees (* 2 sun-anomaly)))
  101. (* -0.6280 (solar-sin-degrees moon-anomaly))
  102. (* 0.0089 (solar-sin-degrees (* 2 moon-anomaly)))
  103. (* -0.0004 (solar-sin-degrees (* 3 moon-anomaly)))
  104. (* 0.0079 (solar-sin-degrees (* 2 moon-lat)))
  105. (* -0.0119 (solar-sin-degrees (+ sun-anomaly moon-anomaly)))
  106. (* -0.0047 (solar-sin-degrees (- sun-anomaly moon-anomaly)))
  107. (* 0.0003 (solar-sin-degrees (+ (* 2 moon-lat) sun-anomaly)))
  108. (* -0.0004 (solar-sin-degrees (- (* 2 moon-lat) sun-anomaly)))
  109. (* -0.0006 (solar-sin-degrees (+ (* 2 moon-lat) moon-anomaly)))
  110. (* 0.0021 (solar-sin-degrees (- (* 2 moon-lat) moon-anomaly)))
  111. (* 0.0003 (solar-sin-degrees
  112. (+ (* 2 moon-anomaly) sun-anomaly)))
  113. (* 0.0004 (solar-sin-degrees
  114. (- sun-anomaly (* 2 moon-anomaly))))
  115. (* -0.0003 (solar-sin-degrees
  116. (+ (* 2 sun-anomaly) moon-anomaly))))))
  117. (adj (+ 0.0028
  118. (* -0.0004 (solar-cosine-degrees
  119. sun-anomaly))
  120. (* 0.0003 (solar-cosine-degrees
  121. moon-anomaly))))
  122. (adjustment (cond ((= phase 1) (+ adjustment adj))
  123. ((= phase 2) (- adjustment adj))
  124. (t adjustment)))
  125. (date (+ date adjustment))
  126. (date (+ date (/ (- calendar-time-zone
  127. (solar-ephemeris-correction
  128. (calendar-extract-year
  129. (calendar-gregorian-from-absolute
  130. (truncate date)))))
  131. 60.0 24.0)))
  132. (time (* 24 (- date (truncate date))))
  133. (date (calendar-gregorian-from-absolute (truncate date)))
  134. (adj (dst-adjust-time date time)))
  135. (list (car adj) (apply 'solar-time-string (cdr adj)) phase)))
  136. (defconst lunar-cycles-per-year 12.3685 ; 365.25/29.530588853
  137. "Mean number of lunar cycles per 365.25 day year.")
  138. ;; FIXME new-moon index; use in lunar-phase-list implies always below.
  139. (defun lunar-index (date)
  140. "Return the lunar index for Gregorian date DATE.
  141. This is 4 times the approximate number of new moons since 1 Jan 1900.
  142. The factor of 4 allows (mod INDEX 4) to represent the four quarters."
  143. (* 4 (truncate
  144. (* lunar-cycles-per-year
  145. ;; Years since 1900, as a real.
  146. (+ (calendar-extract-year date)
  147. (/ (calendar-day-number date) 366.0)
  148. -1900)))))
  149. (defun lunar-phase-list (month year)
  150. "List of lunar phases for three months starting with Gregorian MONTH, YEAR."
  151. (let* ((index (lunar-index (list month 1 year)))
  152. (new-moon (lunar-phase index))
  153. (end-date (let ((end-month month)
  154. (end-year year))
  155. (calendar-increment-month end-month end-year 3)
  156. (list (list end-month 1 end-year))))
  157. ;; Alternative for start-date:
  158. ;;; (calendar-gregorian-from-absolute
  159. ;;; (1- (calendar-absolute-from-gregorian (list month 1 year))))
  160. (start-date (progn
  161. (calendar-increment-month month year -1)
  162. (list (list month
  163. (calendar-last-day-of-month month year)
  164. year))))
  165. list)
  166. (while (calendar-date-compare new-moon end-date)
  167. (if (calendar-date-compare start-date new-moon)
  168. (setq list (append list (list new-moon))))
  169. (setq index (1+ index)
  170. new-moon (lunar-phase index)))
  171. list))
  172. (defun lunar-phase-name (phase)
  173. "Name of lunar PHASE.
  174. 0 = new moon, 1 = first quarter, 2 = full moon, 3 = last quarter."
  175. (nth phase lunar-phase-names))
  176. (defvar displayed-month) ; from calendar-generate
  177. (defvar displayed-year)
  178. ;;;###cal-autoload
  179. (defun calendar-lunar-phases (&optional event)
  180. "Create a buffer with the lunar phases for the current calendar window.
  181. If EVENT is non-nil, it's an event indicating the buffer position to
  182. use instead of point."
  183. (interactive (list last-nonmenu-event))
  184. ;; If called from a menu, with the calendar window not selected.
  185. (with-current-buffer
  186. (if event (window-buffer (posn-window (event-start event)))
  187. (current-buffer))
  188. (message "Computing phases of the moon...")
  189. (let ((m1 displayed-month)
  190. (y1 displayed-year)
  191. (m2 displayed-month)
  192. (y2 displayed-year))
  193. (calendar-increment-month m1 y1 -1)
  194. (calendar-increment-month m2 y2 1)
  195. (calendar-in-read-only-buffer lunar-phases-buffer
  196. (calendar-set-mode-line
  197. (if (= y1 y2)
  198. (format "Phases of the Moon from %s to %s, %d%%-"
  199. (calendar-month-name m1) (calendar-month-name m2) y2)
  200. (format "Phases of the Moon from %s, %d to %s, %d%%-"
  201. (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
  202. (insert
  203. (mapconcat
  204. (lambda (x)
  205. (format "%s: %s %s" (calendar-date-string (car x))
  206. (lunar-phase-name (nth 2 x))
  207. (cadr x)))
  208. (lunar-phase-list m1 y1) "\n")))
  209. (message "Computing phases of the moon...done"))))
  210. ;;;###cal-autoload
  211. (define-obsolete-function-alias 'calendar-phases-of-moon
  212. 'calendar-lunar-phases "23.1")
  213. ;;;###autoload
  214. (defun lunar-phases (&optional arg)
  215. "Display the quarters of the moon for last month, this month, and next month.
  216. If called with an optional prefix argument ARG, prompts for month and year.
  217. This function is suitable for execution in a .emacs file."
  218. (interactive "P")
  219. (save-excursion
  220. (let* ((date (if arg (calendar-read-date t)
  221. (calendar-current-date)))
  222. (displayed-month (calendar-extract-month date))
  223. (displayed-year (calendar-extract-year date)))
  224. (calendar-lunar-phases))))
  225. ;;;###autoload
  226. (define-obsolete-function-alias 'phases-of-moon 'lunar-phases "23.1")
  227. (defvar date)
  228. ;; To be called from diary-list-sexp-entries, where DATE is bound.
  229. ;;;###diary-autoload
  230. (defun diary-lunar-phases (&optional mark)
  231. "Moon phases diary entry.
  232. An optional parameter MARK specifies a face or single-character string to
  233. use when highlighting the day in the calendar."
  234. (let* ((index (lunar-index date))
  235. (phase (lunar-phase index)))
  236. (while (calendar-date-compare phase (list date))
  237. (setq index (1+ index)
  238. phase (lunar-phase index)))
  239. (if (calendar-date-equal (car phase) date)
  240. (cons mark (concat (lunar-phase-name (nth 2 phase)) " "
  241. (cadr phase))))))
  242. ;;;###diary-autoload
  243. (define-obsolete-function-alias 'diary-phases-of-moon
  244. 'diary-lunar-phases "23.1")
  245. ;; For the Chinese calendar the calculations for the new moon need to be more
  246. ;; accurate than those above, so we use more terms in the approximation.
  247. (defun lunar-new-moon-time (k)
  248. "Astronomical (Julian) day number of K th new moon."
  249. (let* ((T (/ k 1236.85))
  250. (T2 (* T T))
  251. (T3 (* T T T))
  252. (T4 (* T2 T2))
  253. (JDE (+ 2451550.09765
  254. (* 29.530588853 k)
  255. (* 0.0001337 T2)
  256. (* -0.000000150 T3)
  257. (* 0.00000000073 T4)))
  258. (E (- 1 (* 0.002516 T) (* 0.0000074 T2)))
  259. (sun-anomaly (+ 2.5534
  260. (* 29.10535669 k)
  261. (* -0.0000218 T2)
  262. (* -0.00000011 T3)))
  263. (moon-anomaly (+ 201.5643
  264. (* 385.81693528 k)
  265. (* 0.0107438 T2)
  266. (* 0.00001239 T3)
  267. (* -0.000000058 T4)))
  268. (moon-argument (+ 160.7108
  269. (* 390.67050274 k)
  270. (* -0.0016341 T2)
  271. (* -0.00000227 T3)
  272. (* 0.000000011 T4)))
  273. (omega (+ 124.7746
  274. (* -1.56375580 k)
  275. (* 0.0020691 T2)
  276. (* 0.00000215 T3)))
  277. (A1 (+ 299.77 (* 0.107408 k) (* -0.009173 T2)))
  278. (A2 (+ 251.88 (* 0.016321 k)))
  279. (A3 (+ 251.83 (* 26.641886 k)))
  280. (A4 (+ 349.42 (* 36.412478 k)))
  281. (A5 (+ 84.66 (* 18.206239 k)))
  282. (A6 (+ 141.74 (* 53.303771 k)))
  283. (A7 (+ 207.14 (* 2.453732 k)))
  284. (A8 (+ 154.84 (* 7.306860 k)))
  285. (A9 (+ 34.52 (* 27.261239 k)))
  286. (A10 (+ 207.19 (* 0.121824 k)))
  287. (A11 (+ 291.34 (* 1.844379 k)))
  288. (A12 (+ 161.72 (* 24.198154 k)))
  289. (A13 (+ 239.56 (* 25.513099 k)))
  290. (A14 (+ 331.55 (* 3.592518 k)))
  291. (correction
  292. (+ (* -0.40720 (solar-sin-degrees moon-anomaly))
  293. (* 0.17241 E (solar-sin-degrees sun-anomaly))
  294. (* 0.01608 (solar-sin-degrees (* 2 moon-anomaly)))
  295. (* 0.01039 (solar-sin-degrees (* 2 moon-argument)))
  296. (* 0.00739 E (solar-sin-degrees (- moon-anomaly sun-anomaly)))
  297. (* -0.00514 E (solar-sin-degrees (+ moon-anomaly sun-anomaly)))
  298. (* 0.00208 E E (solar-sin-degrees (* 2 sun-anomaly)))
  299. (* -0.00111 (solar-sin-degrees
  300. (- moon-anomaly (* 2 moon-argument))))
  301. (* -0.00057 (solar-sin-degrees
  302. (+ moon-anomaly (* 2 moon-argument))))
  303. (* 0.00056 E (solar-sin-degrees
  304. (+ (* 2 moon-anomaly) sun-anomaly)))
  305. (* -0.00042 (solar-sin-degrees (* 3 moon-anomaly)))
  306. (* 0.00042 E (solar-sin-degrees
  307. (+ sun-anomaly (* 2 moon-argument))))
  308. (* 0.00038 E (solar-sin-degrees
  309. (- sun-anomaly (* 2 moon-argument))))
  310. (* -0.00024 E (solar-sin-degrees
  311. (- (* 2 moon-anomaly) sun-anomaly)))
  312. (* -0.00017 (solar-sin-degrees omega))
  313. (* -0.00007 (solar-sin-degrees
  314. (+ moon-anomaly (* 2 sun-anomaly))))
  315. (* 0.00004 (solar-sin-degrees
  316. (- (* 2 moon-anomaly) (* 2 moon-argument))))
  317. (* 0.00004 (solar-sin-degrees (* 3 sun-anomaly)))
  318. (* 0.00003 (solar-sin-degrees (+ moon-anomaly sun-anomaly
  319. (* -2 moon-argument))))
  320. (* 0.00003 (solar-sin-degrees
  321. (+ (* 2 moon-anomaly) (* 2 moon-argument))))
  322. (* -0.00003 (solar-sin-degrees (+ moon-anomaly sun-anomaly
  323. (* 2 moon-argument))))
  324. (* 0.00003 (solar-sin-degrees (- moon-anomaly sun-anomaly
  325. (* -2 moon-argument))))
  326. (* -0.00002 (solar-sin-degrees (- moon-anomaly sun-anomaly
  327. (* 2 moon-argument))))
  328. (* -0.00002 (solar-sin-degrees
  329. (+ (* 3 moon-anomaly) sun-anomaly)))
  330. (* 0.00002 (solar-sin-degrees (* 4 moon-anomaly)))))
  331. (additional
  332. (+ (* 0.000325 (solar-sin-degrees A1))
  333. (* 0.000165 (solar-sin-degrees A2))
  334. (* 0.000164 (solar-sin-degrees A3))
  335. (* 0.000126 (solar-sin-degrees A4))
  336. (* 0.000110 (solar-sin-degrees A5))
  337. (* 0.000062 (solar-sin-degrees A6))
  338. (* 0.000060 (solar-sin-degrees A7))
  339. (* 0.000056 (solar-sin-degrees A8))
  340. (* 0.000047 (solar-sin-degrees A9))
  341. (* 0.000042 (solar-sin-degrees A10))
  342. (* 0.000040 (solar-sin-degrees A11))
  343. (* 0.000037 (solar-sin-degrees A12))
  344. (* 0.000035 (solar-sin-degrees A13))
  345. (* 0.000023 (solar-sin-degrees A14))))
  346. (newJDE (+ JDE correction additional)))
  347. (+ newJDE
  348. (- (solar-ephemeris-correction
  349. (calendar-extract-year
  350. (calendar-gregorian-from-absolute
  351. (floor (calendar-astro-to-absolute newJDE))))))
  352. (/ calendar-time-zone 60.0 24.0))))
  353. (defun lunar-new-moon-on-or-after (d)
  354. "Julian day number of first new moon on or after Julian day number D.
  355. The fractional part is the time of day.
  356. The date and time are local time, including any daylight saving rules,
  357. as governed by the values of `calendar-daylight-savings-starts',
  358. `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
  359. `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
  360. `calendar-time-zone'."
  361. (let* ((date (calendar-gregorian-from-absolute
  362. (floor (calendar-astro-to-absolute d))))
  363. (year (+ (calendar-extract-year date)
  364. (/ (calendar-day-number date) 365.25)))
  365. (k (floor (* (- year 2000.0) lunar-cycles-per-year)))
  366. (date (lunar-new-moon-time k))
  367. (a-date (progn
  368. (while (< date d)
  369. (setq k (1+ k)
  370. date (lunar-new-moon-time k)))
  371. (calendar-astro-to-absolute date)))
  372. (time (* 24 (- a-date (truncate a-date))))
  373. (date (calendar-gregorian-from-absolute (truncate a-date)))
  374. (adj (dst-adjust-time date time)))
  375. (calendar-astro-from-absolute
  376. (+ (calendar-absolute-from-gregorian (car adj))
  377. (/ (cadr adj) 24.0)))))
  378. (provide 'lunar)
  379. ;;; lunar.el ends here