Skip to content

Commit 00c3a44

Browse files
committed
Move dastcom to sbpy.data.utils
1 parent f3bd5a8 commit 00c3a44

File tree

7 files changed

+45
-1256
lines changed

7 files changed

+45
-1256
lines changed

sbpy/data/orbit.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from .. import bib
1919
from . import conf, DataClass
20-
from .. import utils
20+
from . import utils
2121

2222
__all__ = ['Orbit']
2323

@@ -135,14 +135,14 @@ def from_horizons(cls, targetids, id_type='smallbody',
135135
return cls.from_table(all_elem)
136136

137137
@classmethod
138-
def from_dastcom5(cls, name):
138+
def from_dastcom5(cls, identifier, is_record_number=False):
139139
"""Load orbital elements from the DASTCOM5 Database
140140
(ftp://ssd.jpl.nasa.gov/pub/ssd/dastcom5.zip).
141141
142142
Parameters
143143
----------
144-
name: str, mandatory
145-
Name of NEO
144+
identifier: mandatory
145+
Names, numbers, or designations of objects to be queried
146146
147147
Returns
148148
-------
@@ -158,7 +158,10 @@ def from_dastcom5(cls, name):
158158
if not os.path.isdir(dastcom5_dir):
159159
utils.dastcom5.download_dastcom5()
160160

161-
tb = utils.dastcom5.orbit_from_name(name=name)
161+
if is_record_number:
162+
tb = utils.dastcom5.orbit_from_record(record=identifier)
163+
else:
164+
tb = utils.dastcom5.orbit_from_name(name=identifier)
162165
return cls.from_table(tb)
163166

164167
@classmethod

sbpy/data/utils/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
3+
# This sub-module is destined for common non-package specific utility
4+
# functions.
5+
6+
from sbpy.data.utils import dastcom5

sbpy/utils/dastcom5.py renamed to sbpy/data/utils/dastcom5.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import shutil
34
import urllib.request
45
import zipfile
56

@@ -509,7 +510,7 @@ def read_record(record):
509510
return body_data
510511

511512

512-
def download_dastcom5():
513+
def download_dastcom5(update=False):
513514
"""Downloads DASTCOM5 database.
514515
515516
Downloads and unzip DASTCOM5 file in default sbpy path (~/.sbpy).
@@ -519,10 +520,15 @@ def download_dastcom5():
519520
dastcom5_dir = os.path.join(SBPY_LOCAL_PATH, "dastcom5")
520521
dastcom5_zip_path = os.path.join(SBPY_LOCAL_PATH, "dastcom5.zip")
521522

522-
if os.path.isdir(dastcom5_dir):
523+
if os.path.isdir(dastcom5_dir) and not update:
523524
raise FileExistsError(
524525
"dastcom5 is already created in " + os.path.abspath(dastcom5_dir)
525526
)
527+
528+
if os.path.isdir(dastcom5_dir) and update:
529+
shutil.rmtree(dastcom5_dir)
530+
531+
print("Downloading the latest DASTCOM5 Database")
526532
if not zipfile.is_zipfile(dastcom5_zip_path):
527533
if not os.path.isdir(SBPY_LOCAL_PATH):
528534
os.makedirs(SBPY_LOCAL_PATH)
@@ -534,6 +540,7 @@ def download_dastcom5():
534540
)
535541
with zipfile.ZipFile(dastcom5_zip_path) as myzip:
536542
myzip.extractall(SBPY_LOCAL_PATH)
543+
os.remove(dastcom5_zip_path)
537544

538545

539546
def show_download_progress(transferred, block, totalsize):

sbpy/data/utils/tests/__init__.py

Whitespace-only changes.

sbpy/utils/tests/test_dastcom5.py renamed to sbpy/data/utils/tests/test_dastcom5.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
import numpy as np
55
import pytest
66

7-
from sbpy.utils import dastcom5
7+
from sbpy.data.utils import dastcom5
88

99

10-
@mock.patch("sbpy.utils.dastcom5.np.fromfile")
11-
@mock.patch("sbpy.utils.dastcom5.open")
10+
@mock.patch("sbpy.data.utils.dastcom5.np.fromfile")
11+
@mock.patch("sbpy.data.utils.dastcom5.open")
1212
def test_asteroid_db_is_called_with_right_path(mock_open, mock_np_fromfile):
1313
dastcom5.asteroid_db()
1414
mock_open.assert_called_with(dastcom5.AST_DB_PATH, "rb")
1515

1616

17-
@mock.patch("sbpy.utils.dastcom5.np.fromfile")
18-
@mock.patch("sbpy.utils.dastcom5.open")
17+
@mock.patch("sbpy.data.utils.dastcom5.np.fromfile")
18+
@mock.patch("sbpy.data.utils.dastcom5.open")
1919
def test_comet_db_is_called_with_right_path(mock_open, mock_np_fromfile):
2020
dastcom5.comet_db()
2121
mock_open.assert_called_with(dastcom5.COM_DB_PATH, "rb")
2222

2323

24-
@mock.patch("sbpy.utils.dastcom5.np.fromfile")
25-
@mock.patch("sbpy.utils.dastcom5.open")
24+
@mock.patch("sbpy.data.utils.dastcom5.np.fromfile")
25+
@mock.patch("sbpy.data.utils.dastcom5.open")
2626
def test_read_headers(mock_open, mock_np_fromfile):
2727
dastcom5.read_headers()
2828
mock_open.assert_any_call(
@@ -33,9 +33,9 @@ def test_read_headers(mock_open, mock_np_fromfile):
3333
)
3434

3535

36-
@mock.patch("sbpy.utils.dastcom5.read_headers")
37-
@mock.patch("sbpy.utils.dastcom5.np.fromfile")
38-
@mock.patch("sbpy.utils.dastcom5.open")
36+
@mock.patch("sbpy.data.utils.dastcom5.read_headers")
37+
@mock.patch("sbpy.data.utils.dastcom5.np.fromfile")
38+
@mock.patch("sbpy.data.utils.dastcom5.open")
3939
def test_read_record(mock_open, mock_np_fromfile, mock_read_headers):
4040
mocked_ast_headers = np.array(
4141
[(3184, -1, b"00740473", b"00496815")],
@@ -59,10 +59,10 @@ def test_read_record(mock_open, mock_np_fromfile, mock_read_headers):
5959
)
6060

6161

62-
@mock.patch("sbpy.utils.dastcom5.os.makedirs")
63-
@mock.patch("sbpy.utils.dastcom5.zipfile")
64-
@mock.patch("sbpy.utils.dastcom5.os.path.isdir")
65-
@mock.patch("sbpy.utils.dastcom5.urllib.request")
62+
@mock.patch("sbpy.data.utils.dastcom5.os.makedirs")
63+
@mock.patch("sbpy.data.utils.dastcom5.zipfile")
64+
@mock.patch("sbpy.data.utils.dastcom5.os.path.isdir")
65+
@mock.patch("sbpy.data.utils.dastcom5.urllib.request")
6666
def test_download_dastcom5_raises_error_when_folder_exists(
6767
mock_request, mock_isdir, mock_zipfile, mock_makedirs
6868
):
@@ -76,10 +76,10 @@ def test_download_dastcom5_raises_error_when_folder_exists(
7676
)
7777

7878

79-
@mock.patch("sbpy.utils.dastcom5.urllib.request")
80-
@mock.patch("sbpy.utils.dastcom5.os.makedirs")
81-
@mock.patch("sbpy.utils.dastcom5.zipfile")
82-
@mock.patch("sbpy.utils.dastcom5.os.path.isdir")
79+
@mock.patch("sbpy.data.utils.dastcom5.urllib.request")
80+
@mock.patch("sbpy.data.utils.dastcom5.os.makedirs")
81+
@mock.patch("sbpy.data.utils.dastcom5.zipfile")
82+
@mock.patch("sbpy.data.utils.dastcom5.os.path.isdir")
8383
def test_download_dastcom5_creates_folder(
8484
mock_isdir, mock_zipfile, mock_makedirs, mock_request
8585
):
@@ -89,9 +89,9 @@ def test_download_dastcom5_creates_folder(
8989
mock_makedirs.assert_called_once_with(dastcom5.SBPY_LOCAL_PATH)
9090

9191

92-
@mock.patch("sbpy.utils.dastcom5.zipfile")
93-
@mock.patch("sbpy.utils.dastcom5.os.path.isdir")
94-
@mock.patch("sbpy.utils.dastcom5.urllib.request.urlretrieve")
92+
@mock.patch("sbpy.data.utils.dastcom5.zipfile")
93+
@mock.patch("sbpy.data.utils.dastcom5.os.path.isdir")
94+
@mock.patch("sbpy.data.utils.dastcom5.urllib.request.urlretrieve")
9595
def test_download_dastcom5_downloads_file(
9696
mock_request, mock_isdir, mock_zipfile
9797
):

sbpy/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# This sub-module is destined for common non-package specific utility
44
# functions.
55

6-
from sbpy.utils import dastcom5
6+
from .core import *

0 commit comments

Comments
 (0)