簡體   English   中英

Xarray:使用維度名稱切片緯度經度

[英]Xarray: slicing latitude longitude using dimension name

我正在編寫一個程序,該程序將打開 Meteorological NetCDF 數據,將其分割為給定區域,然后進行一些計算,例如:

data =xr.open_dataset(SomeFile)

SlicedData = data.sel(lat=slice(max_lat,min_lat),
                lon=slice(min_lon,max_lon))
ExampleResult = SlicedData.mslp.average('lon')

下面是一個如何構建“數據”的示例:

<xarray.Dataset>
Dimensions:  (lon: 144, lat: 37, level: 17, time: 25)
Coordinates:
  * lon      (lon) float32 -180.0 -177.5 -175.0 -172.5 ... 172.5 175.0 177.5
  * lat      (lat) float32 0.0 -2.5 -5.0 -7.5 -10.0 ... -82.5 -85.0 -87.5 -90.0
  * level    (level) float32 1e+03 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0
  * time     (time) datetime64[ns] 2011-05-25 2011-05-25T06:00:00 ... 2011-05-31
Data variables:
    air      (time, level, lat, lon) float32 ...
    hgt      (time, level, lat, lon) float32 ...
    rhum     (time, level, lat, lon) float32 ...
    omega    (time, level, lat, lon) float32 ...
    uwnd     (time, level, lat, lon) float32 ...
    vwnd     (time, level, lat, lon) float32 ...
    mslp     (time, lat, lon) float32 ... 

但是,不同的數據源使用不同的索引器存儲緯度和經度坐標:例如,它可以是緯度/經度或緯度/經度。 因此,例如,如果使用的索引器是緯度/經度,則如下:

SlicedData = data.sel(lat=slice(max_lat,min_lat),
                    lon=slice(min_lon,max_lon))

將返回:

KeyError: 'lat is not a valid dimension or coordinate' 

有沒有一種方法可以使用 Xarray 使用坐標索引器的字符串對數據進行切片? 例如:

LatIndexer, LonIndexer = 'lat', 'lon'
SlicedData = data.sel(LatIndexer=slice(max_lat,min_lat),
                        LonIndexer=slice(min_lon,max_lon))

只需通過字典:

LatIndexer, LonIndexer = 'lat', 'lon'
SliceData = data.sel(**{LatIndexer: slice(max_lat, min_lat),
                        LonIndexer: slice(max_lon, min_lon)})

或者

LatIndexer, LonIndexer = 'lat', 'lon'
SliceData = data.loc[{LatIndexer: slice(max_lat, min_lat),
                      LonIndexer: slice(max_lon, min_lon)}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM