The JPL Horizons system provides solar system ephemerides (positions, velocities, orbital elements, etc.), and you can query it directly in Python using the astroquery library, which has a dedicated JPLHorizons module.
astroquery¶
- Installation
pip install astroquery
- Query Horizons in Python
The following snippet demonstrates how to query Horizons.
Notes
- id can be the name ("Mars", "Ceres") or numeric ID (499, 2000433, etc.).
- location can be:
- "500@399" → Earth geocenter
- "500@10" → Sun
- "@0" → Solar System Barycenter
- Or even specific observatory codes.
- epochs can be:
- A dictionary with start, stop, and step
- A single Julian date or list of Julian dates
In [8]:
from astroquery.jplhorizons import Horizons
# Object ID for Mars = 499
obj = Horizons(
id='499', # Mars ID (can also use 'Mars')
location='500@399', # Location: 500@399 means "Earth geocenter"
epochs={'start': '2025-01-01', 'stop': '2025-01-10', 'step': '1d'}
)
# Ephemerides (RA, Dec, distance, etc.)
eph = obj.ephemerides()
print(eph)
targetname datetime_str datetime_jd ... alpha_true PABLon PABLat --- --- d ... deg deg deg ---------- ----------------- ----------- ... ---------- -------- ------ Mars (499) 2025-Jan-01 00:00 2460676.5 ... 12.8817 115.2137 2.7681 Mars (499) 2025-Jan-02 00:00 2460677.5 ... 12.103 115.2803 2.7876 Mars (499) 2025-Jan-03 00:00 2460678.5 ... 11.3183 115.342 2.8064 Mars (499) 2025-Jan-04 00:00 2460679.5 ... 10.5288 115.3991 2.8246 Mars (499) 2025-Jan-05 00:00 2460680.5 ... 9.7359 115.4518 2.8422 Mars (499) 2025-Jan-06 00:00 2460681.5 ... 8.9415 115.5007 2.8591 Mars (499) 2025-Jan-07 00:00 2460682.5 ... 8.1478 115.5459 2.8753 Mars (499) 2025-Jan-08 00:00 2460683.5 ... 7.3577 115.5879 2.8908 Mars (499) 2025-Jan-09 00:00 2460684.5 ... 6.5754 115.627 2.9056 Mars (499) 2025-Jan-10 00:00 2460685.5 ... 5.8066 115.6637 2.9197
STK File Generation¶
The following simple Python code can be employed to generate a STK .e file.
In [ ]:
from astroquery.jplhorizons import Horizons
# create Horizon object
id = 'Euclid' # 'Euclid', 'LRO', 'STEREO-A' # spacecraft id/name
location = '500@0' # Coordinate center or observer location
#epochs: time format 'YYYY-MM-DD [HH:MM:SS]', step format: n[y|d|m|s]
epochs = {'start': '2026-01-01', 'stop': '2026-02-01', 'step': '1m'}
space_craft = Horizons(id=id, location=location, epochs=epochs)
print(space_craft)
# Load Vectors
sta_vecs = space_craft.vectors(
refplane='earth', # Reference plane for all output. use 'earth' for equator and equinox reference epoch
aberrations='geometric')
# print(sta_vecs)
# change vector unit
pos_name = ['x', 'y', 'z', 'range']
for n in pos_name:
sta_vecs[n].convert_unit_to('km')
vel_name = ['vx', 'vy', 'vz', 'range_rate']
for n in vel_name:
sta_vecs[n].convert_unit_to('km/s')
print(sta_vecs)
# write to file
file_name = f'./stk_files/{id}_{epochs["start"]}_to_{epochs["stop"]}.e'
pvecs = sta_vecs.to_pandas()
print(pvecs)
pvecs = pvecs.drop(columns=['targetname', 'datetime_str', 'lighttime', 'range', 'range_rate'])
with open(file_name, 'w') as f:
# write stk .e file header
f.write(
"stk.v.12.2\nBEGIN Ephemeris\n" +
" InterpolationMethod Lagrange\n" +
" InterpolationOrder 5\n" +
" DistanceUnit Kilometers\n" +
" CentralBoday Earth\n" +
" CoordinateSystem ICRF\n" +
" TimeFormat JDate\n" +
" EphemerisTimePosVel\n")
# write trajectory x y z vz vy vz
pvecs.to_csv(f, header=False, sep=' ', index=False, lineterminator='\n', float_format='%17.15E')
# write stk .e file end
f.write("END Ephemeris\n")
JPLHorizons instance "Euclid"; location=500@0, epochs={'start': '2026-01-01', 'stop': '2026-02-01', 'step': '1m'}, id_type=None
targetname datetime_jd ... range_rate
--- d ... km / s
-------------------------- ----------------- ... -------------------
Euclid (spacecraft) (-680) 2461041.5 ... 0.12543125118897902
Euclid (spacecraft) (-680) 2461041.500694444 ... 0.12543580735418441
Euclid (spacecraft) (-680) 2461041.501388889 ... 0.12544036347364362
Euclid (spacecraft) (-680) 2461041.502083333 ... 0.12544491954739148
Euclid (spacecraft) (-680) 2461041.502777778 ... 0.12544947557536493
Euclid (spacecraft) (-680) 2461041.503472222 ... 0.12545403155761528
Euclid (spacecraft) (-680) 2461041.504166667 ... 0.12545858749406943
Euclid (spacecraft) (-680) 2461041.504861111 ... 0.12546314338485318
Euclid (spacecraft) (-680) 2461041.505555556 ... 0.12546769922986406
... ... ... ...
Euclid (spacecraft) (-680) 2461072.49375 ... 0.31416519504300416
Euclid (spacecraft) (-680) 2461072.494444444 ... 0.31416958947121465
Euclid (spacecraft) (-680) 2461072.495138889 ... 0.31417398392618673
Euclid (spacecraft) (-680) 2461072.495833333 ... 0.3141783784079299
Euclid (spacecraft) (-680) 2461072.496527778 ... 0.3141827729163825
Euclid (spacecraft) (-680) 2461072.497222222 ... 0.3141871674516792
Euclid (spacecraft) (-680) 2461072.497916667 ... 0.3141915620137396
Euclid (spacecraft) (-680) 2461072.498611111 ... 0.31419595660257077
Euclid (spacecraft) (-680) 2461072.499305556 ... 0.31420035121816664
Euclid (spacecraft) (-680) 2461072.5 ... 0.3142047458605888
Length = 44641 rows
targetname datetime_jd \
0 Euclid (spacecraft) (-680) 2.461042e+06
1 Euclid (spacecraft) (-680) 2.461042e+06
2 Euclid (spacecraft) (-680) 2.461042e+06
3 Euclid (spacecraft) (-680) 2.461042e+06
4 Euclid (spacecraft) (-680) 2.461042e+06
... ... ...
44636 Euclid (spacecraft) (-680) 2.461072e+06
44637 Euclid (spacecraft) (-680) 2.461072e+06
44638 Euclid (spacecraft) (-680) 2.461072e+06
44639 Euclid (spacecraft) (-680) 2.461072e+06
44640 Euclid (spacecraft) (-680) 2.461072e+06
datetime_str x y \
0 A.D. 2026-Jan-01 00:00:00.0000 -2.697655e+07 1.333629e+08
1 A.D. 2026-Jan-01 00:01:00.0000 -2.697834e+07 1.333626e+08
2 A.D. 2026-Jan-01 00:02:00.0000 -2.698013e+07 1.333623e+08
3 A.D. 2026-Jan-01 00:03:00.0000 -2.698192e+07 1.333620e+08
4 A.D. 2026-Jan-01 00:04:00.0000 -2.698371e+07 1.333617e+08
... ... ... ...
44636 A.D. 2026-Jan-31 23:56:00.0000 -9.919541e+07 1.014018e+08
44637 A.D. 2026-Jan-31 23:57:00.0000 -9.919678e+07 1.014007e+08
44638 A.D. 2026-Jan-31 23:58:00.0000 -9.919814e+07 1.013996e+08
44639 A.D. 2026-Jan-31 23:59:00.0000 -9.919951e+07 1.013985e+08
44640 A.D. 2026-Feb-01 00:00:00.0000 -9.920088e+07 1.013974e+08
z vx vy vz lighttime range \
0 5.827147e+07 -29.831410 -4.965222 -2.128077 0.005714 1.480167e+08
1 5.827134e+07 -29.831344 -4.965553 -2.128224 0.005714 1.480168e+08
2 5.827121e+07 -29.831277 -4.965884 -2.128371 0.005714 1.480168e+08
3 5.827109e+07 -29.831211 -4.966215 -2.128518 0.005714 1.480168e+08
4 5.827096e+07 -29.831144 -4.966546 -2.128664 0.005714 1.480168e+08
... ... ... ... ... ... ...
44636 4.429223e+07 -22.793509 -18.319788 -8.052453 0.005737 1.486064e+08
44637 4.429175e+07 -22.793268 -18.320041 -8.052565 0.005737 1.486064e+08
44638 4.429126e+07 -22.793027 -18.320294 -8.052677 0.005737 1.486064e+08
44639 4.429078e+07 -22.792785 -18.320547 -8.052789 0.005737 1.486065e+08
44640 4.429030e+07 -22.792544 -18.320800 -8.052902 0.005737 1.486065e+08
range_rate
0 0.125431
1 0.125436
2 0.125440
3 0.125445
4 0.125449
... ...
44636 0.314187
44637 0.314192
44638 0.314196
44639 0.314200
44640 0.314205
[44641 rows x 12 columns]
Comments
comments powered by Disqus