JiaweiZhuang / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
273 stars 48 forks source link

Issue xe.regridder(); cannot reshape array of size A*B*C*D into shape (A, B, C, D) #134

Open ElianVdb opened 1 year ago

ElianVdb commented 1 year ago

Hello, I have an issue working with the xesmf regridding tool. I am trying to regrid curvilinear POP (parrellel-ocean-program) onto a rectilinear (0.5x0.5) grid. The package xesmf is loaded into python without any issues. The POP-data is defined gx1v3 grid (384 x 320) (384 latitude indeces and 320 longitude Indices). See the figure below to get an idea on how this grid looks like. Grid_POP

To give an idea of my code, first I load the data (in this case, I am interested in the salinity field with dimensions (time, lat, lon)).

**file = "DATA_CESM_SALT_BUDGET/SALT_BUDGET_0_50.nc" File_open = xr.open_dataset(file, decode_times=False)

Load Salt-content

SALT_CESM = np.array(File_open.variables['SALT_INT'])

Load lon-lat-mesh coordinates

file = 'B.E.13.B1850C5.ne30g16.sehires38.003.sunway.pop.h.0001-01.nc' File_open = xr.open_dataset(file, decode_times = False) lon_CESM_Mesh = np.array(File_open.variables['TLONG']) lon_CESM_Mesh[lon_CESM_Mesh>180] = lon_CESM_Mesh[lon_CESM_Mesh>180] - 360 lat_CESM_Mesh = np.array(File_open.variables['TLAT'])**

After the data is loaded, I transform the SALT_CESM file, to a proper xarray file that can be used by the xesmf utilities.

data_xr = xr.DataArray(data = SALT_CESM, coords=dict(latitude=(['latitude', 'longitude'], lat_CESM_Mesh), longitude=(['latitude', 'longitude'], lon_CESM_Mesh), time=('time', time)), dims=["time", "latitude", "longitude"], attrs=dict(key = "SALT", description="Upper layer salinity.", units="g/kg", ))

Then I define a regridder with the xesmf module. The rectilinear grid I want to redefine the salinity data on has resolution 0.5°x0.5°.

ds_out = xe.util.grid_global(0.5, 0.5) regridder = xe.Regridder(data_xr, ds_out, 'bilinear', periodic = True) dr_out = regridder(data_xr)

The last line dr_out = regridder(data_xr) gives the error, attached in the word document below. ValueError Traceback.docx

The final line of the error reads; 'cannot reshape array of size 31850496000 into shape (360, 720, 384, 320)', which is odd because 360,720384*320 = 31850496000 . I therefore believe that this error is somehow related to the sparse matrix format the matrices are in. Furthermore, this problem does not occur when trying to regrid to e.g. a (4°, 5°)-grid, as in the example (https://xesmf.readthedocs.io/en/latest/notebooks/Curvilinear_grid.html).

For reference purposes, I add the two necessary files to run this code (2GB file!);

https://we.tl/t-uq6umtExXd

Any help would be greatly appreciated!!!

syryquil commented 11 months ago

I am also having this exact issue.

DominikStiller commented 4 months ago

I also have this exact issue when regridding POP ocean model output (curvilinear 1°) to rectilinear 1°. This does not occur when I regrid to rectilinear 2°.

anhp95 commented 4 months ago

Any updates on this?

UDTIAN commented 4 months ago

use the xarray.interp to do the interporation

DominikStiller commented 4 months ago

use the xarray.interp to do the interporation

As far as I can tell xr.interp only works with rectilinear grids, not with POP's curvilinear gx1v3 grid.

anhp95 commented 4 months ago

As suggested by @nilsleh, a temporary solution can be found here: #315