Sunrise / set calculations

我的梦境 提交于 2019-11-28 22:02:13

Why all the calls to radians and degrees? I thought the input data was already in decimal degrees.

I get a result of 7:37am if I:

  • strip out all of the calls to radians and degrees
  • correct the lat / long to: 45.9 and -66.6 respectively
  • correct time_in_utc to fall within 0 and 24.

Edit:
As J. F. Sebastian points out, the answer for the sunrise time at this location according to the spreadsheet linked in the question and the answer provided by using the Observer class of the ephem are in the region of 07:01-07:02.

I stopped looking for errors in dassouki's implementation of the US Naval Observatory's algorithm once I got a figure in the right ballpark (07:34 in the comments in the implementation).

Looking into it, this algorithm makes some simplifications and there is variation about what constitutes 'sunrise', some of this is discussed here. However, in my opinion from what I've recently learnt on this matter, these variations should only lead to a difference of a few minutes in sunrise time, rather than over half an hour.

You could use ephem python module:

#!/usr/bin/env python
import datetime
import ephem # to install, type$ pip install pyephem

def calculate_time(d, m, y, lat, long, is_rise, utc_time):
    o = ephem.Observer()
    o.lat, o.long, o.date = lat, long, datetime.date(y, m, d)
    sun = ephem.Sun(o)
    next_event = o.next_rising if is_rise else o.next_setting
    return ephem.Date(next_event(sun, start=o.date) + utc_time*ephem.hour
                      ).datetime().strftime('%H:%M')

Example:

for town, kwarg in { "Fredericton": dict(d=3, m=3, y=2010,
                                         lat='45.959045', long='-66.640509',
                                         is_rise=True,
                                         utc_time=20),

                     "Beijing": dict(d=29, m=3, y=2010,
                                     lat='39:55', long='116:23',
                                     is_rise=True,
                                     utc_time=+8),

                     "Berlin": dict(d=4, m=4, y=2010,
                                    lat='52:30:2', long='13:23:56',
                                    is_rise=False,
                                    utc_time=+2) ,

                     "Moscow": dict(d=4, m=4, y=2010,
                                    lat='55.753975', long='37.625427',
                                    is_rise=True,
                                    utc_time=4) }.items():
    print town, calculate_time(**kwarg)

Output:

Beijing 06:02
Berlin 19:45
Moscow 06:53
Fredericton 07:01

I suspect this has something to do with not actually performing floating point division. In python if a and b are both integers, a / b is also an integer:

   $ python
   >>> 1 / 2
   0

Your options are either to coerce to float one of your arguments (that is, instead of a/b do a float(a) / b) or to make sure the '/' behaves properly in a Python 3K way:

   $ python
   >>> from __future__ import division
   >>> 1 / 2
   0.5

So if you stick that import statement at the top of your file, it may fix your problem. Now / will always produce a float, and to get the old behaviour you can use // instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!