You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.0 KiB

  1. import os
  2. import socket
  3. from pyrocko import util
  4. op = os.path
  5. DATA_DIR = op.join(op.dirname(op.abspath(__file__)), 'data')
  6. os.makedirs(DATA_DIR, exist_ok=True)
  7. class NoInternetConnection(Exception):
  8. pass
  9. def _download_data(fn, dldir=False):
  10. fpath = data_file_path(fn)
  11. if not op.exists(fpath):
  12. if not have_internet():
  13. raise NoInternetConnection(
  14. 'need internet access to download data')
  15. url = 'http://data.pyrocko.org/examples/' + fn
  16. print('downloading %s' % url)
  17. if dldir:
  18. util.download_dir(url, fpath)
  19. else:
  20. util.download_file(url, fpath)
  21. return fpath
  22. def download_dir(fn):
  23. return _download_data(fn, dldir=True)
  24. def download_file(fn):
  25. return _download_data(fn)
  26. def data_file_path(fn):
  27. return op.join(DATA_DIR, fn)
  28. def have_internet():
  29. try:
  30. return 0 < len([
  31. (s.connect(('8.8.8.8', 80)), s.close())
  32. for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]])
  33. except OSError:
  34. return False