xarray指南:索引和选择数据 - 删除标签和维度

目录

本文翻译自 xarray 官方文档 Indexing and selecting data 的部分内容。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xarray as xr

drop_sel() 方法将返回一个新对象,该对象列出删除维度的索引标签:

da = xr.DataArray(
    np.random.rand(4, 3),
    [
        ("time", pd.date_range("2000-01-01", periods=4)),
        ("space", ["IA", "IL", "IN"]),
    ],
)
ds = da.to_dataset(name="foo")
ds
<xarray.Dataset>
Dimensions:  (space: 3, time: 4)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA' 'IL' 'IN'
Data variables:
    foo      (time, space) float64 0.7553 0.4042 0.1169 ... 0.004518 0.6259
ds.drop_sel(space=["IN", "IL"])
<xarray.Dataset>
Dimensions:  (space: 1, time: 4)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA'
Data variables:
    foo      (time, space) float64 0.7553 0.2052 0.6426 0.9223

drop_sel 既是 Dataset 方法又是 DataArray 方法。

使用 drop_dims() 从数据集中删除完整维度。 具有这些维度的所有变量也会被删除:

ds.drop_dims("time")
<xarray.Dataset>
Dimensions:  (space: 3)
Coordinates:
  * space    (space) <U2 'IA' 'IL' 'IN'
Data variables:
    *empty*

实战

构建一个包含多个层次的温度场。

from nwpc_data.grib.cfgrib import load_field_from_file
from nwpc_data.data_finder import find_local_file

temp_vars = []
for level in [500, 700, 850, 900]:
    t = load_field_from_file(
        file_path=find_local_file(
            "grapes_gfs_gmf/grib2/orig",
            start_time="2020031800",
            forecast_time="0h"
        ),
        parameter="t",
        level_type="isobaricInhPa",
        level=level,
    )
    temp_vars.append(t)
temp_array = xr.concat(temp_vars, dim="isobaricInhPa")
temp_ds = temp_array.to_dataset(name="t")
temp_ds
<xarray.Dataset>
Dimensions:        (isobaricInhPa: 4, latitude: 720, longitude: 1440)
Coordinates:
    time           datetime64[ns] 2020-03-18
    valid_time     datetime64[ns] 2020-03-18
    step           timedelta64[ns] 00:00:00
  * longitude      (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
  * latitude       (latitude) float64 89.88 89.62 89.38 ... -89.38 -89.62 -89.88
  * isobaricInhPa  (isobaricInhPa) int64 500 700 850 900
Data variables:
    t              (isobaricInhPa, latitude, longitude) float32 232.26372 ... 238.80554
temp_ds.drop_sel(isobaricInhPa=[850, 500])
<xarray.Dataset>
Dimensions:        (isobaricInhPa: 2, latitude: 720, longitude: 1440)
Coordinates:
    time           datetime64[ns] 2020-03-18
    valid_time     datetime64[ns] 2020-03-18
    step           timedelta64[ns] 00:00:00
  * longitude      (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
  * latitude       (latitude) float64 89.88 89.62 89.38 ... -89.38 -89.62 -89.88
  * isobaricInhPa  (isobaricInhPa) int64 700 900
Data variables:
    t              (isobaricInhPa, latitude, longitude) float32 247.0531 ... 238.80554
temp_ds.drop_dims("isobaricInhPa")
<xarray.Dataset>
Dimensions:     (latitude: 720, longitude: 1440)
Coordinates:
    time        datetime64[ns] 2020-03-18
    valid_time  datetime64[ns] 2020-03-18
    step        timedelta64[ns] 00:00:00
  * longitude   (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
  * latitude    (latitude) float64 89.88 89.62 89.38 ... -89.38 -89.62 -89.88
Data variables:
    *empty*

参考

http://xarray.pydata.org/en/stable/indexing.html#dropping-labels-and-dimensions