Skip to content

How to recognise a rectangular grid? #104

@ChrisBarker-NOAA

Description

@ChrisBarker-NOAA

The current code for recognising a rectangular grid is simple, but broken:

    def recognize(ds: xr.Dataset) -> bool:
        """Recognize if the dataset matches the given grid."""
        lat = ds.cf.coordinates.get("latitude", None)
        lon = ds.cf.coordinates.get("longitude", None)
        if lat is None or lon is None:
            return False

        # Make sure the coordinates are 1D and match
        lat_ndim = ds[lat[0]].ndim
        lon_ndim = ds[lon[0]].ndim
        return lat_ndim == lon_ndim and lon_ndim == 1

All this does is:

  • make sure there are a latitude and longitude coordinate -- fair enough
  • make sure that the first of such are 1-D -- god start, but ....

If you run this code in an unstructured grid, it returns True -- not good.

Why?

Because UGRids have a bunch of 1-D lat and long coords:

In [18]: ds.cf.coordinates
Out[18]: 
{'longitude': ['mesh_boundary_lon',
  'mesh_edge_lon',
  'mesh_face_lon',
  'mesh_node_lon'],
 'latitude': ['mesh_boundary_lat',
  'mesh_edge_lat',
  'mesh_face_lat',
  'mesh_node_lat']}

and they are all 1D.

So: what do I test for?

  1. this code looks at the zeroth lat-lon only -- is that OK? in my examples there are only one of each, but would that always be the case?

  2. if above, it seem we should look and see if variables are using both the lat and lon coordinates ...

But this does seem fragile ...

Anyone have a better idea?

@ocefpaf: any thoughts on this?

@davidhassell: Sorry to ling you on this, but I thught you might have some ideas.

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