data.readers.grib.index
Classes
Represents a list of |
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.FieldListRepresents 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)
Fieldsetobjects 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
nelements. 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 thannelements.- Return type:
object
- bounding_box()
Return the bounding box for each field.
- Returns:
List with one
BoundingBoxperField- 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,listortuple) – 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’sshape.dtype (
str,array.dtypeorNone) – Typecode or data-type of the arrays. When it isNonethe default type used by the underlying data accessor is used. For GRIB it isfloat64.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
keysthe longitudes array from the first field when “lon” is in
keysa 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:
dictofdatatime.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 oneMetadataobject per field. Or it can be a singleMetadataobject 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 aMetaDataobject holding: :param the field metadata. The shape and dtype of the array is controlled by thekwargs.:
- static from_fields(fields)
Create a
SimpleFieldList.- Parameters:
fields (
iterable) – Iterable ofFieldobjects.- 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) – IfTrue(default), the object is sorted by the metadatakeysbefore 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 thekeyschange.- Return type:
object
- head(n=5, **kwargs)
Generate a list like summary of the first
nFields. Same as callinglswithn.- Parameters:
n (
int,None) – The number of messages (n> 0) to be printed from the front.**kwargs (
dict, optional) – Other keyword arguments passed tols.
- Returns:
See
ls.- Return type:
Pandas DataFrame
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
keyfrom all the fields.keywill be automatically added to the keys returned byindices.- Parameters:
key (
str) – Metadata key.- Returns:
Unique, sorted values of
keyfrom all theFields.- Return type:
list
See also
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
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
indicesuses the keys from the “mars” ecCodes namespace. Keys with no valid values are not included. Keys thatindexis called with are automatically added to the original set of keys used inindices.
- 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
iselworks similarly toselbut 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 useFieldList.indices(), or to find out the values of a specific index useFieldList.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 ofFields to be listed. None means all the messages,n > 0means fields from the front, whilen < 0means fields from the back of the fieldlist.keys (
listofstr,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 (
listofstr,dict,None) – List of additional keys tokeys. To specify a column title for each key in the output use a dict.namespace (
str,None) – The namespace to choose thekeysfrom. When it is setkeysandextra_keysare omitted.
- Returns:
DataFrame with one row per
Field.- Return type:
Pandas DataFrame
- metadata(*args, **kwargs)
Return the metadata values for each field.
- Parameters:
*args (
tuple) – Positional arguments defining the metadata keys. Passed toGribField.metadata()**kwargs (
dict, optional) – Keyword arguments passed toGribField.metadata()
- 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
*argsand**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
remappingto 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 (
intorNone) – Set thebitsPerValueGRIB key for each message in the generated output. When None thebitsPerValuestored in the message metadata will be used.
See also
- 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
*argsand**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
remappingto 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
nFields. Same as callinglswith-n.- Parameters:
n (
int,None) – The number of messages (n> 0) to be printed from the back.**kwargs (
dict, optional) – Other keyword arguments passed tols.
- Returns:
See
ls.- Return type:
Pandas DataFrame
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_arrayvalues per field.- Parameters:
**kwargs (
dict, optional) – Keyword arguments passed todata.core.fieldlist.Field.to_array- Returns:
Array containing the field values.
- Return type:
array-like
- to_fieldlist(array_backend=None, array_namespace=None, device=None, **kwargs)
Convert to a new
FieldList.- Parameters:
array_backend (
str,array_namespaceorNone) – Specify the array namespace for the generatedFieldList. Deprecated in version 0.19.0. Usearray_namespaceinstead. In versions before 0.19.0 anArrayBackendwas also accepted here, which is no longer the case.array_namespace (
str,array_namespaceorNone) – The array namespace to be used. New in version 0.19.0.device (
strorNone) – The device where the array will be allocated. When it isNonethe default device is used. New in version 0.19.0.**kwargs (
dict, optional) –kwargsare passed toto_arrayto 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
SimpleFieldListstoring 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 toField.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_numpyvalues per field.- Parameters:
**kwargs (
dict, optional) – Keyword arguments passed todata.core.fieldlist.Field.to_numpy- Returns:
Array containing the field values.
- Return type:
ndarray
- 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 toField.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 tocfgrib, the cfgirb engine is used. No other values are supported.split_dims (
str, oriterableofstr,None) – Dimension or list of dimensions to use for splitting the data into multiple hypercubes. Default is None. Only used whenengine="earthkit". Please note thatsplit_dimsis not a valid option when the Xarray is directly generated viaxarray.open_dataset().xarray_open_dataset_kwargs (
dict, optional) – Keyword arguments passed toxarray.open_dataset(). Either this or**kwargscan 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 asbackend_kwargs. Either**kwargsorxarray_open_dataset_kwargscan 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_variableis 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_keyis enabled.
- rename_variables: dict, None
Mapping to rename variables. Default is None. Only used when
variable_keyis 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_variablekwarg when it is a str). If False, the dataset will contain one variable for each distinct value ofvariable_keymetadata key. The default value (None) expands to False unless theprofileoverwrites it.
- extra_dims: str, or iterable of str, None
Define additional dimensions on top of the predefined dimensions. Only enabled when no
fixed_dimsis 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=Trueand 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_dimsspecified 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_rolesis 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_rolesbehaves 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 theprofileoverwrites 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=Truethe 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 theprofileoverwrites 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_modeis not “valid_time”. Its default value (None) expands to False unless theprofileoverwrites 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
profileoverwrites 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_timesunless theprofileoverwrites 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
profileoverwrites 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
profileoverwrites it.
- attrs_mode: str, None
Define how attributes are generated. Default is “fixed”. The possible values are:
”fixed”: Use the attributes defined in
variable_attrsas variables attributes andglobal_attrsas global attributes.”unique”: Use all the attributes defined in
attrs,variable_attrsandglobal_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 invariable_attrsare always used as variable attributes, while keys inglobal_attrsare always used as global attributes.
- attrs: str, number, callable, dict or list of these, None
Attribute or list of attributes. Only used when
attrs_modeisunique. Its default value (None) expands to [] unless theprofileoverwrites 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 theprofileoverwrites 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 theprofileoverwrites 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
profileoverwrites 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=Falsewithrelease_source=Truecan provide optimised memory usage in certain cases. The default value oflazy_load(None) expands to True unless theprofileoverwrites 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 runto_xarraythe input data becomes unusable, so use this option carefully. The default value ofrelease_source(None) expands to False unless theprofileoverwrites 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 theprofileoverwrites it.
- strict: bool, None
If True, perform stricter checks on hypercube consistency. Its default value (None) expands to False unless the
profileoverwrites 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_namespaceinstead. In versions before 0.19.0 anArrayBackendwas 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 viaxarray.open_dataset(). Its default value (None) expands to False unless theprofileoverwrites it.
When
engine="cfgrib"the following engine specific kwargs are supported:
- Returns:
When
split_dimsis unset a Dataset is returned. Whenengine="earthkit"andsplit_dimsis 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 Datasetortuple
Notes
The default values of
xarray_open_dataset_kwargsor**kwargspassed toxarray.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"andengineare 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.valuesper 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