processor

class anemoi.inference.processor.Processor(context: Context, metadata: Metadata)

Bases: ABC

Abstract base class for processors.

Parameters:
  • context (Context) – The context in which the processor operates.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

abstractmethod process(state: dict[str, Any]) → dict[str, Any]

Process the given state.

Parameters:

state (State) – The state to be processed.

Returns:

The processed state.

Return type:

State

patch_data_request(data_request: dict[str, Any]) → dict[str, Any]

Override if a processor needs to patch the data request (e.g. mars or cds).

Parameters:

data_request (DataRequest) – The data request to be patched.

Returns:

The patched data request.

Return type:

DataRequest

pre_processors

coordinate_reorder

anemoi.inference.pre_processors.coordinate_reorder.build_grid_reordering(src_lat: ndarray[tuple[Any, ...], dtype[Any]], src_lon: ndarray[tuple[Any, ...], dtype[Any]], tgt_lat: ndarray[tuple[Any, ...], dtype[Any]], tgt_lon: ndarray[tuple[Any, ...], dtype[Any]], decimals: int = 4) → ndarray[tuple[Any, ...], dtype[Any]] | None

Build a permutation that maps a source grid onto a target grid ordering.

The source and target grids may store the same physical points in a different order (for example, longitude rows rolled to start at a different meridian, or the prime meridian labelled 360.0 instead of 0.0). The mapping is resolved by matching on rounded (lat, lon) coordinates, so it works for any permutation rather than assuming a simple roll. Longitudes are compared modulo 360 so that 360.0 and 0.0 are treated as the same meridian.

Parameters:
  • src_lat (FloatArray) – Coordinates of the source points (the input state grid), 1-D.

  • src_lon (FloatArray) – Coordinates of the source points (the input state grid), 1-D.

  • tgt_lat (FloatArray) – Coordinates of the target points (the model/metadata grid), 1-D.

  • tgt_lon (FloatArray) – Coordinates of the target points (the model/metadata grid), 1-D.

  • decimals (int) – Rounding used when matching coordinates.

Returns:

None if the grids are already in the same order (identity mapping). Otherwise an integer index array perm of length len(tgt_lat) such that src_field[perm] is aligned to the target order.

Return type:

IntArray | None

Raises:

ValueError – If the two grids do not describe the same set of points (i.e. the mapping is not a bijection): points are missing from one side.

anemoi.inference.pre_processors.coordinate_reorder.backward_mapping(forward_mapping: ndarray[tuple[Any, ...], dtype[Any]] | None) → ndarray[tuple[Any, ...], dtype[Any]] | None

Return the inverse of a reordering permutation.

If forward_mapping maps a source array onto a target ordering via target = source[forward_mapping], then the returned inverse maps back via source = target[inverse] (equivalently inverse[forward_mapping] == arange). This is useful to map a model-ordered result back onto the original input ordering.

Parameters:

forward_mapping (IntArray | None) – A permutation index array (as returned by build_grid_reordering()), or None for the identity mapping.

Returns:

None if forward_mapping is None (identity is its own inverse). Otherwise the inverse permutation, with the same length and dtype as forward_mapping.

Return type:

IntArray | None

Raises:

ValueError – If forward_mapping is not a valid permutation of range(len(forward_mapping)) (contains negative/out-of-range indices or duplicates), which would make it non-invertible.

class anemoi.inference.pre_processors.coordinate_reorder.CoordinateReorder(context: Context, metadata: Metadata, *, decimals: int = 4)

Bases: Processor

Reorder the input state onto the model coordinate ordering.

Some input sources store the same physical grid in a different point order than the model expects – for example longitude rows rolled to start at a different meridian, or the prime meridian labelled 360.0 instead of 0.0. Such a state is misaligned when fed to the model.

This pre-processor matches the state’s (latitudes, longitudes) against the model grid (metadata.latitudes/longitudes, treating 360.0 as 0.0) and permutes the coordinates and every field so they are consistent with the model ordering.

If the two grids are already in the same order the state is returned unchanged. If they do not describe the same set of points a ValueError is raised.

Initialize the CoordinateReorder processor.

Parameters:
  • context (Context) – The context in which the processor operates.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • decimals (int) – Rounding used when matching source and target coordinates.

property forward_mapping: ndarray[tuple[Any, ...], dtype[Any]] | None

The forward_mapping applied by the last process() call.

target = source[forward_mapping]. None means no reordering was needed (identity), or process() has not been called yet.

property backward_mapping: ndarray[tuple[Any, ...], dtype[Any]] | None

Return the inverse of the forward_mapping applied by process().

Maps a model-ordered array back onto the original input ordering via source = target[inverse]. Returns None if no reordering was applied (identity is its own inverse).

process(state: dict[str, Any]) → dict[str, Any]

Reorder the state onto the model grid ordering.

Parameters:

state (State) – The state containing latitudes, longitudes and fields.

Returns:

The reordered state (or the original state if already aligned).

Return type:

State

extract

class anemoi.inference.pre_processors.extract.ExtractBase(context: Context, metadata: Metadata)

Bases: Processor

Base class for pre-processors that extract data from the state.

property indexer: ndarray[tuple[Any, ...], dtype[Any]] | slice
process(state: dict[str, Any]) → dict[str, Any]

Process the state to extract a subset of points based on the indexer.

class anemoi.inference.pre_processors.extract.ExtractSlice(context: Context, metadata: Metadata, *slice_args: int)

Bases: ExtractBase

Extract a subset of points from the state based on a slice.

Parameters:
  • context (Context) – The context in which the processor is running.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • slice_args (int) – Arguments to create a slice object. This can be a single integer or a tuple of integers representing the start, stop, and step of the slice.

forward_transform_filter

mask

class anemoi.inference.pre_processors.mask.MaskValues(context: Context, *, mask: str, param: str | list[str], **kwargs)

Bases: Processor

Replace values in a field with nans from a specified mask in the supporting arrays.

Initialize the MaskValues processor.

Parameters:
  • context (Context) – The context in which the processor operates.

  • mask (str) – The mask to apply to the fields from the supporting arrays or a path to a mask file. Can be npy, grib, or netCDF file.

  • param (str or list of str) – The parameter(s) to which the mask should be applied.

  • kwargs (Any) – Additional keyword arguments to pass to the apply_mask_to_param filter. - mask_value: The value in the mask that indicates where to apply the mask (default: None). - threshold_operator: The operator to use for thresholding the mask if using a threshold (default: None). - threshold: The threshold value for the mask if using operator (default: None).

process(state: dict[str, Any]) → dict[str, Any]

Apply the mask to the specified parameters in the state.

Parameters:

state (State) – The state containing the fields to be masked.

Returns:

The state with the specified parameters masked.

Return type:

State

no_missing_values

class anemoi.inference.pre_processors.no_missing_values.NoMissingValues(context: Context, metadata: Metadata, **kwargs: Any)

Bases: Processor

Replace NaNs with mean.

Initialize the NoMissingValues processor.

Parameters:
  • context (Context) – The context in which the processor operates.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • kwargs (Any) – Additional keyword arguments.

process(state: dict[str, Any]) → dict[str, Any]

Process the fields to replace NaNs with the mean value.

Parameters:

state (State) – The state to process.

Returns:

List of processed state with NaNs replaced by the mean value.

Return type:

list

post_processors

accumulate

class anemoi.inference.post_processors.accumulate.Accumulate(context: Context, metadata: Metadata, *, accumulations: list[str] | None = None, allow_negative: bool = False)

Bases: Processor

Accumulate fields from zero and return the accumulated fields.

Parameters:
  • context (Any) – The context in which the processor is running.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • accumulations (Optional[List[str]], optional) – List of fields to accumulate, by default None. If None, the fields are taken from the context’s checkpoint.

  • allow_negative (bool, optional) – Whether to allow negative values in the accumulation, by default False.

process(state: dict[str, Any]) → dict[str, Any]

Process the state to accumulate specified fields.

Parameters:

state (State) – The state containing fields to be accumulated.

Returns:

The updated state with accumulated fields.

Return type:

State

assign

earthkit_state

Code to wrap and unwrap state dictionaries into earthkit.data field lists. So that we can pass them through anemoi-transorm filters.

class anemoi.inference.post_processors.earthkit_state.StateFieldGeography(field: Any)

Bases: object

Geographical information of a state field.

Parameters:

field (Any) – The field containing geographical data.

property shape: tuple[int, ...]

Shape of the geographical field.

Type:

Tuple

class anemoi.inference.post_processors.earthkit_state.StateFieldMetadata(field: Any)

Bases: RawMetadata

Metadata for a state field.

Parameters:

field (Any) – The field containing metadata.

as_namespace(ns: str) → dict[str, Any]

Convert metadata to a specific namespace.

Parameters:

ns (str) – The namespace to convert the metadata to.

Returns:

The metadata in the specified namespace.

Return type:

Dict[str, Any]

property geography: StateFieldGeography

Geographical information of the field.

Type:

StateFieldGeography

class anemoi.inference.post_processors.earthkit_state.StateField(*args, **kwargs)

Bases: Field

State field containing name, values, and state information.

Parameters:
  • name (str) – The name of the field.

  • values (FloatArray) – The values of the field.

  • state (Dict[str, Any]) – The state information associated with the field.

  • metadata (Dict[str, Any]) – Metadata for the field.

property shape: tuple[int, ...]

Shape of the field.

Type:

Tuple

anemoi.inference.post_processors.earthkit_state.wrap_state(state: dict[str, Any], typed_variables: dict[str, Variable]) → FieldList

Transform a state dictionary into an earthkit.data field list.

Parameters:
  • state (Dict[str, Any]) – The state dictionary to be transformed.

  • typed_variables (dict[str, Variable]) – Metadata for the variables in the state.

Returns:

The transformed field list.

Return type:

ekd.FieldList

anemoi.inference.post_processors.earthkit_state.unwrap_state(fields: FieldList, state: dict[str, Any], namer: Callable, flatten: bool = True) → dict[str, Any]

Transform a earthkit.data field list into a state dictionary.

Parameters:
  • fields (ekd.FieldList) – The field list to be transformed.

  • state (State) – The original state dictionary.

  • namer (Callable) – A function to generate new field names.

  • flatten (bool, optional) – Whether to flatten the field values of non-StateField fields. StateField values are already flat and are never flattened again, which avoids an unnecessary copy. Defaults to True.

Returns:

The transformed state dictionary.

Return type:

Dict[str, Any]

extract

class anemoi.inference.post_processors.extract.ExtractBase(context: Context, metadata: Metadata)

Bases: Processor

Base class for processors that extract data from the state.

property indexer: ndarray[tuple[Any, ...], dtype[Any]] | slice
process(state: dict[str, Any]) → dict[str, Any]

Process the state to extract a subset of points based on the indexer.

Parameters:

state (State) – The state containing fields to be extracted.

Returns:

The updated state with extracted fields.

Return type:

State

class anemoi.inference.post_processors.extract.ExtractSlice(context: Context, metadata: Metadata, *slice_args: int)

Bases: ExtractBase

Extract a subset of points from the state based on a slice.

Parameters:
  • context (Context) – The context in which the processor is running.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • slice_args (int) – Arguments to create a slice object. This can be a single integer or a tuple of integers representing the start, stop, and step of the slice.

transform_filter

class anemoi.inference.post_processors.transform_filter.BackwardTransformFilter(*args, **kwargs)

Bases: TransformFilter

A processor that applies a backward transform filter to a given state.

This class uses a specified filter from the filter registry to process the state by applying a backward transformation.

Initialize the TransformFilter.

Parameters:
  • context (Context) – The context in which the filter is being used.

  • metadata (Metadata) – Metadata corresponding to the dataset this processor is handling.

  • filter (str | dict[str, Any] | None, optional) – The name of the filter or a configuration dictionary for the filter, by default None

  • skip_initial_state (bool, optional) – Whether to skip processing the initial state, by default False

class anemoi.inference.post_processors.transform_filter.ForwardTransformFilter(*args, use_forward: None = None, **kwargs: Any)

Bases: TransformFilter

A processor that applies a forward transform filter to a given state.

This class uses a specified filter from the filter registry to process the state by applying a forward transformation.

Initialize the ForwardTransformFilter.

Parameters:

use_forward (bool, optional) – Whether to use the forward transformation, by default True

undo_coordinate_reorder

class anemoi.inference.post_processors.undo_coordinate_reorder.UndoCoordinateReorder(context: Context, metadata: Metadata)

Bases: Processor

Reverse the operation done to reorder the coordinates of the state arrays, done in the coordinate_reorder pre-processor.

Must be used with the coordinate_reorder pre-processor.

process(state: dict[str, Any]) → dict[str, Any]

Reorder the coordinates of the state arrays back to their original order.

Parameters:

state (State) – The state dictionary.

Returns:

The state dictionary with coordinates reordered to their original order.

Return type:

State