UXARRAY / uxarray

Xarray-styled package for reading and directly operating on unstructured grid datasets following UGRID conventions
https://uxarray.readthedocs.io/
Apache License 2.0
139 stars 30 forks source link

Pass coords to UxDataArray #818

Closed ahijevyc closed 1 week ago

ahijevyc commented 2 weeks ago

Pass along coordinates of UxDataArray when slicing from grid.

Pass self.coords to as coords argument in core/dataarray.py method _slice_from_grid

Otherwise, _slice_from_grid loses coordinate labels (the index values).

I figure if dims, name, and attrs arguments are passed, coords should be too.

I tested uxda.subset.bounding_box(lon_bounds, lat_bounds) on a UxDataArray with and without the pull request. The results are below. The number of Indexes changes from zero to three.

Without passing the coords argument in _slice_from_grid, the result has zero Indexes

xarray.UxDataArray'updraft_helicity_max'valid_time: 24mem: 5initial_time: 2n_face: 19327
array([[[[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.17450754e-04, 0.00000000e+00, 0.00000000e+00],
         [6.97366288e-03, 4.58992086e-03, 6.88771158e-03, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00]],

        [[1.94994651e-03, 2.29698606e-03, 1.26716075e-03, ...,
          6.46118497e-05, 3.72463410e-06, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.02913233e-03, 3.39804428e-05, 8.73764220e-05]],

        [[8.91156844e-04, 1.32420380e-03, 3.09247524e-03, ...,
          1.14122406e-04, 2.35418309e-04, 2.08011316e-03],
         [1.75224006e-04, 1.12577909e-04, 1.69947656e-04, ...,
          2.02424005e-02, 2.01683957e-03, 0.00000000e+00]],

        [[2.08048197e-03, 1.04307907e-03, 8.50136974e-04, ...,
          1.31235253e-02, 1.25350822e-02, 5.65142557e-03],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          3.22981328e-02, 4.79420833e-03, 9.13940486e-04]],

...
        [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [7.28812627e-03, 5.85812889e-03, 1.00370627e-02, ...,
          1.39404610e-02, 4.06184839e-03, 1.46363070e-02]],

        [[6.58893941e-06, 2.95214937e-04, 1.03708780e-05, ...,
          1.32082496e-04, 2.10705548e-05, 0.00000000e+00],
         [4.49749059e-04, 1.13237416e-03, 1.20202790e-03, ...,
          0.00000000e+00, 6.41305960e-05, 0.00000000e+00]],

        [[5.65678965e-06, 1.22046604e-05, 5.65240225e-06, ...,
          8.58512649e-05, 8.82529304e-04, 1.45375612e-04],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          9.32694064e-04, 3.38749215e-03, 1.46793691e-03]],

        [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [4.64940444e-03, 5.64363971e-03, 3.83334141e-03, ...,
          1.32313173e-03, 4.02142177e-04, 7.61992391e-03]]]],
      dtype=float32)
Coordinates: (0)
Indexes: (0)
Attributes:
units :
m^2 s^{-2}
long_name :
Maximum updraft helicity since last output

with coords passed, it has 3 indexes:

xarray.UxDataArray'updraft_helicity_max'valid_time: 24mem: 5initial_time: 2n_face: 19327
array([[[[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.17450754e-04, 0.00000000e+00, 0.00000000e+00],
         [6.97366288e-03, 4.58992086e-03, 6.88771158e-03, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00]],

        [[1.94994651e-03, 2.29698606e-03, 1.26716075e-03, ...,
          6.46118497e-05, 3.72463410e-06, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.02913233e-03, 3.39804428e-05, 8.73764220e-05]],

        [[8.91156844e-04, 1.32420380e-03, 3.09247524e-03, ...,
          1.14122406e-04, 2.35418309e-04, 2.08011316e-03],
         [1.75224006e-04, 1.12577909e-04, 1.69947656e-04, ...,
          2.02424005e-02, 2.01683957e-03, 0.00000000e+00]],

        [[2.08048197e-03, 1.04307907e-03, 8.50136974e-04, ...,
          1.31235253e-02, 1.25350822e-02, 5.65142557e-03],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          3.22981328e-02, 4.79420833e-03, 9.13940486e-04]],

...
        [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [7.28812627e-03, 5.85812889e-03, 1.00370627e-02, ...,
          1.39404610e-02, 4.06184839e-03, 1.46363070e-02]],

        [[6.58893941e-06, 2.95214937e-04, 1.03708780e-05, ...,
          1.32082496e-04, 2.10705548e-05, 0.00000000e+00],
         [4.49749059e-04, 1.13237416e-03, 1.20202790e-03, ...,
          0.00000000e+00, 6.41305960e-05, 0.00000000e+00]],

        [[5.65678965e-06, 1.22046604e-05, 5.65240225e-06, ...,
          8.58512649e-05, 8.82529304e-04, 1.45375612e-04],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          9.32694064e-04, 3.38749215e-03, 1.46793691e-03]],

        [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [4.64940444e-03, 5.64363971e-03, 3.83334141e-03, ...,
          1.32313173e-03, 4.02142177e-04, 7.61992391e-03]]]],
      dtype=float32)
Coordinates: (0)
Indexes: (3)
Attributes:
units :
m^2 s^{-2}
long_name :
Maximum updraft helicity since last output