Skip to content

Commit a4a13e1

Browse files
test_data: adapt to changed NASA download
files_handler.download_file: add default location in doc string nightlight.download_nl_files: break loop on success (really)
1 parent bfed492 commit a4a13e1

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

climada/entity/exposures/litpop/nightlight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def download_nl_files(req_files=np.ones(len(BM_FILENAMES),),
313313
curr_file = url.str() + BM_FILENAMES[num_files] %(year)
314314
LOGGER.info('Attempting to download file from %s', curr_file)
315315
path_check = download_file(curr_file, download_dir=dwnl_path)
316-
continue # leaf loop if sucessful
316+
break # leave loop if sucessful
317317
except ValueError as err:
318318
value_err = err
319319
if path_check: # download succesful

climada/util/files_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def download_file(url, download_dir=None, overwrite=True):
6363
url containing data to download
6464
download_dir : Path or str, optional
6565
the parent directory of the eventually downloaded file
66+
default: local_data.save_dir as defined in climada.conf
6667
overwrite : bool, optional
6768
whether or not an alredy existing file at the target location should be overwritten,
6869
by default True

test_data_api.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@
2020
"""
2121

2222
from pathlib import Path
23+
from sys import dont_write_bytecode
2324
import pandas as pd
2425
import unittest
25-
import urllib
2626
import xmlrunner
2727
import datetime as dt
2828

29-
# solve version problem in pandas-datareader-0.6.0. see:
30-
# https://stackoverflow.com/questions/50394873/import-pandas-datareader-gives-
31-
# importerror-cannot-import-name-is-list-like
32-
pd.core.common.is_list_like = pd.api.types.is_list_like
29+
import numpy as np
3330
from pandas_datareader import wb
3431

35-
from climada.entity.exposures.nightlight import NOAA_SITE, NASA_SITE, BM_FILENAMES
32+
from climada import CONFIG
33+
from climada.entity.exposures.litpop.nightlight import BM_FILENAMES, download_nl_files
3634
from climada.hazard.tc_tracks import IBTRACS_URL, IBTRACS_FILE
3735
from climada.hazard.tc_tracks_forecast import TCForecast
3836
from climada.util.finance import WORLD_BANK_WEALTH_ACC, WORLD_BANK_INC_GRP
@@ -46,14 +44,20 @@ class TestDataAvail(unittest.TestCase):
4644

4745
def test_noaa_nl_pass(self):
4846
"""Test NOAA nightlights used in BlackMarble."""
49-
file_down = download_file(NOAA_SITE + 'F101992.v4.tar')
47+
file_down = download_file(f'{CONFIG.exposures.litpop.nightlights.noaa_url.str()}/F101992.v4.tar')
5048
Path(file_down).unlink()
5149

5250
def test_nasa_nl_pass(self):
5351
"""Test NASA nightlights used in BlackMarble and LitPop."""
54-
url = NASA_SITE + BM_FILENAMES[0]
55-
file_down = download_file(url.replace('*', str(2016)))
56-
Path(file_down).unlink()
52+
req_files = np.zeros(len(BM_FILENAMES))
53+
req_files[0] = 1
54+
year = 2016
55+
dwnl_path = CONFIG.local_data.save_dir.dir()
56+
dwnl_file = dwnl_path.joinpath(BM_FILENAMES[0] % year)
57+
self.assertFalse(dwnl_file.is_file())
58+
download_nl_files(req_files=req_files, dwnl_path=dwnl_path, year=year)
59+
self.assertTrue(dwnl_file.is_file())
60+
dwnl_file.unlink()
5761

5862
def test_wb_wealth_pass(self):
5963
"""Test world bank's wealth data"""

0 commit comments

Comments
 (0)