簡體   English   中英

將多個GeoTIFF圖像的柵格時間序列轉換為NetCDF

[英]Convert raster time series of multiple GeoTIFF images to NetCDF

我有一個柵格時間序列存儲在多個GeoTIFF文件( *.tif )中,我想將其轉換為單個NetCDF文件。 數據為uint16

我可能可以使用gdal_translate使用gdal_translate命令將每個圖像轉換為netcdf:

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

然后使用NCO進行腳本編寫,以從文件名中提取日期,然后進行連接,但是我想知道是否可以使用xarray及其新的rasterio后端在Python中更有效地執行此操作。

我可以輕松讀取文件:

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類的xr.open_mfdataset ,寫入時間值(從文件名中提取),然后將整個聚合寫入netCDF dask處理核心外的內存問題。 :-)

可以用xarraydask完成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)

暫無
暫無

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

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