hainegroup / oceanspy

A Python package to facilitate ocean model data analysis and visualization.
https://oceanspy.readthedocs.io
MIT License
98 stars 32 forks source link

grid metrics when first creating the oceandataset #298

Closed Mikejmnez closed 1 year ago

Mikejmnez commented 1 year ago

Description

Should we allow the grid object to be created incorporating the grid metrics?

So far, metrics have not been used to create and open the od object in any of the dataset. You can see this in oceanpy.ospy_utils (lines 15-33):

def _create_grid(dataset, coords, periodic, face_connections):
    """
    Create xgcm grid by adding comodo attributes to the
    dimensions of the dataset.

    Parameters
    ----------
    dataset: xarray.Dataset
    coords: dict
        E.g., {'Y': {Y: None, Yp1: 0.5}}
    periodic: list
        List of periodic axes.
    face_connections: dict
        dictionary specifying grid topology
    Returns
    -------
    grid: xgcm.Grid
    """

and in lines 60-66:

        grid = xgcm.Grid(dataset, periodic=periodic, face_connections=face_connections)

Incorporating grid metrics is advantageous and very simple. From xgcm's documentation:

metrics = {
    ('X',): ['dxC', 'dxG'], # X distances
    ('Y',): ['dyC', 'dyG'], # Y distances
    ('Z',): ['drW', 'drS', 'drC'], # Z distances
    ('X', 'Y'): ['rA', 'rAz', 'rAs', 'rAw'] # Areas
}
grid = Grid(ds, metrics=metrics)

Allowing the metrics to be passed into Grid allow one-line calculation of derivatives, integrals and such. This is:

Mikejmnez commented 1 year ago

I just went through oceanspy.compute and just realized that the suggestion above would require a lot of changes to each function (like curl, integral, weighted mean, etc etc). So I am closing this as I think it is really unnecessary at this point.