Skip to content

Feature request: call grids by name #390

@veenstrajelmer

Description

@veenstrajelmer

In the process of preventing .grid calls in dfm_tools in Deltares/dfm_tools#363, I was writing the following code to get the grid by name:

import xugrid as xu
def get_grid_by_name(uds, gridname):
    gridnames = uds.ugrid.names
    if gridname not in gridnames:
        raise ValueError(f"gridname '{gridname}' not present in grid, available are: {gridnames}")
    idx_grid = gridnames.index(gridname)
    grid = uds.grids[idx_grid]
    return grid

file_nc = r'p:\archivedprojects\11209233-mad09a-2023-rijn\C_Work\02_lange_duikers\02_berekeningen\dflowfm2d-rijn-j19_6-v2a\computations\hist\hw2018_kort\v15\results\converted_RIJN_0002_map.nc'
uds = xu.open_dataset(file_nc)

grid2d = get_grid_by_name(uds, gridname="mesh2d")
gridnonexistent = get_grid_by_name(uds, gridname="typo")

It would be useful if one could call the grid by their name instead of the index in xugrid instead (for instance in uds.ugrid.get_grid_by_name()), could this be considered as a feature request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions