Skip to content

Interoperability with xgcm and xcape #1360

Open

Description

@jthielen

Following the Pangeo gitter, I just stumbled upon the xcape project: https://github.com/xgcm/xcape. I haven't looked too much into it, but being able to interoperate well with it (after #1353 is in place) may be useful in part towards MetPy's goals of efficient gridded convective parameter calculations (e.g., #480, #1258).

Also, while we're at it, xgcm brings a lot of useful functionality to the table (and is more mature of a project than xcape), so it would also be useful to make sure that MetPy's accessors and calculations can also play nicely with it. Perhaps a future goal could even be leveraging xgcm's derivatives, integration, and interpolation when supplied an xgcm Grid?

Activity

  1. jthielen commented on Feb 9, 2022

    @jthielen
    CollaboratorAuthor

    Here's a motivating example for interoperability with xgcm from #1389:


    I have not used metpy.calc before. Since the results of the derivative calculation is defined at the same grid point as the input, I just expected the result by metpy would be different from that by xgcm.

    But I do would like a try. However, I am a little confused by the difference and usage between xarray and metpy.xarray. Given u and v as two xarray.DataArrays on a stagged grid over the globe:

    In [62]: print(u); print(v)
    <xarray.DataArray 'UVEL' (YC: 720, XG: 1440)>
    dask.array<getitem, shape=(720, 1440), dtype=float32, chunksize=(720, 1440), chunktype=numpy.ndarray>
    Coordinates:
      * YC       (YC) >f4 -89.75 -89.5 -89.25 -89.0 -88.75 ... 89.25 89.5 89.75 90.0
        iter     int32 dask.array<chunksize=(), meta=np.ndarray>
        time     int64 0
        crs      object Projection: latitude_longitude
      * XG       (XG) >f4 0.0 0.25 0.5 0.75 1.0 ... 358.75 359.0 359.25 359.5 359.75
        dyG      (YC, XG) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        dxC      (YC, XG) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        rAw      (YC, XG) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        hFacW    (YC, XG) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        maskW    (YC, XG) bool dask.array<chunksize=(720, 1440), meta=np.ndarray>
    Attributes:
        standard_name:  UVEL
        long_name:      Zonal Component of Velocity (m/s)
        units:          m/s
        mate:           VVEL
    
    <xarray.DataArray 'VVEL' (YG: 720, XC: 1440)>
    dask.array<getitem, shape=(720, 1440), dtype=float32, chunksize=(720, 1440), chunktype=numpy.ndarray>
    Coordinates:
      * XC       (XC) >f4 0.125 0.375 0.625 0.875 ... 359.375 359.625 359.875
        iter     int32 dask.array<chunksize=(), meta=np.ndarray>
        time     int64 0
        crs      object Projection: latitude_longitude
      * YG       (YG) >f4 -89.875 -89.625 -89.375 -89.125 ... 89.375 89.625 89.875
        dxG      (YG, XC) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        dyC      (YG, XC) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        rAs      (YG, XC) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        hFacS    (YG, XC) >f4 dask.array<chunksize=(720, 1440), meta=np.ndarray>
        maskS    (YG, XC) bool dask.array<chunksize=(720, 1440), meta=np.ndarray>
    Attributes:
        standard_name:  VVEL
        long_name:      Meridional Component of Velocity (m/s)
        units:          m/s
        mate:           UVEL

    how to calculate divergence using 'metpy' function? I tried:

    dx, dy = mpcalc.lat_lon_grid_deltas(u.metpy.x, v.metpy.y)
    
    div = mpcalc.divergence(u, v, dx, dy)

    following @kgoebber but not sure if dx and dy are calculated properly, as the coordinates for u and v are slightly different. Besides, how to convert div back to xarray.DataArray. I am currently only farmiliar with xarray...

    Originally posted by @miniufo in #1389 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: CalcPertains to calculationsArea: XarrayPertains to xarray integrationType: FeatureNew functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions