'Turn 1d-indexed xarray variables into 3D coordinates

I have an xarray.Dataset that looks roughly like this:

<xarray.Dataset>
Dimensions:           (index: 286720)
Coordinates:
  * index             (index) int64 0 1 2 3 4 ... 286716 286717 286718 286719
Data variables:
    Time              (index) float64 2.525 2.525 2.525 ... 9.475 9.475 9.475
    ch                (index) int64 1 1 1 1 1 1 1 1 1 1 ... 2 2 2 2 2 2 2 2 2 2
    pixel             (index) int64 1 2 3 4 5 6 ... 1020 1021 1022 1023 1024
    Rough_wavelength  (index) float64 2.698 2.701 2.704 ... 32.05 32.05 32.06
    Count             (index) int64 463 197 265 335 305 ... 285 376 278 0 278

There are only 140 unique values for the Time variable, 2 for the ch(...annel), and 1024 for the pixel value. I'd thus like to turn them into coordinates and completely drop the largely irrelevant index coordinate, something like this:

<xarray.Dataset>
Dimensions:           (Time: 140, ch: 2, pixel: 1024)
Coordinates:
    Time              (time) float64 2.525 ... 9.475
    ch                (ch) int64 1 2
    pixel             (pixel) int64 1 2 3 4 5 6 ... 1020 1021 1022 1023 1024
Data variables:
    Rough_wavelength  (time, ch, pixel) float64 2.698  ... 32.06
    Count             (time, ch, pixel) int64 463  ...  278

Is there a way to do this using xarray? If not, what's a sane way to do this using the standard numpy stack?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source