1# This Python script reads a satellites.json file in the old format
2# (using names for satellite IDs) and ouputs a file that uses catalog numbers
3# as satellite IDs.
4# --Bogdan Marinov
5
6import json
7
8# Open the satellites.json file and read it as a hash
9with open('satellites.json') as old_f:
10    old_json = json.load(old_f)
11
12# For each key/value pair:
13new_json = dict()
14for name, satellite in old_json['satellites'].items():
15    if 'tle2' not in satellite:
16        continue
17
18    # The catalog number is the second number on the second line of the TLE
19    catalog_num = satellite['tle2'].split(' ')[1]
20    satellite['name'] = name
21    if 'lastUpdated' in satellite:
22        del satellite['lastUpdated']
23    new_json[catalog_num] = satellite.copy()
24
25# Save the result hash as satellites_with_numbers.json
26old_json['satellites'] = new_json
27with open('satellites_with_numbers.json', 'w') as new_f:
28    json.dump(old_json, new_f, indent=4)
29