diff --git a/xarray/tutorial.py b/xarray/tutorial.py index 858f08d77e9..889e396e8e1 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -10,6 +10,7 @@ from __future__ import print_function import os as _os +import shutil as _shutil from .backends.api import open_dataset as _open_dataset from .core.pycompat import urlretrieve as _urlretrieve @@ -42,19 +43,30 @@ def load_dataset(name, cache=True, cache_dir=_default_cache_dir, """ longdir = _os.path.expanduser(cache_dir) + tmpdir = _os.sep.join((longdir, '.tmp')) fullname = name + '.nc' localfile = _os.sep.join((longdir, fullname)) + tmpfile = _os.sep.join((longdir, fullname)) if not _os.path.exists(localfile): - + # This will always leave this directory on disk. # May want to add an option to remove it. if not _os.path.isdir(longdir): _os.mkdir(longdir) + if not _os.path.isdir(tmpdir): + _os.mkdir(tmpdir) + url = '/'.join((github_url, 'raw', 'master', fullname)) - _urlretrieve(url, localfile) + _urlretrieve(url, tmpfile) + + if not _os.path.exists(tmpfile): + raise ValueError('File could not be downloaded, please try again') + + _shutil.move(tmpfile, localfile) + print(localfile) ds = _open_dataset(localfile, **kws).load() if not cache: