Skip to content

Instantly share code, notes, and snippets.

@klightspeed
Last active February 12, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klightspeed/115202842477e26106c6e113d714ae29 to your computer and use it in GitHub Desktop.
Save klightspeed/115202842477e26106c6e113d714ae29 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
import json
import edtslib.system
maxsqdist = 4000 ** 2
def openfile(filename, filetype = None):
if filetype is None:
if filename[-4:] == '.bz2':
filetype = 'bz2'
elif filename[-3:] == '.gz':
filetype = 'gz'
elif filename[-3:] == '.xz' or filename[-5:] == '.lzma':
filetype = 'lzma'
if filetype == 'bz2':
import bz2
return bz2.open(filename, 'rt', encoding='utf-8')
elif filetype == 'gzip':
import gzip
return gzip.open(filename, 'rt', encoding='utf-8')
elif filetype == 'lzma':
import lzma
return lzma.open(filename, 'rt', encoding='utf-8')
else:
return open(filename, 'rt', encoding='utf-8')
def getbodies(filename, filetype = None):
for line in openfile(filename):
line = line.strip()
if line[-1] == ',':
line = line[:-1]
if line[0] == '{' and line[-1] == '}':
body = json.loads(line)
yield body
def main():
filename = sys.argv[1]
eightmoonparents = {}
sys.stderr.write('Looking for "h" moons in EDSM data (pass 1)\n')
sys.stderr.flush()
n = 0
hcount = 0
for body in getbodies(filename):
sid = body['systemId']
sysname = body['systemName']
if body['name'][-2:] == ' h':
if sid not in eightmoonparents:
edtssys = edtslib.system.from_name(sysname, allow_known = False)
solsqdist = 0
if edtssys is not None:
edtspos = edtssys.position
solsqdist = edtspos.x ** 2 + edtspos.y ** 2 + edtspos.z ** 2 - edtssys.uncertainty ** 2
if solsqdist < maxsqdist:
eightmoonparents[sid] = set()
else:
eightmoonparents[sid] = None
if sid in eightmoonparents and eightmoonparents[sid] is not None:
eightmoonparents[sid].add(body['name'][:-2])
if 'parents' in body and body['parents'] is not None:
parentid = next((p['Planet'] for p in body['parents'] if 'Planet' in p), None)
if parentid is not None:
eightmoonparents[sid].add(parentid)
hcount += 1
n += 1
if (n % 4096) == 0:
sys.stderr.write('.')
if (n % 262144) == 0:
sys.stderr.write(' {0}/{1}\n'.format(hcount, n))
sys.stderr.flush()
sys.stderr.write(' {0}/{1}\nDone\n'.format(hcount, n))
sys.stderr.write('Looking for gas giants with "h" moons in EDSM data (pass 2)\n')
n = 0
ggcount = 0
for body in getbodies(filename):
sid = body['systemId']
sysname = body['systemName']
if sid in eightmoonparents and body['type'] == 'Planet':
sysemp = eightmoonparents[sid]
if sysemp is not None:
bodytype = body['subType']
bodyname = body['name']
bodyid = body['bodyId']
if (bodyname in sysemp or bodyid in sysemp) and bodytype is not None and 'gas giant' in bodytype.lower():
print(json.dumps(body))
ggcount += 1
n += 1
if (n % 4096) == 0:
sys.stderr.write('.')
if (n % 262144) == 0:
sys.stderr.write(' {0}/{1}\n'.format(ggcount, n))
sys.stderr.flush()
sys.stderr.write(' {0}/{1}\nDone\n'.format(ggcount, n))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment