Skip to content

Commit

Permalink
dl tutorial files to tmp directory, then move them once successful
Browse files Browse the repository at this point in the history
  • Loading branch information
gidden committed May 3, 2017
1 parent a9a12b0 commit 18225e4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion xarray/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,18 +43,27 @@ 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)

ds = _open_dataset(localfile, **kws).load()

Expand Down

0 comments on commit 18225e4

Please sign in to comment.