Skip to content

Commit d5bbca2

Browse files
KatKikerKathleen Kiker
authored andcommitted
B612 neodys tool (#1)
* Adding NEODyS query tool files * Adding init files * Fixing imports * Adding unit tests * Fixing error in tests * Adding comments & documentation * Fixing style issues * Updating Variable Names * Fixes to style and variable names * Adding docs * Adding units tests * Fixing minor issues * Removing NOR * Updating tests Co-authored-by: Kathleen Kiker <[email protected]>
1 parent af87c33 commit d5bbca2

File tree

7 files changed

+319
-0
lines changed

7 files changed

+319
-0
lines changed

astroquery/neodys/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
NEODyS Query Tool
3+
---------------
4+
5+
:Author: B612 Foundation
6+
7+
This package is for querying NEODys website
8+
9+
"""
10+
11+
from astropy import config as _config
12+
13+
14+
class Conf(_config.ConfigNamespace):
15+
"""
16+
Configuration parameters for `astroquery.neodys`.
17+
"""
18+
server = _config.ConfigItem(
19+
['https://newton.spacedys.com/~neodys2/epoch/'],
20+
'Base name of the NEODyS server to use.')
21+
timeout = _config.ConfigItem(
22+
60,
23+
'Time limit for connecting to NEODyS server.')
24+
25+
26+
conf = Conf()
27+
28+
from .core import NEODyS, NEODySClass
29+
30+
__all__ = ['NEODyS', 'NEODySClass',
31+
'Conf', 'conf',
32+
]

astroquery/neodys/core.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import requests
2+
from . import conf
3+
from ..query import BaseQuery
4+
5+
6+
__all__ = ['NEODyS', 'NEODySClass']
7+
8+
9+
class NEODySClass(BaseQuery):
10+
11+
NEODYS_URL = conf.server
12+
TIMEOUT = conf.timeout
13+
14+
def __init__(self):
15+
super(NEODySClass, self).__init__()
16+
17+
def query_object(self, object_id, orbital_element_type="eq", epoch_near_present=0):
18+
"""
19+
Parameters
20+
----------
21+
object_id : str
22+
Name of objects to be searched in NEODyS website
23+
24+
orbital_element_type : str
25+
Sets coordinate system for results.
26+
eq - Equinoctial
27+
ke - Keplerian
28+
Defaults to Equinoctial which is given in higher
29+
precision.
30+
31+
epoch_near_present : bool
32+
Sets the epoch to near present day. Otherwise
33+
defaults to near middle of the arc.
34+
35+
Returns
36+
-------
37+
38+
Returns a dictionary with the following entries:
39+
40+
KEP : float array
41+
The state vector in Keplerian elements.
42+
Units in au and deg.
43+
44+
EQU : float array
45+
The state vector in Equinoctial elements.
46+
47+
COV : float array
48+
The covariance matrix.
49+
50+
COR : float array
51+
The correlation matrix for Keplerian.
52+
53+
MJD : str array
54+
The Mean Julian date, and the time scale
55+
used.
56+
57+
MAG : float array
58+
The absolute magnitude (H) and slope
59+
parameter (G)
60+
61+
Raises
62+
------
63+
ValueError
64+
If input parameters are invalid.
65+
66+
RuntimeError
67+
If connection to website fails.
68+
69+
"""
70+
71+
COV = []
72+
COR = []
73+
NOR = []
74+
75+
if orbital_element_type != 'ke' and orbital_element_type != 'eq':
76+
raise ValueError("OrbitElementType must be ke or eq")
77+
78+
object_url = f'{self.NEODYS_URL}/{object_id}.{orbital_element_type}{epoch_near_present}'
79+
80+
response = requests.get(object_url, timeout=self.TIMEOUT)
81+
82+
if response.status_code != 200:
83+
print(object_url)
84+
raise RuntimeError("Request failed with error {0}: {1}".format(response.status_code, response.text))
85+
86+
ascii_text = (response.text).split('\n')
87+
88+
results = {}
89+
90+
for line in ascii_text:
91+
if 'KEP' in line:
92+
results["Keplerian State Vector"] = [float(x) for x in line.split()[1:]]
93+
if 'EQU' in line:
94+
results["Equinoctial State Vector"] = [float(x) for x in line.split()[1:]]
95+
if 'MJD' in line:
96+
results["Mean Julian Date"] = line.split()[1:]
97+
if 'MAG' in line:
98+
results["Magnitude"] = [float(x) for x in line.split()[1:]]
99+
if 'COV' in line:
100+
COV.extend([float(x) for x in line.split()[1:]])
101+
if 'COR' in line:
102+
COR.extend([float(x) for x in line.split()[1:]])
103+
results["Covariance Matrix"] = COV
104+
results["Keplerian Correlation Matrix"] = COR
105+
results["URL"] = object_url
106+
107+
return results
108+
109+
110+
NEODyS = NEODySClass()

astroquery/neodys/tests/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
format = 'OEF2.0' ! file format
2+
rectype = 'ML' ! record type (1L/ML)
3+
refsys = ECLM J2000 ! default reference system
4+
END_OF_HEADER
5+
2018VP1
6+
! Equinoctial elements: a, e*sin(LP), e*cos(LP), tan(i/2)*sin(LN), tan(i/2)*cos(LN), mean long.
7+
EQU 1.5881497559207589E+00 -0.037761493287417 0.428349632624671 0.018136658554010 0.021742631955843 14.9386830433169
8+
MJD 58430.299591399 TDT
9+
MAG 30.865 0.150
10+
! Non-grav parameters: model used, actual number in use, dimension
11+
LSP 0 0 6
12+
! RMS 5.66778E-04 3.33640E-05 2.25727E-04 7.59772E-06 9.13987E-06 1.37733E-02
13+
! EIG 8.38066E-10 1.69716E-09 1.28167E-08 5.23083E-08 6.36449E-07 6.56682E-04
14+
! WEA 0.86309 -0.05080 0.34374 0.01157 0.01392 -0.36607
15+
COV 3.212371896218492E-07 -1.890888042046556E-08 1.279370251095811E-07
16+
COV 4.306149504229515E-09 5.180213121398058E-09 -7.806384186128134E-06
17+
COV 1.113155399718790E-09 -7.530754184805629E-09 -2.534799484940481E-10
18+
COV -3.049291243327506E-10 4.595165457614499E-07 5.095265031366585E-08
19+
COV 1.714984526300696E-09 2.063091894983725E-09 -3.109000937048165E-06
20+
COV 5.772527813193308E-11 6.944207925149469E-11 -1.046445200258212E-07
21+
COV 8.353717813304041E-11 -1.258850849123837E-07 1.897040272478581E-04
22+
NOR 2.792906501841200E+15 1.091214279085640E+16 -1.547061570068868E+16
23+
NOR 1.761621586690132E+16 -1.570584941749150E+16 -1.657511435714105E+14
24+
NOR 6.985168079071240E+17 -4.787110160483813E+17 -3.804184995389532E+16
25+
NOR 3.494747873155191E+16 -9.086207494297492E+15 3.524435742182211E+17
26+
NOR -2.874629116079826E+16 2.496178291549370E+16 6.299748386561305E+15
27+
NOR 1.852710904000144E+17 -1.661603209855957E+17 3.378845532988509E+14
28+
NOR 1.496956716339183E+17 -3.141838806716318E+14 1.184111705066130E+14
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
import os
3+
4+
from ...utils.testing_tools import MockResponse
5+
6+
from ... import neodys
7+
8+
DATA_FILE = '2018vp1_eq0.txt'
9+
10+
11+
def data_path(filename):
12+
data_dir = os.path.join(os.path.dirname(__file__), 'data')
13+
return os.path.join(data_dir, filename)
14+
15+
16+
# monkeypatch replacement request function
17+
def nonremote_request(self, request_type, url, **kwargs):
18+
with open(data_path(DATA_FILE), 'rb') as f:
19+
response = MockResponse(content=f.read(), url=url)
20+
return response
21+
22+
23+
# use a pytest fixture to create a dummy 'requests.get' function,
24+
# that mocks(monkeypatches) the actual 'requests.get' function:
25+
@pytest.fixture
26+
def patch_request(request):
27+
try:
28+
mp = request.getfixturevalue("monkeypatch")
29+
except AttributeError:
30+
mp = request.getfuncargvalue("monkeypatch")
31+
mp.setattr(neodys.core.NEODySClass, '_request',
32+
nonremote_request)
33+
return mp
34+
35+
36+
def test_neodys_query():
37+
res = neodys.core.NEODyS.query_object(
38+
"2018VP1", orbital_element_type='eq', epoch_near_present=0)
39+
assert res['Equinoctial State Vector'] == [1.5881497559198392,
40+
-0.037761493287362, 0.428349632624304, 0.018136658553998,
41+
0.021742631955829, 14.9386830433394]
42+
assert res['Mean Julian Date'] == ['58430.299591399', 'TDT']
43+
assert res['Magnitude'] == [30.865, 0.15]
44+
assert res['Covariance Matrix'] == [3.212371896244936e-07,
45+
-1.890888042008199e-08, 1.279370251108077e-07, 4.306149504243656e-09,
46+
5.180213121424896e-09, -7.806384186165599e-06, 1.113155399664521e-09,
47+
-7.5307541846631e-09, -2.534799484876558e-10, -3.049291243256377e-10,
48+
4.595165457505564e-07, 5.095265031422349e-08, 1.714984526308656e-09,
49+
2.063091894997213e-09, -3.109000937067305e-06, 5.772527813183736e-11,
50+
6.944207925151111e-11, -1.04644520025806e-07, 8.353717813321847e-11,
51+
-1.258850849126041e-07, 0.0001897040272481179]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
from ... import neodys
3+
4+
5+
def test_neodys_query():
6+
7+
test_object = "2018VP1"
8+
9+
res_kep_0 = neodys.core.NEODyS.query_object(
10+
test_object, orbital_element_type="ke", epoch_near_present=0)
11+
res_kep_1 = neodys.core.NEODyS.query_object(
12+
test_object, orbital_element_type="ke", epoch_near_present=1)
13+
res_eq_0 = neodys.core.NEODyS.query_object(
14+
test_object, orbital_element_type="eq", epoch_near_present=0)
15+
res_eq_1 = neodys.core.NEODyS.query_object(
16+
test_object, orbital_element_type="eq", epoch_near_present=1)
17+
assert len(res_kep_0['Keplerian State Vector']) == 6
18+
assert len(res_kep_0['Covariance Matrix']) == 21
19+
assert res_kep_0['Mean Julian Date'][0] != res_kep_1['Mean Julian Date'][0]
20+
assert len(res_eq_0['Equinoctial State Vector']) == 6
21+
assert len(res_eq_0['Covariance Matrix']) == 21
22+
assert len(res_eq_0['Keplerian Correlation Matrix']) == 0
23+
assert res_eq_0['Mean Julian Date'][0] != res_eq_1['Mean Julian Date'][0]

docs/neodys/neodys.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. doctest-skip-all
2+
3+
.. _astroquery.neodys:
4+
5+
************************************
6+
NEODyS Queries (`astroquery.neodys`)
7+
************************************
8+
9+
Getting started
10+
===============
11+
12+
This module supports retrieving the orbital information of an object from the
13+
NEODyS website (https://newton.spacedys.com/neodys/). The state vector,
14+
covariance matrix, epoch, absolute magnitude, slope parameter, and correlation
15+
matrix are given in dictionary form. The results can be given in either
16+
Keplerian or Equinoctial. The only required argument is the identifier of the
17+
object to be retrieved. Here is a basic example:
18+
19+
.. code-block:: python
20+
21+
>>> from astroquery.neodys import NEODyS
22+
>>> res = neodys.core.NEODyS.query_object("1982YA")
23+
>>> print(res)
24+
{'Equinoctial State Vector': [3.643505823124891, 0.555753608185406,
25+
0.413673328231395, -0.316412712941692, -0.005264652421941, 48.553922087068],
26+
'Mean Julian Date': ['55417.557065659', 'TDT'], 'Magnitude': [17.798, 0.15],
27+
'Covariance Matrix': [6.737866678261391e-15, 3.835925316932412e-14,
28+
-6.640239789108145e-14, -1.439007899529439e-15, -4.938922190348063e-15,
29+
4.994817272482335e-12, 2.656750560890953e-13, -4.11492595235993e-13,
30+
5.390531237956025e-14, -8.210458555809306e-14, 3.034284768527233e-11,
31+
6.987414264633028e-13, -3.461847188742589e-14, 8.072873747426578e-14,
32+
-5.283334219581364e-11, 3.441265766874457e-13, -1.712409537284e-13,
33+
6.293229007026724e-13, 1.418015029378066e-13, -3.898114012957574e-12,
34+
4.0664813765478e-09], 'Keplerian Correlation Matrix': [], 'URL':
35+
'https://newton.spacedys.com/~neodys2/epoch//1982YA.eq0'}
36+
37+
More detailed parameters
38+
------------------------
39+
40+
There are two other optional parameters that can be specified. These
41+
are ``orbital_element_type`` which specifies whether the results will
42+
be in Keplerian or Equinoctial. Valid values are 'ke' for Keplerian
43+
and 'eq' for Equinoctial. The other is ``epoch_near_present``, a flag
44+
which sets the epoc to near present instead of near middle of the arc.
45+
Valid values are '0' for near middle of the arc, and '1' for near
46+
present. The default values are 'eq' and '0'. Note: Keplerian elements
47+
are given to a lower precision on the NEODyS website.
48+
49+
Here's an example with these optional parameters:
50+
51+
.. code-block:: python
52+
53+
>>> from astroquery.neodys import NEODyS
54+
>>> res = neodys.core.NEODyS.query_object(
55+
"1982YA", orbital_element_type="ke", epoch_near_present=1)
56+
>>> print(results)
57+
{'Keplerian State Vector': [3.648336, 0.691188, 35.123, 268.963, 144.376,
58+
170.251], 'Mean Julian Date': ['59200.0000', 'TDT'], 'Magnitude': [17.798,
59+
0.15], 'Covariance Matrix': [7.39777353e-15, -9.00996211e-15, -3.12399412e-13,
60+
-1.41885172e-12, 8.13796963e-12, -2.98795622e-12, 2.61069828e-14,
61+
-2.30621969e-12, -3.23293856e-12, -4.26709227e-12, 2.1454954e-12,
62+
3.66367782e-09, 3.29118054e-09, -3.80504189e-09, 4.73514563e-10,
63+
4.83809216e-09, -6.57723081e-09, 1.26901103e-09, 1.47852822e-08,
64+
-4.09276587e-09, 1.39372059e-09], 'Keplerian Correlation Matrix': [1.0,
65+
-0.648327, -0.06000688, -0.23716436, 0.77812746, -0.93054173, 1.0,
66+
-0.2358108, -0.2876617, -0.21718953, 0.35568159, 1.0, 0.7817286, -0.51699459,
67+
0.2095496, 1.0, -0.77766258, 0.4886979, 1.0, -0.90160069, 1.0], 'URL':
68+
'https://newton.spacedys.com/~neodys2/epoch//1982YA.ke1'}
69+
70+
71+
Reference/API
72+
=============
73+
74+
.. automodapi:: astroquery.neodys
75+
:no-inheritance-diagram:

0 commit comments

Comments
 (0)