From 72a09646f39130fd3e1530139a4b71dfd8d89b8e Mon Sep 17 00:00:00 2001 From: Jesper Baasch-Larsen Date: Tue, 7 May 2019 07:17:34 +0200 Subject: FEATURE_SEPARATE_RETRIEVE_DOWNLOAD: added support for updating request outside while loop --- cdsapi/api.py | 13 +++++++++++++ example-era5-update.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 example-era5-update.py diff --git a/cdsapi/api.py b/cdsapi/api.py index 88a3a8c..fb0b59d 100644 --- a/cdsapi/api.py +++ b/cdsapi/api.py @@ -116,6 +116,14 @@ class Result(object): self.debug(metadata.headers) return metadata + def update(self): + task_url = '%s/tasks/%s' % (self._url, self.reply['request_id']) + self.debug("GET %s", task_url) + + result = self.robust(self.session.get)(task_url, verify=self.verify) + result.raise_for_status() + self.reply = result.json() + def delete(self): if self._deleted: @@ -161,6 +169,7 @@ class Client(object): delete=True, retry_max=500, sleep_max=120, + wait_until_complete=True, info_callback=None, warning_callback=None, error_callback=None, @@ -206,6 +215,7 @@ class Client(object): self.full_stack = full_stack self.delete = delete self.last_state = None + self.wait_until_complete = wait_until_complete self.debug_callback = debug_callback self.warning_callback = warning_callback @@ -271,6 +281,9 @@ class Client(object): else: raise + if not self.wait_until_complete: + return Result(self, reply) + sleep = 1 start = time.time() diff --git a/example-era5-update.py b/example-era5-update.py new file mode 100755 index 0000000..5749e84 --- /dev/null +++ b/example-era5-update.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +# (C) Copyright 2018 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# In applying this licence, ECMWF does not waive the privileges and immunities +# granted to it by virtue of its status as an intergovernmental organisation nor +# does it submit to any jurisdiction. + +import time +import cdsapi + +c = cdsapi.Client(debug=True, wait_until_complete=False) + +r = c.retrieve( + "reanalysis-era5-single-levels", + { + "variable": "2t", + "product_type": "reanalysis", + "date": "2015-12-01", + "time": "14:00", + "format": "netcdf", + }, +) + +sleep = 30 +while True: + r.update() + reply = r.reply + r.info("Request ID: %s, state: %s" % (reply['request_id'], reply['state'])) + + if reply['state'] == 'completed': + break + elif reply['state'] in ('queued', 'running'): + r.info("Request ID: %s, sleep: %s", reply['request_id'], sleep) + time.sleep(sleep) + elif reply['state'] in ('failed',): + r.error("Message: %s", reply['error'].get('message')) + r.error("Reason: %s", reply['error'].get('reason')) + for n in reply.get('error', {}).get('context', {}).get('traceback', '').split('\n'): + if n.strip() == '': + break + r.error(" %s", n) + raise Exception("%s. %s." % (reply['error'].get('message'), reply['error'].get('reason'))) + +r.download("test.nc") -- cgit v1.2.3 From 2c2dcaa67dc57a43b19497b11c58c9b01f1c8057 Mon Sep 17 00:00:00 2001 From: Jesper Baasch-Larsen Date: Thu, 5 Dec 2019 09:27:47 +0100 Subject: FEATURE_SEPARATE_RETRIEVE_DOWNLOAD: request_id as optional argument to update method --- cdsapi/api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cdsapi/api.py b/cdsapi/api.py index fb0b59d..16cbcaf 100644 --- a/cdsapi/api.py +++ b/cdsapi/api.py @@ -116,8 +116,10 @@ class Result(object): self.debug(metadata.headers) return metadata - def update(self): - task_url = '%s/tasks/%s' % (self._url, self.reply['request_id']) + def update(self, request_id=None): + if request_id is None: + request_id = self.reply['request_id'] + task_url = '%s/tasks/%s' % (self._url, request_id) self.debug("GET %s", task_url) result = self.robust(self.session.get)(task_url, verify=self.verify) -- cgit v1.2.3