Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions spicy_snow/processing/generate_dataarrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# faster reprojection utils
import rasterio
import rioxarray
from rasterio.warp import reproject, Resampling, transform_bounds
from rasterio.warp import reproject, Resampling
from rasterio.enums import Resampling as Rsmp
from rasterio.transform import rowcol

Expand Down Expand Up @@ -343,16 +343,13 @@ def generate_forest_fraction_dataarray(aoi, ref = None) -> xr.Dataset:
# clip to the target area BEFORE any computation, otherwise the
# full raster gets loaded into memory and the process is killed.
fcf = rioxarray.open_rasterio(fcf_path).squeeze(drop=True)
# Build the clip box in EPSG:4326 (FCF native CRS). Prefer the
# reprojection target's bounds when available for the tightest crop;
# fall back to the AOI bounds.
if ref is not None:
xmin, ymin, xmax, ymax = transform_bounds(
ref.rio.crs, "EPSG:4326", *ref.rio.bounds()
)
else:
xmin, ymin, xmax, ymax = aoi.bounds
# Clip in EPSG:4326 (FCF native CRS) using AOI bounds. The ref grid
# is also in EPSG:4326 and clipped to aoi.bounds in
# generate_reference_grid, so using ref.rio.bounds() here was
# equivalent but vulnerable to pixel-edge / projection round-trip
# quirks that produced degenerate windows on some AOIs.
# Small halo so reproject_match has neighboring pixels available.
xmin, ymin, xmax, ymax = aoi.bounds
buf = 0.05 # ~5 km in degrees
fcf = fcf.rio.clip_box(
minx=xmin - buf, miny=ymin - buf, maxx=xmax + buf, maxy=ymax + buf
Expand Down
Loading