Ken*_*enC 5 python raster satellite-image rasterio xarray
我想获取栅格(卫星图像)数据,并构建一个DatasetorDataArray来加速我的图像处理(我必须经常处理多波段、多日期的卫星图像)。
数据是每个图像日期的单独波段,我了解如何将每个波段日期转换为 xarray- DataArray。我认为每个波段都有一个变量是最有意义的,并且在每个波段内都有空间 (x, y) 和时间维度。
但是,我无法弄清楚如何做到这一点。
我一直在与一些虚拟乐队合作试图解决这个问题,因此将包括在内以澄清我的数据是什么样子以及我正在尝试做什么。
# Set up dummy 3 x 3 array
dA = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Create 4 dummy images; 2 bands for each of 2 dates (using bands 4 and 5,
# because they're useful for vegetation measures)
d1_b4 = xr.DataArray((dA + 140),
coords={'x': ['1', '2', '3'], 'y': ['a', 'b', 'c']}, dims=('x', 'y'))
d1_b5 = xr.DataArray((dA + 150),
coords={'x': ['1', '2', '3'], 'y': ['a', 'b', 'c']}, dims=('x', 'y'))
d2_b4 = xr.DataArray((dA + 240),
coords={'x': ['1', '2', '3'], 'y': ['a', 'b', 'c']}, dims=('x', 'y'))
d2_b5 = xr.DataArray((dA + 250),
coords={'x': ['1', '2', '3'], 'y': ['a', 'b', 'c']}, dims=('x', 'y'))
# dummy values designed so I can keep track of which array is going
# where while I learn this
Run Code Online (Sandbox Code Playgroud)
然后我想将这些组合成一个DataArray,有两个变量(Band4 和 Band5),每个变量都包含两个图像日期......但不知道如何进行。
创建/导入数组时是否需要添加更多坐标或维度,然后concat沿着这些维度添加?
正如 jhamman 所提到的,很大程度上取决于数据的来源来确定如何组合数据。这是组合您所提供的数据的一种方法,但还有其他方法。
合并这些数据需要多个步骤。DataArrays首先,用您希望其最终出现的变量的名称来命名每个。
d1_b4.name = 'band4'
d1_b5.name = 'band5'
d2_b4.name = 'band4'
d2_b5.name = 'band5'
Run Code Online (Sandbox Code Playgroud)
然后使用xr.merge将它们放入xarray.Datasets 中。ADataset包含多个xarray.DataArrays,它们可以共享部分或全部维度。
d1 = xr.merge([d1_b4, d1_b5])
d2 = xr.merge([d2_b4, d2_b5])
<xarray.Dataset>
Dimensions: (x: 3, y: 3)
Coordinates:
* x (x) <U1 '1' '2' '3'
* y (y) <U1 'a' 'b' 'c'
Data variables:
band4 (x, y) int64 241 242 243 244 245 246 247 248 249
band5 (x, y) int64 251 252 253 254 255 256 257 258 259
Run Code Online (Sandbox Code Playgroud)
最后,合并不同日期的数据。我们需要一个新的维度time,其中包含每个日期的坐标值。我们可以使用 一步完成此操作xr.concat。
xr.concat([d1, d2], dim=pd.Index([1990, 1991], name='time'))
<xarray.Dataset>
Dimensions: (time: 2, x: 3, y: 3)
Coordinates:
* x (x) <U1 '1' '2' '3'
* y (y) <U1 'a' 'b' 'c'
* time (time) int64 1990 1991
Data variables:
band4 (time, x, y) int64 141 142 143 144 145 146 147 148 149 241 242 ...
band5 (time, x, y) int64 151 152 153 154 155 156 157 158 159 251 252 ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1269 次 |
| 最近记录: |