Skip to content

Commit 15e3904

Browse files
Feature/fix nightlight test (#716)
* fix test_load_nightlight_noaa The test was failing the integration check on jenkins. Create fake .tif.gz file, execute test, delate.tif.gz file. * formatting
1 parent cff2ee7 commit 15e3904

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

climada/test/test_nightlight.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import affine
2626
import numpy as np
2727
import scipy.sparse as sparse
28+
import gzip
29+
import tifffile
30+
import io
2831

2932
from shapely.geometry import Polygon
3033
from pathlib import Path
@@ -163,22 +166,34 @@ def test_load_nightlight_noaa(self):
163166
""" Test that data is downloaded if not present in SYSTEM_DIR,
164167
or not downloaded if present. Test the three outputs of the
165168
function."""
166-
167-
# Using an already existing file and without providing arguments
169+
170+
# path of fake .tif.gz file
171+
path_fake_file = SYSTEM_DIR.joinpath('F182013.v4c_web.stable_lights.avg_vis.tif.gz')
172+
# create an empty image
173+
image = np.zeros((100, 100), dtype=np.uint8)
174+
# save the image as .tif
175+
with io.BytesIO() as mem:
176+
tifffile.imwrite(mem, image)
177+
# compressed image to a gzip file
178+
with gzip.GzipFile(path_fake_file, 'wb') as f:
179+
f.write(mem.getvalue())
180+
181+
# using already existing file and without providing arguments
168182
night, coord_nl, fn_light = nightlight.load_nightlight_noaa()
169183
self.assertIsInstance(night, sparse._csr.csr_matrix)
170184
self.assertIn('F182013.v4c_web.stable_lights.avg_vis.tif',str(fn_light))
171-
self.assertTrue(np.array_equal(np.array([[-65,NOAA_RESOLUTION_DEG],
185+
self.assertTrue(np.array_equal(np.array([[-65, NOAA_RESOLUTION_DEG],
172186
[-180, NOAA_RESOLUTION_DEG]]),coord_nl))
173187
os.remove(SYSTEM_DIR.joinpath('F182013.v4c_web.stable_lights.avg_vis.p'))
174-
# With arguments
175-
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(ref_year = 2010, sat_name = 'F18')
188+
189+
# with arguments
190+
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(ref_year = 2013, sat_name = 'F18')
176191
self.assertIsInstance(night, sparse._csr.csr_matrix)
177-
self.assertIn('F182010.v4d_web.stable_lights.avg_vis', str(fn_light))
178-
file = str(SYSTEM_DIR.joinpath('F182010.v4d_web.stable_lights.avg_vis.p'))
179-
os.remove(file)
192+
self.assertIn('F182013.v4c_web.stable_lights.avg_vis.tif', str(fn_light))
193+
os.remove(SYSTEM_DIR.joinpath('F182013.v4c_web.stable_lights.avg_vis.p'))
194+
os.remove(path_fake_file)
180195

181-
# Test raises from wrong input agruments
196+
# test raises from wrong input agruments
182197
with self.assertRaises(ValueError) as cm:
183198
night, coord_nl, fn_light = nightlight.load_nightlight_noaa(
184199
ref_year = 2050, sat_name = 'F150')

0 commit comments

Comments
 (0)