Fieldset object
- class Fieldset
Metview’s Fieldset object represents GRIB data. It is a container-like object with each entry representing a GRIB message.
Construction
Fieldsets can be directly constructed either as empty, with a path to a GRIB file or using read():
import metview as mv # empty Fieldset f1 = mv.Fieldset() # create from GRIB file f2 = mv.read("test.grib") # create from GRIB file f3 = mv.Fieldset(path="test.grib") # create from a set of GRIB files using globbing # (new in metview-python version 1.8.0) f4 = mv.Fieldset(path="test_*.grib") # create from multiple GRIB files f5 = mv.Fieldset(path=["a.grib", "b.grib", "/a/b/c.grib"])
Concatenation
Concatenation can be performed in these ways:
f4 = mv.Fieldset(fields=[f1, f2, f3]) # create from list of Fieldsets f4.append(f2) # append f2 onto the end of f4 f5 = mv.merge(f2, f3)The ‘list of files’ method of constructing a Fieldset shown in the previous section effectively creates a concatenation of those files into a Fieldset.
Indexing
Indexing and slicing works in the standard Python way. There is no such thing as a single field object in Metview, only a Fieldset with a single field, so all of the following return a Fieldset:
f[0] # first field f[1] # second field f[-1] # last field f[0:6] # the first 5 fields f[::2] # every second field my_fields = fs[np.array([1, 2, 0, 5])] # numpy array of indicesIt is also possible to assign fields into given locations in a Fieldset, for example:
grib = mv.read("t_for_xs.grib") grib[0] = grib[0] * 10 grib[4] = mv.sqrt(grib[3])Slicing is done with standard Python notation, e.g.
# select fields 4 to 7, step 2 my_slice = data[4:8:2]For more examples of indexing and slicing Fieldsets, see Slicing and dicing GRIB data.
Iteration
A Fieldset is iterable, with each iteration returning a single-field Fieldset, e.g.
fs = mv.Fieldset(path="test.grib") field_maxes = [f.maxvalue() for f in fs]len(fs) and fs.count() both return the number of fields in the Fieldset.
Inspection
The contents of a Fieldset can be easily inspected using the
ls()anddescribe()methods. See the Inspecting GRIB data notebook for some examples.
Filtering
A set of fields from a Fieldset can be extracted using the
read()andselect()functions. See the Filtering GRIB data notebook for the comparison of these two methods.For simple data extractions with
select()a shorthand notation with the [] operator is also available. E.g. instead ofg = fs.select(shortName="t", level=500, typeOfLevel="isobaricInhPa")we can say:
g = fs["t500hPa"]See more examples here.
Note
select()and its [] operator are only available from metview-python version 1.8.0.
Functions vs methods
Functions that work with Fieldsets can also be used as methods, provided that their first argument is a Fieldset. For example, the following two operations are shown in two equivalent ways:
a = mv.abs(fs) a = fs.abs() b = mv.bitmap(fs, 0) b = fs.bitmap(0)
Per-point methods
Unary functions and methods on Fieldsets act on each grid point of each field. For example, the
abs()method will return a new Fieldset where all the grid values of all the fields have the absolute of their original value.Operations between Fieldsets act on corresponding grid points in the corresponding fields in each Fieldset. Both Fieldsets must have the same number of fields and the same number of points in their corresponding fields. For example, if we have one Fieldset containing analysis data for 99 vertical levels, and another with forecast data for the same 99 levels (stored in the same order) then we can easily compute the difference Fieldset like this:
diff = forecast_fs - analysis_fs # contains 99 fields of differencesSimilarly, operations work between Fieldsets and numbers, for example:
temperature_in_K = mv.read("temp.grib") temperature_in_C = temperature_in_K - 273.15The following list of operators are valid when acting between two Fieldsets and also when acting between a Fieldset and a number. Of these, the logical operators return a Fieldset containing values of 1 where they pass the test, or 0 where they fail.
Operator |
Decription |
|---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
** |
power |
& |
logical and (result is 1s and 0s) |
| |
logical or (result is 1s and 0s) |
~ |
logical not (result is 1s and 0s) |
> |
greater than (result is 1s and 0s) |
>= |
greater than or equal to (result is 1s and 0s) |
< |
less than (result is 1s and 0s) |
<= |
less than or equal to (result is 1s and 0s) |
== |
equal (result is 1s and 0s) |
!= |
not equal (result is 1s and 0s) |
Data extraction
A Fieldset can return an xarray by calling its to_dataset() method.
A Fieldset can return a numpy array of values by calling its values() method.
A Fieldset can return a Geopoints object by calling the grib_to_geo() function.
Methods and functions
List of methods
Compute the absolute value |
|
Compute the absolute vorticity from a relative vorticity |
|
Add up the values per field in a |
|
Compute the arc cosine |
|
Compute the arc sine |
|
Compute the arc tangent |
|
Compute the arc tangent of 2 |
|
Compute the average per field in a |
|
Compute the zonal averages for each field in a |
|
Compute the meridional averages for each field in a |
|
Return the base date(s) of a given |
|
Compute the bearings with respect to a reference in a |
|
Convert numbers to missing values in a |
|
Perform spatial convolution on a |
|
Compute the area-weighted correlation for each field in a |
|
Compute the cosine |
|
Generate a field with the cosine of the latitudes in a |
|
Return the covariance of two |
|
Compute the area-weighted covariance for each field in a |
|
Return information on missing values in a |
|
Convert an xndarray dataset to a |
|
De-accumulate values in a |
|
Print summary of the contents of a |
|
Compute the wind direction |
|
Compute the distances in a |
|
Compute the integer part of a divison |
|
Compute the horizontal divergence of a vector |
|
Compute the exponential |
|
|
Fills missing values along the horizontal line |
Find locations of values in a |
|
Compute first West-East derivative of a |
|
Compute first South-North derivative of a |
|
Convert int GRIB to float GRIB |
|
Compute the frequencies of a |
|
Compute the geostrophic wind on pressure levels in a |
|
Find values in field and returns the result as |
|
Compute horizontal gradient of a |
|
Read GRIB headers using ecCodes keys |
|
Read GRIB headers using ecCodes keys |
|
Read GRIB headers using ecCodes keys |
|
Read GRIB headers using ecCodes keys |
|
Read GRIB headers using ecCodes keys |
|
Read GRIB headers using ecCodes keys |
|
Return list of indexes into the fields of the Fieldset |
|
Writes GRIB headers using ecCodes keys |
|
Writes GRIB headers using ecCodes keys |
|
Writes GRIB headers using ecCodes keys |
|
Writes GRIB headers using ecCodes keys |
|
Writes GRIB headers using ecCodes keys |
|
Set GRIB packing bit width |
|
Compute the grid cell area in a |
|
Build a |
|
Integer part |
|
Convert float GRIB to int GRIB |
|
Compute the surface integral of a |
|
Compute the average weighted by the gridcell area for each field in |
|
Interpolate |
|
Compute the horizontal Laplacian of |
|
Compute the natural logarithm |
|
Compute the base 10 logarithm |
|
Build an output |
|
Dumps metadata for each message in a |
|
Maximum |
|
Maximum value of a |
|
Generate a |
|
Minimum |
|
Interpolate a model level |
|
Compute the integer remainder of a divison |
|
Compute the geopotential on model levels for a |
|
Interpolate a |
|
Return the nearest grid point value from a |
|
Return the nearest grid point value from a |
|
Convert missing values to numbers in a |
|
Interpolate a pressure level |
|
Interpolate pressure level fields to pressure levels |
|
Generate polygon masks for a |
|
Compute the pressure on model levels in a |
|
Compute the verical pressure derivative |
|
Generate masks based on a radius around a point for |
|
Compute he pointwise root mean square for |
|
Compute the area-weighted root mean square for |
|
Compute the second West-East derivative of a |
|
Compute the econd South-North derivative of a |
|
Filter |
|
Compute the sign |
|
Compute the shear deformation of a vector |
|
Compute the sine |
|
Generate a field with the cosine of the latitudes in a |
|
Perform spatial smoothing on a |
|
|
Perform spatial smoothing on a |
Compute the solar zenith angle for a |
|
Sort a |
|
Compute the square root |
|
Return the standard deviation of all the fields in a |
|
Compute the area-weighted standard deviation for each field in a |
|
Compute the stretch deformation of a vector |
|
Return the indexes of the four surrounding grid points in a |
|
Compute the tangent |
|
Generate a field with the tangent of the latitudes in a |
|
Compute the pressure thickness on model levels in a |
|
Convert a |
|
Compute the pressure on model levels in a |
|
Compute the pressure thickness of model levels in a |
|
Perform a vertical integration for a |
|
Return the valid date(s) of a given |
|
Return the values from a data object |
|
Return the variance of all the fields in a |
|
Compute the area-weighted variance for each field in a |
|
Perform a vertical integration for a |
|
Compute the relative vorticity of a vector |