11.Write a Python program to retrieve the real-time location of the International Space Station (ISS) every 5 seconds, store 100 location records in a CSV file, analyze the latitude and longitude data, and plot latitude vs. time and longitude vs. time graphs.

Input: http://api.open-notify.org/iss-now.json

pip install requests pandas matplotlib

iss_tracking/
│
└── iss_tracking.py


iss_tracking.py:

import requests
import pandas as pd
import matplotlib.pyplot as plt
import time
from datetime import datetime

# ISS API
URL = "http://api.open-notify.org/iss-now.json"

records = []

print("ISS Location Tracking Started")

# Collect 100 records
for i in range(100):

    try:
        response = requests.get(URL, timeout=15)
        data = response.json()

        latitude = float(data["iss_position"]["latitude"])
        longitude = float(data["iss_position"]["longitude"])

        timestamp = datetime.now()

        records.append({
            "Time": timestamp,
            "Latitude": latitude,
            "Longitude": longitude
        })

        print(
            f"Record {i+1}: "
            f"Latitude={latitude}, Longitude={longitude}"
        )

    except Exception as e:
        print("Error:", e)

        if i < 99:
            time.sleep(5)

        continue

    if i < 99:
        time.sleep(5)


# Convert to DataFrame
df = pd.DataFrame(records)

# Save CSV
df.to_csv("iss_location.csv", index=False)

print("\nData saved to iss_location.csv")

# Statistical analysis
print("\nStatistical Analysis")
print(df[["Latitude", "Longitude"]].describe())

print("\nAverage Latitude:", df["Latitude"].mean())
print("Average Longitude:", df["Longitude"].mean())

# Latitude vs Time
plt.figure(figsize=(10, 5))
plt.plot(df["Time"], df["Latitude"], marker="o")
plt.xlabel("Time")
plt.ylabel("Latitude")
plt.title("ISS Latitude vs Time")
plt.xticks(rotation=45)
plt.grid()
plt.tight_layout()
plt.show()

# Longitude vs Time
plt.figure(figsize=(10, 5))
plt.plot(df["Time"], df["Longitude"], marker="o")
plt.xlabel("Time")
plt.ylabel("Longitude")
plt.title("ISS Longitude vs Time")
plt.xticks(rotation=45)
plt.grid()
plt.tight_layout()
plt.show()

print("\nISS Analysis Completed")


