dtc_super_resolution.rcm_emulator.pre_processing_module.tiling

Module for generating RACMO and ERA5 tiles for super-resolution training and inference.

Functions

build_loc_tiles(lon, lat, era5_ds, racmo_ds)

Build one ERA5 input tile and one RACMO target tile centered on a user-selected lon/lat (EPSG:4326) location.

clip_data_to_intersection(racmo, era5)

Clip RACMO and ERA5 datasets to their intersection.

dict_to_slice(slice_dict)

Convert a dictionary representation of a slice back to a slice object.

generate_tile_indices(racmo, era5, ...[, ...])

Generate paired RACMO / ERA5 index slices for all tiles.

load_tiles_metadata(filename)

Load tile metadata from a JSON file.

make_tiles(era5, racmo, tile_size, ...[, ...])

Generate tiles for training or inference.

plot_tiles(era5, racmo, tiles[, n])

Plot a subset of tiles with RACMO and ERA5 patches.

save_tiles_metadata(tiles, filename)

Save tile metadata to a JSON file.

save_tiles_to_disk(era5, racmo_files, tiles, ...)

Save RACMO and ERA5 tile patches to disk as compressed .npz files.

slice_to_dict(slice_obj)

Convert a slice object to a dictionary representation.

build_loc_tiles(lon: float, lat: float, era5_ds: Dataset, racmo_ds: Dataset, era5_tile_px: int = 48, racmo_tile_px: int = 224) → tuple[ndarray, ndarray, dict]

Build one ERA5 input tile and one RACMO target tile centered on a user-selected lon/lat (EPSG:4326) location.

Parameters:
  • lon (float) – Longitude of the location selected on the map, in EPSG:4326.

  • lat (float) – Latitude of the location selected on the map, in EPSG:4326.

  • era5_ds (xr.Dataset) – ERA5 dataset already reprojected onto the EPSG:3031 grid (dims: time, variable-per-DataArray, y, x).

  • racmo_ds (xr.Dataset) – RACMO dataset on its native EPSG:3031 grid (must contain ‘surface_mass_balance_glaciated’ or similar).

  • era5_tile_px (int) – Height/width (in ERA5 pixels) of the input tile. Default 48.

  • racmo_tile_px (int) – Height/width (in RACMO pixels) of the target tile. Default 224.

Returns:

  • input_tile (np.ndarray) – Shape [T, 1, len(era5_vars), era5_tile_px, era5_tile_px]

  • target_tile (np.ndarray) – Shape [T, 1, racmo_tile_px, racmo_tile_px]

  • tile_meta (dict) – Info about the tile location (slices, center coords, etc.)

clip_data_to_intersection(racmo: DataArray, era5: DataArray) → tuple[DataArray, DataArray]

Clip RACMO and ERA5 datasets to their intersection.

Because both grids are co-registered (same origin, ERA5 = scale x RACMO), the intersection is simply the overlapping region of the two arrays.

Parameters:
  • racmo (xr.DataArray) – RACMO data array (2D) in EPSG:3031 projection.

  • era5 (xr.DataArray) – ERA5 data array (2D) in EPSG:3031 projection.

Returns:

A tuple containing the clipped RACMO and ERA5 data arrays.

Return type:

tuple[xr.DataArray, xr.DataArray]

dict_to_slice(slice_dict: dict) → slice

Convert a dictionary representation of a slice back to a slice object.

Parameters:

slice_dict (dict) – A dictionary with ‘start’, ‘stop’, and ‘step’ keys.

Returns:

A slice object.

Return type:

slice

generate_tile_indices(racmo: DataArray, era5: DataArray, tile_racmo_h: int, tile_racmo_w: int, scale_x: int, scale_y: int, stride_frac: float = 1.0, context_pad: tuple[int, int] = (0, 0)) → list[dict]

Generate paired RACMO / ERA5 index slices for all tiles.

Because both grids are co-registered (same origin, ERA5 = scale x RACMO), ERA5 pixel k maps to RACMO pixels [k*scale, (k+1)*scale). The stride is snapped to a multiple of the scale factors so tiles always align exactly.

Parameters:
  • racmo (xr.DataArray) – RACMO data array (2D) in EPSG:3031 projection.

  • era5 (xr.DataArray) – ERA5 data array (2D) in EPSG:3031 projection.

  • tile_racmo_h (int) – Height of the RACMO tile in pixels.

  • tile_racmo_w (int) – Width of the RACMO tile in pixels.

  • scale_x (int) – Scale factor in the x-direction (ERA5 pixels per RACMO pixel).

  • scale_y (int) – Scale factor in the y-direction (ERA5 pixels per RACMO pixel).

  • stride_frac (float) – Fraction of tile size used as stride. 1.0 → non-overlapping, 0.5 → 50 % overlap, etc.

  • context_pad (tuple[int, int]) – Extra ERA5 pixels added on each side of the ERA5 patch (height, width), providing spatial context beyond the RACMO target region. E.g. context_pad=(2, 2) gives an ERA5 patch of (tile_era5_h + 4) x (tile_era5_w + 4). Tiles where the padded ERA5 patch would exceed the array bounds are dropped.

Returns:

racmo_slice_y/x - isel slices into racmo_clip era5_slice_y/x - isel slices into era5_clip (padded)

Return type:

list[dict]

load_tiles_metadata(filename: str) → list[dict]

Load tile metadata from a JSON file.

Parameters:

filename (str) – Path to the input JSON file.

Returns:

Each dict contains slices for RACMO and ERA5 tiles.

Return type:

list[dict]

make_tiles(era5: DataArray, racmo: DataArray, tile_size: int, context_pad_h: int, context_pad_w: int, stride_frac: float = 1.0, log_: bool = False) → list[dict]

Generate tiles for training or inference.

Each tile consists of a RACMO patch and a corresponding ERA5 patch.

Parameters:
  • era5 (xr.DataArray) – ERA5 data array (2D) in EPSG:3031 projection.

  • racmo (xr.DataArray) – RACMO data array (2D) in EPSG:3031 projection.

  • tile_size (int) – Size of the tile in pixels.

  • context_pad_h (int) – Height of the era5 context padding in pixels.

  • context_pad_w (int) – Width of the era5 context padding in pixels.

  • stride_frac (float) – Fraction of tile size used as stride. 1.0 → non-overlapping, 0.5 → 50 % overlap, etc.

  • log (bool) – If True, log information about the tiles and context padding.

Returns:

A list of dictionaries, each containing slices for RACMO and ERA5 tiles.

Return type:

list[dict]

plot_tiles(era5: DataArray, racmo: DataArray, tiles: list[dict], n: int = 30) → Figure

Plot a subset of tiles with RACMO and ERA5 patches.

Parameters:
  • era5 (xr.DataArray) – ERA5 data array (2D) in EPSG:3031 projection.

  • racmo (xr.DataArray) – RACMO data array (2D) in EPSG:3031 projection.

  • tiles (list[dict]) – Each dict contains slices for RACMO and ERA5 tiles.

  • n (int) – Number of tiles to plot (randomly sampled).

Returns:

Matplotlib figure containing the plot of tiles.

Return type:

plt.Figure

save_tiles_metadata(tiles: list[dict], filename: str) → None

Save tile metadata to a JSON file.

Parameters:
  • tiles (list[dict]) – Each dict contains slices for RACMO and ERA5 tiles.

  • filename (str) – Path to the output JSON file.

save_tiles_to_disk(era5: DataArray, racmo_files: list[str], tiles: list[dict], output_dir: str, max_nan_frac: float = 0.95) → None

Save RACMO and ERA5 tile patches to disk as compressed .npz files.

For each RACMO file, we iterate over the tiles and save the corresponding RACMO and ERA5 patches to disk.

Tiles are stored as .npz files: tile_<row>_<col>_<year>.npz with keys era5 and racmo. We filter out ocean-only tiles before saving using the RACMO land mask (NaN fraction check).

Parameters:
  • era5 (xr.DataArray) – ERA5 data array (2D) in EPSG:3031 projection.

  • racmo_files (list[str]) – List of paths to RACMO .zarr files.

  • tiles (list[dict]) – Each dict contains slices for RACMO and ERA5 tiles.

  • output_dir (str) – Path to the output directory.

  • max_nan_frac (float) – Maximum fraction of NaN to_numpy() allowed in a tile.

slice_to_dict(slice_obj: slice) → dict

Convert a slice object to a dictionary representation.

Parameters:

slice_obj (slice) – A slice object to convert.

Returns:

A dictionary with ‘start’, ‘stop’, and ‘step’ keys.

Return type:

dict