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

Finding Quasars

“Serena” Quasar’s sparcl ID: 719e7ea6-8b79-11ef-be93-525400f334e1

00:00 In the previous lesson, I showed you how to get started on your dashboard by graphing quasar spectral data from a CSV file. In this lesson, I’ll start the journey of how to get that very same data.

00:12 In order to get quasar spectral data, first you need the ID of a quasar. You could ask nicely for Serena, but the database isn’t going to know what you’re talking about.

00:22 The Astro Lab dataset has a find mechanism, and you’ll use that to find some quasars. Pick a “sparcl” ID of one you like. I’ll suggest to pick Serena, and then in the next lesson, I’ll show you how to get the spectral data for her.

00:38 The sparcl database has a Python client, which you need to install. If you’re going to follow along throughout this course, I’ll be cheating. I have an astronomer on my side who looked up good examples and gave me the ID for Serena.

00:53 This is a non-trivial task as there are millions of quasars in the database. The script on the screen here lets you query from them all using the Sparcl database.

01:04 Sparcl contains more than just quasars, so I’m going to be a good programmer and group the names of things in an enum. This is the client to query Sparcl, and this is that aforementioned enum.

01:17 QSO is short for quasar, but you can also look stars and galaxies up as well.

01:24 The find_quasars function has two required arguments: the right ascension and the declination of our object. Basically, this is where in the sky to look for something.

01:35 The other arguments are optional. Sparkle lets you look things up near a point, so although I’m specifying ra and dec, the degree range argument is how many degrees around the point to look.

01:48 The database goes down to arcseconds, which is a fraction of a degree, so you can pass in small floating-point values here if you wish to be precise.

01:57 The database also includes information on the redshift of the objects you’re looking for. The tricky part is it isn’t always correct. Astronomers have tools to auto-calculate redshift, but they’re error-prone.

02:09 So like with the degrees arguments, you can look over a range of shifts here using the lower and upper values. Finally, you can limit just how many objects come back from your query.

02:20 I’ve defaulted it to five. If you don’t provide a limit, Sparcl will stop you at 500.

02:26 This is where the client connects. It’s a bit of a slow call and it can take a second or more.

02:35 The find method implements our search. outfields specifies the columns you want back from the database. Here I’m asking for the sparcl_id, which I’ll need for later, the actual ra and dec of the object.

02:49 Remember, you’re searching a range around a central point, and the last value here is the date of observation.

02:57 The constraints part of the call is like a WHERE clause in SQL. In fact, that’s how this is implemented in the backend. The spectype constraint specifies what type of object to look for.

03:08 I’ve specified a quasar using our enum. Note, this parameter is a list so you can search on multiple types at a time.

03:16 The redshift constraint says what range of redshift values to search within. This is also a list with the first argument being the lower bound and the second the upper. ra and dec values are also lists using lower and upper bounds for each.

03:32 Here, I’m taking the function’s ra and dec arguments as the center point. Then, adding and subtracting degree ranges to specify the search boundary.

03:41 The Sparcl database has multiple datasets. Some of them are free and some are not. They expire some of the datasets as new ones come out as well. Unless you’ve got an Astro Data Lab account, I’d use this value as it’s one of the longer-lived free datasets.

03:57 When you run the client, you might get deprecation warnings about certain datasets, but you’ll see that in a minute.

04:04 The last parameter to find here is the limit.

04:10 The result of the find call is a wrapper object that has a parameter named records. records is a list of dictionaries, each dictionary containing the outfield keys and corresponding values for the results.

04:23 Here I’m printing out some of the data from our records, starting with the sparcl_id on the first line, the ra and dec values on the second, and the observation date on the third.

04:33 The advantage of having an astronomer to guide me is I know where to look in the sky for my quasar.

04:40 If you run this code as a script, it will search within the given criteria. A less lazy programmer would maybe have changed ra and dec to be command-line arguments, but you can edit the script and play to your heart’s content, point it at any point in the sky and see if you can find a quasar.

04:57 Let’s run this puppy.

05:02 Connecting takes a bit.

05:05 Let me pause it here for a moment. Remember when I said they change the datasets? Well, every time you connect to Sparcl, it warns you about one such upcoming change.

05:14 Thankfully, neither of these are the datasets I’m querying, so I can ignore this. Resuming, takes a second, and there you go, results.

05:25 There are five records here each with a sparcl_id, and in the next lesson I’ll show you how to use this ID to get spectrum data. A careful eye will note that Serena isn’t in this particular response set.

05:38 I’ll leave it as a “where is Waldo” kind of exercise for you to see if you can find her.

05:44 In the next lesson, I’ll show you how to take a sparcl_id and get to the corresponding spectrum data. If I made a joke about flux capacitors here, would I be dating myself?

05:55 Yeah. I’m so old, I’m redshifted.

Become a Member to join the conversation.