-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJPLHorizons.py
58 lines (48 loc) · 1.69 KB
/
JPLHorizons.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
import requests
def get_earth_position_vector(date, velvec=False):
base_url = "https://ssd.jpl.nasa.gov/horizons_batch.cgi"
date2 = date[:10] + " 23:59"
print(date)
# Set up parameters for the API request
params = {
'batch': '1',
'COMMAND': "399",
'CENTER': "'@0'",
'COORD_TYPE': "'RECT'",
'SITE_COORD': "'0,0,0'",
'MAKE_EPHEM': "'YES'",
'TABLE_TYPE': "'VECTORS'",
'OUT_UNITS': "'KM-S'",
'REF_PLANE': "'ECLIPTIC'",
'VEC_LABELS': "'NO'",
'VEC_DELTA_T': "'YES'",
'TARGET': "'10'",
'START_TIME': f"'{date}'",
'STOP_TIME': f"'{date2}'",
'STEP_SIZE': "'1d'",
'CSV_FORMAT': "'YES'"
}
# Make the API request
response = requests.get(base_url, params=params)
# Check if the request was successful
if response.status_code == 200:
# Parse the response and extract the position vector
lines = response.text.split('\n')
# this was merely here to count the lines :P
#for idx_l, l in enumerate(lines):
# print(str(idx_l) + ":" + l)
data_line = lines[54]
values = data_line.split(',')
# Extract x, y, z coordinates from the response
x, y, z = map(float, values[3:6])
vx, vy, vz = map(float, values[6:9])
# Return the position vector (km)
if not velvec:
return [x, y, z]
else:
return [x, y, z], [vx, vy, vz]
# return {'x': x, 'y': y, 'z': z}
else:
print(f"Error: Unable to fetch data. Status code: {response.status_code}")
return None
# print(get_earth_position_vector("2001-01-01 00"))