-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyt_DB03_add_update_orbelements.py
271 lines (235 loc) · 16 KB
/
pyt_DB03_add_update_orbelements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import re
import sys
import time
import datetime
import math
import numpy as np
import urllib.request
import glob, os, bz2, subprocess
import os.path
from numpy import linspace
from decimal import *
import sqlite3
from sqlite3 import Error
import smtplib, ssl
import macadamia_functions as mcd
##### FUNCTION DEFINITIONS -- ORBITAL ELEMENT FILE PROCESSING #####
def download_jpl_orbelem_files(numbr_url,unnum_url,comet_url,numbr_filepath,unnum_filepath,comet_filepath,path_logfile,path_errorfile):
# Download orbital element files
mcd.output_log_entry(path_logfile,'Downloading orbital element files...')
urllib.request.urlretrieve(numbr_url,numbr_filepath)
urllib.request.urlretrieve(unnum_url,unnum_filepath)
urllib.request.urlretrieve(comet_url,comet_filepath)
mcd.output_log_entry(path_logfile,'Downloads complete')
return None
def ingest_numbr_data(numbr_filepath,sqlite_file,path_logfile,path_errorfile):
# Ingesting orbital element data for numbered asteroids
mcd.output_log_entry(path_logfile,'Ingesting orbital element data for numbered asteroids from {:s}...'.format(numbr_filepath))
# Connect to database file
conn = mcd.create_connection(sqlite_file)
cursor = conn.cursor()
with open(numbr_filepath) as input_file:
for _ in range(2): #skip first two header lines
next(input_file)
for line in input_file:
asteroid_number = int(line[0:6].lstrip())
asteroid_name = mcd.add_apostrophe_escape(line[7:24].rstrip())
epoch = int(line[25:30])
orbelems = line[31:].split()
semimaj = float(orbelems[0])
eccen = float(orbelems[1])
inclin = float(orbelems[2])
argperi = float(orbelems[3])
longascnode = float(orbelems[4])
meananom = float(orbelems[5])
hmag = float(orbelems[6])
gparam = float(orbelems[7])
mcd.output_log_entry(path_logfile,'Adding/updating data for asteroid ({:d}) {:s}...'.format(asteroid_number,asteroid_name))
# Check if unnumbered asteroid entry exists with same provisional designation,
# and if so, add numerical designation
query = "SELECT ssobject_id,desig_number,desig_provisional FROM ssobjects WHERE desig_provisional='{:>10s}' and object_type='asteroid'".format(asteroid_name)
cursor.execute(query)
mcd.output_log_entry(path_logfile,query)
row = cursor.fetchone()
if row != None and row[1] == None:
query = "UPDATE ssobjects SET desig_number='{:>7d}' WHERE ssobject_id={:d}".format(asteroid_number,row[0])
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Insert new asteroid entries from newly downloaded JPL catalog
if asteroid_name[0].isalpha():
query = "INSERT OR IGNORE INTO ssobjects(object_type,desig_number,desig_name,h_mag,g_param,epoch_mjd,semimaj_axis,eccentricity,inclination,arg_perihelion,long_ascnode,mean_anomaly,date_added) VALUES ('asteroid','{:>7d}','{:>17s}',{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},'{:s}')".format(asteroid_number,
asteroid_name,hmag,gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
else:
query = "INSERT OR IGNORE INTO ssobjects(object_type,desig_number,desig_provisional,h_mag,g_param,epoch_mjd,semimaj_axis,eccentricity,inclination,arg_perihelion,long_ascnode,mean_anomaly,date_added) VALUES ('asteroid','{:>7d}','{:>10s}',{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},'{:s}')".format(asteroid_number,
asteroid_name,hmag,gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Update existing asteroid entries from newly downloaded JPL catalog
if asteroid_name[0].isalpha():
query = "UPDATE ssobjects SET object_type='asteroid',desig_name='{:>17s}',h_mag={:f},g_param={:f},epoch_mjd={:f},semimaj_axis={:f},eccentricity={:f},inclination={:f},arg_perihelion={:f},long_ascnode={:f},mean_anomaly={:f},date_updated='{:s}' WHERE desig_number='{:>7d}'".format(asteroid_name,
hmag,gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),asteroid_number)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
else:
query = "UPDATE ssobjects SET object_type='asteroid',desig_provisional='{:>10s}',h_mag={:f},g_param={:f},epoch_mjd={:f},semimaj_axis={:f},eccentricity={:f},inclination={:f},arg_perihelion={:f},long_ascnode={:f},mean_anomaly={:f},date_updated='{:s}' WHERE desig_number='{:>7d}'".format(asteroid_name,
hmag,gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),asteroid_number)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
mcd.output_log_entry(path_logfile,'Insertion of osculating orbital elements for numbered asteroids from {:s} complete'.format(numbr_filepath))
return None
def ingest_unnum_data(unnum_filepath,sqlite_file,path_logfile,path_errorfile):
# Ingesting orbital element data for unnumbered asteroids
mcd.output_log_entry(path_logfile,'Ingesting orbital element data for unnumbered asteroids from {:s}...'.format(unnum_filepath))
# Connect to database file
conn = mcd.create_connection(sqlite_file)
cursor = conn.cursor()
with open(unnum_filepath) as input_file:
for _ in range(2): #skip first two header lines
next(input_file)
for line in input_file:
asteroid_desig = mcd.add_apostrophe_escape(line[0:10].rstrip())
epoch = int(line[12:17])
orbelems = line[18:].split()
semimaj = float(orbelems[0])
eccen = float(orbelems[1])
inclin = float(orbelems[2])
argperi = float(orbelems[3])
longascnode = float(orbelems[4])
meananom = float(orbelems[5])
hmag = float(orbelems[6])
gparam = float(orbelems[7])
mcd.output_log_entry(path_logfile,'Adding/updating data for asteroid {:s}...'.format(asteroid_desig))
# Check if asteroid entry exists with same provisional designation
query = "SELECT ssobject_id FROM ssobjects WHERE desig_provisional='{:>10s}' and object_type='asteroid'".format(asteroid_desig)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
row = cursor.fetchone()
if row == None: # If no asteroid entry with same provisional designation exists, add new entry
query = "INSERT OR IGNORE INTO ssobjects(object_type,desig_provisional,h_mag,g_param,epoch_mjd,semimaj_axis,eccentricity,inclination,arg_perihelion,long_ascnode,mean_anomaly,date_added) VALUES ('asteroid','{:>10s}',{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},{:f},'{:s}')".format(asteroid_desig,
hmag,gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
else: # If asteroid entry with same provisional designation exists, update entry
query = "UPDATE ssobjects SET object_type='asteroid',h_mag={:f},g_param={:f},epoch_mjd={:f},semimaj_axis={:f},eccentricity={:f},inclination={:f},arg_perihelion={:f},long_ascnode={:f},mean_anomaly={:f},date_updated='{:s}' WHERE desig_provisional='{:>10s}'".format(hmag,
gparam,epoch,semimaj,eccen,inclin,argperi,longascnode,meananom,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'),asteroid_desig)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
mcd.output_log_entry(path_logfile,'Insertion of osculating orbital elements for unnumbered asteroids from {:s} complete.'.format(unnum_filepath))
return None
def ingest_comet_data(comet_filepath,sqlite_file,path_logfile,path_errorfile):
# Ingesting orbital element data for comets
mcd.output_log_entry(path_logfile,'Ingesting orbital element data for comets from {:s}...'.format(comet_filepath))
# Connect to database file
conn = mcd.create_connection(sqlite_file)
cursor = conn.cursor()
with open(comet_filepath) as input_file:
for _ in range(2): #skip first two header lines
next(input_file)
for line in input_file:
if line[0:3].lstrip() != '':
comet_number = line[0:4]
comet_desig = mcd.add_apostrophe_escape(line[5:43].rstrip())
# Add letter designations for numbered comets with multiple components
if comet_desig[-3] == '-':
comet_number = comet_number + comet_desig[-3:]
elif comet_desig[-2] == '-':
comet_number = comet_number + comet_desig[-2:]
else:
comet_number = None
comet_desig = mcd.add_apostrophe_escape(line[3:43].rstrip())
epoch = int(line[44:51])
orbelems = line[52:].split()
peridist = float(orbelems[0])
eccen = float(orbelems[1])
inclin = float(orbelems[2])
argperi = float(orbelems[3])
longascnode = float(orbelems[4])
time_peri = float(orbelems[5])
if comet_number != None: # If input comet is numbered, insert or update numbered comet record
# Insert new comet entries from newly downloaded JPL catalog
mcd.output_log_entry(path_logfile,'Adding/updating data for comet {:s}/{:s}...'.format(comet_number,comet_desig))
query = "INSERT OR IGNORE INTO ssobjects(object_type,desig_number,desig_name,epoch_mjd,perihelion_dist,eccentricity,inclination,arg_perihelion,long_ascnode,t_perihelion,date_added) VALUES ('comet','{:s}','{:<40s}',{:f},{:f},{:f},{:f},{:f},{:f},{:f},'{:s}')".format(comet_number,
comet_desig,epoch,peridist,eccen,inclin,argperi,longascnode,time_peri,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Update existing comet entries from newly downloaded JPL catalog
query = "UPDATE ssobjects SET object_type='comet',desig_name='{:<40s}',epoch_mjd={:f},perihelion_dist={:f},eccentricity={:f},inclination={:f},arg_perihelion={:f},long_ascnode={:f},t_perihelion={:f},date_updated='{:s}' WHERE desig_number='{:s}'".format(comet_desig,
epoch,peridist,eccen,inclin,argperi,longascnode,time_peri,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'),comet_number)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
else: # If input comet is not numbered, insert or update provisional comet record
# Check if comet entry exists with same provisional designation
mcd.output_log_entry(path_logfile,'Adding/updating data for comet {:s}...'.format(comet_desig))
query = "SELECT ssobject_id FROM ssobjects WHERE desig_provisional='{:<40s}' and object_type='comet'".format(comet_desig)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
row = cursor.fetchone()
if row == None:
query = "INSERT OR IGNORE INTO ssobjects(object_type,desig_provisional,epoch_mjd,perihelion_dist,eccentricity,inclination,arg_perihelion,long_ascnode,t_perihelion,date_added) VALUES ('comet','{:<40s}',{:f},{:f},{:f},{:f},{:f},{:f},{:f},'{:s}')".format(comet_desig,
epoch,peridist,eccen,inclin,argperi,longascnode,time_peri,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
else:
query = "UPDATE ssobjects SET object_type='comet',epoch_mjd={:f},perihelion_dist={:f},eccentricity={:f},inclination={:f},arg_perihelion={:f},long_ascnode={:f},t_perihelion={:f},date_updated='{:s}' WHERE desig_provisional='{:<40s}'".format(epoch,
peridist,eccen,inclin,argperi,longascnode,time_peri,datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'),comet_desig)
mcd.output_log_entry(path_logfile,query)
cursor.execute(query)
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
mcd.output_log_entry(path_logfile,'Insertion of osculating orbital elements for comets from {:s} complete.'.format(comet_filepath))
return None
def main():
# Define filenames and paths
if len(sys.argv)!=3:
print('Usage:\n python3 pyt_add_update_orbelements.py [base_path] [sqlite_file]\n')
print(" (Trailing '/' needed in path specification)\n")
exit()
base_path = sys.argv[1]
sqlite_file = sys.argv[2]
# Validate input parameters
base_path,keep_going = mcd.validate_input_params(base_path,sqlite_file)
if keep_going:
mcd.send_status_email('DB03_add_update_orbelements execution started','DB03_add_update_orbelements execution started.')
current_date = datetime.datetime.today().strftime('%Y%m%d')
dir_downloads = base_path + 'orbital_elements/'
# Create and initialize log file
path_logfile,path_errorfile = mcd.initialize_log_error_file(base_path,'DB03_add_update_orbelements')
if not os.path.isdir(dir_downloads): os.mkdir(dir_downloads)
numbr_url = 'https://ssd.jpl.nasa.gov/dat/ELEMENTS.NUMBR'
unnum_url = 'https://ssd.jpl.nasa.gov/dat/ELEMENTS.UNNUM'
comet_url = 'https://ssd.jpl.nasa.gov/dat/ELEMENTS.COMET'
numbr_filename = 'ELEMENTS_NUMBR_{:s}.dat'.format(current_date)
unnum_filename = 'ELEMENTS_UNNUM_{:s}.dat'.format(current_date)
comet_filename = 'ELEMENTS_COMET_{:s}.dat'.format(current_date)
numbr_filepath = dir_downloads + numbr_filename
unnum_filepath = dir_downloads + unnum_filename
comet_filepath = dir_downloads + comet_filename
# Connect to database file
conn = mcd.create_connection(sqlite_file)
cursor = conn.cursor()
download_jpl_orbelem_files(numbr_url,unnum_url,comet_url,numbr_filepath,unnum_filepath,comet_filepath,path_logfile,path_errorfile)
##### For testing only #####
#numbr_filename = 'ELEMENTS_NUMBR_testing.dat'.format(current_date)
#numbr_filepath = dir_downloads + numbr_filename
ingest_numbr_data(numbr_filepath,sqlite_file,path_logfile,path_errorfile)
#ingest_unnum_data(unnum_filepath,sqlite_file,path_logfile,path_errorfile)
#ingest_comet_data(comet_filepath,sqlite_file,path_logfile,path_errorfile)
# Committing changes and closing the connection to the database file
conn.commit()
conn.close()
mcd.output_log_entry(path_logfile,'Database closed.')
mcd.send_status_email('DB03_add_update_orbelements execution complete','DB03_add_update_orbelements execution started.')
mcd.remove_error_log_if_empty(path_logfile,path_errorfile)
mcd.output_log_entry(path_logfile,'Done.')
return None
if __name__ == '__main__':
main()