@@ -233,6 +233,42 @@ def test_mean_binning_save_index(mock_save, test_datasets, temp_dir):
233233 assert kwargs ["location" ] == temp_dir
234234
235235
236+ def test_mean_binning_with_dict_index (test_datasets ):
237+ """Test that mean_binning uses provided dictionary index directly."""
238+ binning = Binning (
239+ ds_high = test_datasets ["ds_high" ], ds_low = test_datasets ["ds_low" ], var_name = "sla"
240+ )
241+
242+ # Create a custom binning index dictionary
243+ # This maps low-res grid points (i,j) to high-res grid point indices
244+ custom_index = {
245+ (0 , 0 ): [(0 , 0 ), (0 , 1 ), (1 , 0 )], # Map low-res point (0,0) to 3 high-res points
246+ (1 , 1 ): [(2 , 2 ), (2 , 3 ), (3 , 2 )], # Map low-res point (1,1) to 3 high-res points
247+ (2 , 2 ): [(4 , 4 ), (4 , 5 ), (5 , 4 )], # Map low-res point (2,2) to 3 high-res points
248+ }
249+
250+ # Use the custom index directly
251+ result = binning .mean_binning (precomputed_binning_index = custom_index )
252+
253+ # Check result properties
254+ assert isinstance (result , xr .DataArray )
255+ assert result .name == "sla"
256+ assert result .shape == (5 , 5 , 5 ) # (time, lat, lon)
257+
258+ # Check that only the specified points have values (not NaN)
259+ # The custom index only maps 3 points, so only those should have values
260+ non_nan_mask = ~ np .isnan (result .values [0 , :, :]) # Check first time step
261+ assert non_nan_mask [0 , 0 ] # Point (0,0) should have value
262+ assert non_nan_mask [1 , 1 ] # Point (1,1) should have value
263+ assert non_nan_mask [2 , 2 ] # Point (2,2) should have value
264+
265+ # Verify that the values are computed correctly as means
266+ # For example, point (0,0) should be the mean of high-res points (0,0), (0,1), (1,0)
267+ high_data = test_datasets ["ds_high" ]["sla" ].values
268+ expected_value_00 = np .mean ([high_data [0 , 0 , 0 ], high_data [0 , 0 , 1 ], high_data [0 , 1 , 0 ]])
269+ assert np .isclose (result .values [0 , 0 , 0 ], expected_value_00 , rtol = 1e-10 )
270+
271+
236272@patch ("map_binning.binning.load" )
237273def test_mean_binning_load_index (mock_load , test_datasets , temp_dir ):
238274 """Test that mean_binning loads precomputed index when specified."""
0 commit comments