|
3 | 3 | """ |
4 | 4 | import unittest |
5 | 5 | import numpy as np |
| 6 | +from scipy import sparse |
| 7 | + |
6 | 8 | from climada.entity.exposures import nightlight |
7 | 9 | from climada.util.constants import SYSTEM_DIR |
8 | 10 |
|
@@ -71,6 +73,121 @@ def test_download_nightlight_files(self): |
71 | 73 |
|
72 | 74 | # The same length but not the correct length |
73 | 75 | self.assertRaises(ValueError, nightlight.download_nl_files, (1, 0, 1), (1, 1, 1)) |
| 76 | + |
| 77 | + def test_cut_nl_nasa_1_pass(self): |
| 78 | + """Test cut_nl_nasa situation 2->3->4->5.""" |
| 79 | + nl_mat = sparse.lil.lil_matrix([]) |
| 80 | + in_lat = (21599, 21600) |
| 81 | + in_lon = (43199, 43200) |
| 82 | + # 0 2 4 6 (lat: Upper=0) (lon: 0, 1, 2, 3) |
| 83 | + # 1 3 5 7 (lat: Lower=1) (lon: 0, 1, 2, 3) |
| 84 | + in_lat_nb = (1, 0) |
| 85 | + in_lon_nb = (1, 2) |
| 86 | + |
| 87 | + idx_info = [2, -1, False] |
| 88 | + aux_nl = np.zeros((21600, 21600)) |
| 89 | + aux_nl[21599, 21599] = 100 |
| 90 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 91 | + in_lon, in_lat_nb, in_lon_nb) |
| 92 | + |
| 93 | + self.assertEqual(nl_mat.shape, (1, 1)) |
| 94 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 95 | + |
| 96 | + idx_info[0] = 3 |
| 97 | + idx_info[1] = 2 |
| 98 | + aux_nl = np.zeros((21600, 21600)) |
| 99 | + aux_nl[0, 21599] = 101 |
| 100 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 101 | + in_lon, in_lat_nb, in_lon_nb) |
| 102 | + |
| 103 | + self.assertEqual(nl_mat.shape, (2, 1)) |
| 104 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 105 | + self.assertEqual(nl_mat.tocsr()[1, 0], 101.0) |
| 106 | + |
| 107 | + idx_info[0] = 4 |
| 108 | + idx_info[1] = 3 |
| 109 | + aux_nl = np.zeros((21600, 21600)) |
| 110 | + aux_nl[21599, 0] = 102 |
| 111 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 112 | + in_lon, in_lat_nb, in_lon_nb) |
| 113 | + |
| 114 | + self.assertEqual(nl_mat.shape, (2, 2)) |
| 115 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 116 | + self.assertEqual(nl_mat.tocsr()[1, 0], 101.0) |
| 117 | + self.assertEqual(nl_mat.tocsr()[0, 1], 102.0) |
| 118 | + |
| 119 | + idx_info[0] = 5 |
| 120 | + idx_info[1] = 4 |
| 121 | + aux_nl = np.zeros((21600, 21600)) |
| 122 | + aux_nl[0, 0] = 103 |
| 123 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 124 | + in_lon, in_lat_nb, in_lon_nb) |
| 125 | + |
| 126 | + self.assertEqual(nl_mat.shape, (2, 2)) |
| 127 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 128 | + self.assertEqual(nl_mat.tocsr()[1, 0], 101.0) |
| 129 | + self.assertEqual(nl_mat.tocsr()[0, 1], 102.0) |
| 130 | + self.assertEqual(nl_mat.tocsr()[1, 1], 103.0) |
| 131 | + |
| 132 | + def test_cut_nl_nasa_2_pass(self): |
| 133 | + """Test cut_nl_nasa situation 3->5.""" |
| 134 | + nl_mat = sparse.lil.lil_matrix([]) |
| 135 | + in_lat = (21599, 21599) |
| 136 | + in_lon = (43199, 43200) |
| 137 | + # 0 2 4 6 (lat: Upper=0) (lon: 0, 1, 2, 3) |
| 138 | + # 1 3 5 7 (lat: Lower=1) (lon: 0, 1, 2, 3) |
| 139 | + in_lat_nb = (1, 1) |
| 140 | + in_lon_nb = (1, 2) |
| 141 | + |
| 142 | + idx_info = [3, -1, False] |
| 143 | + aux_nl = np.zeros((21600, 21600)) |
| 144 | + aux_nl[0, 21599] = 100 |
| 145 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 146 | + in_lon, in_lat_nb, in_lon_nb) |
| 147 | + |
| 148 | + self.assertEqual(nl_mat.shape, (1, 1)) |
| 149 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 150 | + |
| 151 | + idx_info[0] = 5 |
| 152 | + idx_info[1] = 3 |
| 153 | + aux_nl = np.zeros((21600, 21600)) |
| 154 | + aux_nl[0, 0] = 101 |
| 155 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 156 | + in_lon, in_lat_nb, in_lon_nb) |
| 157 | + |
| 158 | + self.assertEqual(nl_mat.shape, (1, 2)) |
| 159 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 160 | + self.assertEqual(nl_mat.tocsr()[0, 1], 101.0) |
| 161 | + |
| 162 | + def test_cut_nl_nasa_3_pass(self): |
| 163 | + """Test cut_nl_nasa situation 2->4.""" |
| 164 | + nl_mat = sparse.lil.lil_matrix([]) |
| 165 | + in_lat = (21600, 21600) |
| 166 | + in_lon = (43199, 43200) |
| 167 | + # 0 2 4 6 (lat: Upper=0) (lon: 0, 1, 2, 3) |
| 168 | + # 1 3 5 7 (lat: Lower=1) (lon: 0, 1, 2, 3) |
| 169 | + in_lat_nb = (0, 0) |
| 170 | + in_lon_nb = (1, 2) |
| 171 | + |
| 172 | + idx_info = [2, -1, False] |
| 173 | + aux_nl = np.zeros((21600, 21600)) |
| 174 | + aux_nl[21599, 21599] = 100 |
| 175 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 176 | + in_lon, in_lat_nb, in_lon_nb) |
| 177 | + |
| 178 | + self.assertEqual(nl_mat.shape, (1, 1)) |
| 179 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 180 | + |
| 181 | + idx_info[0] = 4 |
| 182 | + idx_info[1] = 2 |
| 183 | + aux_nl = np.zeros((21600, 21600)) |
| 184 | + aux_nl[21599, 0] = 101 |
| 185 | + nightlight.cut_nl_nasa(aux_nl, idx_info, nl_mat, in_lat, |
| 186 | + in_lon, in_lat_nb, in_lon_nb) |
| 187 | + |
| 188 | + self.assertEqual(nl_mat.shape, (1, 2)) |
| 189 | + self.assertEqual(nl_mat.tocsr()[0, 0], 100.0) |
| 190 | + self.assertEqual(nl_mat.tocsr()[0, 1], 101.0) |
74 | 191 |
|
75 | 192 | # Execute Tests |
76 | 193 | TESTS = unittest.TestLoader().loadTestsFromTestCase(TestNightLight) |
|
0 commit comments