Skip to content

Commit

Permalink
773 simplify dfmtcopernicusmarine credentials function (#819)
Browse files Browse the repository at this point in the history
* simplified dfm_tools.download.copernicusmarine_credentials()

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Apr 9, 2024
1 parent 7b13c3c commit 37d0de8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
28 changes: 8 additions & 20 deletions dfm_tools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dfm_tools.errors import OutOfRangeError
import cdsapi
import copernicusmarine
from copernicusmarine.core_functions.credentials_utils import InvalidUsernameOrPassword
import cftime
import getpass
import shutil
Expand Down Expand Up @@ -316,27 +317,14 @@ def copernicusmarine_remove_manual_credentials_file():

def copernicusmarine_credentials():
"""
To login at copernicusmarine if this was not the case yet.
If the credentials file is present, the function returns None.
If the credentials file is not present, get_and_check_username_password first
checks env vars and if not present it prompts the user for credentials.
Feeding the returned credentials to copernicusmarine.login() generates the credentials file.
If the file is available, it gets the credentials from the file.
Login at copernicusmarine if user not logged in yet.
Works via prompt, environment variables or credentials file.
"""
from copernicusmarine.core_functions.credentials_utils import (
DEFAULT_CLIENT_CREDENTIALS_FILEPATH,
InvalidUsernameOrPassword,
get_and_check_username_password,
)
login_kwargs = dict(username=None, password=None, credentials_file=DEFAULT_CLIENT_CREDENTIALS_FILEPATH, no_metadata_cache=False)
if not DEFAULT_CLIENT_CREDENTIALS_FILEPATH.is_file():
print("Downloading CMEMS data requires a Copernicus Marine username and password, sign up for free at: https://data.marine.copernicus.eu/register.")
username, password = get_and_check_username_password(**login_kwargs)
success = copernicusmarine.login(username, password)
if not success:
raise InvalidUsernameOrPassword("invalid credentials")
else:
_, _ = get_and_check_username_password(**login_kwargs)
print("Downloading CMEMS data requires a Copernicus Marine username and password, "
"sign up for free at: https://data.marine.copernicus.eu/register.")
success = copernicusmarine.login(skip_if_user_logged_in=True)
if not success:
raise InvalidUsernameOrPassword("Invalid credentials, please try again")


def copernicusmarine_reset(remove_folder=False, overwrite_cache=True, update_package=False):
Expand Down
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- simplified `rwsddl_ssh_retrieve_data()` by using `ddlpy.dataframe_to_xarray()` in [#805](https://github.com/Deltares/dfm_tools/pull/815)
- updated cmems analysisforecast bio dataset ids in [#816](https://github.com/Deltares/dfm_tools/pull/816)
- from ftp to copernicusmarine files service for insitu data in [#818](https://github.com/Deltares/dfm_tools/pull/818)
- simplified `dfmt.download.copernicusmarine_credentials()` in [#819](https://github.com/Deltares/dfm_tools/pull/819)

### Fix
- fixed xy coordinates for cmems insitu netcdf files in [#807](https://github.com/Deltares/dfm_tools/pull/807)
Expand Down

0 comments on commit 37d0de8

Please sign in to comment.