From fae3d4e1497d0757a2eae32699555ce38f656f62 Mon Sep 17 00:00:00 2001 From: Erik Holmgren Date: Fri, 15 Nov 2024 12:03:41 +0100 Subject: [PATCH] Some updates for compatibility with cdsapi = 0.7.4. The behaviour of the current `Client` is apparently unstable according to https://github.com/ecmwf-projects/cads-api-client/issues/94. --- cdsapi_helper/cli.py | 7 ++++++- cdsapi_helper/download.py | 14 +++++++------- cdsapi_helper/utils.py | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cdsapi_helper/cli.py b/cdsapi_helper/cli.py index 669649f..f5ea0a8 100755 --- a/cdsapi_helper/cli.py +++ b/cdsapi_helper/cli.py @@ -120,8 +120,13 @@ def download_cds( # Should go back up to download_request. continue # Everything is in the queue. - elif (df.state == "queued").any(): + elif ( + (df.state == "queued").any() + or (df.state == "running").any() + or (df.state == "accepted").any() + ): # Wait 30 minutes before checking the status again. + click.echo("Requests are running, waiting 30 min.") sleep(60 * 30) else: check_request_again = False diff --git a/cdsapi_helper/download.py b/cdsapi_helper/download.py index 5e33efc..f835648 100644 --- a/cdsapi_helper/download.py +++ b/cdsapi_helper/download.py @@ -59,13 +59,12 @@ def update_request(dry_run: bool) -> None: ): try: if not dry_run: - result = cdsapi.api.Result( - client, {"request_id": request.request_id} - ) + result = client.client.get_remote(request.request_id) result.update() df.at[request.Index, "state"] = result.reply["state"] - except HTTPError: + except HTTPError as err: print(f"Request {request.Index} not found") + print(err) df.at[request.Index, "state"] = "deleted" df.to_csv("./cds_requests.csv") @@ -95,13 +94,13 @@ def download_request( def download_helper( request: pd.core.frame.pandas, - filename_spec, + filename_spec: list, client: cdsapi.Client, dry_run: bool = False, ) -> str: if request.state == "completed": try: - result = cdsapi.api.Result(client, {"request_id": request.request_id}) + result = client.client.get_remote(request.request_id) result.update() filename = build_filename(request, filename_spec) if not dry_run: @@ -109,8 +108,9 @@ def download_helper( return "downloaded" else: return request.state - except HTTPError: + except HTTPError as e: print("Request not found") + print(e) return request.state else: # No change to state. diff --git a/cdsapi_helper/utils.py b/cdsapi_helper/utils.py index fb48fa3..489094f 100644 --- a/cdsapi_helper/utils.py +++ b/cdsapi_helper/utils.py @@ -88,7 +88,7 @@ def request_to_df(request: dict, reply: dict, req_hash: str) -> pd.DataFrame: def build_filename(request: dict, filename_spec: list) -> str: - filetype = ".nc" if request.format == "netcdf" else ".grib" + filetype = ".nc" if request.data_format == "netcdf" else ".grib" filename_parts = [] for var in filename_spec: part = str_to_list(getattr(request, var))