Skip to content

Commit 2e3ed82

Browse files
fix test_nightlight.test_load_nightlight_noaa interference
1 parent f95b267 commit 2e3ed82

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

climada/entity/exposures/litpop/nightlight.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ def load_nightlight_noaa(ref_year=2013, sat_name=None):
543543
str(ref_year) + '*.stable_lights.avg_vis'))
544544
# check if file exists in SYSTEM_DIR, download if not
545545
if glob.glob(fn_light + ".p"):
546-
fn_light = glob.glob(fn_light + ".p")[0]
546+
fn_light = sorted(glob.glob(fn_light + ".p"))[0]
547547
with open(fn_light, 'rb') as f_nl:
548548
nightlight = pickle.load(f_nl)
549549
elif glob.glob(fn_light + ".tif.gz"):
550-
fn_light = glob.glob(fn_light + ".tif.gz")[0]
550+
fn_light = sorted(glob.glob(fn_light + ".tif.gz"))[0]
551551
fn_light, nightlight = unzip_tif_to_py(fn_light)
552552
else:
553553
# iterate over all satellites if no satellite name provided

climada/test/test_nightlight.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,29 +185,30 @@ def test_load_nightlight_noaa(self):
185185
# compressed image to a gzip file
186186
with gzip.GzipFile(SYSTEM_DIR.joinpath(gzfile), 'wb') as f:
187187
f.write(mem.getvalue())
188-
SYSTEM_DIR.joinpath(pfile).unlink(missing_ok=True)
189-
190-
# using already existing file and without providing arguments
191-
night, coord_nl, fn_light = nightlight.load_nightlight_noaa()
192-
self.assertIsInstance(night, sparse._csr.csr_matrix)
193-
self.assertIn(tiffile, str(fn_light))
194-
self.assertTrue(np.array_equal(np.array([[-65, NOAA_RESOLUTION_DEG],
195-
[-180, NOAA_RESOLUTION_DEG]]),coord_nl))
196-
SYSTEM_DIR.joinpath(pfile).unlink()
197-
198-
# with arguments
199-
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(ref_year=year, sat_name=sat_name)
200-
self.assertIsInstance(night, sparse._csr.csr_matrix)
201-
self.assertIn(tiffile, str(fn_light))
202-
SYSTEM_DIR.joinpath(pfile).unlink()
203-
SYSTEM_DIR.joinpath(gzfile).unlink()
204-
205-
# test raises from wrong input agruments
206-
with self.assertRaises(ValueError) as cm:
207-
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(
208-
ref_year=2050, sat_name='F150')
209-
self.assertEqual('Nightlight intensities for year 2050 and satellite F150 do not exist.',
210-
str(cm.exception))
188+
189+
try:
190+
# with arguments
191+
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(ref_year=year, sat_name=sat_name)
192+
self.assertIsInstance(night, sparse._csr.csr_matrix)
193+
self.assertIn(tiffile, str(fn_light))
194+
195+
# using already existing file and without providing arguments
196+
night, coord_nl, fn_light = nightlight.load_nightlight_noaa()
197+
self.assertIsInstance(night, sparse._csr.csr_matrix)
198+
self.assertIn(pfile, str(fn_light))
199+
self.assertTrue(np.array_equal(np.array([[-65, NOAA_RESOLUTION_DEG],
200+
[-180, NOAA_RESOLUTION_DEG]]),coord_nl))
201+
202+
# test raises from wrong input agruments
203+
with self.assertRaises(ValueError) as cm:
204+
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(
205+
ref_year=2050, sat_name='F150')
206+
self.assertEqual('Nightlight intensities for year 2050 and satellite F150 do not exist.',
207+
str(cm.exception))
208+
finally:
209+
# clean up
210+
SYSTEM_DIR.joinpath(pfile).unlink(missing_ok=True)
211+
SYSTEM_DIR.joinpath(gzfile).unlink(missing_ok=True)
211212

212213
def test_untar_noaa_stable_nighlight(self):
213214
""" Testing that input .tar file is moved into SYSTEM_DIR,

0 commit comments

Comments
 (0)