data.readers.grib.index

Classes

GribFieldList

Represents a list of GribFields.

Package Contents

class data.readers.grib.index.GribFieldList(*args, **kwargs)

Bases: earthkit.data.readers.grib.pandas.PandasMixIn, earthkit.data.readers.grib.xarray.XarrayMixIn, earthkit.data.core.fieldlist.FieldList

Represents a list of GribFields.

We can iterate through the fields as follows:

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test6.grib")
>>> len(ds)
6
>>> for f in ds:
...     print(f)
...
GribField(t,1000,20180801,1200,0,0)
GribField(u,1000,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)

Fieldset objects can be concatenated with the + operator:

>>> import earthkit.data
>>> ds1 = earthkit.data.from_source("file", "docs/examples/test.grib")
>>> len(ds1)
2
>>> ds2 = earthkit.data.from_source("file", "docs/examples/test6.grib")
>>> len(ds2)
6
>>> ds = ds1 + ds2
>>> len(ds)
8

Standard Python slicing works:

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test6.grib")
>>> len(ds)
6
>>> ds[0]
GribField(t,1000,20180801,1200,0,0)
>>> for f in ds[0:3]:
...     print(f)
GribField(t,1000,20180801,1200,0,0)
GribField(u,1000,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
>>> for f in ds[0:4:2]:
...     print(f)
GribField(t,1000,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
>>> ds[-1]
GribField(v,850,20180801,1200,0,0)
>>> for f in ds[-2:]:
...     print(f)
GribField(u,850,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)

Slicing also works with a list or an ndarray:

>>> for f in ds[[1, 3]]:
...     print(f)
...
GribField(u,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
>>> for f in ds[np.array([1, 3])]:
...     print(f)
...
GribField(u,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
property availability
property availability_path
batched(n)

Iterate through the object in batches of n.

Parameters:

n (int) – Batch size.

Returns:

Returns an iterator yielding batches of n elements. Each batch is a new object containing a view to the data in the original object, so no data is copied. The last batch may contain fewer than n elements.

Return type:

object

bounding_box()

Return the bounding box for each field.

Returns:

List with one BoundingBox per Field

Return type:

list

config(name)
cube(*args, **kwargs)
data(keys=('lat', 'lon', 'value'), flatten=False, dtype=None, index=None)

Return the values and/or the geographical coordinates.

Only works when all the fields have the same grid geometry.

Parameters:
  • keys (str, list or tuple) – Specifies the type of data to be returned. Any combination of “lat”, “lon” and “value” is allowed here.

  • flatten (bool) – When it is True the “lat”, “lon” arrays and the “value” arrays per field will all be flattened. Otherwise they will preserve the field’s shape.

  • dtype (str, array.dtype or None) – Typecode or data-type of the arrays. When it is None the default type used by the underlying data accessor is used. For GRIB it is float64.

  • index (array indexing object, optional) – The index of the values to be extracted from each field. When it is None all the values are extracted.

Returns:

The elements of the array (in the order of the keys) are as follows:

  • the latitudes array from the first field when “lat” is in keys

  • the longitudes array from the first field when “lon” is in keys

  • a values array per field when “values” is in keys

Return type:

array-like

Raises:

ValueError – When not all the fields have the same grid geometry.

Examples

  • /examples/grib_lat_lon_value.ipynb

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test6.grib")
>>> len(ds)
6
>>> d = ds.data()
>>> d.shape
(8, 7, 12)
>>> d[0, 0, 0]  # first latitude
90.0
>>> d[1, 0, 0]  # first longitude
0.0
>>> d[2:, 0, 0]  # first value per field
array([272.56417847,  -6.28688049,   7.83348083, 272.53916931,
        -4.89837646,   8.66096497])
>>> d = ds.data(keys="lon")
>>> d.shape
(1, 7, 12)
>>> d[0, 0, 0]  # first longitude
0.0
datetime()

Return the unique, sorted list of dates and times in the fieldlist.

Returns:

Dict with items “base_time” and “valid_time”.

Return type:

dict of datatime.datetime

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "tests/data/t_time_series.grib")
>>> ds.datetime()
{'base_time': [datetime.datetime(2020, 12, 21, 12, 0)],
'valid_time': [
    datetime.datetime(2020, 12, 21, 12, 0),
    datetime.datetime(2020, 12, 21, 15, 0),
    datetime.datetime(2020, 12, 21, 18, 0),
    datetime.datetime(2020, 12, 21, 21, 0),
    datetime.datetime(2020, 12, 23, 12, 0)]}
default_encoder()
describe(*args, **kwargs)

Generate a summary of the fieldlist.

static from_array(array, metadata)

Create an SimpleFieldList.

Parameters:
  • array (array-like, list) – The fields’ values. When it is a list it must contain one array per field.

  • metadata (list, Metadata) – The fields’ metadata. Must contain one Metadata object per field. Or it can be a single Metadata object when all the fields have the same metadata.

:param In the generated SimpleFieldList: :param each field is represented by an array: :param storing the field values and a MetaData object holding: :param the field metadata. The shape and dtype of the array is controlled by the kwargs.:

static from_fields(fields)

Create a SimpleFieldList.

Parameters:

fields (iterable) – Iterable of Field objects.

Return type:

SimpleFieldList

static from_numpy(array, metadata)
group_by(*keys, sort=True)

Iterate through the object in groups defined by metadata keys.

Parameters:
  • *keys (tuple) – Positional arguments specifying the metadata keys to group by. Keys can be a single or multiple str, or a list or tuple of str.

  • sort (bool, optional) – If True (default), the object is sorted by the metadata keys before grouping. Sorting is only applied if the object is supporting the sorting operation.

Returns:

Returns an iterator yielding batches of elements grouped by the metadata keys. Each batch is a new object containing a view to the data in the original object, so no data is copied. It generates a new group every time the value of the keys change.

Return type:

object

head(n=5, **kwargs)

Generate a list like summary of the first n Fields. Same as calling ls with n.

Parameters:
  • n (int, None) – The number of messages (n > 0) to be printed from the front.

  • **kwargs (dict, optional) – Other keyword arguments passed to ls.

Returns:

See ls.

Return type:

Pandas DataFrame

See also

ls, tail

Notes

The following calls are equivalent:

ds.head()
ds.head(5)
ds.head(n=5)
ds.ls(5)
ds.ls(n=5)
index(key)

Return the unique, sorted values of the specified metadata key from all the fields. key will be automatically added to the keys returned by indices.

Parameters:

key (str) – Metadata key.

Returns:

Unique, sorted values of key from all the Fields.

Return type:

list

See also

index

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/tuv_pl.grib")
>>> ds.index("level")
[300, 400, 500, 700, 850, 1000]
indices(squeeze=False)

Return the unique, sorted values for a set of metadata keys (see below) from all the fields. Individual keys can be also queried by index.

Parameters:

squeeze (False) – When True only returns the metadata keys that have more than one values.

Returns:

Unique, sorted metadata values from all the Fields.

Return type:

dict

See also

index

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/tuv_pl.grib")
>>> ds.indices()
{'class': ['od'], 'stream': ['oper'], 'levtype': ['pl'], 'type': ['an'],
'expver': ['0001'], 'date': [20180801], 'time': [1200], 'domain': ['g'],
'number': [0], 'levelist': [300, 400, 500, 700, 850, 1000],
'param': ['t', 'u', 'v']}
>>> ds.indices(squeeze=True)
{'levelist': [300, 400, 500, 700, 850, 1000], 'param': ['t', 'u', 'v']}
>>> ds.index("level")
[300, 400, 500, 700, 850, 1000]

By default indices uses the keys from the “mars” ecCodes namespace. Keys with no valid values are not included. Keys that index is called with are automatically added to the original set of keys used in indices.

isel(*args, **kwargs)

Uses metadata value indices to select a subset of the elements from a fieldlist-like object.

Parameters:
  • *args (tuple) – Positional arguments specifying the filter conditions. (See below for details).

  • **kwargs (dict, optional) – Other keyword arguments specifying the metadata keys to perform the filtering on. (See below for details).

Returns:

Returns a new object with the filtered elements. It contains a view to the data in the original object, so no data is copied.

Return type:

object

Notes

isel works similarly to sel but conditions are specified by indices of metadata keys. A metadata index stores the unique, sorted values of the corresponding metadata key from all the fields in the input data. If the object is a obj:FieldList <data.readers.grib.index.FieldList> to list the indices that have more than one values use FieldList.indices(), or to find out the values of a specific index use FieldList.index().

Filter conditions are specified by a set of metadata keys either by a dictionary (in *args) or a set of **kwargs. Both single or multiple keys are allowed to use and each can specify the following type of filter values:

  • single index:

    ds.sel(param=1)
    
  • list of indices:

    ds.sel(param=[1, 3])
    
  • slice of values (behaves like normal Python indexing, stop value not included):

    # filter levels on level indices 1 and 2
    ds.sel(level=slice(1,3))
    

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/tuv_pl.grib")
>>> len(ds)
18
>>> ds.indices
{'levelist': (1000, 850, 700, 500, 400, 300), 'param': ('t', 'u', 'v')}
>>> subset = ds.isel(param=0)
>>> len(ds)
6
>>> for f in subset:
...     print(f)
...
GribField(t,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
GribField(t,700,20180801,1200,0,0)
GribField(t,500,20180801,1200,0,0)
GribField(t,400,20180801,1200,0,0)
GribField(t,300,20180801,1200,0,0)
>>> subset = ds.isel(param=[1, 2], level=slice(2, 4))
>>> len(subset)
4
>>> for f in subset:
...     print(f)
...
GribField(u,700,20180801,1200,0,0)
GribField(v,700,20180801,1200,0,0)
GribField(u,500,20180801,1200,0,0)
GribField(v,500,20180801,1200,0,0)
ls(n=None, keys=None, extra_keys=None, namespace=None)

Generate a list like summary using a set of metadata keys.

Parameters:
  • n (int, None) – The number of Fields to be listed. None means all the messages, n > 0 means fields from the front, while n < 0 means fields from the back of the fieldlist.

  • keys (list of str, dict, None) – Metadata keys. If it is None the following default set of keys will be used: “centre”, “shortName”, “typeOfLevel”, “level”, “dataDate”, “dataTime”, “stepRange”, “dataType”, “number”, “gridType”. To specify a column title for each key in the output use a dict.

  • extra_keys (list of str, dict, None) – List of additional keys to keys. To specify a column title for each key in the output use a dict.

  • namespace (str, None) – The namespace to choose the keys from. When it is set keys and extra_keys are omitted.

Returns:

DataFrame with one row per Field.

Return type:

Pandas DataFrame

See also

head, tail

metadata(*args, **kwargs)

Return the metadata values for each field.

Parameters:
Returns:

List with one item per GribField

Return type:

list

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test.grib")
>>> ds.metadata("param")
['2t', 'msl']
>>> ds.metadata("param", "units")
[('2t', 'K'), ('msl', 'Pa')]
>>> ds.metadata(["param", "units"])
[['2t', 'K'], ['msl', 'Pa']]
order_by(*args, remapping=None, patches=None, **kwargs)

Changes the order of the elements in a fieldlist-like object.

Parameters:
  • *args (tuple) – Positional arguments specifying the metadata keys to perform the ordering on. (See below for details)

  • remapping (dict) –

    Defines new metadata keys from existing ones that we can refer to in *args and **kwargs. E.g. to define a new key “param_level” as the concatenated value of the “param” and “level” keys use:

    remapping={"param_level": "{param}{level}"}
    

    See below for a more elaborate example.

  • **kwargs (dict, optional) – Other keyword arguments specifying the metadata keys to perform the ordering on. (See below for details)

Returns:

Returns a new object with reordered elements. It contains a view to the data in the original object, so no data is copied.

Return type:

object

Examples

Ordering by a single metadata key (“param”). The default ordering direction is ascending:

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test6.grib")
>>> for f in ds.order_by("param"):
...     print(f)
...
GribField(t,850,20180801,1200,0,0)
GribField(t,1000,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)s
GribField(u,1000,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)

Ordering by multiple keys (first by “level” then by “param”):

>>> for f in ds.order_by(["level", "param"]):
...     print(f)
...
GribField(t,850,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)
GribField(t,1000,20180801,1200,0,0)
GribField(u,1000,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)

Specifying the ordering direction:

>>> for f in ds.order_by(param="ascending", level="descending"):
...     print(f)
...
GribField(t,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
GribField(u,1000,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)

Using the list of all the values of a key (“param”) to define the order:

>>> for f in ds.order_by(param=["u", "t", "v"]):
...     print(f)
...
GribField(u,1000,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)
GribField(t,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)

Using remapping to specify the order by a key created from two other keys (we created key “param_level” from “param” and “levelist”):

>>> ordering = ["t850", "t1000", "u1000", "v850", "v1000", "u850"]
>>> remapping = {"param_level": "{param}{levelist}"}
>>> for f in ds.order_by(param_level=ordering, remapping=remapping):
...     print(f)
...
GribField(t,850,20180801,1200,0,0)
GribField(t,1000,20180801,1200,0,0)
GribField(u,1000,20180801,1200,0,0)
GribField(v,850,20180801,1200,0,0)
GribField(v,1000,20180801,1200,0,0)
GribField(u,850,20180801,1200,0,0)
property parent
projection()

Return the projection information shared by all the fields.

Return type:

Projection

Raises:

ValueError – When not all the fields have the same grid geometry

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test.grib")
>>> ds.projection()
<Projected CRS: +proj=eqc +ellps=WGS84 +a=6378137.0 +lon_0=0.0 +to ...>
Name: unknown
Axis Info [cartesian]:
- E[east]: Easting (unknown)
- N[north]: Northing (unknown)
- h[up]: Ellipsoidal height (metre)
Area of Use:
- undefined
Coordinate Operation:
- name: unknown
- method: Equidistant Cylindrical
Datum: Unknown based on WGS 84 ellipsoid
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
>>> ds.projection().to_proj_string()
'+proj=eqc +ellps=WGS84 +a=6378137.0 +lon_0=0.0 +to_meter=111319.4907932736 +no_defs +type=crs'
save(filename, append=False, bits_per_value=None)

Write all the fields into a file.

Parameters:
  • filename (str) – The target file path.

  • append (bool) – When it is true append data to the target file. Otherwise the target file be overwritten if already exists.

  • bits_per_value (int or None) – Set the bitsPerValue GRIB key for each message in the generated output. When None the bitsPerValue stored in the message metadata will be used.

See also

write

sel(*args, remapping=None, **kwargs)

Uses metadata values to select a subset of the elements from a fieldlist-like object.

Parameters:
  • *args (tuple) – Positional arguments specifying the filter condition as dict. (See below for details).

  • remapping (dict) –

    Creates new metadata keys from existing ones that we can refer to in *args and **kwargs. E.g. to define a new key “param_level” as the concatenated value of the “param” and “level” keys use:

    remapping={"param_level": "{param}{level}"}
    

    See below for a more elaborate example.

  • **kwargs (dict, optional) – Other keyword arguments specifying the filter conditions. (See below for details).

Returns:

Returns a new object with the filtered elements. It contains a view to the data in the original object, so no data is copied.

Return type:

object

Notes

Filter conditions are specified by a set of metadata keys either by a dictionary (in *args) or a set of **kwargs. Both single or multiple keys are allowed to use and each can specify the following type of filter values:

  • single value:

    ds.sel(param="t")
    
  • list of values:

    ds.sel(param=["u", "v"])
    
  • slice of values (defines a closed interval, so treated as inclusive of both the start

and stop values, unlike normal Python indexing):

# filter levels between 300 and 500 inclusively
ds.sel(level=slice(300, 500))

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/tuv_pl.grib")
>>> len(ds)
18

Selecting by a single key (“param”):

>>> subset = ds.sel(param="t")
>>> for f in subset:
...     print(f)
...
GribField(t,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
GribField(t,700,20180801,1200,0,0)
GribField(t,500,20180801,1200,0,0)
GribField(t,400,20180801,1200,0,0)
GribField(t,300,20180801,1200,0,0)

Selecting by multiple keys (“param”, “level”) with a list and slice of values:

>>> subset = ds.sel(param=["u", "v"], level=slice(400, 700))
>>> for f in subset:
...     print(f)
...
GribField(u,700,20180801,1200,0,0)
GribField(v,700,20180801,1200,0,0)
GribField(u,500,20180801,1200,0,0)
GribField(v,500,20180801,1200,0,0)
GribField(u,400,20180801,1200,0,0)
GribField(v,400,20180801, 1200,0,0)

Using remapping to specify the selection by a key created from two other keys (we created key “param_level” from “param” and “levelist”):

>>> subset = ds.sel(
...     param_level=["t850", "u1000"],
...     remapping={"param_level": "{param}{levelist}"},
... )
>>> for f in subset:
...     print(f)
...
GribField(u,1000,20180801,1200,0,0)
GribField(t,850,20180801,1200,0,0)
tail(n=5, **kwargs)

Generate a list like summary of the last n Fields. Same as calling ls with -n.

Parameters:
  • n (int, None) – The number of messages (n > 0) to be printed from the back.

  • **kwargs (dict, optional) – Other keyword arguments passed to ls.

Returns:

See ls.

Return type:

Pandas DataFrame

See also

head, ls

Notes

The following calls are equivalent:

ds.tail()
ds.tail(5)
ds.tail(n=5)
ds.ls(-5)
ds.ls(n=-5)
to_array(**kwargs)

Return all the fields’ values as an array. It is formed as the array of the data.core.fieldlist.Field.to_array values per field.

Parameters:

**kwargs (dict, optional) – Keyword arguments passed to data.core.fieldlist.Field.to_array

Returns:

Array containing the field values.

Return type:

array-like

See also

values, to_numpy

to_fieldlist(array_backend=None, array_namespace=None, device=None, **kwargs)

Convert to a new FieldList.

Parameters:
  • array_backend (str, array_namespace or None) – Specify the array namespace for the generated FieldList. Deprecated in version 0.19.0. Use array_namespace instead. In versions before 0.19.0 an ArrayBackend was also accepted here, which is no longer the case.

  • array_namespace (str, array_namespace or None) – The array namespace to be used. New in version 0.19.0.

  • device (str or None) – The device where the array will be allocated. When it is None the default device is used. New in version 0.19.0.

  • **kwargs (dict, optional) – kwargs are passed to to_array to extract the field values the resulting object will store.

Returns:

  • a new fieldlist containing :class`ArrayField` fields

Return type:

SimpleFieldList

Examples

The following example will convert a fieldlist read from a file into a SimpleFieldList storing single precision field values.

>>> import numpy as np
>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/tuv_pl.grib")
>>> ds.path
'docs/examples/tuv_pl.grib'
>>> r = ds.to_fieldlist(array_namespace="numpy", dtype=np.float32)
>>> r
SimpleFieldList(fields=18)
>>> hasattr(r, "path")
False
>>> r.to_numpy().dtype
dtype('float32')
to_latlon(index=None, **kwargs)

Return the latitudes/longitudes shared by all the fields.

Parameters:
  • index (array indexing object, optional) – The index of the latitudes/longitudes to be extracted. When it is None all the values are extracted.

  • **kwargs (dict, optional) – Keyword arguments passed to Field.to_latlon()

Returns:

Dictionary with items “lat” and “lon”, containing the arrays of the latitudes and longitudes, respectively.

Return type:

dict

Raises:

ValueError – When not all the fields have the same grid geometry

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test.grib")
>>> for f in ds:
...     print(f.shape)
...
(11, 19)
(11, 19)
>>> r = ds.to_latlon()
>>> for k, v in r.items():
...     print(f"{k}: shape={v.shape}")
...
lat: shape=(11, 19)
lon: shape=(11, 19)
>>> r["lon"][:2]
array([[-27., -23., -19., -15., -11.,  -7.,  -3.,   1.,   5.,   9.,  13.,
 17.,  21.,  25.,  29.,  33.,  37.,  41.,  45.],
[-27., -23., -19., -15., -11.,  -7.,  -3.,   1.,   5.,   9.,  13.,
 17.,  21.,  25.,  29.,  33.,  37.,  41.,  45.]])
to_numpy(**kwargs)

Return all the fields’ values as an ndarray. It is formed as the array of the data.core.fieldlist.Field.to_numpy values per field.

Parameters:

**kwargs (dict, optional) – Keyword arguments passed to data.core.fieldlist.Field.to_numpy

Returns:

Array containing the field values.

Return type:

ndarray

See also

to_array, values

to_pandas(latitude=None, longitude=None, **kwargs)

Convert into a pandas dataframe

to_points(**kwargs)

Return the geographical coordinates shared by all the fields in the data’s original Coordinate Reference System (CRS).

Parameters:

**kwargs (dict, optional) – Keyword arguments passed to Field.to_points()

Returns:

Dictionary with items “x” and “y”, containing the arrays of the x and y coordinates, respectively.

Return type:

dict

Raises:

ValueError – When not all the fields have the same grid geometry.

to_target(target, *args, **kwargs)

Write data into the specified target.

to_tensor(*args, **kwargs)
to_xarray(engine='earthkit', xarray_open_dataset_kwargs=None, **kwargs)

Convert the FieldList into an Xarray Dataset.

Parameters:
  • engine (str, optional) – The Xarray engine to use for generating the dataset. Default value is "earthkit". If set to cfgrib, the cfgirb engine is used. No other values are supported.

  • split_dims (str, or iterable of str, None) – Dimension or list of dimensions to use for splitting the data into multiple hypercubes. Default is None. Only used when engine="earthkit". Please note that split_dims is not a valid option when the Xarray is directly generated via xarray.open_dataset().

  • xarray_open_dataset_kwargs (dict, optional) – Keyword arguments passed to xarray.open_dataset(). Either this or **kwargs can be used, but not both.

  • **kwargs (dict, optional) –

    Any keyword arguments that can be passed to xarray.open_dataset(). Engine specific keywords are automatically grouped and passed as backend_kwargs. Either **kwargs or xarray_open_dataset_kwargs can be used, but not both.

    When engine="earthkit" the following engine specific kwargs are supported:

    • profile: str, dict or None

      Provide custom default values for most of the kwargs. Currently, the “mars” and “grid” built-in profiles are available, otherwise an explicit dict can be used. None is equivalent to an empty dict. When a kwarg is specified it will update a default value if it is a dict otherwise it will overwrite it. See: Xarray engine: profiles for more information.

    • variable_key: str, None

      Metadata key to specify the dataset variables. It cannot be defined as a dimension. Default is “param” (in earthkit-data this is the same as “shortName”). Only enabled when mono_variable is False or None.

    • drop_variables: str, or iterable of str, None

      A variable or list of variables to drop from the dataset. Default is None. Only used when variable_key is enabled.

    • rename_variables: dict, None

      Mapping to rename variables. Default is None. Only used when variable_key is enabled.

    • mono_variable: bool, str, None

      If True or str, the dataset will contain a single variable called “data” (or the value of the mono_variable kwarg when it is a str). If False, the dataset will contain one variable for each distinct value of variable_key metadata key. The default value (None) expands to False unless the profile overwrites it.

    • extra_dims: str, or iterable of str, None

      Define additional dimensions on top of the predefined dimensions. Only enabled when no fixed_dims is specified. Default is None. It can be a single item or a list. Each item is either a metadata key, or a dict/tuple defining mapping between the dimension name and the metadata key. The whole option can be a dict. E.g.

      # use key "expver" as a dimension
      extra_dims = "expver"
      # use keys "expver" and "steam" as a dimension
      extra_dims = ["expver", "stream"]
      # define dimensions "expver", mars_stream" and "mars_type" from
      # metadata keys "expver", "stream" and "type"
      extra_dims = [
          "expver",
          {"mars_stream": "stream"},
          ("mars_type", "type"),
      ]
      extra_dims = [
          {
              "expver": "expver",
              "mars_stream": "stream",
              "mars_type": "type",
          }
      ]
      
    • drop_dims: str, or iterable of str, None

      Single or multiple dimensions to be ignored. Default is None. Default is None.

    • ensure_dims: str, or iterable of str, None

      Dimension or dimensions that should be kept even when squeeze=True and their size is only 1. Default is None.

    • fixed_dims: str, or iterable of str, None

      Define all the dimensions to be generated. When used no other dimensions will be created. Might be incompatible with other settings. Default is None. It can be a single item or a list. Each item is either a metadata key, or a dict/tuple defining mapping between the dimension name and the metadata key. The whole option can be a dict. E.g.:

      # use key "step" as a dimension
      fixed_dims = "step"
      # use keys "step" and "levelist" as a dimension
      extra_dims = ["step", "levelist"]
      # define dimensions "step", level" and "level_type" from
      # metadata keys "step", "levelist" and "levtype"
      extra_dims = [
          "step",
          {"level": "levelist"},
          ("level_type", "levtype"),
      ]
      extra_dims = [
          {"step": "step", "level": "levelist", "level_type": "levtype"}
      ]
      
    • dim_roles: dict, None

      Specify the “roles” used to form the predefined dimensions. The predefined dimensions are automatically generated when no fixed_dims specified and comprise the following (in a fixed order):

      • ensemble forecast member dimension

      • temporal dimensions (controlled by time_dim_mode)

      • vertical dimensions (controlled by level_dim_mode)

      dim_roles is a mapping between the “roles” and the metadata keys representing the roles. The possible roles are as follows:

      • ”number”: metadata key interpreted as ensemble forecast members

      • ”date”: metadata key interpreted as date part of the “forecast_reference_time”

      • ”time”: metadata key interpreted as time part of the “forecast_reference_time”

      • ”step”: metadata key interpreted as forecast step

      • ”forecast_reference_time”: if not specified or None or empty the forecast reference time is built using the “date” and “time” roles

      • ”valid_time”: if not specified or None or empty the valid time is built using the “validityDate” and “validityTime” metadata keys

      • ”level”: metadata key interpreted as level

      • ”level_type”: metadata key interpreted as level type

      The default values are as follows:

      {
          "number": "number",
          "date": "dataDate",
          "time": "dataTime",
          "step": "step",
          "forecast_reference_time": None,
          "valid_date": None,
          "level": "level",
          "level_type": "typeOfLevel",
      }
      

      dims_roles behaves differently to the other kwargs in the sense that it does not override but update the default values. So e.g. to change only “number” in the defaults it is enough to specify: “dim_roles={“number”: “perturbationNumber”}.

    • dim_name_from_role_name: bool, None

      If True, the dimension names are formed from the role names. Otherwise the dimension names are formed from the metadata keys specified in dim_roles. Its default value (None) expands to True unless the profile overwrites it. Only used when no fixed_dims` are specified. New in version 0.15.0.

    • rename_dims: dict, None

      Mapping to rename dimensions. Default is None.

    • dims_as_attrs: str, or iterable of str, None

      Dimension or list of dimensions which should be turned to variable attributes if they have only one value for the given variable. Default is None.

    • time_dim_mode: str, None

      Define how predefined temporal dimensions are formed. The default is “forecast”. The possible values are as follows:

      • ”forecast”: adds two dimensions:

        • ”forecast_reference_time”: built from the “date” and “time” roles (see dim_roles) as np.datetime64 values

        • ”step”: built from the “step” role. When decode_times=True the values are np.timedelta64

      • ”valid_time”: adds a dimension called “valid_time” as described by the “valid_time” role (see dim_roles). Will contain np.datetime64 values.

      • ”raw”: the “date”, “time” and “step” roles are turned into 3 separate dimensions

    • level_dim_mode: str, None

      Define how predefined vertical dimensions are formed. The default is “level”. The possible values are:

      • ”level”: adds a single dimension according to the “level” role (see dim_roles)

      • ”level_per_type”: adds a separate dimensions for each level type based on the “level” and “level_type” roles.

      • ”level_and_type”: Use a single dimension for combined level and type of level.

    • squeeze: bool, None

      Remove dimensions which have only one valid value. Not applies to dimensions in ensure_dims. Its default value (None) expands to True unless the profile overwrites it.

    • add_valid_time_coord: bool, None

      If True, add the valid_time coordinate containing np.datetime64 values to the dataset. Only makes effect when time_dim_mode is not “valid_time”. Its default value (None) expands to False unless the profile overwrites it.

    • decode_times: bool, None

      If True, decode date and datetime coordinates into datetime64 values. If False, leave coordinates representing date-like GRIB keys (e.g. “date”, “validityDate”) encoded as native int values. The default value (None) expands to True unless the profile overwrites it.

    • decode_timedelta: bool, None

      If True, decode coordinates representing time-like or duration-like GRIB keys (e.g. “time”, “validityTime”, “step”) into timedelta64 values. If False, leave time-like coordinates encoded as native int values, while duration-like coordinates will be encoded as int with the units attached to the coordinate as the “units” attribute. If None (default), assume the same value of decode_times unless the profile overwrites it.

    • add_geo_coords: bool, None

      Add geographic coordinates to the dataset when field values are represented by a single “values” dimension. Its default value (None) expands to True unless the profile overwrites it.

    • flatten_values: bool, None

      If True, flatten the values per field resulting in a single dimension called “values” representing a field. If False, the field shape is used to form the field dimensions. When the fields are defined on an unstructured grid (e.g. reduced Gaussian) or are spectral (e.g. spherical harmonics) this option is ignored and the field values are always represented by a single “values” dimension. Its default value (None) expands to False unless the profile overwrites it.

    • attrs_mode: str, None

      Define how attributes are generated. Default is “fixed”. The possible values are:

      • ”fixed”: Use the attributes defined in variable_attrs as variables attributes and global_attrs as global attributes.

      • ”unique”: Use all the attributes defined in attrs, variable_attrs and global_attrs. When an attribute has unique value for a dataset it will be a global attribute, otherwise it will be a variable attribute. However, this logic is only applied if a unique variable attribute can be a global attribute according to the CF conventions Appendix A. (e.g. “units” cannot be a global attribute). Additionally, keys in variable_attrs are always used as variable attributes, while keys in global_attrs are always used as global attributes.

    • attrs: str, number, callable, dict or list of these, None

      Attribute or list of attributes. Only used when attrs_mode is unique. Its default value (None) expands to [] unless the profile overwrites it. The following attributes are supported:

      • str: Name of the attribute used as a metadata key to generate the value of the attribute. Can also be specified by prefixing with “key=” (e.g. “key=level”). When prefixed with “namespace=” it specifies a metadata namespace (e.g. “namespace=parameter”), which will be added as a dict to the attribute.

      • callable: A callable that takes a Metadata object and returns a dict of attributes

      • dict: A dictionary of attributes with the keys as the attribute names. If the value is a callable it takes the attribute name and a Metadata object and returns the value of the attribute. A str value prefixed with “key=” or “namespace=” is interpreted as explained above. Any other values are used as the pre-defined value for the attribute.

    • variable_attrs: str, number, callable, dict or list of these, None

      Variable attribute or attributes. For the allowed values see attrs. Its default value (None) expands to [] unless the profile overwrites it.

    • global_attrs: str, number, dict or list of these, None

      Global attribute or attributes. For the allowed values see attrs. Its default value (None) expands to [] unless the profile overwrites it.

    • coord_attrs: dict, None

      To be documented. Default is None.

    • add_earthkit_attrs: bool, None

      If True, add earthkit specific attributes to the dataset. Its default value (None) expands to True unless the profile overwrites it.

    • rename_attrs: dict, None

      A dictionary of attribute to rename. Default is None.

    • fill_metadata: dict, None

      Define fill_metadata values to metadata keys. Default is None.

    • remapping: dict, None

      Define new metadata keys for indexing. Default is None.

    • lazy_load: bool, None

      If True, the resulting Dataset will load data lazily from the underlying data source. If False, a Dataset holding all the data in memory and decoupled from the backend source will be created. Using lazy_load=False with release_source=True can provide optimised memory usage in certain cases. The default value of lazy_load (None) expands to True unless the profile overwrites it.

    • release_source: bool, None

      Only used when lazy_load=False. If True, memory held in the input fields are released as soon as their values are copied into the resulting Dataset. This is done per field to avoid memory spikes. The release operation is currently only supported for GRIB fields stored entirely in memory, e.g. when read from a stream. When a field does not support the release operation, this option is ignored. Having run to_xarray the input data becomes unusable, so use this option carefully. The default value of release_source (None) expands to False unless the profile overwrites it.

    • allow_holes: bool, None

      If False, GRIB fields must form a full hypercube (without holes). If True, a dataset will be created from any GRIB fields and its coordinates will be a union of coordinates of the fields (outer join). Values corresponding to missing GRIB fields will be filled with NaN. The default value of allow_holes (None) expands to False unless the profile overwrites it.

    • strict: bool, None

      If True, perform stricter checks on hypercube consistency. Its default value (None) expands to False unless the profile overwrites it.

    • dtype: str, numpy.dtype or None

      Typecode or data-type of the array data.

    • array_backend: str, array namespace, None

      The array namespace to use for array operations. The default value (None) is expanded to “numpy”. Deprecated since version 0.19.0. Please use array_namespace instead. In versions before 0.19.0 an ArrayBackend was also accepted here, which is no longer the case.

    • array_namespace: str, array namespace,None

      The array namespace to use for array operations. The default value (None) is expanded to “numpy”. ** New in version 0.19.0**.

    • direct_backend: bool, None

      If True, the backend is used directly bypassing xarray.open_dataset() and ignoring all non-backend related kwargs. If False, the data is read via xarray.open_dataset(). Its default value (None) expands to False unless the profile overwrites it.

    When engine="cfgrib" the following engine specific kwargs are supported:

    • ignore_keys: list, None

      It specifies the metadata keys that should be ignored when reading the GRIB messages in the backend. Please note that is not supported by cfgirb, but implemented in earthkit-data.

    • For the rest of the supported keyword arguments, please refer to the cfgirb documentation.

Returns:

When split_dims is unset a Dataset is returned. When engine="earthkit" and split_dims is set a tuple is returned. The first element of the tuple is the list of Datasets and the second element is the list of corresponding dictionaries with the spitting keys/values (one dictionary per Dataset).

Return type:

Xarray Dataset or tuple

Notes

The default values of xarray_open_dataset_kwargs or **kwargs passed to xarray.open_dataset() are as follows:

  • when engine="earthkit":

    {"cache": True, "chunks": None, "engine": "earthkit"}
    
  • when engine="cfgrib":

    {"backend_kwargs": {"errors": "raise", "ignore_keys": [], "squeeze": False,},
    "cache": True, "chunks": None, "engine": "cfgrib"}
    

Please note that settings errors="raise" and engine are always enforced and cannot be changed.

Examples

>>> import earthkit.data
>>> fs = earthkit.data.from_source("file", "test6.grib")
>>> ds = fs.to_xarray(time_dim_mode="forecast")
>>> # also possible to use the xarray_open_dataset_kwargs
>>> ds = fs.to_xarray(
...     xarray_open_dataset_kwargs={
...         "backend_kwargs": {"time_dim_mode": "forecast"}
...     }
... )
to_xarray_cfgrib(user_kwargs)
to_xarray_earthkit(user_kwargs)
unique_values(*coords, remapping=None, patches=None, progress_bar=False)

Given a list of metadata attributes, such as date, param, levels, returns the list of unique values for each attributes

property values

Get all the fields’ values as a 2D array. It is formed as the array of GribField.values per field.

See also

to_array

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "docs/examples/test.grib")
>>> for f in ds:
...     print(f.values.shape)
...
(209,)
(209,)
>>> v = ds.values
>>> v.shape
(2, 209)
>>> v[0][:3]
array([262.78027344, 267.44726562, 268.61230469])
Type:

array-like

write(f, **kwargs)

Write all the fields to a file object.

Parameters:
  • f (file object) – The target file object.

  • **kwargs (dict, optional) – Other keyword arguments passed to the underlying field implementation.

See also

read