Defining What You'll Need
00:00 This lesson’s goal is to build a graph that helps you figure out when best to look at any given target. To do that, you need a set of viewing times. Night tends to be optimum and the sky coordinates of the target.
00:13
Once you’ve got the sky coordinates, you translate that into an AltAz
00:18
and to do that you need your location, which you’ll recall is squirreled away in the config.py
file from the last lesson.
00:26
Astropy’s coordinates module has a class for altitude azimuth values. So the translation will be from sky coordinate objects to an AltAz
. One note that I actually set a set of viewing times.
00:39 The idea is for the graph to show where the target is over a range of times.
00:44 Remember pandas in the columns of a data frame while pandas is built on top of the NumPy library, which does all sorts of number crunching and has its own data structures.
00:52
The NumPy data structure that I’ll be using is an array, which is kind of like a single column in a data frame. I’m going to use NumPy to create an array of times, which is one of the possible inputs to the AltAz
class.
01:06
So instead of passing in a single time and a location, you can actually pass in an array of times and you’ll get back an array of AltAz
objects.
01:14 You don’t have to install NumPy because it’s one of pandas’ dependencies and if you install pandas, you’ll already have it.
01:22
Once you’ve got the data you want to visualize it. To do that, you use the Matplotlib library. It has a call for creating line graphs and with a little bit of configuration, you can pass your set of AltAz
coordinates to it directly and get them plotted in the graph.
01:37 The altitude of the horizon is going to be zero. Things below the horizon will be negative. So this graph will show when your target is hidden by the curvature of the Earth, or you know if you’re a flat earther below the topmost turtle or whatever else you believe.
01:53 And seeing as I’m going to be using Matplotlib to do all this graphing stuff, well you’re going to need to install Matplotlib, which of course takes pip.
02:01 And don’t forget to use a virtual environment.
02:05 If you’ve played with dates and times at all in code, you’re likely to have the scars from the experience, possibly mental, possibly physical, from banging your head against the wall.
02:15 Well as one axis of our plot is time. That means you’ll have to play with times, times in Python come in two flavors. Naive means there’s no time zone information associated with it.
02:28
Well aware means there is time zone information associated with it. If you just want the local time, you can call the now()
function on the datetime
class and then get the time out of that without further arguments.
02:41 This will be a naive time.
02:43
You could argue about whether that’s a good thing or not, but that’s how Python does it. I suspect this is mostly due to history as the original datetime
code.
02:52 Probably neglected to think about time zones at all design decisions, nothing like having a sore rear end from them coming back to nibble on you later.
03:00
The datetime
class has an astimezone()
method, which can be used to turn a naive time into an aware one or if it’s already an aware one, you can translate it from its time zone into a different one.
03:16
Got it. Good. One last thing. Now of course you need a time zone. There are actually third-party libraries out there to do this, the most popular of which is called pytz
, but as of Python 3.9, the zoneinfo
module is part of the standard library and so I’d recommend you use that.
03:34
And as you encountered in the last lesson, Astropy has its own time object and that’s what the AltAz
translation expects. And as I also mentioned before, Astropy’s time doesn’t take a Python datetime
object to initialize.
03:48
So you’ve gotta do a bit of conversion if you’re starting with a Python datetime
object. In the previous lesson, I didn’t care about the time, so I got around some of it by hard coding midnight.
03:59
Our situation this time around is a little more complicated. I still want to start with a Python datetime
object because this time around, not only do I want midnight and other things, but I also need it translated.
04:10
It needs to be in UTC. AltAz
expects UTC. You could do this manually in Astropy’s time mechanism, but mucking with the dates and times is easier in Python.
04:22 So to get a range of Astropy time objects, you start off with a range of naive hours. These will actually be integers acting as offsets. I’m going to use 9:00 PM to 10:00 AM using zero as midnight and therefore minus three is 9:00 PM.
04:39
The NumPy function linspace()
takes a start and end and returns a continuous sample of numbers between those two values. I’m gonna use that to get my spread of time offsets.
04:50 Then that range of hours needs to be shifted based on local time. To do that, I need a reference point: local midnight. To do that, you start with a naive midnight, which of course is actually tomorrow.
05:04 And then apply the local time zone. And since Astropy wants UTC, you then have to shift that applied aware time zone to midnight in UTC. Once you’ve done all that, you can use that object to create an Astropy time.
05:20
You’ll remember the trick I used before calling strftime()
to cast the reference time to a string and then pass that string to Astropy for parsing.
05:28 With all that done, the last thing you need to do is take that time and use it to shift the naive viewing range to the localized midnight.
05:37 This time stuff is fun, huh? Alright, that’s enough slides. Let’s go look at.
Become a Member to join the conversation.