The International Space Station (ISS) is an iconic symbol of human achievement in space exploration. As it orbits our planet at a breathtaking speed of approximately 28, 000 kilometers per hour, the ISS offers an incredible platform for scientific research and international cooperation. Keeping track of the ISS's location and the astronauts on board is an intriguing endeavor, and Python provides a powerful tool to accomplish just that. In this article, we'll explore a Python code snippet that allows us to track the ISS in real-time and obtain information about the astronauts aboard.
Understanding the CodeBefore diving into the code, let's get an overview of what it does and how it achieves the task of tracking the ISS and retrieving astronaut data. The code utilizes various Python libraries and functions, making it an excellent example of how Python can interact with web APIs and perform mathematical calculations. Here are the key components of the code:
import requests
import json
from math import radians, sin, cos, sqrt, atan2
# Function to calculate the distance between two sets of coordinates
def calculate_distance(lat1, lon1, lat2, lon2):
# Radius of the Earth in kilometers
earth_radius = 6371.0
# Convert latitude and longitude from strings to floats and then to radians
lat1 = radians(float(lat1))
lon1 = radians(float(lon1))
lat2 = radians(float(lat2))
lon2 = radians(float(lon2))
# Haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = earth_radius * c
return distance
# Function to get ISS data
def get_iss_data():
url = "http://api.open-notify.org/iss-now.json"
response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.text)
return data
else:
return None
# Function to display ISS details
def display_iss_data(data):
if data:
timestamp = data["timestamp"]
latitude = data["iss_position"]["latitude"]
longitude = data["iss_position"]["longitude"]
print(f"ISS details as of {timestamp}:")
print(f"Latitude: {latitude}")
print(f"Longitude: {longitude}")
# Coordinates of Bangalore
bangalore_lat = 12.9716
bangalore_lon = 77.5946
# Calculate the distance and altitude
distance = calculate_distance(bangalore_lat, bangalore_lon, latitude, longitude)
print(f"Distance from Bangalore to ISS: {distance} km")
else:
print("Failed to retrieve ISS data. Please check your internet connection.")
# Function to get information about astronauts on the ISS
def get_iss_astronauts():
url = "http://api.open-notify.org/astros.json"
response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.text)
return data
else:
return None
# Function to display information about astronauts on the ISS
def display_iss_astronauts(astronauts_data):
if astronauts_data:
print("Astronauts currently on the ISS:")
for astronaut in astronauts_data["people"]:
name = astronaut["name"]
craft = astronaut["craft"]
print(f"{name} on board {craft}")
else:
print("Failed to retrieve ISS astronaut data. Please check your internet connection.")
Libraries and FunctionsThe code imports the following libraries and defines a mathematical function for distance calculation. The libraries include requests
, json
, and mathematical functions from the math
library for distance calculations.
The calculate_distance
function computes the distance between two sets of coordinates on the Earth's surface using the Haversine formula. This function will be used to find the distance between a fixed location (Bangalore) and the current ISS position.
The get_iss_data
function sends an HTTP request to the Open Notify API to fetch real-time ISS data. This includes the ISS's current location (latitude and longitude) and a timestamp.
The display_iss_data
function takes the retrieved ISS data and presents it to the user. It shows the ISS's latitude and longitude and calculates the distance from Bangalore to the ISS, offering a real-world context for the ISS's location.
The get_iss_data
function is crucial for tracking the ISS. It connects to the Open Notify API and retrieves the real-time location of the ISS. The use of a public API like this makes it easy for anyone with internet connectivity to access current ISS data.
The display_iss_data
function then takes the obtained data and presents it to the user. It shows the ISS's real-time location and its distance from a fixed point (Bangalore).
The following code sections are responsible for obtaining information about astronauts currently on the ISS:
# Function to get information about astronauts on the ISS
def get_iss_astronauts():
url = "http://api.open-notify.org/astros.json"
response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.text)
return data
else:
return None
Displaying Astronaut InformationThe following code section is responsible for displaying the information about astronauts currently on the ISS:
# Function to display information about astronauts on the ISS
def display_iss_astronauts(astronauts_data):
if astronauts_data:
print("Astronauts currently on the ISS:")
for astronaut in astronauts_data["people"]:
name = astronaut["name"]
craft = astronaut["craft"]
print(f"{name} on board {craft}")
else:
print("Failed to retrieve ISS astronaut data. Please check your internet connection.")
The Main FunctionThe main function, responsible for executing all the above components, is as follows:
if __name__ == "__main__":
iss_data = get_iss_data()
display_iss_data(iss_data)
# Get information about astronauts on the ISS
astronauts_data = get_iss_astronauts()
display_iss_astronauts(astronauts_data)
In the main function, we first retrieve and display real-time ISS data using get_iss_data
and display_iss_data
. Then, we proceed to obtain and display information about the astronauts currently on the ISS using get_iss_astronauts
and display_iss_astronauts
.
To use the code, follow these steps:
- Ensure you have Python installed.
- Install the required libraries, if not already installed, using
pip install requests
.
Once you've met these prerequisites, you can run the code by simply executing the script. It will display the ISS's real-time location and the astronauts currently on board.
ConclusionTracking the International Space Station and staying informed about the astronauts aboard is an exciting way to engage with space exploration. Python's versatility and the availability of web APIs like Open Notify make it accessible to anyone interested in monitoring the ISS. Whether you're a space enthusiast or a developer looking to learn about web API interactions and data processing in Python, this code provides an excellent starting point for exploration. Happy tracking!
Additional ResourcesFor further exploration and learning, consider these additional resources:
Comments
Please log in or sign up to comment.