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, timezone

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

records = []

print("ISS Location Tracking Started")

# Collect exactly 100 successful records
while len(records) < 100:

    request_started = time.monotonic()

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

        data = response.json()

        if data.get("message") != "success":
            raise ValueError("Invalid response from ISS API")

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

        timestamp = datetime.fromtimestamp(
            data["timestamp"],
            tz=timezone.utc
        )

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

        print(
            f"Record {len(records)}: "
            f"Latitude={latitude}, Longitude={longitude}"
        )

        if len(records) < 100:
            elapsed = time.monotonic() - request_started
            time.sleep(max(0, 5 - elapsed))

    except Exception as e:
        print("Error:", e)
        print("Retrying in 5 seconds...")
        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")

