processor
- class anemoi.inference.processor.Processor(context: Context, metadata: Metadata)
Bases:
ABCAbstract base class for processors.
- Parameters:
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.0instead of0.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 that360.0and0.0are 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:
Noneif the grids are already in the same order (identity mapping). Otherwise an integer index arraypermof lengthlen(tgt_lat)such thatsrc_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_mappingmaps a source array onto a target ordering viatarget = source[forward_mapping], then the returnedinversemaps back viasource = target[inverse](equivalentlyinverse[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()), orNonefor the identity mapping.- Returns:
Noneifforward_mappingisNone(identity is its own inverse). Otherwise the inverse permutation, with the same length and dtype asforward_mapping.- Return type:
IntArray | None
- Raises:
ValueError – If
forward_mappingis not a valid permutation ofrange(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:
ProcessorReorder 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.0instead of0.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, treating360.0as0.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
ValueErroris raised.Initialize the CoordinateReorder processor.
- Parameters:
- property forward_mapping: ndarray[tuple[Any, ...], dtype[Any]] | None
The forward_mapping applied by the last
process()call.target = source[forward_mapping].Nonemeans no reordering was needed (identity), orprocess()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]. ReturnsNoneif no reordering was applied (identity is its own inverse).
extract
- class anemoi.inference.pre_processors.extract.ExtractBase(context: Context, metadata: Metadata)
Bases:
ProcessorBase class for pre-processors that extract data from the state.
- class anemoi.inference.pre_processors.extract.ExtractSlice(context: Context, metadata: Metadata, *slice_args: int)
Bases:
ExtractBaseExtract 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:
ProcessorReplace 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).
no_missing_values
post_processors
accumulate
- class anemoi.inference.post_processors.accumulate.Accumulate(context: Context, metadata: Metadata, *, accumulations: list[str] | None = None, allow_negative: bool = False)
Bases:
ProcessorAccumulate 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.
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:
objectGeographical information of a state field.
- Parameters:
field (Any) – The field containing geographical data.
- class anemoi.inference.post_processors.earthkit_state.StateFieldMetadata(field: Any)
Bases:
RawMetadataMetadata for a state field.
- Parameters:
field (Any) – The field containing metadata.
- property geography: StateFieldGeography
Geographical information of the field.
- Type:
- class anemoi.inference.post_processors.earthkit_state.StateField(*args, **kwargs)
Bases:
FieldState 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.
- 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.
- 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-
StateFieldfields.StateFieldvalues 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:
ProcessorBase class for processors that extract data from the state.
- class anemoi.inference.post_processors.extract.ExtractSlice(context: Context, metadata: Metadata, *slice_args: int)
Bases:
ExtractBaseExtract 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:
TransformFilterA 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:
TransformFilterA 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:
ProcessorReverse 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.