1

I'd like to set up a Python program to be able to pull sunrise/sunset from various locations to trigger lights in the local location to symbolize the remote sunrise as it would be -- if you were actually there. What I mean by this, is if you live in Berlin, and YOUR sunrise/sunset in the middle of December is 7:45am/4:15pm, you could have a little LED that would light when some tropical sunrise is happening (say Hawaii). But this is happening in reaction to your own local time.

So, using Python's ephem, and pytz, and localtime, pull the information for sunrise/sunset for various locations, and trigger events based on each location.

I've set up a test program using Vancouver, BC and Georgetown, French Guyana as a test case, and it mostly works -- but the sunrise/sunset for Georgetown is completely wrong.

You can cut and paste this entire thing into a Python window to test, and please forgive the extraneous time calls, but I find it interesting to see what each of the time calls pull.

Nonetheless, what you'll see is that the Guyana.date is absolutely correct, but the sunrise/sunset is something like 1:53 AM / 13:57 PM, which is completely whacked. Any ideas on how this could have gone so horribly, horribly wrong?

EDITED TO REMOVE UNNECESSARY CODE

import ephem
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import time 

Guyana = ephem.Observer()
Guyana.lat = '5'
Guyana.lon = '58.3'
Guyana.horizon = 0
Guyana.elevation = 80 
Guyana.date = datetime.utcnow()

sun = ephem.Sun()


print("Guyana.date is ",Guyana.date)
print("Guyana sunrise is at",Guyana.next_rising(sun))
print("Guyana sunset is going to be at ",Guyana.next_setting(sun))

The results of this are as follows:

Guyana.date is  2014/10/4 16:47:36
Guyana sunrise is at 2014/10/5 01:53:26
Guyana sunset is going to be at  2014/10/5 13:57:05

What is so wrong about this, is that the actual sunrise in Guyana today is 5:40am, so the 1:53:26 is off by not just hours, but in many ways off.

Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21

1 Answers1

2

To answer your updated version: positive longitudes refer to East but Guyana (America) is to the west from Greenwich therefore you should use minus: Guyana.lon = '-58.3' then the time of the sunrise becomes:

Guyana sunrise is at 2014/10/5 09:39:47

The time is in UTC, you could convert it to the local (Guyana) time:

>>> utc_dt = Guyana.next_rising(sun).datetime().replace(tzinfo=pytz.utc)
>>> print(utc_dt.astimezone(pytz.timezone('America/Guyana')))
2014-10-05 05:39:46.673263-04:00

5:40am local time seems reasonable for sunrise.


From ephem documentation:

Dates always use Universal Time, never your local time zone.

As I said in my answer to your previous question:

You should pass datetime.utcnow() to the observer instead of your local time.

i.e., Vancouver.date = now is wrong because you use datetime.now() that returns a naive local time (pass datetime.utcnow() (or ephem.now()) instead), Guyana.date = utc_dt.astimezone(FrenchGuyanaTZ) is wrong because FrenchGuyanaTZ timezone is probably has a non-zero UTC offset (pass just utc_dt instead).

Unrelated: a correct way to find the timestamp for the current time is time.time() i.e., gmNow should be equal to timetimeNow (always). As I said:

you should use time.time() instead of time.mktime(time.localtime()) the later might return wrong result during DST transitions.

The correct way to find the current time in UTC is:

utc_dt_naive = datetime.utcnow()

Or if you need an aware datetime object:

utc_dt = datetime.now(utc_timezone)
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • What is it you do for a living that puts you into the path of so MUCH UTC knowledge?? :) Luckily, between the naive and aware, all of the timezones I'm planning on using for the remote sunset time are equatorial, and don't use DST (thank god). I think whatever is going wrong now perhaps has more to do with ephem, than pytz, as the Guyana date/time is correct, yet the sunrise is apparently at 1:53am. – Octoth0rpe Oct 03 '14 at 13:26
  • @Octoth0rpe: I'm sorry but it is not me. It is `ephem`'s documentation that requires UTC timezone (I've added the direct quote to the answer). Assume that `ephem`, `pytz`, `datetime` are right and you use them wrong -- there are bugs in the libraries but it is more likely that the actual error is in your code until you have more experience. About Guyana: get the sunrise time as UTC and then convert it to the Guyana's timezone (what is 1:53am? -- is it time in your local timezone, in UTC, in Guyana's timezone?) – jfs Oct 03 '14 at 13:45
  • Thanks J.F, I wasn't even being sarcastic, I'm quite serious. The amount of finangling around all the different flavours of UTC/Datetime etc has caught me off guard, and I'm humbled by somebody who has their brain around it as much as you do. The weird thing about the 1:53am sunrise for Guyana, is that it's not even off by just hours, also minutes. Sunrise today is actually 5:40am, yet ephem is reporting it to occur at 1:53. – Octoth0rpe Oct 04 '14 at 16:19
  • edited my original question to whiddle it down to the nuts and bolts. – Octoth0rpe Oct 04 '14 at 16:55
  • ok, that helps. The decimal degrees of Guyana's longitude is actually '-58', not '+58'. Now I'm getting the proper sunrise time, but in my own local time. One step closer! Guyana.date is 2014/10/4 18:44:12 Guyana sunrise is at 2014/10/5 09:40:07 Guyana sunset is going to be at 2014/10/4 21:43:27 – Octoth0rpe Oct 04 '14 at 18:45
  • @Octoth0rpe: my answer explicitly says that the time is in UTC if you convert it to Guyana's local time (as shown in my answer) then it is: 5:40am (sunrise) and 5:44pm (sunset) correspondingly (GYT-0400 time). – jfs Oct 04 '14 at 18:50
  • Mr Sebastian, my wife says I should type "I love you" here, but let's not go quite that far. I'll stick with THANK YOU! THANK YOU!!. This has been quite a journey. I'm a bit disappointed with myself for not solving it completely on my own, but relieved to finally be able to move on to step 2. Again, Thank you! – Octoth0rpe Oct 05 '14 at 16:01