def spatial_aggregation(target_dataset, lon_min, lon_max, lat_min, lat_max):
    """ Spatially subset a dataset within the given longitude and latitude boundaryd_lon-grid_space, grid_lon+grid_space
    :param target_dataset: Dataset object that needs spatial subsetting
    :type target_dataset: Open Climate Workbench Dataset Object
    :param lon_min: minimum longitude (western boundary)
    :type lon_min: float
    :param lon_max: maximum longitude (eastern boundary)
    :type lon_min: float
    :param lat_min: minimum latitude (southern boundary) 
    :type lat_min: float
    :param lat_min: maximum latitude (northern boundary) 
    :type lat_min: float
    :returns: A new spatially subset Dataset
    :rtype: Open Climate Workbench Dataset Object
    """

    if target_dataset.lons.ndim == 1 and target_dataset.lats.ndim == 1:
        new_lon, new_lat = np.meshgrid(target_dataset.lons, target_dataset.lats)
    elif target_dataset.lons.ndim == 2 and target_dataset.lats.ndim == 2:
        new_lon = target_datasets.lons
        new_lat = target_datasets.lats
 
    y_index, x_index = np.where((new_lon >= lon_min) & (new_lon <= lon_max) & (new_lat >= lat_min) & (new_lat <= lat_max))[0:2]

    #new_dataset = ds.Dataset(target_dataset.lats[y_index.min():y_index.max()+1],
    #                         target_dataset.lons[x_index.min():x_index.max()+1],
    #                         target_dataset.times,
    #                         target_dataset.values[:,y_index.min():y_index.max()+1,x_index.min():x_index.max()+1],
    #                         target_dataset.variable,
    #                         target_dataset.name) 
    return target_dataset.values[:,y_index.min():y_index.max()+1,x_index.min():x_index.max()+1]
dataset/ETHPy150Open apache/climate/RCMES/statistical_downscaling/run_statistical_downscaling.py/spatial_aggregation/original