python实现计算日出日落时间算法

  1 #worm_Love sunHelper
  2 #!/usr/bin/python
  3 
  4 import time
  5 import math
  6 
  7 class getImagingDate:
  8     """Get the Imaging Date"""
  9     year  = time.localtime().tm_year
 10     month = time.localtime().tm_mon
 11     day   = time.localtime().tm_mday
 12 
 13 class SunHelper():
 14 
 15     def __init__(self, day, month, year, latitude, longitude):
 16         self.day       = day
 17         self.month     = month
 18         self.year      = year
 19         self.latitude  = latitude
 20         self.longitude = longitude
 21         print'The input date: Year: "%d" Month "%d" Day "%d"' % (year, month, day)
 22 
 23     def getsunrise(self):
 24         zenith = 90.83333333
 25         print zenith
 26 
 27         # 1. first calculate the day of the year
 28         N1 = math.floor(275 * self.month / 9)
 29         N2 = math.floor((self.month + 9) / 12)
 30         N3 = (1 + math.floor((self.year - 4 * math.floor(self.year / 4) + 2) / 3))
 31         dayOfYear = N1 - (N2 * N3) + self.day - 30
 32 
 33         print dayOfYear
 34 
 35         localOffset = math.floor(-1 * self.longitude * 24/360)
 36 
 37         print localOffset
 38 
 39         #2. convert the longitude to hour value and calculate an approximate time
 40         lngHour = self.longitude / 15
 41         t = dayOfYear + ((6  - lngHour) / 24)
 42         #if you want sunset time:
 43         #t = dayOfYear + ((18 - lngHour) / 24)
 44         print lngHour
 45         print t
 46 
 47         #3. calculate the Sun's mean anomaly
 48         M = (0.9856 * t) - 3.289
 49         print M
 50 
 51         #4. calculate the Sun's true 
 52         #NOTE: L potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
 53         L = M + (1.916 * math.sin(M * 3.1415926 / 180)) + (0.020 * math.sin(2 * M * 3.1415926 / 180)) + 282.634
 54 
 55         print L
 56         L = L - 360
 57         print L
 58 
 59         #5
 60         #NOTE: RA potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
 61         #a. calculate the Sun's right ascension
 62         #RA = math.atan(0.91764 * math.tan(L))
 63         RA = (180/3.1415926) * math.atan(0.91764 * math.tan(L * 3.1415926 / 180))
 64         print RA
 65 
 66         #b. right ascension value needs to be in the same quadrant as L
 67         Lquadrant  = (math.floor( L/90)) * 90
 68         RAquadrant = (math.floor(RA/90)) * 90
 69         RA = RA + (Lquadrant - RAquadrant)
 70 
 71         #c. right ascension value needs to be converted into hours
 72         RA = RA / 15
 73 
 74         #6. calculate the Sun's declination
 75         sinDec = 0.39782 * math.sin(L* 3.1415926 / 180)
 76         cosDec = math.cos(math.asin(sinDec))
 77 
 78         #7
 79         #a. calculate the Sun's local hour angle
 80         cosH = (math.cos(zenith * 3.1415926 / 180) - (sinDec * math.sin(self.latitude * 3.1415926 / 180))) / (cosDec * math.cos(self.latitude * 3.1415926 / 180))
 81         print cosH
 82         if (cosH < -1):
 83             sunsetT = 0
 84             print sunsetT
 85             return sunsetT
 86         if (cosH > 1):
 87             sunriseT = 0
 88             print sunriseT
 89             return sunriseT
 90         #the sun never rises on this location (on the specified date)
 91 
 92         #b. finish calculating H and convert into hours
 93         H = 360 - 180/3.1415926 * math.acos(cosH)
 94         #if you want sunset time:
 95         #H = 180/3.1415926 * math.acos(cosH)
 96         H = H / 15
 97 
 98         #8. calculate local mean time of rising/setting
 99         T = H + RA - (0.06571 * t) - 6.622
100 
101         #9. adjust back to UTC
102         UT = T - lngHour
103         #NOTE: UT potentially needs to be adjusted into the range [0,24) by adding/subtracting 24
104         print UT
105 
106         #10. convert UT value to local time zone of latitude/longitude
107         sunriseT = UT - localOffset
108 
109         print sunriseT
110 
111         return sunriseT
112 
113 imagingDate = getImagingDate()
114 
115 sunHelper   = SunHelper(imagingDate.day, imagingDate.month, imagingDate.year, 40.1, -83.0)
116 #sunHelper   = SunHelper(imagingDate.day, imagingDate.month, imagingDate.year, 38.9427564, -92.3266239)
117 sunHelper.getsunrise()
118 sunHelper.getsunset()

做一个项目的时候用python实现了一下网上的计算日出日落的算法,还是蛮闹心的,希望有些帮助吧~

本人只是用python实现了一下,算法来源:http://williams.best.vwh.net/sunrise_sunset_algorithm.htm

转载于:https://www.cnblogs.com/ww-worm/archive/2012/12/11/worm_Love.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值