From d1ea545c33af094dea13def150a4b92956dc154c Mon Sep 17 00:00:00 2001 From: Alec Koumjian Date: Fri, 3 Nov 2023 16:31:11 -0400 Subject: [PATCH 1/2] alternative option for checking pyoorb initialization --- adam_core/propagator/pyoorb.py | 62 ++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/adam_core/propagator/pyoorb.py b/adam_core/propagator/pyoorb.py index 19db4b91..fd184fdd 100644 --- a/adam_core/propagator/pyoorb.py +++ b/adam_core/propagator/pyoorb.py @@ -4,6 +4,7 @@ raise ImportError("PYOORB is not installed.") import enum +import logging import os import warnings from typing import Optional, Union @@ -22,6 +23,8 @@ from .propagator import EphemerisType, OrbitType, Propagator from .utils import _assert_times_almost_equal +logger = logging.getLogger(__name__) + class OpenOrbTimescale(enum.Enum): UTC = 1 @@ -36,37 +39,47 @@ class OpenOrbOrbitType(enum.Enum): KEPLERIAN = 3 +PYOORB_INIT_CACHCE = {} + + +def process_safe_oorb_init(ephfile): + """ + Initializes pyoorb only if it hasn't been initialized in this process before + """ + pid = os.getpid() + if pid in PYOORB_INIT_CACHCE: + logger.debug(f"PYOORB already initialized for process {pid}") + return + + logger.debug(f"Initializing PYOORB for process {pid}") + PYOORB_INIT_CACHCE[pid] = True + err = oo.pyoorb.oorb_init(ephfile) + if err != 0: + raise RuntimeError(f"PYOORB returned error code: {err}") + + class PYOORB(Propagator): def __init__( self, *, dynamical_model: str = "N", ephemeris_file: str = "de430.dat" ): - self.dynamical_model = dynamical_model self.ephemeris_file = ephemeris_file - env_var = "ADAM_CORE_PYOORB_INITIALIZED" - if env_var in os.environ.keys() and os.environ[env_var] == "True": - pass - else: - if os.environ.get("OORB_DATA") is None: - if os.environ.get("CONDA_PREFIX") is None: - raise RuntimeError( - "Cannot find OORB_DATA directory. Please set the OORB_DATA environment variable." - ) - else: - os.environ["OORB_DATA"] = os.path.join( - os.environ["CONDA_PREFIX"], "share/openorb" - ) - - oorb_data = os.environ["OORB_DATA"] - - # Prepare pyoorb - ephfile = os.path.join(oorb_data, self.ephemeris_file) - err = oo.pyoorb.oorb_init(ephfile) - if err == 0: - os.environ[env_var] = "True" + if os.environ.get("OORB_DATA") is None: + if os.environ.get("CONDA_PREFIX") is None: + raise RuntimeError( + "Cannot find OORB_DATA directory. Please set the OORB_DATA environment variable." + ) else: - warnings.warn(f"PYOORB returned error code: {err}") + os.environ["OORB_DATA"] = os.path.join( + os.environ["CONDA_PREFIX"], "share/openorb" + ) + + oorb_data = os.environ["OORB_DATA"] + + # Prepare pyoorb + ephfile = os.path.join(oorb_data, self.ephemeris_file) + process_safe_oorb_init(ephfile) return @@ -292,7 +305,6 @@ def _propagate_orbits(self, orbits: OrbitType, times: Timestamp) -> OrbitType: ) elif isinstance(orbits, VariantOrbits): - # Map the object and orbit IDs back to the input arrays object_ids = orbits.object_id.to_numpy(zero_copy_only=False)[orbit_ids_] orbit_ids = orbits.orbit_id.to_numpy(zero_copy_only=False)[orbit_ids_] @@ -445,7 +457,6 @@ def _generate_ephemeris( ) if isinstance(orbits, Orbits): - # Map the object and orbit IDs back to the input arrays orbit_ids = orbits.orbit_id.to_numpy(zero_copy_only=False)[ orbit_ids_idx @@ -494,7 +505,6 @@ def _generate_ephemeris( ephemeris_list.append(ephemeris) elif isinstance(orbits, VariantOrbits): - # Map the object and orbit IDs back to the input arrays object_ids = orbits.object_id.to_numpy(zero_copy_only=False)[ orbit_ids_idx From 07fe9be61a088f5039195c95243eee52b2ce129b Mon Sep 17 00:00:00 2001 From: Alec Koumjian Date: Wed, 8 Nov 2023 11:48:45 -0500 Subject: [PATCH 2/2] fmt --- adam_core/propagator/pyoorb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adam_core/propagator/pyoorb.py b/adam_core/propagator/pyoorb.py index fd184fdd..7c24df86 100644 --- a/adam_core/propagator/pyoorb.py +++ b/adam_core/propagator/pyoorb.py @@ -6,7 +6,6 @@ import enum import logging import os -import warnings from typing import Optional, Union import numpy as np