Skip to content

Commit

Permalink
updated to copernicusmarine v2 (#1046)
Browse files Browse the repository at this point in the history
* update copernicusmarine minimal version

* update login method

* removed cmems registration method

* updated copernicusmarine.get arguments

* temporarily convert dates back to isoformat
  • Loading branch information
veenstrajelmer authored Jan 8, 2025
1 parent 349aaa2 commit 20a1a32
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
20 changes: 14 additions & 6 deletions dfm_tools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def download_CMEMS(varkey,
# subset enough data in case of data with daily timesteps.
date_min = pd.Timestamp(date_min).floor('1d')
date_max = pd.Timestamp(date_max).ceil('1d')

if dataset_id is None:
dataset_id = copernicusmarine_get_dataset_id(varkey, date_min, date_max)
if buffer is None:
Expand All @@ -222,8 +222,10 @@ def download_CMEMS(varkey,
maximum_longitude = longitude_max,
minimum_latitude = latitude_min,
maximum_latitude = latitude_max,
start_datetime = date_min,
end_datetime = date_max,
# temporarily convert back to strings because of https://github.com/mercator-ocean/copernicus-marine-toolbox/issues/261
# TODO: revert, see https://github.com/Deltares/dfm_tools/issues/1047
start_datetime = date_min.isoformat(),
end_datetime = date_max.isoformat(),
)

Path(dir_output).mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -373,9 +375,15 @@ def copernicusmarine_credentials():
Login at copernicusmarine if user not logged in yet.
Works via prompt, environment variables or credentials file.
"""
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)
# first check whether (valid) credentials are already present
success = copernicusmarine.login(check_credentials_valid=True)
if success:
return

# call the login function if no (valid) credentials present
success = copernicusmarine.login()

# raise if credentials are incorrect
if not success:
raise InvalidUsernameOrPassword("Invalid credentials, please try again")

Expand Down
8 changes: 2 additions & 6 deletions dfm_tools/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,10 @@ def cmems_ssh_read_catalog(source, overwrite=True):
copernicusmarine_credentials()
copernicusmarine.get(
dataset_id=dataset_id,
service="files",
index_parts=True,
filter="*index_history.txt",
output_directory=dir_index,
overwrite_output_data=True,
force_download=True,
overwrite=True,
no_directories=True,
)
else:
Expand Down Expand Up @@ -567,11 +565,9 @@ def cmems_ssh_retrieve_data(row, dir_output, time_min=None, time_max=None,
copernicusmarine.get(
dataset_id=dataset_id,
dataset_part="history",
service="files",
filter=url_file,
output_directory=tempdir,
overwrite_output_data=True,
force_download=True,
overwrite=True,
no_directories=True,
disable_progress_bar=disable_progress_bar,
)
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ dependencies = [
"pydap>=3.4.0",
#erddapy>=2.0.0 supports pandas>=2.0.0
"erddapy>=2.0.0",
#copernicusmarine>=1.3.5 is the latest version and the developers recommend to always use the latest version until 2.0.0 is released: https://github.com/Deltares/dfm_tools/issues/936
# TODO: remove when dfm_tools is adjusted to not-yet-released 2.0 version: https://github.com/Deltares/dfm_tools/issues/933
"copernicusmarine>=1.3.5,<2.0.0",
#copernicusmarine>=2.0.0 has breaking changes compared to v1
"copernicusmarine>=2.0.0",
#rws-ddlpy>=0.6.0 `ddlpy.measurements_amount()` returns all amounts
"rws-ddlpy>=0.6.0",
#pooch>=1.1.0 has attribute retrieve
Expand Down

0 comments on commit 20a1a32

Please sign in to comment.