Skip to content

Instantly share code, notes, and snippets.

@briantjacobs
Last active October 1, 2017 23:57
Show Gist options
  • Save briantjacobs/86eead31f054f4ce033106f16a8b05aa to your computer and use it in GitHub Desktop.
Save briantjacobs/86eead31f054f4ce033106f16a8b05aa to your computer and use it in GitHub Desktop.
Scripts to process HORIZONS ephemeris data
import re
import os
import csv
import json
import sys
import StringIO
import dateutil.parser
import datetime
def main(inputFile):
base = os.path.basename(inputFile)
name = os.path.splitext(base)[0]
file = open(inputFile, "rU")
data = file.read()
starSplit = data.split("*******************************************************************************")
csvHeader = starSplit[5].replace(" ", "").replace("\n", "")
csvStart = data.split("$$SOE")[1]
csvBody = csvStart.split("$$EOE")[0]
csvAll = StringIO.StringIO(csvHeader + "\r" + csvBody)
csvReader = csv.DictReader(csvAll,delimiter=',')
csvRows = list(csvReader)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
# strip the space
for row in csvRows:
#strip all values
for key, value in row.iteritems():
row[key] = value.strip()
# change the date key
row["date"] = row.pop("CalendarDate(TDB)")
row["date"] = row["date"].replace("A.D. ", "").replace(".0000", "");
# e.g.: 1997-Oct-15 12:00:00.0000 to 1997-10-15 12:00:00.0000
for i, d in enumerate(months):
idx = "0" + str(i+1) if len(str(i+1)) == 1 else str(i+1)
row["date"] = row["date"].replace(d, idx)
# reduce the precision a little
row["X"] = float(format(float(row["X"]), '.10f'))
row["Y"] = float(format(float(row["Y"]), '.10f'))
row["Z"] = float(format(float(row["Z"]), '.10f'))
del row["JDTDB"]
del row[""]
## write to file if this is called independently on the commandline
if __name__ == "__main__":
jsonfile = open('src/ngm-assets/data/'+name+'.json', 'w')
jsonfile.write(json.dumps(csvRows,indent=4))
jsonfile.close()
else:
return csvRows
file.close()
## is this running from commandline?
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment