|
2 | 2 | import logging |
3 | 3 | import netCDF4 as nc |
4 | 4 | import numpy as np |
| 5 | +from time import perf_counter |
5 | 6 |
|
6 | 7 | from joblib import Parallel, delayed, dump, load |
7 | 8 | from tqdm import tqdm |
@@ -95,6 +96,81 @@ def get_fraction_per_soil_type(lu): |
95 | 96 | return fracs |
96 | 97 |
|
97 | 98 |
|
| 99 | +def calculate_soil_fraction_optimized(target_grid, soil_types_raw, nearest_target_cell_to_raw_cells, ncpu=2): |
| 100 | + """ |
| 101 | + target_grid: target ICON grid |
| 102 | + soil_types_raw: landuse class for each cell from the HWSD dataset (LU variable) |
| 103 | + nearest_target_cell_to_raw_cell: indices of the cell from the target ICON grid which is nearest to each cell of the raw grid (from HWSD dataset) |
| 104 | + """ |
| 105 | + ncells_target = target_grid.lons.size |
| 106 | + nsoil_types = 13 |
| 107 | + |
| 108 | + soil_ids = np.arange(1, nsoil_types+1) |
| 109 | + soil_fractions_target = np.zeros((ncells_target, nsoil_types)) |
| 110 | + |
| 111 | + target_cells, n_nearest_raw_cells = np.unique(nearest_target_cell_to_raw_cells, return_counts=True) |
| 112 | + |
| 113 | + for soil_id in tqdm(soil_ids[:1]): |
| 114 | + |
| 115 | + target_cells_with_soil_type, n_nearest_raw_cells_with_soil_type = np.unique(np.where(soil_types_raw == soil_id, nearest_target_cell_to_raw_cells, -1), return_counts=True) |
| 116 | + |
| 117 | + for target_cell_id in np.arange(ncells_target): |
| 118 | + |
| 119 | + soil_fraction = np.array(n_nearest_raw_cells_with_soil_type[target_cells_with_soil_type == target_cell_id] / n_nearest_raw_cells[target_cells == target_cell_id]) |
| 120 | + |
| 121 | + if len(soil_fraction) != 0: |
| 122 | + soil_fractions_target[target_cell_id, soil_id - 1] = soil_fraction |
| 123 | + |
| 124 | + return soil_fractions_target |
| 125 | + |
| 126 | + |
| 127 | +def calculate_soil_fraction_test(tg, lus, idxs, ncpu=2): |
| 128 | + """ |
| 129 | + lus: LU classes from HWSD data |
| 130 | + idxs: indices corrsponding to icon grid for each grid in HWSD data |
| 131 | + tg: ICON grid |
| 132 | + """ |
| 133 | + soil_types = np.arange(1, 14) |
| 134 | + fracs = np.zeros((tg.lons.size, soil_types.size)) |
| 135 | + grid_ids, grid_counts = np.unique(idxs, return_counts=True) |
| 136 | + print("soil_types:", soil_types.shape) |
| 137 | + print("fracs:", fracs.shape) |
| 138 | + print("grid_ids:", grid_ids.shape) |
| 139 | + print("grid_counts:", grid_counts.shape) |
| 140 | + |
| 141 | + def get_fraction_per_soil_type(lu): |
| 142 | + print("lus:", lus.shape) |
| 143 | + print("idxs:", idxs.shape) |
| 144 | + test=np.where(lus == lu, idxs, -1) |
| 145 | + print("test:", test.shape) |
| 146 | + start=perf_counter() |
| 147 | + grid_class, grid_count = np.unique(np.where(lus == lu, idxs, -1), |
| 148 | + return_counts=True) |
| 149 | + end=perf_counter() |
| 150 | + print("grid_class:", grid_class.shape) |
| 151 | + print("grid_count:", grid_count.shape) |
| 152 | + print("Sect 1:", end-start) |
| 153 | + start=perf_counter() |
| 154 | + for grid_id in grid_class: |
| 155 | + frac = np.array(grid_count[grid_class == grid_id] / |
| 156 | + grid_counts[grid_ids == grid_id]) |
| 157 | + if len(frac) != 0: |
| 158 | + fracs[grid_id, lu - 1] = frac |
| 159 | + end=perf_counter() |
| 160 | + print("Sect 2:", end-start) |
| 161 | + |
| 162 | + #Parallel(n_jobs=13, |
| 163 | + # max_nbytes='100M', |
| 164 | + # mmap_mode='w+', |
| 165 | + # backend='threading')(delayed(get_fraction_per_soil_type)(lu) |
| 166 | + # for lu in tqdm(soil_types)) |
| 167 | + |
| 168 | + for lu in tqdm(soil_types[:1]): |
| 169 | + get_fraction_per_soil_type(lu) |
| 170 | + |
| 171 | + return fracs |
| 172 | + |
| 173 | + |
98 | 174 | # -------------------------------------------------------------------------- |
99 | 175 | # -------------------------------------------------------------------------- |
100 | 176 | # initialize logger |
@@ -171,6 +247,22 @@ def get_fraction_per_soil_type(lu): |
171 | 247 | lons = np.array(tg.lons) |
172 | 248 | lats = np.array(tg.lats) |
173 | 249 |
|
| 250 | +vlons = np.array(tg.vlons) |
| 251 | +vlats = np.array(tg.vlats) |
| 252 | + |
| 253 | +lon_min = np.min(vlons) |
| 254 | +lon_max = np.max(vlons) |
| 255 | +lat_min = np.min(vlats) |
| 256 | +lat_max = np.max(vlats) |
| 257 | + |
| 258 | +lon_mask = (raw_lon >= lon_min) & (raw_lon <= lon_max) |
| 259 | +lat_mask = (raw_lat >= lat_min) & (raw_lat <= lat_max) |
| 260 | + |
| 261 | +raw_lon = raw_lon[lon_mask] |
| 262 | +raw_lat = raw_lat[lat_mask] |
| 263 | +raw_lus = raw_lus[np.ix_(lat_mask, lon_mask)] |
| 264 | + |
| 265 | + |
174 | 266 | # -------------------------------------------------------------------------- |
175 | 267 | # -------------------------------------------------------------------------- |
176 | 268 | logging.info("") |
@@ -199,9 +291,11 @@ def get_fraction_per_soil_type(lu): |
199 | 291 | logging.info("") |
200 | 292 | logging.info("============= Calculate LU Fraction for target grid ========") |
201 | 293 | logging.info("") |
202 | | - |
| 294 | +start=perf_counter() |
203 | 295 | fracs = calculate_soil_fraction(tg, soil_types, neighbor_ids, ncpu=2) |
| 296 | +end=perf_counter() |
204 | 297 |
|
| 298 | +print("Elapsed time:", end-start) |
205 | 299 | #-------------------------------------------------------------------------- |
206 | 300 | #-------------------------------------------------------------------------- |
207 | 301 | logging.info('') |
@@ -271,3 +365,4 @@ def get_fraction_per_soil_type(lu): |
271 | 365 | logging.info('') |
272 | 366 | logging.info('============= extpar_art_to_buffer done =======') |
273 | 367 | logging.info('') |
| 368 | + |
0 commit comments