summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorb8raoult <53792887+b8raoult@users.noreply.github.com>2020-06-10 09:49:24 +0100
committerGitHub <noreply@github.com>2020-06-10 09:49:24 +0100
commit534e07b50b0954c0ae06eae35961a89ac8a03f59 (patch)
treeb89c0c3bccfbfd28f609706a238e5db2f596744e
parente358418b2eb89e17b4863326345bfe7c9fef307e (diff)
parent2c2dcaa67dc57a43b19497b11c58c9b01f1c8057 (diff)
Merge pull request #3 from jblarsen/feature_separate_retrieve_download
FEATURE_SEPARATE_RETRIEVE_DOWNLOAD: support for updating requests
-rw-r--r--cdsapi/api.py15
-rwxr-xr-xexample-era5-update.py47
2 files changed, 62 insertions, 0 deletions
diff --git a/cdsapi/api.py b/cdsapi/api.py
index d4c5a60..70efa25 100644
--- a/cdsapi/api.py
+++ b/cdsapi/api.py
@@ -192,6 +192,16 @@ class Result(object):
self.debug(metadata.headers)
return metadata
+ 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)
+ result.raise_for_status()
+ self.reply = result.json()
+
def delete(self):
if self._deleted:
@@ -238,6 +248,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,
@@ -287,6 +298,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
@@ -411,6 +423,9 @@ class Client(object):
else:
raise
+ if not self.wait_until_complete:
+ return Result(self, reply)
+
sleep = 1
while True:
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")