#
# Calculation sunrise/sunset
# example from Nautical Almanac Office, United States Naval Observatory
#
# Inputs:
#	day, month, year:      date of sunrise/sunset
#	latitude, longitude:   location for sunrise/sunset
#	zenith:                Sun's zenith for sunrise/sunset
#	  offical      = 90 degrees 50'
#	  civil        = 96 degrees
#	  nautical     = 102 degrees
#	  astronomical = 108 degrees
#
eval (define (day-of-year dd mm yyyy) \
 (let* ((N1 (floor (/ (* 275 mm) 9))) \
        (N2 (floor (/ (+ 9 mm) 12))) \
        (N3 (+ 1 (floor (/ (- yyyy (* 4 (floor (/ yyyy 4))) -2) 3)))) \
        (N (- N1 (* N2 N3) (- dd) 30))) \
   N))
eval (define (hrmin T offset) \
  (let* ((ohr (floor T)) \
         (h (modulo (+ ohr offset) 24)) \
         (m (floor (* 60 (- T ohr))))) \
    (string-append (if (> 10 h) "0" "") (number->string h) ":" \
                   (if (> 10 m) "0" "") (number->string m) \
                   " UTC" (if (> 0 offset) "-" "+") \
                   (number->string (abs offset)))))  
eval (define (inbound L) \
  (cond ((> L 360) (- L 360)) \
        ((< L 360) (+ L 360)) \
        (else L)))
eval (define (sind x) (sin (* 0.01745329 x)))
eval (define (cosd x) (cos (* 0.01745329 x)))
eval (define (tand x) (tan (* 0.01745329 x)))
eval (define (asind x) (* 57.29578 (asin x)))
eval (define (acosd x) (* 57.29578 (acos x)))
eval (define (atand x) (* 57.29578 (atan x)))
eval (define (sunrise dd mm yyyy latitude longitude rise zenith offset) \
 "sunrise dd mm yyyy lat long rise [rise/set] zenith [90 5/6] offset [10]"
 (let* ((lngHour (/ longitude 15)) \
        (N (day-of-year dd mm yyyy)) \
        (t (if (string=? rise "rise") \
              (+ N (/ (- 6 lngHour) 24)) \
              (+ N (/ (- 18 lngHour) 24)))) \
        (M (- (* 0.9856 t) 3.289)) \
        (L (inbound (+ M (* 1.916  (sind M)) (* 0.020 (sin (* 2 M))) 282.634))) \
        (RA (atand (* 0.91764 (tand L)))) \
        (Lquadrant  (* (floor (/ L 90))  90)) \
        (Rquadrant (* (floor (/ RA 90))  90)) \
        (RAH  (/ (+ RA (- Lquadrant Rquadrant)) 15)) \
        (sinDec (* 0.39782 (sind L))) \
        (cosDec (cosd (asind sinDec))) \
        (cosH (/ (- (cosd zenith) (* sinDec (sind latitude))) \
                 (* cosDec (cosd latitude)))) \
        (H  (if (string=? rise "rise") \
               (/ (- 360 (acosd cosH)) 15) \
               (/ (acosd cosH) 15))) \
        (T (+ H RAH (- (* 0.06571 t)) (- 6.622))) \
        (UT (- T lngHour))) \
    (hrmin UT offset)))
eval (define (sunrise-today) \
 (let* ((dy (substring (date) 0 8)) \
        (yyyy (string->number (substring dy 0 4))) \
        (mm (string->number (substring dy 4 6))) \
        (dd (string->number (substring dy 6 8)))  \
        (latitude -27.5)  \
        (longitude 153)  \
        (zenith (+ 90 (/ 5 6))) \
        (offset 10)) \
   (display (string-append "Date   : " dy)) (newline) \
   (display (string-append "Sunrise: " (sunrise dd mm yyyy latitude longitude "rise" zenith offset))) \
   (newline) \
   (display (string-append "Sunset : " (sunrise dd mm yyyy latitude longitude "set" zenith offset)))))
macro brisbane
 eval (sunrise-today)
;;;; 
