@@ -50,6 +50,7 @@ def get_topo_colormap():
5050def count_land_ocean_cells (grid , params , reader ):
5151 """
5252 Count how many cells in the ICON grid are land vs ocean based on ETOPO data.
53+ Also computes land fraction for each cell for gradient visualization.
5354
5455 Parameters
5556 ----------
@@ -63,12 +64,14 @@ def count_land_ocean_cells(grid, params, reader):
6364 Returns
6465 -------
6566 tuple
66- (land_count, ocean_count, land_cells, ocean_cells)
67+ (land_count, ocean_count, land_cells, ocean_cells, land_fractions )
6768 land_cells and ocean_cells are lists of cell indices
69+ land_fractions is array of land fraction [0-1] for each cell
6870 """
6971 n_cells = grid .clat .size
7072 land_cells = []
7173 ocean_cells = []
74+ land_fractions = np .zeros (n_cells ) # Store land fraction for each cell
7275
7376 print (f"Checking { n_cells } cells for land/ocean classification..." )
7477
@@ -126,16 +129,23 @@ def count_land_ocean_cells(grid, params, reader):
126129 simplex_lat = tri .tri_lat_verts [tri_idx ]
127130 simplex_lon = tri .tri_lon_verts [tri_idx ]
128131
129- # Check if land
130- if utils .is_land (cell , simplex_lat , simplex_lon , topo ):
132+ # Check if land (binary classification)
133+ is_land_cell = utils .is_land (cell , simplex_lat , simplex_lon , topo )
134+
135+ # Calculate land fraction (fraction of cell with elevation > 0m)
136+ land_points = np .sum (cell .topo > 0.0 )
137+ total_points = cell .topo .size
138+ land_fractions [c_idx ] = land_points / total_points if total_points > 0 else 0.0
139+
140+ if is_land_cell :
131141 land_cells .append (c_idx )
132142 else :
133143 ocean_cells .append (c_idx )
134144
135- return len (land_cells ), len (ocean_cells ), land_cells , ocean_cells
145+ return len (land_cells ), len (ocean_cells ), land_cells , ocean_cells , land_fractions
136146
137147
138- def plot_global_topography_and_grid (grid , params , reader , land_cells , ocean_cells , output_dir ):
148+ def plot_global_topography_and_grid (grid , params , reader , land_cells , ocean_cells , land_fractions , output_dir ):
139149 """
140150 Create plots of global topography and ICON grid.
141151
@@ -151,6 +161,8 @@ def plot_global_topography_and_grid(grid, params, reader, land_cells, ocean_cell
151161 List of land cell indices
152162 ocean_cells : list
153163 List of ocean cell indices
164+ land_fractions : array
165+ Array of land fraction [0-1] for each cell
154166 output_dir : Path
155167 Output directory for plots
156168 """
@@ -163,29 +175,37 @@ def plot_global_topography_and_grid(grid, params, reader, land_cells, ocean_cell
163175 # Create figure with 3 subplots
164176 fig = plt .figure (figsize = (20 , 6 ))
165177
166- # Plot 1: Land/Ocean classification on grid
178+ # Plot 1: Land/Ocean classification on grid with gradient
167179 ax1 = fig .add_subplot (131 , projection = 'mollweide' )
168180
169181 # Convert to Mollweide projection coordinates (radians, but centered at 0)
170182 lon_plot = np .deg2rad (clon_deg )
171183 lon_plot [lon_plot > np .pi ] -= 2 * np .pi # Wrap to [-π, π]
172184 lat_plot = np .deg2rad (clat_deg )
173185
174- # Plot ocean cells in blue
175- if ocean_cells :
176- ax1 .scatter (lon_plot [ocean_cells ], lat_plot [ocean_cells ],
177- c = 'blue' , s = 1 , alpha = 0.5 , label = 'Ocean' )
178-
179- # Plot land cells in green
180- if land_cells :
181- ax1 .scatter (lon_plot [land_cells ], lat_plot [land_cells ],
182- c = 'green' , s = 1 , alpha = 0.5 , label = 'Land' )
186+ # Create a custom colormap from blue (ocean) to green (land)
187+ from matplotlib .colors import LinearSegmentedColormap
188+ colors_gradient = ['#0066cc' , '#3399ff' , '#66ccff' , '#99ff99' , '#66cc66' , '#339933' , '#006600' ]
189+ n_bins = 100
190+ cmap_land_ocean = LinearSegmentedColormap .from_list ('land_ocean' , colors_gradient , N = n_bins )
191+
192+ # Plot all cells with color based on land fraction
193+ scatter = ax1 .scatter (lon_plot , lat_plot ,
194+ c = land_fractions ,
195+ cmap = cmap_land_ocean ,
196+ s = 3 ,
197+ alpha = 0.8 ,
198+ vmin = 0.0 ,
199+ vmax = 1.0 )
200+
201+ # Add colorbar
202+ cbar = plt .colorbar (scatter , ax = ax1 , orientation = 'horizontal' , pad = 0.05 , shrink = 0.6 )
203+ cbar .set_label ('Land Fraction (0=Ocean, 1=Land)' , fontsize = 9 )
183204
184205 ax1 .set_title (f'ICON R2B4 Grid: Land/Ocean Classification\n '
185206 f'Land: { len (land_cells )} cells, Ocean: { len (ocean_cells )} cells\n '
186207 f'Land %: { 100 * len (land_cells )/ (len (land_cells )+ len (ocean_cells )):.2f} %' ,
187208 fontsize = 12 , fontweight = 'bold' )
188- ax1 .legend (loc = 'lower left' )
189209 ax1 .grid (True )
190210
191211 # Plot 2: All grid cells
@@ -250,7 +270,7 @@ def plot_global_topography_and_grid(grid, params, reader, land_cells, ocean_cell
250270
251271 # Count land/ocean cells
252272 print ("\n Counting land/ocean cells..." )
253- land_count , ocean_count , land_cells , ocean_cells = count_land_ocean_cells (
273+ land_count , ocean_count , land_cells , ocean_cells , land_fractions = count_land_ocean_cells (
254274 grid , params , reader
255275 )
256276
@@ -268,7 +288,7 @@ def plot_global_topography_and_grid(grid, params, reader, land_cells, ocean_cell
268288 # Create plots
269289 print ("\n Creating plots..." )
270290 output_dir = Path ("outputs" ) / "verification"
271- plot_global_topography_and_grid (grid , params , reader , land_cells , ocean_cells , output_dir )
291+ plot_global_topography_and_grid (grid , params , reader , land_cells , ocean_cells , land_fractions , output_dir )
272292
273293 # Save plotting data for debugging
274294 print ("\n Saving plotting data for debugging..." )
@@ -286,13 +306,14 @@ def plot_global_topography_and_grid(grid, params, reader, land_cells, ocean_cell
286306 clon_deg = clon_deg ,
287307 land_cells = np .array (land_cells ),
288308 ocean_cells = np .array (ocean_cells ),
309+ land_fractions = land_fractions ,
289310 n_cells = n_cells ,
290311 land_count = land_count ,
291312 ocean_count = ocean_count ,
292313 etopo_cg = params .etopo_cg
293314 )
294315 print (f" Data saved: { data_file } " )
295- print (f" Contains: cell coordinates, land/ocean classifications, and counts" )
316+ print (f" Contains: cell coordinates, land/ocean classifications, land fractions, and counts" )
296317
297318 print ("\n ✓ Verification complete!" )
298319 print (f" Land cells: { land_count } " )
0 commit comments