Skip to content

Commit b7fd1f3

Browse files
Miguel de Val-Borrokeflavich
authored andcommitted
Use requests module to download HITRAN data
1 parent 7befdc4 commit b7fd1f3

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

astroquery/hitran/reader.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
from collections import OrderedDict
22
from astropy.table import Table
3-
from astropy import log
43
from astropy.config import paths
4+
from astropy.utils.console import ProgressBar
5+
from ..utils import commons
56
import os
6-
try:
7-
import urllib.request as urllib2
8-
except ImportError:
9-
import urllib2
107

118

129
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
13-
HITRAN_URL = 'http://hitran.org'
10+
HITRAN_URL = 'http://hitran.org/lbl/api'
1411
cache_location = os.path.join(paths.get_cache_dir(), 'astroquery', 'hitran')
1512
if not os.path.exists(cache_location):
1613
os.makedirs(cache_location)
@@ -279,24 +276,21 @@ def download_hitran(m, i, numin, numax):
279276
"""
280277
iso_id = str(ISO[(m,i)][ISO_INDEX['id']])
281278
mol_name = ISO[(m,i)][ISO_INDEX['mol_name']]
282-
url = HITRAN_URL + '/lbl/api?' + \
283-
'iso_ids_list=' + iso_id + '&' + \
284-
'numin=' + str(numin) + '&' + \
285-
'numax=' + str(numax)
286-
try:
287-
req = urllib2.urlopen(url)
288-
except urllib2.HTTPError:
289-
raise Exception('Failed to retrieve data for given parameters.')
290-
except urllib2.URLError:
291-
raise Exception('Cannot connect to {0}.'.format(HITRAN_URL))
292-
CHUNK = 64 * 1024
293279
filename = os.path.join(cache_location, '{0}.data'.format(mol_name))
280+
CHUNK = 64 * 1024
281+
data = dict(iso_ids_list=iso_id, numin=numin, numax=numax)
294282
with open(filename, 'w') as fp:
295-
while True:
296-
chunk = req.read(CHUNK)
297-
if not chunk: break
298-
fp.write(chunk.decode('utf-8'))
299-
log.info('{0} bytes written to {1}'.format(CHUNK, filename))
283+
response = commons.send_request(HITRAN_URL, data, 10,
284+
request_type='GET')
285+
if 'Content-Length' in response.headers:
286+
total_length = response.headers.get('Content-Length')
287+
pb = ProgressBar(int(total_length))
288+
for chunk in response.iter_content(chunk_size=CHUNK):
289+
fp.write(chunk.decode('utf-8'))
290+
try:
291+
pb.update(CHUNK)
292+
except NameError:
293+
pass
300294

301295
def read_hitran_file(filename, formats=None,
302296
formatfile=os.path.join(DATA_DIR, 'readme.txt')):

0 commit comments

Comments
 (0)