Selecting variables

Selecting is the action of filtering the dataset by its second dimension (variables).

select

If you pass a list, the resulting dataset contains the requested variables in the order given by the list. Here, 2t comes first and tp second, regardless of their order in the original dataset:

ds = open_dataset(dataset, select=["2t", "tp"])

If you pass a set, the resulting dataset contains the same variables, but their original order in the dataset is preserved (sets are unordered, so they cannot impose an order):

ds = open_dataset(dataset, select={"2t", "tp"})

drop

You can also drop some variables:

ds = open_dataset(dataset, drop=["10u", "10v"])

reorder

and reorder them:

… using a list:

ds = open_dataset(
    dataset,
    reorder=["2t", "msl", "sp", "10u", "10v"],
)

… or using a dictionary:

ds = open_dataset(
    dataset,
    reorder={
        "2t": 0,
        "msl": 1,
        "sp": 2,
        "10u": 3,
        "10v": 4,
    },
)

rename

You can also rename variables:

ds = open_dataset(dataset, rename={"2t": "t2m"})

This will be useful when you join datasets and do not want variables from one dataset to override the ones from the other.

number

If a dataset is an ensemble, you can select one or more specific members using the number option. See Number Protocol in the Selecting members section for details.

rescale

When combining datasets, you may want to rescale the variables so that they have matching units. This can be done with the rescale option:

# Scale and offset can be passed as a dictionary...

ds = open_dataset(
    dataset,
    rescale={"2t": {"scale": 1.0, "offset": -273.15}},
)

# ... a tuple of floating points ...

ds = open_dataset(
    dataset,
    rescale={"2t": (1.0, -273.15)},
)

# ... or a tuple of strings representing units.

ds = open_dataset(
    dataset,
    rescale={"2t": ("K", "degC")},
)

# Several variables can be rescaled at once.

ds = open_dataset(
    dataset,
    rescale={
        "2t": ("K", "degC"),
        "tp": ("m", "mm"),
    },
)

The rescale option will also rescale the statistics. The rescaling is currently limited to simple linear conversions.

When provided with units, the rescale option uses the cfunits package to find the scale and offset attributes of the units and uses these to rescale the data.

Warning

When providing units, the library assumes that the mapping between them is a linear transformation. No check is done to ensure this is the case.