|
1 | 1 | from collections import OrderedDict |
2 | 2 | from astropy.table import Table |
3 | | -from astropy import log |
4 | 3 | from astropy.config import paths |
| 4 | +from astropy.utils.console import ProgressBar |
| 5 | +from ..utils import commons |
5 | 6 | import os |
6 | | -try: |
7 | | - import urllib.request as urllib2 |
8 | | -except ImportError: |
9 | | - import urllib2 |
10 | 7 |
|
11 | 8 |
|
12 | 9 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
13 | | -HITRAN_URL = 'http://hitran.org' |
| 10 | +HITRAN_URL = 'http://hitran.org/lbl/api' |
14 | 11 | cache_location = os.path.join(paths.get_cache_dir(), 'astroquery', 'hitran') |
15 | 12 | if not os.path.exists(cache_location): |
16 | 13 | os.makedirs(cache_location) |
@@ -279,24 +276,21 @@ def download_hitran(m, i, numin, numax): |
279 | 276 | """ |
280 | 277 | iso_id = str(ISO[(m,i)][ISO_INDEX['id']]) |
281 | 278 | 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 |
293 | 279 | 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) |
294 | 282 | 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 |
300 | 294 |
|
301 | 295 | def read_hitran_file(filename, formats=None, |
302 | 296 | formatfile=os.path.join(DATA_DIR, 'readme.txt')): |
|
0 commit comments