Series: Code IT -1 | Python to Track ISS real time
This series of blogs are technical coding blogs, where I’ll post several python projects that I make in my leisure time. Newbie coders, engineering students and all python enthusiasts can make use of this tutorial. Learn it from here, make your own version, and populate your GitHub!
How did I came across this Idea ?
The first in this series of blog is about how I made a python script that tracks the international space station in real time. Okay, I admit, this idea did not come to me on its own. I was inspired. I came across this beginner, who posted about his beginner python project, which tracked ISS in real time. And viola! I thought, why not make own version of this.
So if you are first year CS student, or a newcomer to python, you can learn a lot from this particular project. Not only you’ll learn scripting clever application, you’ll have a new project for your GitHub! So let’s break down this application into a problem statement, and then solve it one by one :)
What exactly does our application should do ?
It should be a terminal application (best for beginners)
It should extract real time data like longitude and latitude of ISS
This real time data must be stored somewhere. This could be used for data analysis, or to plot it on a map.
We will use the data to plot it on a map.
So let’s start doing this, part by part.
Part 1: extract longitude and latitude.
For this, we are going to make use of an API.
This is the ‘Where is the ISS at’ API. We can extract the data needed from this source. You may also try using Open Notify for this. <http://open-notify.org/Open-Notify-API/>
We need following modules for this python script : request, time, csv, pandas, plotly express. The first three might be pre-installed with your latest Python3. If not, you can pip install these quickly.
now let’s make use of the api we selected. It’s always a good practice to make use of functions while coding. Let’s make a function that make use of this api, to get data as json object. We can then use this to extract longitude and latitude.
Here, we are making use of the request module. It helps us to send http request to the specified webpage. This basically returns a ‘response object’. More specifically, we are using the get() method of this module, that sends out a GET request. And lastly, the json() method returns a json object back.
If you dive into the entire response you got, you’ll be able to understand how here we are extracting data like latitude and longitude.
Part 2: Load this data onto a CSV file in real time.
After initializing a blank list data_list(line3), we will append the latitude and longitude(line 5 & 6) that’s returned as result when the function get_iss_data() is called (line 2). If you want, you can print this out, incase you want to show result on a terminal (line 7).
we make use of writerow (and not writerows) to write this data onto a csv file. It is recommended that you created a csv file and enter ‘latitude,longitude’ into it, so that the header for the data is created. I did not include that in the code.
Part 3: Plotting the saved data onto a Map
Now let’s make use of this saved data and plot the longitude and latitude on the map. But let’s step back see what exactly are we trying to achieve. If you track and save data for one second, you might get one or two pairs of longitude and latitude. If you track this for considerable amount of time, and plot the same on the world map, you are in essence tracking the movement of the International space station for that time and date. This is quite useful and has large scope for research and monitoring.
Here we created a separate function that does the plotting job for us. First we are loading the csv data onto a data frame. Basic Pandas. Later we are making use of the plotly express module
“In a geographic scatter plot, each row of data_frame
is represented by a symbol mark on a map.” — from plotly express documentation page.
this is what scatter_geo does for us. Make sure that you have typed out the header in your csv file as ‘latitude’ and ‘longitude’. Make sure the same are specified as parameter here. Graph object figures support an update_layout()
method that may be used to update multiple nested properties of a figure's layout.
And finally we are printing this out. The result will be displayed on your default browser. It will pop open once the script is executed on your system.
And that’s it. You have done it. You have successfully tracked the ISS, logged the data and also visualized it using python, all with a very simple script!
Give a pat on your back. Self love is important.
Now sit back and relax. A general advice to students : don’t overstress yourself, try to love what you do, and you will enjoy your coding life more. And this is just the beginning….