Skip to content

Commit

Permalink
era5: Logging differentiates between request and download (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
coroa authored Jun 10, 2020
1 parent b0d64b2 commit 3e59287
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions atlite/datasets/era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
import weakref
import cdsapi

try:
from contextlib import nullcontext
except ImportError:
# nullcontext was introduced in Python 3.7, so let's add a fallback for 3.6
import contextlib
@contextlib.contextmanager
def nullcontext():
yield

from ..gis import maybe_swap_spatial_dims

import logging
Expand Down Expand Up @@ -247,19 +256,26 @@ def retrieve_data(product, chunks=None, tmpdir=None, lock=None, **updates):
assert {'year', 'month', 'variable'}.issubset(request), (
"Need to specify at least 'variable', 'year' and 'month'")

result = cdsapi.Client(quiet=True, info_callback=logger.debug)\
.retrieve(product, request)
client = cdsapi.Client(quiet=True, info_callback=logger.debug)
result = client.retrieve(product, request)

if len(request['year']) > 1:
timedesc = f"{request['year'][0]}-{request['year'][-1]}"
else:
year = request['year'][0]
timedesc = f"{year}.{request['month'][0]}-{year}.{request['month'][-1]}"

fd, target = mkstemp(suffix='.nc', dir=tmpdir)
os.close(fd)
logger.info(f"CDS: Requested variable {request['variable']} from {product} for {timedesc}")

try:
if lock is not None:
lock.acquire()
if lock is None:
lock = nullcontext()

with lock:
fd, target = mkstemp(suffix='.nc', dir=tmpdir)
os.close(fd)

logger.info(f"CDS: Downloading variable {request['variable']} for {timedesc} to {target}")
result.download(target)
finally:
if lock is not None:
lock.release()

ds = xr.open_dataset(target, chunks=chunks or {})
if tmpdir is None:
Expand Down Expand Up @@ -309,8 +325,6 @@ def get_data(cutout, feature, tmpdir, lock=None, **creation_parameters):
func = globals().get(f"get_data_{feature}")
sanitize_func = globals().get(f"sanitize_{feature}")

logger.info(f"Downloading data for feature '{feature}' to {tmpdir}.")

def retrieve_once(time):
ds = func({**retrieval_params, **time})
if sanitize and sanitize_func is not None:
Expand Down

0 comments on commit 3e59287

Please sign in to comment.