Skip to content

Commit b4b6827

Browse files
committed
backup
1 parent e9641ef commit b4b6827

4 files changed

Lines changed: 792 additions & 740 deletions

File tree

xcube_resampling/affine.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
import numpy as np
2727
import xarray as xr
2828
from dask_image import ndinterp
29+
import pyproj
2930

3031
from .constants import (
3132
AffineTransformMatrix,
3233
AggFunction,
3334
FillValues,
3435
FloatInt,
3536
PreventNaNPropagations,
37+
SCALE_LIMIT,
3638
SpatialAggMethods,
3739
SpatialInterpMethodInt,
3840
SpatialInterpMethods,
@@ -44,6 +46,7 @@
4446
_get_prevent_nan_propagation,
4547
_get_spatial_agg_method,
4648
_get_spatial_interp_method_int,
49+
_prep_spatial_interp_methods_downscale,
4750
_select_variables,
4851
normalize_grid_mapping,
4952
)
@@ -243,6 +246,49 @@ def resample_dataset(
243246
return xr.Dataset(data_vars=data_vars, coords=coords, attrs=dataset.attrs)
244247

245248

249+
def _downscale_source_dataset(
250+
source_ds: xr.Dataset,
251+
source_gm: GridMapping,
252+
target_gm: GridMapping,
253+
interp_methods: SpatialInterpMethods | None,
254+
agg_methods: SpatialAggMethods | None,
255+
prevent_nan_propagations: PreventNaNPropagations,
256+
transformer: pyproj.Transformer | None = None,
257+
) -> (xr.Dataset, GridMapping):
258+
if interp_methods in [0, "nearest"]:
259+
return source_ds, source_gm
260+
261+
if transformer is None:
262+
target_xres = target_gm.x_res
263+
target_yres = target_gm.y_res
264+
else:
265+
target_bbox = transformer.transform_bounds(*target_gm.xy_bbox)
266+
target_xres = (target_bbox[2] - target_bbox[0]) / target_gm.width
267+
target_yres = (target_bbox[3] - target_bbox[1]) / target_gm.height
268+
269+
x_scale = source_gm.x_res / target_xres
270+
y_scale = source_gm.y_res / target_yres
271+
if x_scale >= SCALE_LIMIT and y_scale >= SCALE_LIMIT:
272+
return source_ds, source_gm
273+
274+
w, h = np.floor(x_scale * source_gm.width), np.floor(y_scale * source_gm.height)
275+
downscaled_size = (w if w >= 2 else 2, h if h >= 2 else 2)
276+
277+
source_ds = resample_dataset(
278+
source_ds,
279+
((1 / x_scale, 0, 0), (0, 1 / y_scale, 0)),
280+
(source_gm.xy_dim_names[1], source_gm.xy_dim_names[0]),
281+
downscaled_size,
282+
source_gm.tile_size,
283+
_prep_spatial_interp_methods_downscale(interp_methods),
284+
agg_methods,
285+
prevent_nan_propagations,
286+
)
287+
source_gm = GridMapping.from_dataset(source_ds)
288+
289+
return source_ds, source_gm
290+
291+
246292
def _resample_array(
247293
array: da.Array,
248294
affine_matrix: AffineTransformMatrix,

0 commit comments

Comments
 (0)