Deltares / MeshKernelPy

A Python Wrapper for the MeshKernel
https://deltares.github.io/MeshKernelPy/
MIT License
22 stars 4 forks source link

mismatch edge order xugrid and meshkernel #72

Closed veenstrajelmer closed 3 months ago

veenstrajelmer commented 1 year ago

Describe the bug The ordering of the edge-orthogonality array seems different than the ordering of the edges in the xugrid.UgridDataset. Would be convenient if they are aligned so we can easily plot these type of values.

To Reproduce

from meshkernel import MeshKernel, ProjectionType, MakeGridParameters
import xarray as xr
import dfm_tools as dfmt

#general settings
lon_min,lon_max = -1,-0.2
lat_min,lat_max = 49.1,49.6
lon_res,lat_res = 0.1,0.1

figsize = (10,4)
crs = 'EPSG:4326'

# Create an instance of MakeGridParameters and set the values
make_grid_parameters = MakeGridParameters()
make_grid_parameters.angle = 0.0
make_grid_parameters.origin_x = lon_min
make_grid_parameters.origin_y = lat_min
make_grid_parameters.upper_right_x = lon_max
make_grid_parameters.upper_right_y = lat_max
make_grid_parameters.block_size_x = lon_res
make_grid_parameters.block_size_y = lat_res

mk2 = MeshKernel(projection=ProjectionType.SPHERICAL)
mk2.curvilinear_compute_rectangular_grid_on_extension(make_grid_parameters)
mk2.curvilinear_convert_to_mesh2d()

mesh2d_grid2 = mk2.mesh2d_get()

uds = dfmt.meshkernel_to_UgridDataset(mk2, crs=crs)

aa = mk2.mesh2d_get_orthogonality()
uds['ortho'] = xr.DataArray(aa.values,dims=(uds.grid.edge_dimension))
uds['ortho'] = uds['ortho'].where(uds['ortho']!=-999) #TODO: hardcoded -999, better to have nans in this array already
uds['ortho'].ugrid.plot() #TODO: ordering of the orthogonality array does not seem to align with the edge-order in the uds

Gives: image

Expected behavior The missing values are now scattered around trough the grid, but they probably belong to the outer edges of the grid. Not sure if this is a meshkernel issue or a xugrid issue.

Version info (please complete the following information):

Huite commented 1 year ago

This is an issue in xugrid indeed. In the from_meshkernel method, the edge_nodes from the meshkernel aren't used. Consequently, xugrid generates its own edge_node_connectivity, which doesn't match the order of the orthogonality array.

I'll make a change to use the edge_nodes from the meshkernel object.

veenstrajelmer commented 3 months ago

This will be fixed in xugrid after https://github.com/Deltares/xugrid/pull/250 is merged.