Created
April 16, 2022 18:58
-
-
Save rruntsch/ed10cfebed6cfc576aa2e67c52f26271 to your computer and use it in GitHub Desktop.
This Python program calls the NASA Fireball Data API to retrieve fireball records in JSON format. It then writes the records to a CSV file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Name: c_nasa_fireball_data.py | |
# Date: April 15, 2022 | |
# Author: Randy Runtsch | |
# | |
# Description: | |
# | |
# Obtain all fireball records, as JSON structures, from the NASA Jet Propulsion Lab Fireball API. | |
# Write each record to the specified output folder and file. | |
# | |
import requests | |
import json | |
import csv | |
class c_nasa_fireball_data: | |
def __init__(self, out_file_nm): | |
# Set the file name variable and create the parameters for the API request. | |
self.out_file_nm = out_file_nm | |
headers = {'Content-type': 'application/json'} | |
# Get data in JSON format and then write it to a CSV file. | |
json_data = self.get_data(headers) | |
self.write_data_to_csv(json_data) | |
def get_data(self, headers): | |
# Post the data request to the BLS API. Return the resulting JSON structure. | |
post = requests.post('https://ssd-api.jpl.nasa.gov/fireball.api', headers = headers) | |
json_data = json.loads(post.text) | |
return json_data | |
def write_data_to_csv(self, json_data): | |
# Convert the data from JSON format to CSV records. Write | |
# each record to the specified output file. | |
# Open the output file. Then, set up the field names for the CSV records and set up the CSV writer. | |
with open(self.out_file_nm, mode = 'w', newline = '') as data_file: | |
data_writer = csv.writer(data_file, delimiter = ',', quotechar = '"', quoting = csv.QUOTE_ALL) | |
# Write CSV file header. | |
fieldnames = json_data['fields'] | |
data_writer.writerow(fieldnames) | |
# Write each record to the output file. | |
for record in json_data['data']: | |
#Write the CSV record to the output file. | |
data_writer.writerow(record) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Name: get_nasa_fireball_data.py | |
# Date: April 15, 2022 | |
# Author: Randy Runtsch | |
# | |
# Description: | |
# | |
# Use the c_nasa_fireball_data class to retrieve all fireball | |
# records from the NASA Jet Propulsion Lab Fireball API and | |
# write them to a CSV file. | |
# | |
from c_nasa_fireball_data import c_nasa_fireball_data | |
c_nasa_fireball_data('c:/nasa_data/fireball.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment