You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sunrise.py 3.2KB

  1. from math import cos, acos, sin, asin, degrees, radians
  2. import datetime
  3. import time
  4. # Source:
  5. # http://pagesperso-orange.fr/jean-paul.cornec/heures_lc.htm
  6. class sunriseClass:
  7. def __init__(self):
  8. self.theDay=0
  9. self.theDate=[]
  10. self.solarDeclination=0
  11. self.equationOfTime=0
  12. self.latitude=0
  13. self.longitude=0
  14. self.sunrise=0
  15. self.sunset=0
  16. self.meridian=0
  17. self.duration=0
  18. self.sunriseTime=0
  19. self.sunsetTime=0
  20. self.meridianTime=0
  21. self.durationTime=0
  22. def getDay(self,d,m,y):
  23. d=float(d)
  24. m=float(m)
  25. y=float(y)
  26. n1 = int( (m* 275.0)/9.0 )
  27. n2 = int( (m+9.0)/12.0 )
  28. k = 1.0 + int( (y-4.0*int(y/4.0)+2.0)/3.0 )
  29. n=n1-n2*k+d-30.0
  30. return int(n)
  31. def getEoT(self,j):
  32. j=float(j)
  33. m = 357.0+(0.9856*j)
  34. c = (1.914*sin(radians(m))) + (0.02*sin(radians(2.0*m)))
  35. l = 280.0 + c + (0.9856*j)
  36. r=(-2.465*sin(radians(2.0*l))) + (0.053*sin(radians(4.0*l)))
  37. Equ=(c+r)*4.0
  38. return Equ/60.0
  39. def getDec(self,j):
  40. j=float(j)
  41. m = 357.0+(0.9856*j)
  42. c = (1.914*sin(radians(m))) + (0.02*sin(radians(2.0*m)))
  43. l = 280.0 + c + (0.9856*j)
  44. sinDec= 0.3978*sin(radians(l))
  45. return degrees(asin(sinDec))
  46. def getHo(self,Dec,Lat,Lon):
  47. cosHo=( -0.01454-sin(radians(self.solarDeclination))*sin(radians(self.latitude)) ) / ( cos(radians(self.solarDeclination))*cos(radians(self.latitude)) )
  48. return (degrees(acos(cosHo))/15.0)
  49. def setNumericalDate(self,d,m,y):
  50. self.theDate=[d,m,y]
  51. self.theDay=self.getDay(d,m,y)
  52. self.solarDeclination=self.getDec(self.theDay)
  53. self.equationOfTime=self.getEoT(self.theDay)
  54. return None
  55. def setLocation(self,Lat,Lon):
  56. self.latitude=Lat
  57. self.longitude=Lon
  58. return None
  59. def getHM(self,nH):
  60. h=int(nH)
  61. m=int(((nH*60.0)%60)+0.5)
  62. return '%d:%02d' % (h,m)
  63. def calculateWithUTC(self,UTC):
  64. mLon=(self.longitude*4.0)/60.0
  65. Ho=self.getHo(self.solarDeclination,self.latitude,self.longitude)
  66. self.meridian=12.0+self.equationOfTime-mLon+UTC
  67. self.sunrise=self.meridian-Ho
  68. self.sunset=self.meridian+Ho
  69. self.duration=2.0*Ho
  70. self.meridianTime=self.getHM(self.meridian)
  71. self.sunriseTime=self.getHM(self.sunrise)
  72. self.sunsetTime=self.getHM(self.sunset)
  73. self.durationTime=self.getHM(self.duration)
  74. return None
  75. if __name__ == "__main__":
  76. thisDay=datetime.date.today()
  77. mySunrise=sunriseClass()
  78. mySunrise.setNumericalDate(thisDay.day,thisDay.month,thisDay.year)
  79. #mySunrise.setLocation(34.052222,-118.243611) # Los Angeles, UTC -8
  80. #mySunrise.setLocation(41.012222,28.975833) # Istanbul, UTC +2
  81. mySunrise.setLocation(48.856667,2.350833) # Paris, UTC +1
  82. #mySunrise.setLocation(46.711667,4.696667) # renseigner votre location
  83. mySunrise.calculateWithUTC(+(1+time.localtime().tm_isdst))
  84. coucher = mySunrise.sunset
  85. lever = mySunrise.sunrise
  86. print("Heure lever {}".format(mySunrise.sunriseTime))
  87. print("Heure coucher {}".format(mySunrise.sunsetTime))