Deltares / xugrid

Xarray and unstructured grids
https://deltares.github.io/xugrid/
MIT License
62 stars 8 forks source link

Regridder fails for partitioned dataarray #99

Closed veenstrajelmer closed 1 year ago

veenstrajelmer commented 1 year ago

Regridding a dataset that was merged with merge_partitions raises "ValueError: Dimension 1 has 2 blocks, adjust_chunks specified with 1 blocks". Also, when not subsetting a layer, it raises "TypeError: reduce() of empty sequence with no initial value"

MWE:

import glob
import xugrid as xu

def open_partitioned_dataset(file_nc): 
    file_nc_list = glob.glob(file_nc)[:2] #two partitions are enough to reproduce the behaviour
    partitions = []
    for iF, file_nc_one in enumerate(file_nc_list):
        uds = xu.open_dataset(file_nc_one, chunks={'time':1})
        partitions.append(uds)
    ds_merged_xu = xu.merge_partitions(partitions)
    return ds_merged_xu

nc_reference = r'p:\archivedprojects\11200975-hongkongwaq\WAQ\04_scenarios\S02x_FINAL\DFM_OUTPUT_HK-FMWAQ\HK-FMWAQ_*_map.nc'
nc_scenarios = r'p:\archivedprojects\11200975-hongkongwaq\WAQ\04_scenarios\S04x3_FINAL\DFM_OUTPUT_HK-FMWAQ\HK-FMWAQ_*_map.nc'

# load data
ref_xr    = open_partitioned_dataset(nc_reference)
ref_data  = ref_xr.sel(time=slice('2016-01-01','2016-01-02'))
ref_data = ref_data.isel(mesh2d_nLayers=19) #maintaining layer dimension results in "TypeError: reduce() of empty sequence with no initial value"
ref_data = ref_data['mesh2d_sa1'] # regridding currently only works for uda and not uds: https://github.com/Deltares/xugrid/issues/88
scen_xr   = open_partitioned_dataset(nc_scenarios)

# regrid with xugrid
regridder = xu.CentroidLocatorRegridder(source=ref_data, target=scen_xr)
ref_data_regrid = regridder.regrid(ref_data) # Regridder cannot handle multiple partitions: ValueError: Dimension 1 has 2 blocks, adjust_chunks specified with 1 blocks
SCLaan commented 1 year ago

I did a test with https://github.com/Deltares/xugrid/pull/120 This fix solves my problem in the MWE above.

Huite commented 1 year ago

Thanks for letting us know! There seems to be a single test failing still, but once that's fixed, we'll merge this to main.

JoerivanEngelen commented 1 year ago

Fix is merged