I am working with spatial and flat data using xarray. The spatial data out_grid.FID looks like:
<xarray.DataArray 'FID' (y: 200, x: 261)>
array([[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]])
Coordinates:
* y (y) float64 1.717e+06 1.717e+06 ... 1.715e+06 1.715e+06
* x (x) float64 -1.02e+06 -1.02e+06 ... -1.017e+06 -1.017e+06
spatial_ref int32 0
Attributes:
name: FID
long_name: FID
_FillValue: nan
The FID values in the grid are identifiers I need to relate to a flat dataset data.band_data:
<xarray.DataArray 'band_data' (band: 1, y: 8784, x: 351)>
[3083184 values with dtype=float32]
Coordinates:
* band (band) int32 1
* x (x) float64 0.5 1.5 2.5 3.5 4.5 ... 347.5 348.5 349.5 350.5
* y (y) float64 0.5 1.5 2.5 3.5 ... 8.782e+03 8.782e+03 8.784e+03
spatial_ref int32 ...
Attributes:
results_Subbasin-1_ATI_coordinates: /external_variables/Subbasin-1:::/...
results_Subbasin-1_ATI_dss_data_type: PER-AVER
results_Subbasin-1_ATI_missing_value: -3.4028234663852886e+38
results_Subbasin-1_ATI_units: DEG C
where the positional index of x dimension in the flat data corresponds to the FID identifier in the spatial dataset.
My idea is to create a lookup dictionary mapDict I want to map each value in mapDict to each value in the out_grid.FID data array.
grid_structure = out_grid.FID.to_pandas()
_a = xr.DataArray(coords = out_grid.FID.coords, name = k)
data = xr.open_dataset(v, engine = 'rasterio')
for i in range(data.band_data.shape[1]):
flatData = list(data.band_data.isel(y=i).data.squeeze())
posIndex = [float(i+1) for i in range(len(flatData))]
mapDict = dict(zip(posIndex, flatData))
new_grid = grid_structure.stack().map(mapDict).unstack()
tmp = xr.DataArray(data = new_grid, coords = out_grid.FID.coords, attrs = {'parameter':k}, name = k)
_a = xr.concat([_a, tmp], 'time')
Is there a better approach?
