i7aof.extrap

Extrapolation utilities and workflows that drive the Fortran horizontal and vertical extrapolation executables for ISMIP. See i7aof.extrap for developer architecture and Workflows for usage notes.

Utilities for generating Fortran extrapolation namelists.

This subpackage currently ships a Jinja2 template that can be rendered to produce a combined horizontal + vertical extrapolation namelist consumed by the Fortran executables.

i7aof.extrap.get_template_path() str

Return the filesystem path to the bundled Jinja2 namelist template.

i7aof.extrap.load_template_text() str

Return the contents of the bundled Jinja2 namelist template.

Extrapolate remapped CMIP ct/sa fields horizontally and vertically.

Workflow

Consumes remapped monthly ct/sa files produced by the remap step:

workdir/remap/<model>/<scenario>/Omon/ct_sa/*ismip<res>.nc

Produces per input file and per variable vertically extrapolated outputs:

workdir/extrap/<model>/<scenario>/Omon/ct_sa/*ismip<res>_extrap.nc

with ct_sa in the filename replaced by the variable name (ct or sa).

Two external Fortran executables are invoked sequentially for each variable:

  • i7aof_extrap_horizontal (&horizontal_extrapolation namelist)

  • i7aof_extrap_vertical (&vertical_extrapolation namelist)

A single combined namelist (containing both groups) is rendered from the Jinja2 template namelist_template.nml.j2 via load_template_text().

Parallel, chunked execution

Time is processed in chunks to limit memory and file sizes. Chunks run serially or in parallel (process-based) depending on configuration. Each chunk writes a per-chunk input, runs the Fortran executables, and records stdout/stderr and any Python traceback in a chunk log under <out>_tmp/logs. Abrupt worker failures are detected and reported with the set of completed vs. pending chunks for quick triage.

Supporting data (auto-generated if missing)

If required inputs for IMBIE basin masks or topography are absent, the workflow attempts to build them on the fly inside workdir:

  • IMBIE basins: uses i7aof.imbie.masks.make_imbie_masks() to produce imbie/basinNumbers_<res>.nc (downloading shapefiles as needed).

  • Topography: constructs the configured dataset (e.g. BedMachine)

    via i7aof.topo.get_topo(); if the remapped ISMIP file is missing it runs download_and_preprocess_topo() (which may require a manually downloaded source file for licensed data) followed by``remap_topo_to_ismip()``.

If a required manual download (e.g., BedMachine source) is not present an informative FileNotFoundError is raised.

i7aof.extrap.cmip.extrap_cmip(model: str, scenario: str, workdir: str | None = None, user_config_filename: str | None = None, variables: Sequence[str] = ('ct', 'sa'), keep_intermediate: bool = False, num_workers: int | str | None = None) None

Extrapolate remapped CMIP ct/sa fields horizontally and vertically in time chunks, optionally in parallel.

Overview

For each input file under workdir/remap/<model>/<scenario>/Omon/ct_sa/*ismip<res>.nc, this function:

  1. Splits the time dimension into chunks (extrap_cmip.time_chunk).

  2. For each chunk, writes a per-chunk input on the ISMIP grid with coordinates ensured and only required variables kept.

  3. Invokes the Fortran executables sequentially (horizontal then vertical) using a rendered namelist.

  4. Captures Fortran stdout/stderr in a per-chunk log and appends a Python traceback on any error for that chunk.

  5. Concatenates vertical outputs along time and injects grid coordinates/variables into the final output file.

Execution is process-parallel with a configurable number of workers. When a worker fails, the failing chunk indices and log path are reported; if the pool crashes, the set of completed vs. pending chunks is logged to direct debugging. Dask is required and used with a synchronous scheduler during per-chunk input writes to avoid multi-threaded HDF5 access. BLAS/OMP threads are pinned to 1 for each worker. HDF5 file locking is relaxed by default.

param model:

CMIP model name.

type model:

str

param scenario:

Scenario key (e.g., historical or ssp585).

type scenario:

str

param workdir:

Base working directory; if omitted, uses config [workdir] base_dir.

type workdir:

str, optional

param user_config_filename:

Optional user config overriding defaults.

type user_config_filename:

str, optional

param variables:

Variable names to extrapolate (default: ct and sa).

type variables:

sequence of str, optional

param keep_intermediate:

Keep horizontal temps and namelists if True; otherwise delete the *_tmp directory after finalization.

type keep_intermediate:

bool, optional

param num_workers:

Number of parallel workers. Pass an integer, or "auto"/0 to use cpu_count - 1. If omitted, read from config [extrap_cmip] num_workers.

type num_workers:

int | str | None, optional

i7aof.extrap.cmip.main() None

Extrapolate a remapped climatology (no time dimension) to depth levels.

This mirrors the CMIP extrapolation workflow but simplifies it to a single pass per variable because the climatology file has no time dimension. It reuses shared helpers in i7aof.extrap.shared for prerequisite data, namelist rendering, executable invocation, and finalization.

Expected input (produced by ismip7-antarctic-remap-clim):

workdir/remap/climatology/<name>/<file>_ismip<res>.nc

Outputs (per variable) are written to:

workdir/extrap/climatology/<name>/<file>_ismip<res>_<var>_extrap.nc

Two external Fortran executables are invoked sequentially:

  • i7aof_extrap_horizontal

  • i7aof_extrap_vertical

The combined namelist is rendered from the existing template.

i7aof.extrap.clim.extrap_climatology(clim_name: str, *, workdir: str | None = None, user_config_filename: str | None = None, variables: tuple[str, ...] = ('ct', 'sa'), keep_intermediate: bool = False)

Extrapolate a remapped climatology file for the given variables.

Parameters:
  • clim_name (str) – Name of the climatology (must match the file used in remap step).

  • workdir (str, optional) – Base working directory (defaults to [workdir] base_dir).

  • user_config_filename (str, optional) – Optional user config file path overriding defaults.

  • variables (tuple of str, optional) – Variables to extrapolate (default: ('ct', 'sa')).

  • keep_intermediate (bool, optional) – Keep temporary directory if True.

i7aof.extrap.clim.main()