@@ -96,7 +96,10 @@ def get_fraction_per_soil_type(lu):
9696 return fracs
9797
9898
99- def calculate_soil_fraction_optimized (target_grid , soil_types_raw , nearest_target_cell_to_raw_cells , ncpu = 2 ):
99+ def calculate_soil_fraction_optimized (target_grid ,
100+ soil_types_raw ,
101+ nearest_target_cell_to_raw_cells ,
102+ ncpu = 2 ):
100103 """
101104 target_grid: target ICON grid
102105 soil_types_raw: landuse class for each cell from the HWSD dataset (LU variable)
@@ -105,21 +108,29 @@ def calculate_soil_fraction_optimized(target_grid, soil_types_raw, nearest_targe
105108 ncells_target = target_grid .lons .size
106109 nsoil_types = 13
107110
108- soil_ids = np .arange (1 , nsoil_types + 1 )
111+ soil_ids = np .arange (1 , nsoil_types + 1 )
109112 soil_fractions_target = np .zeros ((ncells_target , nsoil_types ))
110113
111- target_cells , n_nearest_raw_cells = np .unique (nearest_target_cell_to_raw_cells , return_counts = True )
114+ target_cells , n_nearest_raw_cells = np .unique (
115+ nearest_target_cell_to_raw_cells , return_counts = True )
112116
113117 for soil_id in tqdm (soil_ids [:1 ]):
114118
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 )
119+ target_cells_with_soil_type , n_nearest_raw_cells_with_soil_type = np .unique (
120+ np .where (soil_types_raw == soil_id ,
121+ nearest_target_cell_to_raw_cells , - 1 ),
122+ return_counts = True )
116123
117124 for target_cell_id in np .arange (ncells_target ):
118125
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 ])
126+ soil_fraction = np .array (
127+ n_nearest_raw_cells_with_soil_type [target_cells_with_soil_type
128+ == target_cell_id ] /
129+ n_nearest_raw_cells [target_cells == target_cell_id ])
120130
121131 if len (soil_fraction ) != 0 :
122- soil_fractions_target [target_cell_id , soil_id - 1 ] = soil_fraction
132+ soil_fractions_target [target_cell_id ,
133+ soil_id - 1 ] = soil_fraction
123134
124135 return soil_fractions_target
125136
@@ -141,30 +152,30 @@ def calculate_soil_fraction_test(tg, lus, idxs, ncpu=2):
141152 def get_fraction_per_soil_type (lu ):
142153 print ("lus:" , lus .shape )
143154 print ("idxs:" , idxs .shape )
144- test = np .where (lus == lu , idxs , - 1 )
155+ test = np .where (lus == lu , idxs , - 1 )
145156 print ("test:" , test .shape )
146- start = perf_counter ()
157+ start = perf_counter ()
147158 grid_class , grid_count = np .unique (np .where (lus == lu , idxs , - 1 ),
148159 return_counts = True )
149- end = perf_counter ()
160+ end = perf_counter ()
150161 print ("grid_class:" , grid_class .shape )
151162 print ("grid_count:" , grid_count .shape )
152- print ("Sect 1:" , end - start )
153- start = perf_counter ()
163+ print ("Sect 1:" , end - start )
164+ start = perf_counter ()
154165 for grid_id in grid_class :
155166 frac = np .array (grid_count [grid_class == grid_id ] /
156167 grid_counts [grid_ids == grid_id ])
157168 if len (frac ) != 0 :
158169 fracs [grid_id , lu - 1 ] = frac
159- end = perf_counter ()
160- print ("Sect 2:" , end - start )
170+ end = perf_counter ()
171+ print ("Sect 2:" , end - start )
161172
162173 #Parallel(n_jobs=13,
163174 # max_nbytes='100M',
164175 # mmap_mode='w+',
165176 # backend='threading')(delayed(get_fraction_per_soil_type)(lu)
166177 # for lu in tqdm(soil_types))
167-
178+
168179 for lu in tqdm (soil_types [:1 ]):
169180 get_fraction_per_soil_type (lu )
170181
@@ -262,7 +273,6 @@ def get_fraction_per_soil_type(lu):
262273raw_lat = raw_lat [lat_mask ]
263274raw_lus = raw_lus [np .ix_ (lat_mask , lon_mask )]
264275
265-
266276# --------------------------------------------------------------------------
267277# --------------------------------------------------------------------------
268278logging .info ("" )
@@ -291,11 +301,11 @@ def get_fraction_per_soil_type(lu):
291301logging .info ("" )
292302logging .info ("============= Calculate LU Fraction for target grid ========" )
293303logging .info ("" )
294- start = perf_counter ()
304+ start = perf_counter ()
295305fracs = calculate_soil_fraction (tg , soil_types , neighbor_ids , ncpu = 2 )
296- end = perf_counter ()
306+ end = perf_counter ()
297307
298- print ("Elapsed time:" , end - start )
308+ print ("Elapsed time:" , end - start )
299309#--------------------------------------------------------------------------
300310#--------------------------------------------------------------------------
301311logging .info ('' )
@@ -365,4 +375,3 @@ def get_fraction_per_soil_type(lu):
365375logging .info ('' )
366376logging .info ('============= extpar_art_to_buffer done =======' )
367377logging .info ('' )
368-
0 commit comments