Assigning a constant value

A quick way to produce a field where every grid point holds the same constant is the * 0 + c idiom:

const_field = f * 0 + c

Multiplying by 0 zeros out all values while returning a new Field with the same metadata as f. Adding c then sets every grid point to that constant. The original field is never modified.

This is useful for creating reference fields, filling missing data, or building mask boundaries — all while preserving the grid geometry, parameter, and time metadata of an existing field.

[1]:
import earthkit.data as ekd

Load data

[2]:
ds = ekd.from_source("sample", "tuv_pl.grib").to_fieldlist()
t_fields = ds.sel({"parameter.variable": "t"})
t = t_fields[0]
print(f"param: {t.parameter.variable()}, level: {t.vertical.level()}, shape: {t.geography.shape()}")
param: t, level: 1000, shape: (7, 12)

Single field

[3]:
# Fill every grid point with 273.15 K (0 °C), keeping t's metadata
freezing = t * 0 + 273.15

print(f"min:  {freezing.values.min():.2f} K")
print(f"max:  {freezing.values.max():.2f} K")
print(f"mean: {freezing.values.mean():.2f} K")

# Metadata is inherited from t
print(f"\nparam: {freezing.parameter.variable()}")
print(f"level: {freezing.vertical.level()}")
print(f"shape: {freezing.geography.shape()}")
min:  273.15 K
max:  273.15 K
mean: 273.15 K

param: t
level: 1000
shape: (7, 12)

FieldList

[4]:
# Apply to a whole FieldList — every field gets the same constant
const_all = t_fields * 0 + 273.16
print("Constant zero C FieldList:")
for f in const_all:
    print(f"  level {f.vertical.level():4d}: all values = {f.values.mean():.2f}")
Constant zero C FieldList:
  level 1000: all values = 273.16
  level  850: all values = 273.16
  level  700: all values = 273.16
  level  500: all values = 273.16
  level  400: all values = 273.16
  level  300: all values = 273.16
[ ]: