Advertisement
Guest User

astro_calendar.py

a guest
Oct 17th, 2015
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.90 KB | None | 0 0
  1. # esempio: come usare questa classe in una funzione esterna
  2. # from calendar import sun
  3. # s = sun()
  4. # oppore s = (latitudine,longitudine)
  5. # alba = s.sunrise()                                    restituisce oggetto time
  6. # tramonto = s.sunset()                                 restituisce oggetto time
  7. # zenith = s.solarnoon()                                restituisce oggetto time
  8. # local_time = datetime.datetime.now()                  restituisce oggetto datatime
  9. # utc_time = datetime.datetime.utcnow()                 restituisce oggetto datatime
  10. # oppure utc_time = s. timeutc()                        restituisce oggetto datatime
  11.  
  12. from math import cos,sin,acos,asin,tan
  13. from math import degrees as deg, radians as rad
  14. from datetime import date,datetime,time
  15. import datetime
  16. import urllib2
  17. import string
  18.  
  19. class sun:
  20.         def __init__(self,lat=43.252,long=13.5097): # default Corridonia  
  21.                 # __init__      Costruttore di oggetto
  22.                 self.lat=lat
  23.                 self.long=long
  24.  
  25.         # MIA FUNZIONE: data e ora UTC presa da internet
  26.         # esempio chiamata alla funzione self.timeutc()
  27.         def timeutc(self):
  28.                 nomefile = "http://just-the-time.appspot.com/"
  29.                 line = urllib2.urlopen(nomefile).read()
  30.                 line = line[:-1]
  31.                 [data,ora,description] = string.split(line," ") # separo le variabili
  32.                 [anno,mese,giorno] = string.split(data,"-")
  33.                 [ore,minuti,secondi] = string.split(ora,":")
  34.                 millisecondi = 0
  35.                 return datetime.datetime(int(anno),int(mese),int(giorno),int(ore),int(minuti),int(secondi),millisecondi)
  36.  
  37.         # alba      
  38.         def sunrise(self,when=None):
  39. #               if when is None : when = datetime.datetime.now()
  40. #               datetime.datetime.now() mi restituisce data eora attuale
  41. #               datetime.datetime.utcnow() mi restituisce data e ora UTC (di Greenwich)
  42.                 if when is None : when = datetime.datetime.utcnow()
  43.                 self.__preptime(when)
  44.                 self.__calc()
  45.                 return sun.__timefromdecimalday(self.sunrise_t)
  46.  
  47.         # tramonto
  48.         def sunset(self,when=None):
  49.                 if when is None : when = datetime.datetime.utcnow()
  50.                 self.__preptime(when)
  51.                 self.__calc()
  52.                 return sun.__timefromdecimalday(self.sunset_t)
  53.  
  54.         # mezzogiorno solare
  55.         def solarnoon(self,when=None):
  56.                 if when is None : when = datetime.datetime.utcnow()
  57.                 self.__preptime(when)
  58.                 self.__calc()
  59.                 return sun.__timefromdecimalday(self.solarnoon_t)
  60.  
  61.         @staticmethod
  62.         def __timefromdecimalday(day):
  63.                 # day formatta i tre valori di self.solarnoon_t self.sunrise_t self.sunset_t
  64.                 hours  = 24.0*day
  65.                 h      = int(hours)
  66.                 minutes= (hours-h)*60
  67.                 m      = int(minutes)
  68.                 seconds= (minutes-m)*60
  69.                 s      = int(seconds)
  70.                 return time(hour=h,minute=m,second=s)
  71.  
  72.         def __preptime(self,when):
  73.                 # datetime days are numbered in the Gregorian calendar  
  74.                 # while the calculations from NOAA are distibuted as  
  75.                 # OpenOffice spreadsheets with days numbered from  
  76.                 # 1/1/1900. The difference are those numbers taken for  
  77.                 # 18/12/2010  
  78.                 self.day = when.toordinal()-(734124-40529) # la data attuale in giorni dai primi del 1900
  79.                 t=when.time() # t a come valore l'ora attuale  
  80.                 self.time= (t.hour + t.minute/60.0 + t.second/3600.0)/24.0
  81. # timezone ora solare +1 ora legale +2
  82. #               imposto timezone a 0 cosi ottengo ora UTC rispetto a Greenwich
  83.                 self.timezone=0
  84.                 offset=when.utcoffset() # offset vale None
  85.                 if not offset is None:
  86.                         self.timezone=offset.seconds/3600.0
  87.  
  88.     def __calc(self):
  89.             timezone = self.timezone # in hours, east is positive  
  90.                 longitude= self.long     # in decimal degrees, east is positive  
  91.                 latitude = self.lat      # in decimal degrees, north is positive  
  92.  
  93.                 time  = self.time # percentage past midnight, i.e. noon  is 0.5  
  94.                 day      = self.day     # daynumber 1=1/1/1900  
  95.  
  96.                 Jday     =day+2415018.5+time-timezone/24 # Julian day  
  97.                 Jcent    =(Jday-2451545)/36525    # Julian century  
  98.  
  99.                 Manom    = 357.52911+Jcent*(35999.05029-0.0001537*Jcent)
  100.                 Mlong    = 280.46646+Jcent*(36000.76983+Jcent*0.0003032)%360
  101.                 Eccent   = 0.016708634-Jcent*(0.000042037+0.0001537*Jcent)
  102.                 Mobliq   = 23+(26+((21.448-Jcent*(46.815+Jcent*(0.00059-Jcent*0.001813))))/60)/60
  103.                 obliq    = Mobliq+0.00256*cos(rad(125.04-1934.136*Jcent))
  104.                 vary     = tan(rad(obliq/2))*tan(rad(obliq/2))
  105.                 Seqcent  = sin(rad(Manom))*(1.914602-Jcent*(0.004817+0.000014*Jcent))+sin(rad(2*Manom))*(0.019993-0.000101*Jcent)+sin(rad(3*Manom))*0.000289
  106.  
  107.                 Struelong= Mlong+Seqcent
  108.                 Sapplong = Struelong-0.00569-0.00478*sin(rad(125.04-1934.136*Jcent))
  109.                 declination = deg(asin(sin(rad(obliq))*sin(rad(Sapplong))))
  110.  
  111.                 eqtime   = 4*deg(vary*sin(2*rad(Mlong))-2*Eccent*sin(rad(Manom))+4*Eccent*vary*sin(rad(Manom))*cos(2*rad(Mlong))-0.5*vary*vary*sin(4*rad(Mlong))-1.25*Eccent*Eccent*sin(2*rad(Manom)))
  112.  
  113.                 hourangle= deg(acos(cos(rad(90.833))/(cos(rad(latitude))*cos(rad(declination)))-tan(rad(latitude))*tan(rad(declination))))
  114.  
  115.                 self.solarnoon_t=(720-4*longitude-eqtime+timezone*60)/1440
  116.                 self.sunrise_t  =self.solarnoon_t-hourangle*4/1440
  117.                 self.sunset_t   =self.solarnoon_t+hourangle*4/1440
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement