earthkit.data.sources.forcings¶
Attributes¶
Classes¶
A simple implementation of the Data interface that provides default implementations |
|
Data component handler for Field that provides implementation |
|
A simple FieldList implementation containing a list of fields. |
|
Functions¶
|
|
|
Module Contents¶
- class earthkit.data.sources.forcings.ForcingMaker(field)¶
- cos_julian_day(date)¶
- cos_latitude(date)¶
- cos_latitude_()¶
- cos_local_time(date)¶
- cos_longitude(date)¶
- cos_longitude_()¶
- cos_solar_zenith_angle(date)¶
- ecef_x(date)¶
- ecef_xyz()¶
- ecef_y(date)¶
- ecef_z(date)¶
- field¶
- grid_points()¶
- insolation(date)¶
- julian_day(date)¶
- latitude(date)¶
- latitude_()¶
- local_time(date)¶
- longitude(date)¶
- longitude_()¶
- shape¶
- sin_julian_day(date)¶
- sin_latitude(date)¶
- sin_latitude_()¶
- sin_local_time(date)¶
- sin_longitude(date)¶
- sin_longitude_()¶
- toa_incident_solar_radiation(date)¶
- class earthkit.data.sources.forcings.ForcingsData(forcings)¶
Bases:
earthkit.data.data.fieldlist.FieldListDataA simple implementation of the Data interface that provides default implementations for some methods.
- property available_types¶
Return the list of available types that this data object can be converted to.
- Type:
list[str]
- describe()¶
Provide a description of the FieldList data.
- Returns:
A description of the FieldList data including the number of fields.
- Return type:
str
- is_stream()¶
Return False as this data object is not a stream.
- to(to_type, *args, **kwargs)¶
Convert the data object into another type.
- Parameters:
to_type (
strorAny) – The type to convert to. It can be a str or an object that can be used to determine the target type. In the latter case, the object can be an earthkit-data object or an object that earthkit-data supports as an input. If no suitable conversion method is found, a NotImplementedError will be raised.*args – Positional arguments to pass to the conversion method.
**kwargs – Keyword arguments to pass to the conversion method.
- Returns:
The converted data object in the target type.
- Return type:
Any- Raises:
NotImplementedError – If conversion to the target type is not implemented.
- to_array(*args, **kwargs)¶
Convert into an array of a given array-like type.
This method is not implemented for this data object and raises NotImplementedError.
- to_featurelist(*args, **kwargs)¶
Convert into a featurelist.
This method is not implemented for this data object and raises NotImplementedError.
- to_fieldlist(*args, **kwargs)¶
Convert into a FieldList.
- Parameters:
*args – Positional arguments (unused).
**kwargs – Keyword arguments (unused).
- Returns:
The source FieldList object.
- Return type:
- to_geopandas(**kwargs)¶
Convert into a GeoPandas dataframe.
This method is not implemented for this data object and raises NotImplementedError.
- to_numpy(*args, **kwargs)¶
Convert into a numpy array.
This method is not implemented for this data object and raises NotImplementedError.
- to_pandas(*args, **kwargs)¶
Convert into a pandas dataframe.
This method is not implemented for this data object and raises NotImplementedError.
- to_target(target, *args, **kwargs)¶
Write the data to a target.
- Parameters:
target (
str) – The target to write to. Seeto_target()for more details on the supported targets.*args – Positional arguments to pass to the
to_target()**kwargs – Keyword arguments to pass to the
to_target(). Cannot specifydatain kwargs.
See also
- to_value(*args, **kwargs)¶
Convert into a single value.
This method is not implemented for this data object and raises NotImplementedError.
- to_xarray(*args, **kwargs)¶
Convert into an Xarray dataset.
- Parameters:
*args – Positional arguments to pass to the source’s to_xarray method.
**kwargs – Keyword arguments to pass to the source’s to_xarray method.
- Returns:
An Xarray dataset containing the FieldList data.
- Return type:
- Raises:
NotImplementedError – If conversion to Xarray is not supported.
- class earthkit.data.sources.forcings.ForcingsFieldData(proc, date)¶
Bases:
earthkit.data.field.handler.data.DataFieldComponentHandlerData component handler for Field that provides implementation for some of the base class methods.
It is still an abstract class, subclasses must implement
get_values().- KEYS = ()¶
- NAME = 'data'¶
- check(owner)¶
Check that the data is consistent with the field’s shape.
- property component¶
Return the FieldComponent.
- classmethod create_empty()¶
- date¶
- dump(owner, name, result, prefix_keys=False)¶
Populate the namespace dictionary for this SpecFieldComponent.
- classmethod from_any(data, **dict_kwargs)¶
Create a DataFieldComponentHandler object from any input.
- Parameters:
data (
Any) – The input data from which to create the DataFieldComponent instance.dict_kwargs (
dict, optional) – Additional keyword arguments to be passed when creating the instance from a dictionary.
- Returns:
An instance of DataFieldComponent. If the input is already an instance of DataFieldComponent, it is returned as is. Otherwise, it is assumed to be a specification object and a new DataFieldComponent instance is created from it.
- Return type:
DataFieldComponent
- classmethod from_dict(d)¶
Create DataFieldComponentHandler from a dictionary.
- get(key, default=None, *, astype=None, raise_on_missing=False)¶
Return the value for
key.- Parameters:
key (
str) – Keydefault (
value) – Specify the default value forkey. Returned whenkeyis not found or its value is a missing value and raise_on_missing isFalse.astype (
type as str,intorfloat) – Return/access type forkey. When it is supportedastypeis passed to the underlying accessor as an option. Otherwise the value is cast toastypeafter it is taken from the accessor.raise_on_missing (
bool) – When it is True raises an exception ifkeyis not found or it has a missing value.
- Returns:
Returns the
keyvalue. Returnsdefaultifkeyis not found or it has a missing value andraise_on_missingis False.- Return type:
value- Raises:
KeyError – If
raise_on_missingis True andkeyis not found or it has a missing value.
- get_grib_context(context)¶
Get the GRIB context for the data component of the field.
- get_values(dtype=None, copy=True)¶
Get the values stored in the field as an array.
- proc¶
- set(values=None, **kwargs)¶
Set metadata fields for the field.
- Parameters:
**kwargs (
dict) – Metadata fields to be set.- Returns:
A new instance of FieldData with the updated metadata.
- Return type:
FieldData
- set_values(array)¶
Set the values of the field.
- Parameters:
array (
array-like) – The values to be set in the field.- Returns:
A new instance of FieldData with the updated values.
- Return type:
FieldData
- to_dict()¶
- property values¶
Get the values stored in the field as a 1D array.
- Type:
array-like
- class earthkit.data.sources.forcings.ForcingsFieldList(source_or_dataset=None, *, request={}, **kwargs)¶
Bases:
earthkit.data.indexing.simple.SimpleFieldListA simple FieldList implementation containing a list of fields.
This is a top level earthkit-data object and can be directly imported from
earthkit.data.It can be created with the top level factory function
create_fieldlist()or by the static methodfrom_fields()that is available for allFieldListimplementations.Examples
A SimpleFieldList can be created from a list of fields:
>>> import earthkit.data as ekd >>> fl = ekd.SimpleFieldList(list_of_fields)
It can also be created by the top level factory function
create_fieldlist():>>> import earthkit.data as ekd >>> fl = ekd.create_fieldlist(list_of_fields)
or with the
from_fields()static method that is available for allFieldListimplementations:>>> import earthkit.data as ekd >>> fl = ekd.FieldList.from_fields(list_of_fields) >>> fl = ekd.SimpleFieldList.from_fields(list_of_fields) >>> fl = fl_other.from_fields(list_of_fields)
- batched(n)¶
Iterate through the fieldlist in batches of
nfields.- Parameters:
n (
int) – Batch size.- Returns:
Returns an iterator yielding batches of
nfields. Each batch is a new fieldlist containing a view to the data in the original object, so no data is copied. The last batch may contain fewer thannfields.- Return type:
object
- 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
How-to examples:
GRIB: getting latitudes, longitudes and values (regular LL grid)
GRIB: getting latitudes, longitudes and values (reduced Gaussian grid)
More examples:
>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "test6.grib").to_fieldlist() >>> len(fl) 6 >>> d = fl.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 = fl.data(keys="lon") >>> d.shape (1, 7, 12) >>> d[0, 0, 0] # first longitude 0.0
- describe(*args, **kwargs)¶
Generate a summary of the fieldlist.
- static from_fields(fields=None)¶
Create a fieldlist from the given fields.
- Parameters:
fields (
iterable,Field,None) – Iterable ofFieldobjects. When it is None or empty, an empty fieldlist is returned.- Returns:
A fieldlist object containing the given fields. When
fieldsis None or empty, anEmptyFieldListis returned. Otherwise aSimpleFieldListis returned.- Return type:
- property geography¶
Return the geography of the FieldList.
- Returns:
If the fields in the FieldList have the same grid geometry, the returned geography is the one of the first field. Otherwise an error is raised.
- Return type:
GeographyBase- Raises:
ValueError – When not all the fields have the same grid geometry or the FieldList is empty.
- get(keys, default=None, astype=None, raise_on_missing=False, output='auto', group_by_key=False, flatten_dict=False, remapping=None, patch=None)¶
Return values for the specified keys from all the fields.
- Parameters:
keys (
str,list,tuple) – Specify the field metadata keys to extract. Can be a single key (str) or multiple keys as a list/tuple of str. Keys are assumed to be of the form “component.key”. For example, “time.valid_datetime” or “parameter.name”. Keys from the raw field metadata (if any) can be accessed using the “metadata.key” syntax. For example, when aFieldwas created from a GRIB message, the ecCodes GRIB keys can be accessed as “metadata.shortName” or “metadata.level”.default (
Any,None) – Specify the default value(s) forkeys. Returned when the given key is not found andraise_on_missingis False. Whendefaultis a single value, it is used for all the keys. Otherwise it must be a list/tuple of the same length askeys.astype (
type as str,intorfloat) – Return type forkeys. Whenastypeis a single type, it is used for all the keys. Otherwise it must be a list/tuple of the same length askeys.raise_on_missing (
bool) – When True, raises KeyError if any ofkeysis not found.output (
type,str) –Specify the output structure type in conjunction with
group_by_key. Whengroup_byis False (default) the output is a list with one item per field andoutputhas the following effect on the items:- ”auto” (default):
when
keysis a str returns a single value per fieldwhen
keysis a list/tuple returns a list/tuple of values per field
list or “list”: returns a list of values per field.
tuple or “tuple”: returns a tuple of values per field.
dict or “dict”: returns a dictionary with keys and their values per field.
When
group_by_keyis True the output is grouped by key and return an object with one item per key. The item per key contains the list of values for that key from all the fields. Whenoutputis “dict” a dict is returned, otherwise a list.group_by_key (
bool) – When True the output is grouped by key as described inoutput.flatten_dict (
bool) – When True andoutputis dict, for each field if any of the values in the returned dict is itself a dict, it is flattened to depth 1 by concatenating the keys with a dot. For example, if the returned dict is{"a": {"x": 1, "y": 2}, "b": 3}, it becomes{"a.x": 1, " a.y": 2, "b": 3}. This option is ignored whenoutputis not dict.remapping (
dict, optional) –Create new metadata keys from existing ones. E.g. to define a new key “param_level” as the concatenated value of the “parameter.variable” and “vertical.level” keys use:
remapping={"param_level": "{parameter.variable}{vertical.level}"}
patch (
dict, optional) – A dictionary of patch to be applied to the returned values.
- Returns:
The returned value depends on the
outputandgroup_by_keyparameters. See above.- Return type:
list,dict- Raises:
KeyError – If
raise_on_missingis True and any ofkeysis not found.
Examples
>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "test.grib").to_fieldlist() >>> fl.get("parameter.variable") ['2t', 'msl'] >>> fl.get(["parameter.variable", "parameter.units"]) [('2t', 'K'), ('msl', 'Pa')] >>> fl.get(("parameter.variable", "parameter.units")) [['2t', 'K'], ['msl', 'Pa']]
- graph(depth=0)¶
- group_by(*keys, sort=True)¶
Iterate through the fieldlist 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 fieldlist is sorted by the metadatakeysbefore grouping.
- Returns:
Returns an iterator yielding batches of fields grouped by the metadata
keys. Each batch is a new fieldlist 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 calling
lswithn.- Parameters:
n (
int,None) – The number of fields (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:
fl.head() fl.head(5) fl.head(n=5) fl.ls(5) fl.ls(n=5)
- ignore()¶
Indicates to ignore this source in concatenation/merging.
- Return type:
bool
- ls(n=None, keys='default', extra_keys=None, collections=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 fields,n > 0means fields from the front, whilen < 0means fields from the back of the fieldlist.keys (
listofstr,dict,None) – The metadata keys to extract. Ifkeys="default", a built-in default set of keys is used. To specify a column title for each key in the output use a dict as a mapping from the keys to the column titles.extra_keys (
listofstr,dict,None) – List of additional keys on top ofkeys. To specify a column title for each key in the output use a dict as a mapping from the keys to the column titles.collections (
str,listofstr,None) – The collections to extract. Can be a single collection (str) or multiple collections as a list of str. A collection is a component of the field (e.g. “time”, “parameter”, “geography”, etc.) as a dictionary. It can also be a collection within the raw “metadata” component. For example, when aFieldwas created from a GRIB message, the ecCodes GRIB “namespaces” can be accessed as collections, e.g. “metadata.mars” means the ecCodes GRIB “mars” namespace.
- Returns:
DataFrame with one row per
Field.- Return type:
Pandas DataFrame
- maker¶
- classmethod merge(sources)¶
- metadata(keys, **kwargs)¶
Return the raw metadata values for each field.
- Parameters:
*args (
tuple) – Positional arguments defining the metadata keys. Passed toField.metadata()**kwargs (
dict, optional) – Keyword arguments passed toField.metadata()
- Returns:
List with one item per
Field- Return type:
list
Examples
>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "test.grib").to_fieldlist() >>> fl.metadata("param") ['2t', 'msl'] >>> fl.metadata("param", "units") [('2t', 'K'), ('msl', 'Pa')] >>> fl.metadata(["param", "units"]) [['2t', 'K'], ['msl', 'Pa']]
- mutate()¶
- mutate_source()¶
- name = None¶
- classmethod new_mask_index(*args, **kwargs)¶
- order_by(*args, remapping=None, patch=None, **kwargs)¶
Change the order of the fields in a fieldlist.
- Parameters:
*args (
tuple) – Positional arguments specifying the metadata keys to perform the ordering on. Each argument can be a single key (str) or multiple keys as a list/tuple of str or a dictionary. Any metadata keys thatearthkit.data.core.field.Field.get()accepts can be used here. The order of the keys defines the priority of the ordering. When a dictionary is used it must specify the ordering direction or the order of the values for each key. The ordering direction can be either “ascending” or “descending” (the default is “ascending”). The order of values for a key is defined by a list of values for that key, which must include all the available values for that key in the fieldlist. See the examples below for more details.remapping (
dict) –Define new metadata keys from existing ones to use in
*argsand**kwargs. E.g. to define a new key “param_level” as the concatenated value of the “parameter.variable” and “vertical.level” keys use:remapping={"param_level": "{parameter.variable}{vertical.level}"}
See below for a more elaborate example.
**kwargs (
dict, optional) – Other keyword arguments specifying the metadata keys to perform the ordering on. Used in the same way as a dictionary in*args.
- Returns:
Returns a MaskFieldList with the reordered fields. It provides a view to the data in the original object, so no data is copied. When called without any arguments it returns the original fieldlist.
- Return type:
MaskFieldList,FieldList
Examples
How-to examples:
Ordering by a single metadata key (“parameter.variable”). The default ordering direction is
ascending:>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "test6.grib").to_fieldlist() >>> for f in fl.order_by("parameter.variable"): ... print(f) ... Field(t,850,20180801,1200,0,0) Field(t,1000,20180801,1200,0,0) Field(u,850,20180801,1200,0,0) Field(u,1000,20180801,1200,0,0) Field(v,850,20180801,1200,0,0) Field(v,1000,20180801,1200,0,0)
Ordering by multiple keys (first by “vertical.level” then by “parameter.variable”). The default ordering direction is
ascendingfor both keys:>>> for f in fl.order_by(["vertical.level", "parameter.variable"]): ... print(f) ... Field(t,850,20180801,1200,0,0) Field(u,850,20180801,1200,0,0) Field(v,850,20180801,1200,0,0) Field(t,1000,20180801,1200,0,0) Field(u,1000,20180801,1200,0,0) Field(v,1000,20180801,1200,0,0)
Specifying the ordering direction:
>>> for f in fl.order_by( ... {"parameter.variable": "ascending", "vertical.level": "descending"} ... ): ... print(f) Field(t,1000,20180801,1200,0,0) Field(t,850,20180801,1200,0,0) Field(u,1000,20180801,1200,0,0) Field(u,850,20180801,1200,0,0) Field(v,1000,20180801,1200,0,0) Field(v,850,20180801,1200,0,0)
Using the list of all the values of a key (“parameter.variable”) to define the order:
>>> for f in fl.order_by({"parameter.variable": ["u", "t", "v"]}): ... print(f) Field(u,1000,20180801,1200,0,0) Field(u,850,20180801,1200,0,0) Field(t,1000,20180801,1200,0,0) Field(t,850,20180801,1200,0,0) Field(v,1000,20180801,1200,0,0) Field(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 “parameter.variable” and “vertical.level”):>>> ordering = ["t850", "t1000", "u1000", "v850", "v1000", "u850"] >>> remapping = {"param_level": "{parameter.variable}{vertical.level}"} >>> for f in fl.order_by({"param_level": ordering}, remapping=remapping): ... print(f) Field(t,850,20180801,1200,0,0) Field(t,1000,20180801,1200,0,0) Field(u,1000,20180801,1200,0,0) Field(v,850,20180801,1200,0,0) Field(v,1000,20180801,1200,0,0) Field(u,850,20180801,1200,0,0)
- property parent¶
The parent source, if any.
- procs¶
- sel(*args, remapping=None, **kwargs)¶
Select the fields matching the given metadata conditions.
- Parameters:
*args (
tuple) –Positional arguments specifying the filter conditions as a dict. Both single or multiple keys are allowed to use. When multiple filter conditions are specified, they are combined with a logical AND operator. Each metadata key in the filter conditions can specify the following type of filter values:
single value:
fl.sel({parameter.variable: "t"})
list of values:
fl.sel({parameter.variable: ["u", "v"]})
slice of values (defines a closed interval, so treated as inclusive of both the start
and stop values, unlike normal Python indexing). The following example filters the fields with “vertical.level” between 300 and 500 inclusively:
fl.sel({vertical.level: slice(300, 500)})
Date and time related keys from the “time” field component are automatically normalised for comparison. This is also applied to the following keys from the raw metadata: “metadata.base_datetime”, “metadata.valid_datetime” and “metadata.step_timedelta”.
For example, when filtering by “time.valid_datetime” the following calls are equivalent:
>>> fl.sel({ "time.valid_datetime": "2018-08-01T12:00:00"}) >>> fl.sel({ "time.valid_datetime": datetime(2018, 8, 1, 12, 0) })
Similarly, when filtering by “time.step” the following calls are equivalent (values are assumed to be in hours when the unit is not specified):
>>> fl.sel({ "time.step": "6h"}) >>> fl.sel({ "time.step": 6}) >>> fl.sel({ "time.step": "360m"}) >>> fl.sel({ "time.step": timedelta(hours=6)})
remapping (
dict) –Define new metadata keys from existing ones to use in
*argsand**kwargs. E.g. to define a new key “param_level” as the concatenated value of the “parameter.variable” and “vertical.level” keys use:>>> remapping={"param_level": "{parameter.variable}{vertical.level}"}
See below for a more elaborate example.
**kwargs (
dict, optional) – Other keyword arguments specifying the filter conditions.
- Returns:
Returns a MaskFieldList with the reordered fields. It provides a view to the data in the original object, so no data is copied. When called without any arguments it returns the original fieldlist.
- Return type:
MaskFieldlist,FieldList
Examples
How-to examples:
More examples:
>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "tuv_pl.grib").to_fieldlist() >>> len(fl) 18
Selecting by a single key (“parameter.variable”) with a single value:
>>> fl1 = fl.sel({parameter.variable: "t"}) >>> for f in fl1: ... print(f) ... Field(t,1000,20180801,1200,0,0) Field(t,850,20180801,1200,0,0) Field(t,700,20180801,1200,0,0) Field(t,500,20180801,1200,0,0) Field(t,400,20180801,1200,0,0) Field(t,300,20180801,1200,0,0)
Selecting by multiple keys (“parameter.variable”, “vertical.level”) with a list and slice of values:
>>> fl1 = fl.sel( ... {parameter.variable: ["u", "v"], vertical.level: slice(400, 700)} ... ) >>> for f in fl1: ... print(f) ... Field(u,700,20180801,1200,0,0) Field(v,700,20180801,1200,0,0) Field(u,500,20180801,1200,0,0) Field(v,500,20180801,1200,0,0) Field(u,400,20180801,1200,0,0) Field(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 “parameter.variable” and “vertical.level”):>>> fl1 = fl.sel( ... param_level=["t850", "u1000"], ... remapping={"param_level": "{parameter.variable}{vertical.level}"}, ... ) >>> for f in fl1: ... print(f) ... Field(u,1000,20180801,1200,0,0) Field(t,850,20180801,1200,0,0)
- set(*args, **kwargs)¶
Set the metadata values for each field.
All the arguments are passed to
earthkit.data.core.field.Field.set()for each field in the fieldlist.- Parameters:
*args (
tuple) – Positional arguments defining the metadata keys and values.**kwargs (
dict, optional) – Keyword arguments defining the metadata keys and values.
- Returns:
New FieldList with the updated metadata values.
- Return type:
FieldList
See also
- source_filename = None¶
- tail(n=5, **kwargs)¶
Generate a list like summary of the last
nfields.Same as calling
lswith-n.- Parameters:
n (
int,None) – The number of fields (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:
fl.tail() fl.tail(5) fl.tail(n=5) fl.ls(-5) fl.ls(n=-5)
- to_array(**kwargs)¶
Return the values of all the fields as an array.
It is formed as the array of the
earthkit.data.core.field.Field.to_arrayvalues per field.- Parameters:
flatten (
bool) – When it is True the values are flattened per field. Otherwise an array with the field’sshapeis returned per field.dtype (
str,array.dtypeorNone) – Typecode or data-type of the array. When it isNonethe default type used by the underlying data accessor is used. For GRIB it isfloat64.copy (
bool) – When it is True a copy of the data values per field is returned. Otherwise a view is returned where possible.array_namespace (
str,array_namespaceorNone) – The array namespace to be used. When it isNonethe underlying array format of the field is used. For GRIB it is “numpy”.device (
strorNone) – The device where the array will be allocated. When it isNonethe default device is used.index (
array indexing object, optional) – The index of the values to be extracted per field. When it is None all the values are extracted. is None all the values are extracted.
- Returns:
Array containing the field values. It is formed as the array of values extracted by
earthkit.data.core.field.Field.to_arrayper field.- Return type:
array-like
Examples
How-to examples:
- to_cube(*args, **kwargs)¶
Convert to a cube-like structure.
This method is intended to support fieldlist usage in the
anemoi-datasetspackage. Planned to be removed in the future and useto_tensorinstead.
- to_data_object()¶
Convert this source into a data object, if possible.
- to_fieldlist(array_namespace=None, device=None, flatten=False, dtype=None)¶
Change how the values stored in each field in a fieldlist.
This method converts the data values in each field to an array with a given
array_namespaceand/ordevice. The resulting fieldlist is then composed of the converted fields. The field values are extracted using theearthkit.data.core.field.Field.to_array()method of each field.When a field already stores its values as an array with a matching
array_namespaceanddevice, a copy of that array is made and stored in the resulting field. This means that even if called without any arguments, the resulting fieldlist will have its own copy of the data values.The primary use of this method is to convert the values in GRIB fields loaded from disk to in-memory arrays. By default, the values in GRIB fields loaded from disk are not stored as arrays in memory, but rather as references to the on-disk data. This allows for efficient access to the data without loading it all into memory at once. However, in some cases it may be desirable to convert these values to in-memory arrays for faster access or for compatibility with other libraries. This method provides a way to do that while preserving the metadata of the fields.
- Parameters:
array_namespace (
str,array_namespaceorNone) – The array namespace to be used for the field values in the resulting fieldlist. When it is None, the default array namespace used by the underlying data accessor of the field is used. For GRIB it is “numpy”.device (
strorNone) – The device where the array will be allocated. When it isNonethe default device is used.flatten (
bool) – When it is True the values are flattened per field. Otherwise the array will have the field’sshapeper field.dtype (
str,array.dtypeorNone) – Typecode or data-type of the array. When it isNonethe default type used by the underlying data accessor per field is used. For GRIB it isfloat64.
- Returns:
New fieldlist formed from the converted fields.
- Return type:
SimpleFieldList
Examples
How-to examples:
The following example will convert a fieldlist read from a GRIB file into a
SimpleFieldListstoring data values as single precision arrays in each field.>>> import numpy as np >>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "tuv_pl.grib").to_fieldlist() >>> r = fl.to_fieldlist(array_namespace="numpy", dtype=np.float32) >>> r.to_numpy().dtype dtype('float32')
- to_numpy(**kwargs)¶
Return the values of all the fields as a Numpy array.
- Parameters:
flatten (
bool) – When it is True the values are flattened per field. Otherwise an ndarray with the field’sshapeis returned per field.dtype (
str,numpy.dtypeorNone) – Typecode or data-type of the array. When it isNonethe default type used by the underlying data accessor of the field is used. For GRIB it isfloat64.copy (
bool) – When it is True a copy of the data values per field is returned. Otherwise a view is returned where possible.index (
ndarray indexing object, optional) – The index of the values to be extracted per field. When it is None all the values are extracted.
- Returns:
Array containing the field values. It is formed as the array of values extracted by
earthkit.data.core.field.Field.to_numpyper field.- Return type:
ndarray
Examples
How-to examples:
- to_pandas(latitude=None, longitude=None, **kwargs)¶
- to_target(target, *args, **kwargs)¶
- to_tensor(*args, **kwargs)¶
Convert to a tensor-like structure.
This method is intended to use internally to support the Xarray engine, which converts fieldlist to a tensor-like structure before converting it to an Xarray Dataset or DataArray.
- 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, which can only work with GRIB data. 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. The default profile is “earthkit”. An explicit dict can be used. None is equivalent to an empty dict. When a kwarg is specified it will update the corresponding profile value if it is a dict otherwise it will overwrite it. See: Xarray engine: profiles for more information.
- variable_key: str, None
The metadata key which will be used to name the Xarray Dataset variables. Default is “parameter.variable” (which in the case of GRIB data is the same as “metadata.shortName” and “metadata.param”). The same key cannot be used to define any dimension. 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 list of str, dict or tuple, or 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 metadata key or a list. If 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 GRIB key "expver" as a dimension extra_dims = "metadata.expver" # use keys "metadata.expver" and "metadata.steam" as a dimension extra_dims = ["metadata.expver", "metadata.stream"] # define dimensions "expver", "mars_stream" and "mars_type" from # GRIB keys "expver", "stream" and "type" extra_dims = [ "metadata.expver", {"mars_stream": "metadata.stream"}, ("mars_type", "metadata.type"), ] extra_dims = [ { "expver": "metadata.expver", "mars_stream": "metadata.stream", "mars_type": "metadata.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
Every item may be one of the following:
Dimension name: A dimension that must always be preserved in the output, even when
squeeze=Trueand its size is 1, or when it appears indims_as_attrs.Metadata key: A key whose value defines an additional, non-squeezable dimension. When a metadata key is listed here, it does not need to be repeated in
extra_dims.
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 "time.step" as a dimension fixed_dims = "time.step" # use keys "time.step" and "vertical.level" as a dimension extra_dims = ["time.step", "vertical.level"] # define dimensions "step", level" and "level_type" from # metadata keys "metadata.step", "metadata.levelist" and "metadata.levtype" extra_dims = [ "metadata.step", {"level": "metadata.levelist"}, ("level_type", "metadata.levtype"), ] extra_dims = [ {"step": "metadata.step", "level": "metadata.levelist", "level_type": "metadata.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_dims)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:”member”: metadata key interpreted as ensemble forecast members
”forecast_reference_time”: metadata key interpreted as forecast reference time. Can be a single metadata key, or a list/tuple of two metadata keys representing the date and time parts of the forecast reference time. Alternatively, it can be a dict with “date” and “time” keys specifying the corresponding metadata keys. Used when
"forecast_reference_time"is intime_dims.”step”: metadata key interpreted as forecast step
”valid_time”: metadata key interpreted as valid time. Used when
"valid_time"is intime_dimsoradd_valid_time_coordis True.”date”: metadata key interpreted as base date. Used when
"date"is intime_dims.”time”: metadata key interpreted as base time. Used when
"time"is intime_dims.”level”: metadata key interpreted as level
”level_type”: metadata key interpreted as level type
The default values are as follows:
{ "member": "ensemble.member", "forecast_reference_time": "time.forecast_reference_time", "step": "time.step", "valid_time": "time.valid_datetime", "date": "time.base_date", "time": "time.base_time", "level": "vertical.level", "level_type": "vertical.level_type", }
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 “member” in the default it is enough to specify: “dim_roles={“member”: “metadata.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 nofixed_dimsare 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
A dimension name or a list of dimension names that should be converted into variable attributes when they have only a single value for the corresponding variable. Note that such size-1 dimensions are still preserved if they are explicitly listed in
ensure_dims. The default isNone.
- time_dims: str, list of str, or None
Explicitly specify the time dimension(s) to construct, together with their order. Each element is a role name from
dim_roles. The default is["forecast_reference_time", "step"]. Common configurations:["forecast_reference_time", "step"]: two dimensions for forecast reference time and step (default)["valid_time"]: a single valid-time dimension["date", "time", "step"]: three separate raw dimensions
- level_dim_mode: str, None
Controls how predefined vertical dimensions are constructed. The default is
"level". Valid values are:"level": Creates two separate dimensions,"level"and"level_type", as defined by the corresponding roles indim_roles."level_per_type": Uses a template dimension"<level_per_type>"that is expanded into one or more vertical dimensions. The dimension name is taken from the metadata key with the role"level_type"(e.g."pressure"), and the coordinate values come from the metadata key with the role"level"(e.g.[500, 700, 850, 1000])."level_and_type": Produces a single combined dimension,"level_and_type", in which the level value and the level type are merged.
- 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 takes effect when
"valid_time"is not intime_dims. Its default value (None) expands to False unless theprofileoverwrites it.
- decode_times: bool, None
If True, decode date and datetime coordinates into
datetime64values. If False, leave the coordinates in their native type (e.g.intif the coordinates come from the GRIB key like “date” or “validityDate”). The default value (None) expands to True unless theprofileoverwrites it.
- decode_timedelta: bool, None
If True, decode time-like or duration-like coordinates into
timedelta64values. If False, leave the coordinates in their native type (e.g.intif the coordinates come from the GRIB key like “time”, “validityTime”, “step”); additionally, the duration-like coordinates (e.g. derived from the GRIB key like “step”, “endStep”, etc.) will have the attribute “units” appropriately set (to “minutes”, “hours”, etc.). If None (default), assume the same value ofdecode_timesunless theprofileoverwrites it.
- aux_coords: dict, None
Mapping from an auxiliary coordinate label to a tuple:
(metadata_key: str, dataset_dimension(s): str or iterable of str). The default value is None.
- 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 fromattrshas 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=vertical.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 Field object and returns a dict of attributes, e.g.:
def rounded_wavelength(field): wl = field.get("metadata.wavelength") if wl is not None: return {"wavelength": round(wl)} else: return {}
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 Field object and returns the value of the attribute, e.g.:
def ensure_rounded(key, field): val = field.get(key) try: return round(val) except Exception: return val
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 values to metadata keys. Default is None.
- remapping: dict, None
Define new metadata keys for indexing. Any key provided in
remappingmay be referenced when specifying options such asvariable_key,extra_dims,ensure_dims,aux_coordsand others. 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("sample", "pl.grib") >>> ds = fs.to_xarray(time_dims=["forecast_reference_time", "step"]) >>> # also possible to use the xarray_open_dataset_kwargs >>> ds = fs.to_xarray( ... xarray_open_dataset_kwargs={ ... "backend_kwargs": {"time_dims": ["forecast_reference_time", "step"]} ... } ... )
- unique(*args, sort=False, drop_none=True, squeeze=False, unwrap_single=False, remapping=None, patch=None, cache=True, progress_bar=False)¶
Return the unique values for a given set of metadata keys.
- Parameters:
*args (
tuple) – Positional arguments specifying the metadata keys to collect unique values for.sort (
bool, optional) – Whether to sort the collected unique values. Default is False.drop_none (
bool, optional) – Whether to drop None values from the collected unique values. Default is True.squeeze (
bool, optional) – When True only returns the metadata keys that have more than one values. Default is False.unwrap_single (
bool, optional) – When True and only one metadata key is specified, the unique values are returned as a tuple instead of a dict. Default is False.remapping (
dict, optional) – A dictionary for remapping keys or values during collection. Default is None.patch (
dict, optional) – A dictionary for patching key values during collection. Default is None.cache (
bool, optional) – Whether to use an a cache attached to the fieldlist for previously collected unique values. Default is True.progress_bar (
bool, optional) – Whether to display a progress bar during collection. Default is False.
- Returns:
A dictionary containing the unique values for the specified metadata keys.
- Return type:
dict
- property values¶
Return the values of all the fields as a 2D array.
- Returns:
Array containing the values of all the fields. The return array is formed as the array of the flattened values extracted from each field by
Field.values.- Return type:
array-like
Examples
>>> import earthkit.data as ekd >>> fl = ekd.from_source("sample", "test.grib").to_fieldlist() >>> for f in fl: ... print(f.values.shape) ... (209,) (209,) >>> v = fl.values >>> v.shape (2, 209) >>> v[0][:3] array([262.78027344, 267.44726562, 268.61230469])
- class earthkit.data.sources.forcings.ForcingsInnerData(source_or_dataset=None, request={}, **kwargs)¶
- dates¶
- field¶
- static find_dates(source_or_dataset, request)¶
- static find_latlons(request)¶
- static find_numbers(source_or_dataset, request)¶
- static normalise_request(**request)¶
- numbers¶
- params¶
- earthkit.data.sources.forcings.index_to_coords(index, shape)¶
- earthkit.data.sources.forcings.make_datetime(date, time)¶
- earthkit.data.sources.forcings.source¶