# Grid shift and computation window This notebook covers two parameters that change *which* grid samples are computed: `grid_shift` applies a global offset to all grid coordinate values, and `win` restricts the output to a sub-region of the full-resolution grid. **What you'll learn** - How `grid_shift` differs from `array_in_origin` - How to apply a global coordinate offset to the grid - How to restrict computation to a window with `win` - How to size output datasets when using a window ## Setting things up We reuse all previously prepared inputs: source raster, grid file, grid mask, source raster mask, and the geometry polygons from notebook 005. ## Shifting the grid coordinates with `grid_shift` `grid_shift` accepts a `(shift_row, shift_col)` tuple of integers or floats and applies it to all grid coordinates **before** any grid geometry computation. This is different from `array_in_origin`, which is used internally by the chain to account for the current tile's origin within the source raster; `array_in_origin` does not affect grid geometry calculations, whereas `grid_shift` does. ```python with rasterio.open(GRID_IN_F64, "r") as grid_in_ds, \ rasterio.open(RASTER_IN, "r") as array_src_ds, \ rasterio.open(grid_mask_in_path, "r") as grid_mask_in_ds, \ rasterio.open(raster_mask_in_path_u8, "r") as raster_mask_in_ds, \ rasterio.open(output_raster_path, "w", **raster_out_open_args) as array_out_ds, \ rasterio.open(output_mask_path, "w", **mask_out_open_args) as mask_out_ds: basic_grid_resampling_chain( grid_ds = grid_in_ds, grid_row_coords_band = 1, grid_col_coords_band = 2, grid_resolution = grid_resolution, array_src_ds = array_src_ds, array_src_bands = 1, array_out_ds = array_out_ds, interp = "cubic", nodata_out = 0, win = None, grid_shift = (50.5, 30.), # <= global coordinate offset mask_out_ds = mask_out_ds, grid_mask_in_ds = grid_mask_in_ds, grid_mask_in_unmasked_value = grid_mask_in_valid_value, grid_mask_in_band = 1, array_src_mask_ds = raster_mask_in_ds, array_src_mask_band = 1, array_src_mask_validity_pair = (array_src_mask_validity_valid, array_src_mask_validity_invalid), array_src_geometry_origin = (0., 0.), array_src_geometry_pair = [geometry_valid, invalid_polygon], ) ``` ![grid_shift_output.png](grid_resampling_chain_006_shift_and_window_files/grid_shift_output.png) The shift is applied uniformly to all grid coordinates; the grid mask pattern itself is unaffected. ## Limiting computation with `win` The `win` parameter restricts the output to a sub-region of the full-resolution grid. Indices are expressed in the full-resolution output coordinate frame, **after** `grid_resolution` oversampling. The output datasets must be sized to match the window. ```python win = np.asarray([[120, 280], [70, 240]]) ``` ![window_input.png](grid_resampling_chain_006_shift_and_window_files/window_input.png) The orange dashed rectangle marks the requested computation window. The output dataset sizes must be adjusted accordingly. ```python raster_out_open_args_win = { "driver": "GTiff", "dtype": np.float64, "height": win[0][1] - win[0][0] + 1, "width": win[1][1] - win[1][0] + 1, "count": 1, } mask_out_open_args_win = { "driver": "GTiff", "dtype": np.uint8, "height": win[0][1] - win[0][0] + 1, "width": win[1][1] - win[1][0] + 1, "count": 1, "nbits": 1, } with rasterio.open(GRID_IN_F64, "r") as grid_in_ds, \ rasterio.open(RASTER_IN, "r") as array_src_ds, \ rasterio.open(grid_mask_in_path, "r") as grid_mask_in_ds, \ rasterio.open(raster_mask_in_path_u8, "r") as raster_mask_in_ds, \ rasterio.open(output_raster_path, "w", **raster_out_open_args_win) as array_out_ds, \ rasterio.open(output_mask_path, "w", **mask_out_open_args_win) as mask_out_ds: basic_grid_resampling_chain( grid_ds = grid_in_ds, grid_row_coords_band = 1, grid_col_coords_band = 2, grid_resolution = grid_resolution, array_src_ds = array_src_ds, array_src_bands = 1, array_out_ds = array_out_ds, interp = "cubic", nodata_out = 0, win = win, # <= restrict to a sub-window mask_out_ds = mask_out_ds, grid_mask_in_ds = grid_mask_in_ds, grid_mask_in_unmasked_value = grid_mask_in_valid_value, grid_mask_in_band = 1, array_src_mask_ds = raster_mask_in_ds, array_src_mask_band = 1, array_src_mask_validity_pair = (array_src_mask_validity_valid, array_src_mask_validity_invalid), array_src_geometry_origin = (0., 0.), array_src_geometry_pair = [geometry_valid, invalid_polygon], ) ``` ![apply_window_output.png](grid_resampling_chain_006_shift_and_window_files/apply_window_output.png)