Skip to content

Commit

Permalink
Do not import ecmwf-api-client for standalone MARS access (#489)
Browse files Browse the repository at this point in the history
* Do not import ecmwf-api-client for standalone MARS access
  • Loading branch information
sandorkertesz authored Oct 16, 2024
1 parent b047a25 commit 6730370
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/release_notes/version_0.10_updates.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Version 0.10 Updates
/////////////////////////


Version 0.10.8
===============

Fixes
++++++

- Fixed issue when tried to import "ecmwf-api-client" unnecessarily when the standalone MARS client was invoked for retrieving data from MARS.


Version 0.10.7
===============

Expand Down
16 changes: 11 additions & 5 deletions src/earthkit/data/sources/mars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
import os
import subprocess

try:
import ecmwfapi
except ImportError:
raise ImportError("MARS access requires 'ecmwf-api-client' to be installed")

from earthkit.data.core.settings import SETTINGS
from earthkit.data.core.temporary import temp_file

Expand Down Expand Up @@ -49,13 +44,24 @@ def execute(self, request, target):
check=True,
)

@staticmethod
def enabled():
if SETTINGS.get("use-standalone-mars-client-when-available"):
return os.path.exists(StandaloneMarsClient.EXE)
return False


class MarsRetriever(ECMWFApi):
def service(self):
if SETTINGS.get("use-standalone-mars-client-when-available"):
if os.path.exists(StandaloneMarsClient.EXE):
return StandaloneMarsClient()

try:
import ecmwfapi
except ImportError:
raise ImportError("MARS access requires 'ecmwf-api-client' to be installed")

if self.prompt:
prompt = MARSAPIKeyPrompt()
prompt.check()
Expand Down
6 changes: 5 additions & 1 deletion src/earthkit/data/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from earthkit.data import from_source
from earthkit.data.readers.text import TextReader
from earthkit.data.sources.empty import EmptySource
from earthkit.data.sources.mars import StandaloneMarsClient

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,7 +91,10 @@ def modules_installed(*modules):
return True


NO_MARS = not os.path.exists(os.path.expanduser("~/.ecmwfapirc"))
NO_MARS_DIRECT = not StandaloneMarsClient.enabled()
NO_MARS_API = not os.path.exists(os.path.expanduser("~/.ecmwfapirc"))
NO_MARS = NO_MARS_API and NO_MARS_DIRECT

NO_CDS = not os.path.exists(os.path.expanduser("~/.cdsapirc"))
NO_HDA = not os.path.exists(os.path.expanduser("~/.hdarc"))
IN_GITHUB = os.environ.get("GITHUB_WORKFLOW") is not None
Expand Down

0 comments on commit 6730370

Please sign in to comment.