博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现计算日出日落时间算法
阅读量:7104 次
发布时间:2019-06-28

本文共 4431 字,大约阅读时间需要 14 分钟。

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.622100 101         #9. adjust back to UTC102         UT = T - lngHour103         #NOTE: UT potentially needs to be adjusted into the range [0,24) by adding/subtracting 24104         print UT105 106         #10. convert UT value to local time zone of latitude/longitude107         sunriseT = UT - localOffset108 109         print sunriseT110 111         return sunriseT112 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

你可能感兴趣的文章
Git常用命令及日常问题集锦
查看>>
强技术推动高需求,但是未来市值将缩水?看国外分析师如何评价苹果
查看>>
深入 Nginx 之配置篇
查看>>
程序员编程10大哲理!血的教训,后人警惕!
查看>>
数据库事务的四种隔离级别
查看>>
关于Vue.nextTick()的使用
查看>>
WPF项目示例1:入门
查看>>
Dubbo 新编程模型之注解驱动
查看>>
从template到DOM(Vue.js源码角度看内部运行机制)
查看>>
Sequelize 中文文档 v4 - Working with legacy tables - 使用遗留表
查看>>
《Java Concurrency in Practice》中三个VehicleTracker例子的分析
查看>>
spring-springmvc项目介绍
查看>>
为什么要从0开始计数
查看>>
ThinkJS 3.0 正式版发布!
查看>>
js简单前端模板引擎实现
查看>>
初识Java(译)
查看>>
MegaTags:网站META标签生成器
查看>>
PHP图片处理之二维码加文字
查看>>
树莓派新系统用户配置
查看>>
格式化创建文件系统、内核支持的文件系统、指定卷标、查看超级块信息、文件系统修复...
查看>>