In [72]:
from os.path import join, splitext
from os import rename, remove
from astropy.utils.console import ProgressBar
from astropy.utils.data import download_file
from zipfile import ZipFile
In [16]:
from sunpy import config
data_dir = config.get('downloads', 'download_dir')
In [61]:
def url_exists(url, timeout=2):
"""
Checks whether a url is online.
Parameters
----------
url: str
A string containing a URL
Returns
-------
value: bool
Examples
--------
>>> from sunpy.net.helio import parser
>>> url_exists('http://www.google.com')
True
>>> url_exists('http://aslkfjasdlfkjwerf.com')
False
"""
try:
fd = urllib2.urlopen(url, timeout=timeout)
except urllib2.HTTPError, e:
#print(url)
#print(e.reason)
return False
except urllib2.URLError, e:
#print(url)
#print(e.reason)
return False
else:
return True
In [35]:
url_exists('http://www.google.com')
Out[35]:
In [36]:
base_urls = ('http://www.bsdft.com/', 'http://hesperia.gsfc.nasa.gov/~schriste/sunpy-sample-data/')
In [37]:
f = download_file(join(base_urls[1], "AIA20110319_105400_0171.fits"))
In [73]:
def download(progress=True):
"""
Download the sample data.
"""
files = [
"AIA20110319_105400_0171.fits",
"hsi_image_20101016_191218.fits",
"eit_l1_20020625_100011.fits",
"BIR_20110922_103000_01.fit",
"hsi_calib_ev_20020220_1106_20020220_1106_25_40.fits",
"swap_lv1_20120101_001607.fits",
"aia.lev1.193A_2013-09-21T16_00_06.84Z.image_lev1.fits.zip"
]
file_paths = []
for base_url in base_urls:
for file_name in files:
if url_exists(join(base_url, file_name)):
f = download_file(join(base_url, file_name))
real_name, ext = splitext(file_name)
if ext == '.zip':
print("Unpacking: %s" % real_name)
with ZipFile(f, 'r') as zip_file:
zip_file.extract(real_name, data_dir)
remove(f)
else:
# move files to the data directory
rename(f, join(data_dir, file_name))
In [75]:
download()