Package elisa :: Package plugins :: Package bad :: Package weather :: Module sun
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.weather.sun

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  # Copyright 2006, Frank Scholz <coherence@beebits.net> 
 18   
 19  """ calculates sunrise or sunset for a given time and place 
 20      the code is from the last millenium 
 21      and as far as I can remember modeled after 
 22      some formular in some book about astronomy 
 23  """ 
 24   
 25   
 26  import os,sys 
 27  import string 
 28  import math 
 29  import time 
 30   
31 -def normalize(z, d):
32 if not d: 33 raise ("Trying to normalize with zero offset") 34 35 while z < 0: 36 z = z + d 37 while z >= d: 38 z = z - d 39 40 return z
41
42 -def suntime(type, lat, lon, tz, yday):
43 44 """ type = rise or set 45 tz = diff from gmt 46 date = time.localtime() 47 yday = date[7] 48 """ 49 50 a = 1.5708 51 b = math.pi 52 c = 4.71239 53 d = 6.28319 54 e = 0.0174533 * lat 55 f = 0.0174533 * lon 56 g = 0.261799 * tz 57 58 """ Twilight (R) 59 astronomical R = -.309017 60 nautical R = -.207912 61 civil R = -.104528 62 """ 63 64 """ Sunrise/sunset """ 65 r = -.0145439 66 67 if type == 'rise': 68 j = a 69 else: 70 j = c 71 72 k = yday + ((j - f) / d) 73 l = (k * .017202) - .0574039 # solar mean anomaly 74 m = l + .0334405 * math.sin (l) 75 m = m + 4.93289 + 3.49066e-4 * math.sin (2 * l) 76 77 m = normalize (m, d) 78 79 if (m / a) - int (m/a) == 0: 80 m = m + 4.84814e-6 81 82 p = math.sin (m) / math.cos (m) # solar right ascension 83 p = math.atan2 (.91746 * p, 1) 84 85 if m > c: 86 p = p + d 87 elif m > a: 88 p = p + b 89 90 q = .39782 * math.sin (m) # solar declination 91 q = q / math.sqrt (-q * q + 1) 92 q = math.atan2 (q, 1) 93 94 s = r - (math.sin(q) * math.sin (e)) 95 s = s / (math.cos (q) * math.cos (e)) 96 97 if abs (s) > 1: 98 return None # Null phenomenon 99 100 s = s / math.sqrt (-s * s + 1) 101 s = a - math.atan2 (s, 1) 102 103 if type == 'rise': 104 s = d -s 105 t = s + p - 0.0172028 * k - 1.73364 # local apparent time 106 u = t - f 107 v = u + g 108 v = normalize (v, d) 109 v = v*3.81972 110 111 hour = int (v) 112 min = int ((v - hour) * 60 + .5) 113 114 return (hour, min)
115 116 if __name__ == "__main__": 117 print time.localtime() 118 s = suntime ('rise', 41.23, 2.11, 1, time.localtime()[7]) 119 print s 120 s = suntime ('set', 41.23, 2.11, 1, time.localtime()[7]) 121 print s 122