import http.client
import json
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
API_KEY = "***********"
start_date = "2019-01-01"
end_date = datetime.today().strftime('%Y-%m-%d')
conn = http.client.HTTPSConnection("api.nasa.gov")
conn.request("GET", f"/DONKI/FLR?startDate={start_date}&endDate={end_date}&api_key={API_KEY}")
response = conn.getresponse()
data = response.read().decode("utf-8")
conn.close()
flr_data = json.loads(data)
df = pd.json_normalize(flr_data)
print(df.tail())
print(df.tail(10))
def categorize_event(classType):
categories = []
if 'B' in classType:
categories.append('B')
if 'C' in classType:
categories.append('C')
if 'M' in classType:
categories.append('M')
if 'X' in classType:
categories.append('X')
return categories
df['categories'] = df['classType'].apply(categorize_event)
category_count = df.explode('categories')['categories'].value_counts()
plt.figure(figsize=(8, 6))
category_count.plot(kind='bar', color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'])
plt.xlabel('Solar Event Class')
plt.ylabel('Frequency')
plt.title('Frequency of Certain Solar Events')
plt.show()
df['date'] = pd.to_datetime(df['peakTime'], errors='coerce')
df = df.dropna(subset=['date'])
df['year'] = df['date'].dt.year
year_counts = df['year'].value_counts().sort_index()
plt.figure(figsize=(8, 6))
plt.bar(year_counts.index, year_counts.values, color='orange')
plt.xlabel('Year')
plt.ylabel('Number of Solar Flares')
plt.title('Detected Solar Events by Year')
plt.show()
Language:Python