Skip to content

Instantly share code, notes, and snippets.

@aleksbrgt
Last active March 10, 2017 22:02
Show Gist options
  • Save aleksbrgt/e96c78d660e5866cffa57f4fe709adff to your computer and use it in GitHub Desktop.
Save aleksbrgt/e96c78d660e5866cffa57f4fe709adff to your computer and use it in GitHub Desktop.
Quick and dirty script to parse catalog entries and make fixtures
"""
Quick and dirty script to parse catalog entries and make fixtures
"""
cat_cols = {
'international_designator': [0,10,'text'],
'norad_catalog_number': [13,17,'text'],
'has_payload': [20,20,'bool'],
'operational_status_code': [21,21,'text','?'],
'names': [23,46,'text'],
'owner': [49,53,'text'],
'launch_date': [56,65,'date'],
'launch_site': [68,72,'text'],
'decay_date': [75,84,'date'],
'orbital_period': [87,93,'double'],
'inclination': [96,100,'double'],
'apogee': [103,108,'int'],
'perigee': [111,116,'int'],
'radar_cross_section': [119,126,'double'],
'orbital_status_code': [129,131,'text','EA0'],
}
cat_header = 'catalog.CatalogEntry'
import datetime
def parse_line(line):
obj = {
'model': cat_header,
'fields': {},
}
for col in cat_cols:
data = line[cat_cols[col][0] : cat_cols[col][1] + 1]
data = data.strip()
t = cat_cols[col][2]
try:
if len(data) == 0:
data = cat_cols[col][3]
except IndexError:
if t == 'int' or t == 'float':
data = 0
elif t == 'text':
data = ''
elif t == 'date':
data = '1970-01-01'
else:
data = False
if t == 'int':
data = int(data)
elif t == 'double':
if data == 'N/A':
data = 0
data = float(data)
elif t == 'bool':
data = data is '*'
elif t == 'date':
data = "{} 00:00:00+00:00".format(data)
obj['fields'][col] = data
return obj
import json
fixture = []
with open('satcat.txt', 'r') as input, open('fixture.json', 'w') as output:
for entry in input:
parsed = parse_line(entry)
# Avoid duplicates (in my case, I already have the iss in another fixture)
if parsed['fields']['norad_catalog_number'] != "25544":
fixture.append(parsed)
output.write(json.dumps(fixture))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment