wmi*_*l93 3 python python-xarray rasterio
我有大约 75 个完全相同区域的高程二维栅格图 (tif),每个都是在不同时间获取的。我想使用 来堆叠这些xarray。我可以读取每个栅格(见下文),但目前没有时间坐标,因为我需要从每个文件的标题中提取时间(下面的文件中的 2017-02-15T06:13:38Z)。
da = xr.open_rasterio('tifs/DTSLOS_20170122_20190828_D79H_2017-02-15T06:13:38Z.tif')
da
<xarray.DataArray (y: 12284, x: 17633)>
[216603772 values with dtype=float64]
Coordinates:
band int64 1
* y (y) float64 59.62 59.62 59.62 59.62 59.62 ... 49.8 49.8 49.8 49.8
* x (x) float64 -12.17 -12.17 -12.17 -12.17 ... 1.931 1.932 1.932 1.933
Attributes:
transform: (0.0008, 0.0, -12.172852, 0.0, -0.0008, 59.623425)
crs: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,2...
res: (0.0008, 0.0008)
is_tiled: 1
nodatavals: (-9999.0,)
Run Code Online (Sandbox Code Playgroud)
我假设我应该解决这个问题的方法是向每个数据数组添加时间,然后堆叠/连接它们,但我是 xarray 的新手,正在努力弄清楚如何做到这一点。
小智 5
您需要使用datetime.strptimetime将相应的字符串转换为日期时间时间字符串,并将其设置为要组合数据集的维度。您还需要扩展此维度,因此在使用时xr.combine_by_coords您可以沿该维度组合数据数组。一种方法是
import xarray as xr
from datetime import datetime
import pandas as pd
#collecting datasets when looping over your files
list_da = []
for path in ...:
#path = "tifs/DTSLOS_20170122_20190828_D79H_2017-02-15T06:13:38Z.tif"
da = xr.open_rasterio(path)
time = path.split("_")[-1].split("Z")[0]
dt = datetime.strptime(time,"%Y-%m-%dT%H:%M:%S")
dt = pd.to_datetime(dt)
da = da.assign_coords(time = dt)
da = da.expand_dims(dim="time")
list_da.append(da)
#stack dataarrays in list
ds = xr.combine_by_coords(list_da)
Run Code Online (Sandbox Code Playgroud)
这就是我处理数据的方式。不确定这是否是最优雅的解决方案,但它对我有用
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |