Transform data

A transformation changes a dataset you have read and returns a new dataset. Call one by name with transform(), and chain the next call straight after. Nothing is computed until you write the result or ask for values.

Prerequisites

No. 1 A loaded dataset and transformation plugins

Install DEFAIR as described in Install DEFAIR. pip install defair includes defair-ops, which provides the transformations. Load your product as a DEFAIR Dataset before running the snippets that refer to dataset or filtered, and choose channel names and spatial bounds that exist in that product.

No. 2 Target grid and memory for reprojection

For a HEALPix target, use HEALPix nside selection to choose nside for your product, and read its land-water mask example before resampling categorical data. For conservative regridding, check the memory measurements in Run on a Dask cluster. The weights need memory in proportion to the source pixel count, so choose an input extent and a machine with enough memory.

The built-in transformations

Name

What it does

content_filter

Keep or drop variables by name

spatial_filter

Keep the pixels inside a latitude and longitude box or a polygon

temporal_filter

Keep the time steps inside a time range

temporal_aggregate

Reduce the time axis to hourly, daily or longer bins

reprojection

Move the data onto another map grid, including HEALPix

alignment

Put several datasets on one shared grid and merge them

mask_filter

Set pixels to missing where a mask or a quality flag says so

rasterise

Burn a shapefile or another vector file onto the dataset’s grid as a mask

angles

Compute the solar zenith angle for every pixel

List them from Python with list_transformations():

from defair.plugin_manager import list_transformations

print(list_transformations())

Each transformation’s options are the keyword arguments of its transform() method. The API reference lists them.

Select variables

# Keep only specific channels
filtered = dataset.transform(
    "content_filter",
    include_vars=["ch1", "ch2", "ch9"]
)

# Or exclude unwanted variables
filtered = dataset.transform(
    "content_filter",
    exclude_vars=["ch10", "ch11"]
)

A dataset carries a grid mapping variable, which records the map projection the data uses. Each channel points at it by name. content_filter keeps that variable whenever one of the channels you kept still points at it. The filtered result therefore still records where on the Earth its pixels are.

Chain transformations

Each call returns a dataset, so the next call follows on:

result = (
    Dataset.from_source("/path/to/file.nat")
    .transform("content_filter", include_vars=["ch1", "ch9"])
    .transform("spatial_filter", lat_min=35, lat_max=45, lon_min=5, lon_max=20)
    .transform("reprojection", target="EPSG:4326", resolution=0.05)
)

Filter before you reproject: a smaller input makes every later step cheaper.

Reproject onto a map grid

Reprojecting moves data from one map grid onto another. Name the target grid by its EPSG code and give the pixel size:

grid = dataset.transform("reprojection", target="EPSG:4326", resolution=0.05)

The unit of resolution follows the target. It is degrees for a latitude and longitude grid and metres for a projected one. Pass resolution_unit="km" to give it in kilometres instead.

resampling chooses how the source pixels are turned into target pixels:

  • "nearest" takes the value of the nearest source pixel. Use it for flags and classes, where an average of two codes would mean nothing.

  • "bilinear", the default, blends the neighbouring source pixels. Use it for measurements.

  • "cubic" and "average" are also accepted for a gridded source.

  • A swath product, one that carries a latitude and longitude for every pixel, accepts "nearest", "bilinear", "bicubic" and "idw_cubic".

Conservative regridding is a different method, chosen with backend="conservative" and resampling="conservative": every target cell receives the area-weighted mean of the source cells that overlap it, which preserves totals. It costs memory in proportion to the number of source pixels.

Run on a Dask cluster has the memory figures.

HEALPix reprojection of geostationary images

HEALPix is a way of dividing the sphere into equal-area cells, used by many weather models. MSG SEVIRI and MTG FCI images can be reprojected onto it straight from their native image plane:

healpix = filtered.transform(
    "reprojection",
    target="healpix:1024",
    resampling="mean",  # or "nearest"
)

The number after healpix: is the nside, which sets how fine the grid is.

See HEALPix nside selection for which value matches each product.

DEFAIR takes this direct path only when it is sure of the geometry. The dataset has to have one science grid, with evenly spaced projection axes in metres and an unambiguous geostationary grid mapping under the CF conventions, the metadata rules most Earth science tools expect.

If the reader also supplied a common data model, that model has to agree with the CF mapping on the grid, the satellite longitude, the satellite height, the sweep axis and the mapping name. Filtered MSG data is a special case that still qualifies: when content_filter removed the mapping variable itself, the reader’s retained definition is used in its place.

When the metadata is missing, ambiguous, irregular or contradictory, DEFAIR does not guess. It falls back to the longitude and latitude HEALPix resamplers.

The two direct methods sample as follows:

  • nearest maps each HEALPix cell centre back to the nearest native pixel centre, then keeps every occurrence of the finite source minimum and maximum in the HEALPix cell that contains it. If minimum and maximum map to the same cell, the cell keeps the maximum.

  • mean evaluates the 16 nested child centres two levels down of each output cell and averages the finite native samples. This is deterministic target supersampling, not a histogram of source pixels.

Neither method is right for every case. As a starting point, mean usually suits continuous variables and nearest categorical ones, but the better choice depends on the data and on what the output is for. An nside much coarser than the source can also introduce aliasing.

Weigh the extreme-preservation pass described above when the variable is categorical. It spreads the highest and lowest class codes into every cell they touch, which is rarely what you want from a class map. HEALPix nside selection works through the trade-off with a land-water mask example.

Both methods stay lazy, and both write ring-ordered HEALPix coordinates whose latitude is the authalic (equal-area) latitude on WGS84.

Some options do nothing on this path. split_every and auto_optimize_chunking apply only to source-forward mean, so they leave native geostationary supersampling as it is. radius_of_influence and epsilon change neither nearest path.

A HEALPix target also accepts resampling="idw", which fills each cell by inverse distance weighting from the nearest source points. It is a path of its own, separate from the two direct methods above and from EPSG reprojection. It takes the power, neighbours, radius_of_influence and epsilon options, and DEFAIR raises an error if you use it on a sinusoidal equal-area source. An EPSG target has no idw at all, and a swath source offers idw_cubic in its place.

Combine datasets

alignment puts two or more datasets on one grid and merges them into one dataset.

What to do next

Use Build a multi-source data cube for a complete alignment example. See Run workflows for more workflow examples, or the tutorials for executable Python notebooks.