Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Translating Coordinates

00:00 This is a function that I’ll be putting in my script for now, I’m just going to show it to you on its own and then I’ll combine it in a few minutes. The purpose of this function is to get the local viewing times shifted to UTC so that AstroPay can use it for its alt as calcs.

00:16 As I mentioned before, midnight is technically tomorrow, so I’m using the today() method to get today and a timedelta object to adjust that to tomorrow.

00:25 If you were doing this kind of thing a lot in your code, I would recommend Googling around for time libraries. There are some good ones out there that handle things like tomorrow or next week or three Sundays from now.

00:37 I’ve used Arrow, Maya, and DeLorean, and they’re all decent libraries and they’ve all got their strengths and weaknesses. Didn’t want to bother with yet another third-party library here, so I just stuck with timedelta, but just know there’s other choices out there.

00:51 Note that the value of tomorrow is only a date, not a datetime. I want a datetime of midnight. Calling combine() adds time information to a date object, and by calling the time() function without any arguments, I get back midnight.

01:07 So the midnight variable now contains a naive datetime object for midnight tomorrow.

01:13 Since I don’t want a naive object, I use the .astimezone() call to add my time zone information to it.

01:22 And you’ll recall that Astropy doesn’t want local time. It wants UTC, so I call .astimezone() again,

01:29 as my midnight value is aware. This second call translates it from Toronto time to UTC,

01:37 and then in order to head towards creating an Astropy time, I take the aware midnight value and turn it into a string, and then use that string to construct the Astropy time project.

01:49 It was all a little messy, wasn’t it? The final step here is what you actually came here for. You can consider the naive viewing times NumPy array, like a series of deltas.

01:58 It starts with minus three, which is three hours before midnight. Somewhere along the way you get zero meaning midnight and et cetera up to 10:00 AM. When you do math on a NumPy array, you do math on each of the values in the array.

02:12 By adding our Astropy time to the naive viewing times, the end result is a new NumPy array of Astropy time objects with the deltas applied. So what gets returned is an array full of UTC viewing times.

Become a Member to join the conversation.