data.core.fieldlist
Classes
Represent a Field. |
|
Represent a list of |
Module Contents
- class data.core.fieldlist.Field
Bases:
earthkit.data.core.BaseRepresent a Field.
- an_datetime()
- property array_backend
Return the array namespace of the field.
- Type:
ArrayBackend
- property array_namespace
Return the array namespace of the field.
- Type:
ArrayBackend
- base_datetime()
- batched(*args)
Return iterator for batches of data
- bounding_box()
Return the bounding box of the field.
- Return type:
- abstract clone(*, values=None, metadata=None, **kwargs)
Create a new
ClonedFieldthe with updated values and/or metadata.- Parameters:
values (
array-likeorNone) – The values to be stored in the newClonedField. When it isNonethe resultingClonedFieldwill access the values from the original field.metadata (
dict,MetadataorNone) – If it is a dictionary, it is merged with**kwargsand interpreted in the same way as**kwargs. If it is aMetadataobject, it is used as the new metadata. In this case**kwargscannot be used.**kwargs (
dict, optional) – Keys and values to update the metadata with. Metadata values can also becallableswith the following positional arguments: original_field, key, original_metadata. The newClonedFieldwill contain a reference to the original metadata object and keys not present inkwargswill be accessed from the original field.
- Returns:
The new field with updated values and/or metadata keeping a reference to the original field.
- Return type:
ClonedField- Raises:
ValueError – If
metadatais aMetadataobject and**kwargsis not empty.
- copy(*, values=None, flatten=False, dtype=None, array_backend=None, array_namespace=None, device=None, metadata=None)
Create a new
ArrayFieldby copying the values and metadata.- Parameters:
values (
array-likeorNone) – The values to be stored in the newField. When it isNonethe values extracted from the original field by usingto_arraywithflatten,dtypeandarray_backendand copied to the new field.flatten (
bool) – Control the shape of the values when they are extracted from the original field. WhenTrue, flatten the array, otherwise the field’s shape is kept. Only used whenvaluesis not provided.dtype (
str,array.dtypeorNone) – Control the typecode or data-type of the values when they are extracted from the original field. IfNone, the default type used by the underlying data accessor is used. For GRIB it isfloat64. Only used whenvaluesis not provided.array_backend (
str,array_namespaceorNone) – Control the array namespace of the values when they are extracted from the original field. IfNone, the underlying array format of the field is used. Only used whenvaluesis not provided. Deprecated since 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) – Control the array namespace of the values when they are extracted from the original field. When it isNonethe underlying array format of the field is used. New in version 0.19.0.device (
strorNone) – The device where the array will be allocated. When it isNonethe default device is used.metadata (
MetadataorNone) – The metadata to be stored in the newField. When it isNonea copy of the metadata of the original field is used.
- Return type:
ArrayField
- data(keys=('lat', 'lon', 'value'), flatten=False, dtype=None, index=None)
Return the values and/or the geographical coordinates for each grid point.
- 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 a flat array per key is returned. Otherwise an array with the field’sshapeis returned for each key.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 and or the latitudes/longitudes to be extracted. When it is None all the values and/or coordinates are extracted.
- Returns:
An multi-dimensional array containing one array per key is returned (following the order in
keys). The underlying array format of the field is used. Whenkeysis a single value only the array belonging to the key is returned.- Return type:
array-like
Examples
/examples/grib_lat_lon_value.ipynb
>>> import earthkit.data >>> ds = earthkit.data.from_source("file", "docs/examples/test6.grib") >>> d = ds[0].data() >>> d.shape (3, 7, 12) >>> d[0, 0, 0] # first latitude 90.0 >>> d[1, 0, 0] # first longitude 0.0 >>> d[2, 0, 0] # first value 272.56417847 >>> d = ds[0].data(keys="lon") >>> d.shape (7, 12) >>> d[0, 0] # first longitude 0.0
- datetime()
Return the date and time of the field.
- 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[4].datetime() {'base_time': datetime.datetime(2020, 12, 21, 12, 0), 'valid_time': datetime.datetime(2020, 12, 21, 18, 0)}
- default_encoder()
- describe(*args, **kwargs)
Generate a summary of the Field.
- dump(namespace=all, **kwargs)
Generate dump with all the metadata keys belonging to
namespace.In a Jupyter notebook it is represented as a tabbed interface.
- Parameters:
namespace (
str,list,tuple,Noneorall) –The namespace to dump. The following namespace values have a special meaning:
all: all the available namespaces will be used.- None or empty str: all the available keys will be used
(without a namespace qualifier)
**kwargs (
dict, optional) – Other keyword arguments used for testing only
- Returns:
Dict-like object with one item per namespace. In a Jupyter notebook represented as a tabbed interface to browse the dump contents.
- Return type:
NamespaceDump
Examples
- grid_points()
- grid_points_unrotated()
- group_by(*args)
Return iterator for batches of data grouped by metadata keys
- h_datetime()
- indexing_datetime()
- abstract isel(*args, **kwargs)
- ls(*args, **kwargs)
Generate a list like summary using a set of metadata keys.
- Parameters:
*args (
tuple) – Positional arguments passed toFieldList.ls.**kwargs (
dict, optional) – Other keyword arguments passed toFieldList.ls.
- Returns:
DataFrame with one row.
- Return type:
Pandas DataFrame
- property mars_area
- property mars_grid
- classmethod merge(*args, **kwargs)
Merge the object with other ones.
- abstract message()
Return a buffer containing the encoded message for message based formats (e.g. GRIB).
- Return type:
bytes
- metadata(*keys, astype=None, remapping=None, patches=None, **kwargs)
Return metadata values from the field.
When called without any arguments returns a
Metadataobject.- Parameters:
*keys (
tuple) – Positional arguments specifying metadata keys. Can be empty, in this case all the keys from the specifiednamespacewill be used. (See examples below).astype (
type name,listortuple) – Return types forkeys. A single value is accepted and applied to all thekeys. Otherwise, must have same the number of elements askeys. Only used whenkeysis not empty.remapping (
dict, optional) –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}"}
**kwargs (
dict, optional) –Other keyword arguments:
- namespace:
str,list,tuple,Noneorall The namespace to choose the
keysfrom. Whenkeysis empty andnamespaceisallall the available namespaces will be used. Whenkeysis non emptynamespacecannot specify multiple values and it cannot beall. Whennamespaceis None or empty str all the available keys will be used (without a namespace qualifier).
- namespace:
- default: value, optional
Specifies the same default value for all the
keysspecified. Whendefaultis not present and a key is not found or its value is a missing valuemetadatawill raise KeyError.
- Returns:
when called without any arguments returns a
Metadataobject- when
keysis not empty: returns single value when
keysis a strotherwise returns the same type as that of
keys(listortuple)
- when
- when
keysis empty: when
namespaceis None or an empty str returns adictwith all the available keys and valueswhen
namespaceisstrreturns adictwith the keys and values in that namespaceotherwise returns a
dictwith one item per namespace (dict of dict)
- when
- Return type:
single value,list,tuple,dictorMetadata- Raises:
KeyError – If no
defaultis set and a key is not found in the message or it has a missing value.
Examples
>>> import earthkit.data >>> ds = earthkit.data.from_source("file", "docs/examples/test.grib")
Calling without arguments:
>>> r = ds[0].metadata() >>> r <earthkit.data.readers.grib.metadata.GribMetadata object at 0x164ace170> >>> r["name"] '2 metre temperature'
Getting keys with their native type:
>>> ds[0].metadata("param") '2t' >>> ds[0].metadata("param", "units") ('2t', 'K') >>> ds[0].metadata(("param", "units")) ('2t', 'K') >>> ds[0].metadata(["param", "units"]) ['2t', 'K'] >>> ds[0].metadata(["param"]) ['2t'] >>> ds[0].metadata("badkey") KeyError: 'badkey' >>> ds[0].metadata("badkey", default=None)
Prescribing key types:
>>> ds[0].metadata("centre", astype=int) 98 >>> ds[0].metadata(["paramId", "centre"], astype=int) [167, 98] >>> ds[0].metadata(["centre", "centre"], astype=[int, str]) [98, 'ecmf']
Using namespaces:
>>> ds[0].metadata(namespace="parameter") {'centre': 'ecmf', 'paramId': 167, 'units': 'K', 'name': '2 metre temperature', 'shortName': '2t'} >>> ds[0].metadata(namespace=["parameter", "vertical"]) {'parameter': {'centre': 'ecmf', 'paramId': 167, 'units': 'K', 'name': '2 metre temperature', 'shortName': '2t'}, 'vertical': {'typeOfLevel': 'surface', 'level': 0}} >>> r = ds[0].metadata(namespace=all) >>> r.keys() dict_keys(['default', 'ls', 'geography', 'mars', 'parameter', 'statistics', 'time', 'vertical']) >>> r = ds[0].metadata(namespace=None) >>> len(r) 186 >>> r["name"] '2 metre temperature'
- mutate()
- abstract order_by(*args, **kwargs)
Reorder the elements of the object.
- projection()
Return information about the projection.
- Return type:
Projection
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'
- property resolution
- property rotation
- save(filename, append=False, **kwargs)
Write the field into a file.
- Parameters:
filename (
str, optional) – The target file path, if not defined attempts will be made to detect the filenameappend (
bool, optional) – When it is true append data to the target file. Otherwise the target file be overwritten if already exists. Default is False**kwargs (
dict, optional) – Other keyword arguments passed towrite.
- abstract sel(*args, **kwargs)
Filter the object based on metadata.
- property shape
Get the shape of the field.
For structured grids the shape is a tuple in the form of (Nj, Ni) where:
ni: the number of gridpoints in i direction (longitude for a regular latitude-longitude grid)
nj: the number of gridpoints in j direction (latitude for a regular latitude-longitude grid)
For other grid types the number of gridpoints is returned as
(num,)- Type:
tuple
- to_array(flatten=False, dtype=None, array_backend=None, array_namespace=None, device=None, index=None)
Return the values stored in the field.
- Parameters:
flatten (
bool) – When it is True a flat array is returned. Otherwise an array with the field’sshapeis returned.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.array_backend (
str,array_namespaceorNone) – The array namespace to be used. When it isNonethe underlying array format of the field is used. Deprecated since 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. When it isNonethe underlying array format of the field is used. New in version 0.19.0.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 and to be extracted. When it is None all the values are extracted
- Returns:
Field values.
- Return type:
array-array
- to_latlon(flatten=False, dtype=None, index=None)
Return the latitudes/longitudes of all the gridpoints in the field.
- Parameters:
flatten (
bool) – When it is True 1D arrays are returned. Otherwise arrays with the field’sshapeare returned.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 latitudes/longitudes to be extracted. When it is None all the values are extracted.
- Returns:
Dictionary with items “lat” and “lon”, containing the arrays of the latitudes and longitudes, respectively. The underlying array format of the field is used.
- Return type:
dict
See also
- to_numpy(flatten=False, dtype=None, index=None)
Return the values stored in the field as an ndarray.
- Parameters:
flatten (
bool) – When it is True a flat ndarray is returned. Otherwise an ndarray with the field’sshapeis returned.dtype (
str,numpy.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.index (
ndarray indexing object, optional) – The index of the values and to be extracted. When it is None all the values are extracted
- Returns:
Field values
- Return type:
ndarray
- abstract to_pandas(**kwargs)
Convert into a pandas dataframe
- to_points(flatten=False, dtype=None, index=None)
Return the geographical coordinates in the data’s original Coordinate Reference System (CRS).
- Parameters:
flatten (
bool) – When it is True 1D arrays are returned. Otherwise arrays with the field’sshapeare returned.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 coordinates to be extracted. When it is None all the values are extracted.
- Returns:
Dictionary with items “x” and “y”, containing the arrays of the x and y coordinates, respectively. The underlying array format of the field is used.
- Return type:
dict- Raises:
ValueError – When the coordinates in the data’s original CRS are not available.
See also
- to_target(target, *args, **kwargs)
Write the field into a target object.
- Parameters:
target (
object) – The target object to write the field into.*args (
tuple) – Positional arguments used to specify the target object.**kwargs (
dict, optional) – Other keyword arguments used to write the field into the target object.
- to_xarray(*args, **kwargs)
Convert the Field into an Xarray Dataset.
- Parameters:
*args (
tuple) – Positional arguments passed toFieldList.to_xarray.**kwargs (
dict, optional) – Other keyword arguments passed toFieldList.to_xarray.
- Return type:
Xarray Dataset
- 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
- valid_datetime()
- property values
Get the values stored in the field as a 1D array.
- Type:
array-like
- write(f, **kwargs)
- class data.core.fieldlist.FieldList(**kwargs)
Bases:
earthkit.data.core.index.IndexRepresent a list of
Fields.- 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, **kwargs)
Write all the fields into a file.
- Parameters:
filename (
str, optional) – The target file path, if not defined attempts will be made to detect the filenameappend (
bool, optional) – When it is true append data to the target file. Otherwise the target file be overwritten if already exists. Default is False**kwargs (
dict, optional) – Other keyword arguments passed towrite.
See also
writeGribFieldList.save()SimpleFieldList.save()
- 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
- abstract to_pandas(**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)
- abstract to_xarray(**kwargs)
Convert into an xarray dataset
- 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