gpt4 book ai didi

python - 将多个 GeoTIFF 图像的栅格时间序列转换为 NetCDF

转载 作者:行者123 更新时间:2023-12-04 23:38:24 27 4
gpt4 key购买 nike

我有一个存储在多个 GeoTIFF 中的栅格时间序列我想转换为单个 *.tif 的文件 ( NetCDF )文件。数据是uint16 .

我可能可以使用 gdal_translate使用以下方法将每个图像转换为 netcdf:

 gdal_translate -of netcdf -co FORMAT=NC4 20150520_0164.tif foo.nc

然后用 NCO 编写一些脚本从文件名中提取日期然后连接,但我想知道我是否可以使用 xarray 在 Python 中更有效地执行此操作它是新的 rasterio后端。

我可以轻松读取文件:
import glob
import xarray as xr
f = glob.glob('*.tif')
da = xr.open_rasterio(f[0])
da

返回
<xarray.DataArray (band: 1, y: 5490, x: 5490)>
[30140100 values with dtype=uint16]
Coordinates:
* band (band) int64 1
* y (y) float64 5e+05 5e+05 5e+05 5e+05 5e+05 4.999e+05 4.999e+05 ...
* x (x) float64 8e+05 8e+05 8e+05 8e+05 8.001e+05 8.001e+05 ...
Attributes:
crs: +init=epsg:32620

我可以将其中之一写入 NetCDF:
ds.to_netcdf('foo.nc')

但理想情况下,我可以使用类似 xr.open_mfdataset 的东西,写入时间值(从文件名中提取),然后将整个聚合写入 netCDF .并有 dask处理核外内存问题。 :-)

可以用 xarray 做这样的事情吗?和 dask ?

最佳答案

Xarray 应该能够为您完成 concat 步骤。我在下面稍微调整了你的例子。将文件名解析为有用的内容取决于您。

import glob
import pandas as pd
import xarray as xr

def time_index_from_filenames(filenames):
'''helper function to create a pandas DatetimeIndex
Filename example: 20150520_0164.tif'''
return pd.DatetimeIndex([pd.Timestamp(f[:8]) for f in filenames])

filenames = glob.glob('*.tif')
time = xr.Variable('time', time_index_from_filenames(filenames))
chunks = {'x': 5490, 'y': 5490, 'band': 1}
da = xr.concat([xr.open_rasterio(f, chunks=chunks) for f in filenames], dim=time)

关于python - 将多个 GeoTIFF 图像的栅格时间序列转换为 NetCDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com