|
25 | 25 | import affine |
26 | 26 | import numpy as np |
27 | 27 | import scipy.sparse as sparse |
| 28 | +import gzip |
| 29 | +import tifffile |
| 30 | +import io |
28 | 31 |
|
29 | 32 | from shapely.geometry import Polygon |
30 | 33 | from pathlib import Path |
@@ -163,22 +166,34 @@ def test_load_nightlight_noaa(self): |
163 | 166 | """ Test that data is downloaded if not present in SYSTEM_DIR, |
164 | 167 | or not downloaded if present. Test the three outputs of the |
165 | 168 | 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 |
168 | 182 | night, coord_nl, fn_light = nightlight.load_nightlight_noaa() |
169 | 183 | self.assertIsInstance(night, sparse._csr.csr_matrix) |
170 | 184 | 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], |
172 | 186 | [-180, NOAA_RESOLUTION_DEG]]),coord_nl)) |
173 | 187 | 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') |
176 | 191 | 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) |
180 | 195 |
|
181 | | - # Test raises from wrong input agruments |
| 196 | + # test raises from wrong input agruments |
182 | 197 | with self.assertRaises(ValueError) as cm: |
183 | 198 | night, coord_nl, fn_light = nightlight.load_nightlight_noaa( |
184 | 199 | ref_year = 2050, sat_name = 'F150') |
|
0 commit comments