Skip to content

Commit 0e26e4a

Browse files
committed
Added final optimized version
1 parent 02b23ee commit 0e26e4a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

python/extpar_art_to_buffer.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def generate_memory_map(raw_lus, soiltype_memmap_filename,
6969
return lus, idxs
7070

7171

72+
<<<<<<< Updated upstream
7273
def calculate_soil_fraction(tg, lus, idxs, ncpu=2):
7374
"""
7475
lus: LU classes from HWSD data
@@ -100,17 +101,22 @@ def calculate_soil_fraction_optimized(target_grid,
100101
soil_types_raw,
101102
nearest_target_cell_to_raw_cells,
102103
ncpu=2):
104+
=======
105+
def calculate_soil_fraction(target_grid, soil_types_raw, nearest_target_cell_to_raw_cells, ncpu=13):
106+
>>>>>>> Stashed changes
103107
"""
104108
target_grid: target ICON grid
105109
soil_types_raw: landuse class for each cell from the HWSD dataset (LU variable)
106-
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)
110+
nearest_target_cell_to_raw_cells: indices of the cell from the target ICON grid which is nearest to each cell of the raw grid (from HWSD dataset)
107111
"""
108112
ncells_target = target_grid.lons.size
109113
nsoil_types = 13
114+
nthreads = min(nsoil_types, ncpu)
110115

111116
soil_ids = np.arange(1, nsoil_types + 1)
112117
soil_fractions_target = np.zeros((ncells_target, nsoil_types))
113118

119+
<<<<<<< Updated upstream
114120
target_cells, n_nearest_raw_cells = np.unique(
115121
nearest_target_cell_to_raw_cells, return_counts=True)
116122

@@ -120,9 +126,19 @@ def calculate_soil_fraction_optimized(target_grid,
120126
np.where(soil_types_raw == soil_id,
121127
nearest_target_cell_to_raw_cells, -1),
122128
return_counts=True)
129+
=======
130+
n_nearest_raw_cells = np.bincount(nearest_target_cell_to_raw_cells.ravel(), minlength=ncells_target)
123131

124-
for target_cell_id in np.arange(ncells_target):
132+
def get_fraction_per_soil_type(soil_id):
133+
n_nearest_raw_cells_with_soil_type = np.bincount(nearest_target_cell_to_raw_cells[soil_types_raw == soil_id], minlength=ncells_target)
134+
>>>>>>> Stashed changes
125135

136+
np.divide( n_nearest_raw_cells_with_soil_type,
137+
n_nearest_raw_cells,
138+
out = soil_fractions_target[:, soil_id-1],
139+
where = n_nearest_raw_cells != 0 )
140+
141+
<<<<<<< Updated upstream
126142
soil_fraction = np.array(
127143
n_nearest_raw_cells_with_soil_type[target_cells_with_soil_type
128144
== target_cell_id] /
@@ -131,10 +147,18 @@ def calculate_soil_fraction_optimized(target_grid,
131147
if len(soil_fraction) != 0:
132148
soil_fractions_target[target_cell_id,
133149
soil_id - 1] = soil_fraction
150+
=======
151+
Parallel(n_jobs=nthreads,
152+
max_nbytes='100M',
153+
mmap_mode='w+',
154+
backend='threading')(delayed(get_fraction_per_soil_type)(soil_id)
155+
for soil_id in tqdm(soil_ids))
156+
>>>>>>> Stashed changes
134157

135158
return soil_fractions_target
136159

137160

161+
<<<<<<< Updated upstream
138162
def calculate_soil_fraction_test(tg, lus, idxs, ncpu=2):
139163
"""
140164
lus: LU classes from HWSD data
@@ -182,6 +206,8 @@ def get_fraction_per_soil_type(lu):
182206
return fracs
183207

184208

209+
=======
210+
>>>>>>> Stashed changes
185211
# --------------------------------------------------------------------------
186212
# --------------------------------------------------------------------------
187213
# initialize logger

0 commit comments

Comments
 (0)