From f44d2e0e308664b2d8e698a3e4ef24275e7b0d03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Jonglez?= <clement@jonglez.space>
Date: Tue, 12 Mar 2024 10:54:50 +0100
Subject: [PATCH] setup_orekit_curdir: optionally support loading data from
 orekitdata library

With default value False in order not to break compatibility with existing code
---
 python_files/pyhelpers.py | 63 ++++++++++++++++++++++++++-------------
 1 file changed, 42 insertions(+), 21 deletions(-)

diff --git a/python_files/pyhelpers.py b/python_files/pyhelpers.py
index 555e21d..9092157 100644
--- a/python_files/pyhelpers.py
+++ b/python_files/pyhelpers.py
@@ -55,13 +55,16 @@ def download_orekit_data_curdir(filename='orekit-data.zip'):
         shutil.copyfileobj(response, out_file)
 
 
-def setup_orekit_curdir(filename='orekit-data.zip'):
+def setup_orekit_curdir(filename='orekit-data.zip', from_pip_library=False):
     """Setup the java engine with orekit.
 
     This function loads the Orekit data from either:
         - A zip in the current directory (by default orekit-data.zip),
         - A folder,
-    depending on whether `filename` is the path to a file or to a folder.
+        - From the `orekitdata` Python library, installable via pip (see below)
+    depending on whether `filename` is the path to a file or to a folder, and whether from_pip_library is True or False
+
+    The `orekitdata` library is installable with `pip install git+https://gitlab.orekit.org/orekit/orekit-data.git`
 
     Then the function sets up the Orekit DataProviders to access it.
 
@@ -71,30 +74,48 @@ def setup_orekit_curdir(filename='orekit-data.zip'):
 
     Args:
         filename (str): Name of zip or folder with orekit data. Default filename is 'orekit-data.zip'
-
+        from_pip_library (bool), default False: if True, will first try to load the data from the `orekitdata` python library
 
     """
 
     DM = DataContext.getDefault().getDataProvidersManager()
-    datafile = File(filename)
-    if not datafile.exists():
-        print('File or folder:', datafile.getAbsolutePath(), ' not found')
-        print("""
-
-        The Orekit library relies on some external data for physical models. 
-        Typical data are the Earth Orientation Parameters and the leap seconds history, 
-        both being provided by the IERS or the planetary ephemerides provided by JPL. 
-        Such data is stored in text or binary files with specific formats that Orekit knows 
-        how to read, and needs to be provided for the library to work.
-
-        You can download a starting file with this data from the orekit gitlab at:
-        https://gitlab.orekit.org/orekit/orekit-data
-
-        or by the function:
-        orekit.pyhelpers.download_orekit_data_curdir()
 
-        """)
-        return
+    data_load_from_library_sucessful = False
+    if from_pip_library:
+        try:
+            import orekitdata
+            datafile = File(orekitdata.__path__[0])
+            if not datafile.exists():
+                print(f"""Unable to find orekitdata library folder,
+                      will try to load Orekit data using the folder or filename {filename}""")
+            else:
+                filename = orekitdata.__path__[0]
+                data_load_from_library_sucessful = True
+        except ImportError:
+            print(f"""Failed to load orekitdata library.
+                  Install with `pip install git+https://gitlab.orekit.org/orekit/orekit-data.git`
+                  Will try to load Orekit data using the folder or filename {filename}""")
+
+    if not data_load_from_library_sucessful:
+        datafile = File(filename)
+        if not datafile.exists():
+            print('File or folder:', datafile.getAbsolutePath(), ' not found')
+            print("""
+
+            The Orekit library relies on some external data for physical models.
+            Typical data are the Earth Orientation Parameters and the leap seconds history,
+            both being provided by the IERS or the planetary ephemerides provided by JPL.
+            Such data is stored in text or binary files with specific formats that Orekit knows
+            how to read, and needs to be provided for the library to work.
+
+            You can download a starting file with this data from the orekit gitlab at:
+            https://gitlab.orekit.org/orekit/orekit-data
+
+            or by the function:
+            orekit.pyhelpers.download_orekit_data_curdir()
+
+            """)
+            return
 
     if os.path.isdir(filename):
         crawler = DirectoryCrawler(datafile)
-- 
GitLab