Search code examples
pythonpyephem

PyEphem: Time mismatch when calculating angle from time of the sun and then time from angle


This script is supposed to get the transit time, get the angle of the transit time, and then get the time when the angle is reached. The two print statements I gave should give equivalent outputs.

import ephem
obs = ephem.Observer()
obs.lat = '30'
obs.long = '30'
sun = ephem.Sun(obs)
obs.date = sun.transit_time

sun.compute(obs)
altitude = sun.alt
obs.horizon = altitude
print(obs.next_setting(ephem.Sun(), use_center = True))

Instead, this is giving me a NeverUpError. That doesn't make any sense. I tried with coordinates of 20,20. It didn't give an error but the times were mismatched.

Traceback (most recent call last): File "test.py", line 11, in print(obs.next_setting(ephem.Sun(), use_center = True)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ephem/init.py", line 498, in next_setting return self._riset_helper(body, start, use_center, False, False) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ephem/init.py", line 466, in _riset_helper d0 = visit_transit() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ephem/init.py", line 410, in visit_transit % (body.name, d)) ephem.NeverUpError: 'Sun' transits below the horizon at 2017/7/12 10:05:39


Solution

  • First: following what appears to be an earlier discussion, you do seem to have edited the question into good shape by this point. I can paste it into a file and run it and get the same error.

    Second: the problem may be that you are searching for a circumstance called an “osculation” using a solver that is designed instead to find a straight intersection. A routines like next_setting() is expecting to find that at one time of day the sun is above the horizon, then some time later is below the horizon, and then it narrows in on the exact moment when that transition occurs. But you are setting for it the problem of finding the next setting given a horizon that is as high as the sun’s maximum altitude, if I'm reading your code correctly — and given that setup, there will never be a moment when the sun is “up” because it will never be higher (more or less) than its height above the horizon at the moment of transit. When you set the horizon that far up into the sky, in other words, you create what to PyEphem looks like a day without a sunrise, so it sensibly reports to you that it cannot find a sunset.

    You say that your goal is to “get the time when the angle is reached” — but isn't that the transit_time itself? I am not sure I understand why the transit_time isn't already exactly the time you are looking for.