Skip to content

Commit

Permalink
Changed make_retriever to only take aux config attributes
Browse files Browse the repository at this point in the history
Changed make_retriever to only take aux config. Removed global variable in  for class Observatory.
  • Loading branch information
Little-Ryugu committed Dec 13, 2024
1 parent 7e006bc commit 331b122
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/sorcha/ephemeris/simulation_data_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pooch


def make_retriever(sconfigs, directory_path: str = None) -> pooch.Pooch:
def make_retriever(auxconfigs, directory_path: str = None) -> pooch.Pooch:
"""Helper function that will create a Pooch object to track and retrieve files.
Parameters
Expand All @@ -11,8 +11,8 @@ def make_retriever(sconfigs, directory_path: str = None) -> pooch.Pooch:
registry : dictionary, optional
A dictionary of file names to SHA hashes. Generally we'll not use SHA=None
because the files we're tracking change frequently. Default = REGISTRY
sconfigs: dataclass
Dataclass of configuration file arguments.
auxconfigs: dataclass
Dataclass of auxiliary configuration file arguments.
Returns
-------
: pooch
Expand All @@ -25,7 +25,7 @@ def make_retriever(sconfigs, directory_path: str = None) -> pooch.Pooch:
return pooch.create(
path=dir_path,
base_url="",
urls=sconfigs.auxiliary.urls,
registry=sconfigs.auxiliary.registry,
urls=auxconfigs.urls,
registry=auxconfigs.registry,
retry_if_failed=25,
)
11 changes: 3 additions & 8 deletions src/sorcha/ephemeris/simulation_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
import spiceypy as spice
from pooch import Decompress
from sorcha.utilities.sorchaConfigs import sorchaConfigs, auxiliaryConfigs
from sorcha.ephemeris.simulation_constants import RADIUS_EARTH_KM
from sorcha.ephemeris.simulation_geometry import ecliptic_to_equatorial
from sorcha.ephemeris.simulation_data_files import make_retriever
Expand Down Expand Up @@ -125,16 +124,12 @@ def parse_orbit_row(row, epochJD_TDB, ephem, sun_dict, gm_sun, gm_total):
return tuple(np.concatenate([equatorial_coords, equatorial_velocities]))


default = sorchaConfigs()
default.auxiliary = auxiliaryConfigs() # using the default observatory_codes in the auxiliaryConfigs class


class Observatory:
"""
Class containing various utility tools related to the calculation of the observatory position
"""

def __init__(self, args, sconfigs, oc_file=default.auxiliary.observatory_codes):
def __init__(self, args, sconfigs, oc_file=None):
"""
Initialization method
Expand All @@ -149,8 +144,8 @@ def __init__(self, args, sconfigs, oc_file=default.auxiliary.observatory_codes):
"""
self.observatoryPositionCache = {} # previously calculated positions to speed up the process

if oc_file == sconfigs.auxiliary.observatory_codes:
retriever = make_retriever(sconfigs, args.ar_data_file_path)
if oc_file == None:
retriever = make_retriever(sconfigs.auxiliary, args.ar_data_file_path)

# is the file available locally, if so, return the full path
if os.path.isfile(os.path.join(retriever.abspath, sconfigs.auxiliary.observatory_codes)):
Expand Down
6 changes: 3 additions & 3 deletions src/sorcha/ephemeris/simulation_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_assist_ephemeris(args, sconfigs) -> tuple:
"""
pplogger = logging.getLogger(__name__)

retriever = make_retriever(sconfigs, args.ar_data_file_path)
retriever = make_retriever(sconfigs.auxiliary, args.ar_data_file_path)
planets_file_path = retriever.fetch(sconfigs.auxiliary.jpl_planets)
small_bodies_file_path = retriever.fetch(sconfigs.auxiliary.jpl_small_bodies)
ephem = Ephem(planets_path=planets_file_path, asteroids_path=small_bodies_file_path)
Expand All @@ -69,14 +69,14 @@ def furnish_spiceypy(args, sconfigs):

pplogger = logging.getLogger(__name__)

retriever = make_retriever(sconfigs, args.ar_data_file_path)
retriever = make_retriever(sconfigs.auxiliary, args.ar_data_file_path)

for kernel_file in sconfigs.auxiliary.ordered_kernel_files:
retriever.fetch(kernel_file)

# check if the META_KERNEL file exists. If it doesn't exist, create it.
if not os.path.exists(os.path.join(retriever.abspath, sconfigs.auxiliary.meta_kernel)):
build_meta_kernel_file(sconfigs, retriever)
build_meta_kernel_file(sconfigs.auxiliary, retriever)

# try to get the META_KERNEL file. If it's not there, error out.
try:
Expand Down
10 changes: 5 additions & 5 deletions src/sorcha/utilities/generate_meta_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@
"""


def build_meta_kernel_file(sconfigs, retriever: pooch.Pooch) -> None:
def build_meta_kernel_file(auxconfigs, retriever: pooch.Pooch) -> None:
"""Builds a specific text file that will be fed into `spiceypy` that defines
the list of spice kernel to load, as well as the order to load them.
Parameters
----------
retriever : pooch
Pooch object that maintains the registry of files to download
sconfigs: dataclass
Dataclass of configuration file arguments.
auxconfigs: dataclass
Dataclass of auxiliary configuration file arguments.
Returns
---------
None
"""
# build meta_kernel file path
meta_kernel_file_path = os.path.join(retriever.abspath, sconfigs.auxiliary.meta_kernel)
meta_kernel_file_path = os.path.join(retriever.abspath, auxconfigs.meta_kernel)

# build a meta_kernel.txt file
with open(meta_kernel_file_path, "w") as meta_file:
meta_file.write("\\begindata\n\n")
meta_file.write(f"PATH_VALUES = ('{retriever.abspath}')\n\n")
meta_file.write("PATH_SYMBOLS = ('A')\n\n")
meta_file.write("KERNELS_TO_LOAD=(\n")
for file_name in sconfigs.auxiliary.ordered_kernel_files:
for file_name in auxconfigs.ordered_kernel_files:
shortened_file_name = _build_file_name(retriever.abspath, retriever.fetch(file_name))
meta_file.write(f" '{shortened_file_name}',\n")
meta_file.write(")\n\n")
Expand Down
8 changes: 4 additions & 4 deletions src/sorcha/utilities/retrieve_ephemeris_data_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _decompress(fname, action, pup): # pragma: no cover
pooch.Decompress(method="auto", name=os.path.splitext(fname)[0]).__call__(fname, action, pup)


def _remove_files(sconfigs, retriever: pooch.Pooch) -> None: # pragma: no cover
def _remove_files(auxconfigs, retriever: pooch.Pooch) -> None: # pragma: no cover
"""Utility to remove all the files tracked by the pooch retriever. This includes
the decompressed ObservatoryCodes.json file as well as the META_KERNEL file
that are created after downloading the files in the DATA_FILES_TO_DOWNLOAD
Expand All @@ -41,11 +41,11 @@ def _remove_files(sconfigs, retriever: pooch.Pooch) -> None: # pragma: no cover
------------
retriever : pooch
Pooch object that maintains the registry of files to download.
sconfigs: dataclass
Dataclass of configuration file arguments.
auxconfigs: dataclass
Dataclass of auxiliary configuration file arguments.
"""

for file_name in sconfigs.auxiliary.data_file_list:
for file_name in auxconfigs.data_file_list:
file_path = retriever.fetch(file_name)
print(f"Deleting file: {file_path}")
os.remove(file_path)
Expand Down
12 changes: 6 additions & 6 deletions src/sorcha_cmdline/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def execute(args):
)
from functools import partial
import concurrent.futures
from sorcha.utilities.sorchaConfigs import sorchaConfigs, auxiliaryConfigs
from sorcha.utilities.sorchaConfigs import auxiliaryConfigs

# default file names and urls (stored in auxiliaryConfigs)
default_files = sorchaConfigs()
default_files.auxiliary = auxiliaryConfigs()

default_files = auxiliaryConfigs()
# create the Pooch retriever that tracks and retrieves the requested files
retriever = make_retriever(default_files, args.cache)

Expand All @@ -62,21 +62,21 @@ def execute(args):
_remove_files(default_files, retriever)
else:
print("Checking cache for existing files.")
found_all_files = _check_for_existing_files(retriever, default_files.auxiliary.data_file_list)
found_all_files = _check_for_existing_files(retriever, default_files.data_file_list)

if not found_all_files:
# create a partial function of `Pooch.fetch` including the `_decompress` method
fetch_partial = partial(retriever.fetch, processor=_decompress, progressbar=True)

# download the data files in parallel
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(fetch_partial, default_files.auxiliary.data_files_to_download)
executor.map(fetch_partial, default_files.data_files_to_download)

# build the meta_kernel.txt file
build_meta_kernel_file(default_files, retriever)

print("Checking cache after attempting to download and create files.")
_check_for_existing_files(retriever, default_files.auxiliary.data_file_list)
_check_for_existing_files(retriever, default_files.data_file_list)


if __name__ == "__main__":
Expand Down

0 comments on commit 331b122

Please sign in to comment.