Skip to content

Commit ea2ca28

Browse files
committed
Backup of first investigation
1 parent 1c197f1 commit ea2ca28

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

python/extpar_art_to_buffer.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import netCDF4 as nc
44
import numpy as np
5+
from time import perf_counter
56

67
from joblib import Parallel, delayed, dump, load
78
from tqdm import tqdm
@@ -95,6 +96,81 @@ def get_fraction_per_soil_type(lu):
9596
return fracs
9697

9798

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+
98174
# --------------------------------------------------------------------------
99175
# --------------------------------------------------------------------------
100176
# initialize logger
@@ -171,6 +247,22 @@ def get_fraction_per_soil_type(lu):
171247
lons = np.array(tg.lons)
172248
lats = np.array(tg.lats)
173249

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+
174266
# --------------------------------------------------------------------------
175267
# --------------------------------------------------------------------------
176268
logging.info("")
@@ -199,9 +291,11 @@ def get_fraction_per_soil_type(lu):
199291
logging.info("")
200292
logging.info("============= Calculate LU Fraction for target grid ========")
201293
logging.info("")
202-
294+
start=perf_counter()
203295
fracs = calculate_soil_fraction(tg, soil_types, neighbor_ids, ncpu=2)
296+
end=perf_counter()
204297

298+
print("Elapsed time:", end-start)
205299
#--------------------------------------------------------------------------
206300
#--------------------------------------------------------------------------
207301
logging.info('')
@@ -271,3 +365,4 @@ def get_fraction_per_soil_type(lu):
271365
logging.info('')
272366
logging.info('============= extpar_art_to_buffer done =======')
273367
logging.info('')
368+

python/lib/grid_def.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ def __init__(self, gridfile):
260260
self.lons = np.rad2deg(self.grid.variables["clon"][:])
261261
self.lats = np.rad2deg(self.grid.variables["clat"][:])
262262

263+
self.vlons = np.rad2deg(self.grid.variables["vlon"][:])
264+
self.vlats = np.rad2deg(self.grid.variables["vlat"][:])
265+
263266
def cdo_sellonlat(self):
264267
'''
265268
create the string for the cdo option "-sellonlatbox"

0 commit comments

Comments
 (0)