xarray.dataset: re-shuffle variables (not coordinates) so that they appear in certain order with print or ncdump

Viewed 26

I was wondering if there exists the possibility of re-shuffling variables in an xarray.dataset...

I have a dataset where I've been appending variables following a certain order given by a certain process... Then, when I print the dataset in a Python console (or when executing ncdump -h output.nc I get that same order, but I wish to get variables in another order.

What I get:

<xarray.Dataset>
Dimensions:                                                  (stations: 6,
                                                              Rrs_bands: 110,
                                                              Rrs_S3A_OLCI_bands: 21)
Coordinates:
  * stations                                                 (stations) <U16 ...
  * Rrs_bands                                                (Rrs_bands) float64 ...
  * Rrs_S3A_OLCI_bands                                       (Rrs_S3A_OLCI_bands) float64 ...
Data variables: (12/69)
    Rrs                                                      (stations, Rrs_bands) float64 ...
    acquisition_time                                         (stations) object ...
    lon                                                      (stations) float64 ...
    lat                                                      (stations) float64 ...
    depth                                                    (stations) float64 ...
    Rrs_qa                                                   (stations) float64 ...
    ...                                                       ...
    total_column_formaldehyde                                (stations) float64 ...
    total_column_hydrogen_peroxide                           (stations) float64 ...
    total_column_hydroxyl_radical                            (stations) float64 ...
    total_column_nitric_acid                                 (stations) float64 ...
    total_column_peroxyacetyl_nitrate                        (stations) float64 ...
    Rrs_S3A_OLCI                                             (stations, Rrs_S3A_OLCI_bands) float64 ...
Attributes:
    description:    In situ database file (IDB) for insitu set Geneva (in sit...

What I want:

<xarray.Dataset>
Dimensions:                                                  (stations: 6,
                                                              Rrs_bands: 110,
                                                              Rrs_S3A_OLCI_bands: 21)
Coordinates:
  * stations                                                 (stations) <U16 ...
  * Rrs_bands                                                (Rrs_bands) float64 ...
  * Rrs_S3A_OLCI_bands                                       (Rrs_S3A_OLCI_bands) float64 ...
Data variables: (12/69)
    acquisition_time                                         (stations) object ...
    lon                                                      (stations) float64 ...
    lat                                                      (stations) float64 ...
    depth                                                    (stations) float64 ...
    ...                                                       ...
    total_column_formaldehyde                                (stations) float64 ...
    total_column_hydrogen_peroxide                           (stations) float64 ...
    total_column_hydroxyl_radical                            (stations) float64 ...
    total_column_nitric_acid                                 (stations) float64 ...
    total_column_peroxyacetyl_nitrate                        (stations) float64 ...
    Rrs                                                      (stations, Rrs_bands) float64 ...
    Rrs_qa                                                   (stations) float64 ...
    Rrs_S3A_OLCI                                             (stations, Rrs_S3A_OLCI_bands) float64 ...
Attributes:
    description:    In situ database file (IDB) for insitu set Geneva (in sit...

Is there an easy way of re-organizing this listing when printing or doing ncdump?

Thanks :)

1 Answers

yep!

ds = ds[list_of_variables_in_desired_order]

Datasets are built on (ordered) dictionaries of data variables. If you create a new dataset object with keys in the desired order, the result will be a copy which preserves the order in which you pass the variables.

To specify the order of coordinates and variables at the same time, pass the coordinates as well:

ds = ds[
    list_of_coordinates_in_desired_order
    + list_of_variables_in_desired_order
]

So in your case:

ds = ds[[
    "stations",
    "Rrs_bands",
    "Rrs_S3A_OLCI_bands",
    "acquisition_time",
    "lon",
    "lat",
    "depth",
    ..., # complete the list of other variables
    "total_column_formaldehyde",
    "total_column_hydrogen_peroxide",
    "total_column_hydroxyl_radical",
    "total_column_nitric_acid",
    "total_column_peroxyacetyl_nitrate",
    "Rrs",
    "Rrs_qa",
    "Rrs_S3A_OLCI",
]]

See the API docs for xr.Dataset.__getitem__:

Dataset.__getitem__(key) Access variables or coordinates of this dataset as a DataArray or a subset of variables or a indexed dataset.

Indexing with a list of names will return a new Dataset object.

Related