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 indices

It 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() and describe() methods. See the Inspecting GRIB data notebook for some examples.

Filtering

A set of fields from a Fieldset can be extracted using the read() and select() 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 of

g = 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 differences

Similarly, operations work between Fieldsets and numbers, for example:

temperature_in_K = mv.read("temp.grib")
temperature_in_C = temperature_in_K - 273.15

The 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.

Table Title

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

abs()

Compute the absolute value

absolute_vorticity()

Compute the absolute vorticity from a relative vorticity Fieldset

accumulate()

Add up the values per field in a Fieldset

acos()

Compute the arc cosine

asin()

Compute the arc sine

atan()

Compute the arc tangent

atan2()

Compute the arc tangent of 2 Fieldset objects

average()

Compute the average per field in a Fieldset

average_ew()

Compute the zonal averages for each field in a Fieldset

average_ns()

Compute the meridional averages for each field in a Fieldset

base_date()

Return the base date(s) of a given Fieldset

bearing()

Compute the bearings with respect to a reference in a Fieldset point

bitmap()

Convert numbers to missing values in a Fieldset

convolve()

Perform spatial convolution on a Fieldset

corr_a()

Compute the area-weighted correlation for each field in a Fieldset

cos()

Compute the cosine

coslat()

Generate a field with the cosine of the latitudes in a Fieldset

covar()

Return the covariance of two Fieldset objects

covar_a()

Compute the area-weighted covariance for each field in a Fieldset

datainfo()

Return information on missing values in a Fieldset

dataset_to_fieldset()

Convert an xndarray dataset to a Fieldset

deacc()

De-accumulate values in a Fieldset

describe()

Print summary of the contents of a Fieldset

direction()

Compute the wind direction

distance()

Compute the distances in a Fieldset or Geopoints to a reference point

div()

Compute the integer part of a divison

divergence()

Compute the horizontal divergence of a vector Fieldset

exp()

Compute the exponential

fill_missing_values_ew()

Fills missing values along the horizontal line

find()

Find locations of values in a Fieldset

first_derivative_x()

Compute first West-East derivative of a Fieldset

first_derivative_y()

Compute first South-North derivative of a Fieldset

float()

Convert int GRIB to float GRIB

frequencies()

Compute the frequencies of a Fieldset

geostrophic_wind()

Compute the geostrophic wind on pressure levels in a Fieldset

gfind()

Find values in field and returns the result as Geopoints

gradient()

Compute horizontal gradient of a Fieldset

grib_get()

Read GRIB headers using ecCodes keys

grib_get_double()

Read GRIB headers using ecCodes keys

grib_get_double_array()

Read GRIB headers using ecCodes keys

grib_get_long()

Read GRIB headers using ecCodes keys

grib_get_long_array()

Read GRIB headers using ecCodes keys

grib_get_string()

Read GRIB headers using ecCodes keys

grib_indexes()

Return list of indexes into the fields of the Fieldset

grib_set()

Writes GRIB headers using ecCodes keys

grib_set_double()

Writes GRIB headers using ecCodes keys

grib_set_long()

Writes GRIB headers using ecCodes keys

grib_set_long_array()

Writes GRIB headers using ecCodes keys

grib_set_string()

Writes GRIB headers using ecCodes keys

gribsetbits()

Set GRIB packing bit width

grid_cell_area()

Compute the grid cell area in a Fieldset

indexes()

Build a Fieldset containing each gridpoint’s indexed position in the given vector

int()

Integer part

integer()

Convert float GRIB to int GRIB

integral()

Compute the surface integral of a Fieldset

integrate()

Compute the average weighted by the gridcell area for each field in Fieldset

interpolate()

Interpolate Fieldset values to the specified location

laplacian()

Compute the horizontal Laplacian of Fieldset

latitudes()

Return the latitudes of a Fieldset or Geopoints

log()

Compute the natural logarithm

log10()

Compute the base 10 logarithm

longitudes()

Return the longitudes from a Fieldset or Geopoints

lookup()

Build an output Fieldset using the values in the first as indices into the second

ls()

Dumps metadata for each message in a Fieldset

mask()

Generate masks for a Fieldset or Geopoints

max()

Maximum

maxvalue()

Maximum value of a Fieldset

mean()

Return the mean of the values in a Fieldset or Geopoints

mean_ew()

Generate a Fieldset out of East-West means

merge()

Merges 2 sets of Fieldset or Geopoints

min()

Minimum

minvalue()

Minimum value of a Fieldset or Geopoints

ml_to_hl()

Interpolate a model level Fieldset to height levels

mod()

Compute the integer remainder of a divison

mvl_geopotential_on_ml()

Compute the geopotential on model levels for a Fieldset

mvl_ml2hPa()

Interpolate a Fieldset on model levels to pressure levels (in hPa)

nearest_gridpoint()

Return the nearest grid point value from a Fieldset

nearest_gridpoint_info()

Return the nearest grid point value from a Fieldset

nobitmap()

Convert missing values to numbers in a Fieldset

pl_to_hl()

Interpolate a pressure level Fieldset to height levels

pl_to_pl()

Interpolate pressure level fields to pressure levels

poly_mask()

Generate polygon masks for a Fieldset

pressure()

Compute the pressure on model levels in a Fieldset (deprecated)

pressure_derivative()

Compute the verical pressure derivative

rmask()

Generate masks based on a radius around a point for Fieldset

rms()

Compute he pointwise root mean square for Fieldset

rms_a()

Compute the area-weighted root mean square for Fieldset

second_derivative_x()

Compute the second West-East derivative of a Fieldset

second_derivative_y()

Compute the econd South-North derivative of a Fieldset

select()

Filter Fieldset data by ecCodes keys with metadata indexing

set_latitudes()

Set the latitudes in a Fieldset or Geopoints

set_longitudes()

Set the longitudes in a Fieldset or Geopoints

set_values()

Set the values in a Fieldset or Geopoints

sgn()

Compute the sign

shear_deformation()

Compute the shear deformation of a vector Fieldset

sin()

Compute the sine

sinlat()

Generate a field with the cosine of the latitudes in a Fieldset

smooth_gaussian()

Perform spatial smoothing on a Fieldset

smooth_n_points()

Perform spatial smoothing on a Fieldset

solar_zenith_angle()

Compute the solar zenith angle for a Fieldset

sort()

Sort a Fieldset

sqrt()

Compute the square root

stdev()

Return the standard deviation of all the fields in a Fieldset

stdev_a()

Compute the area-weighted standard deviation for each field in a Fieldset

stretch_deformation()

Compute the stretch deformation of a vector Fieldset

sum()

Compute the sum of the values in a Fieldset or Geopoints

surrounding_points_indexes()

Return the indexes of the four surrounding grid points in a Fieldset

tan()

Compute the tangent

tanlat()

Generate a field with the tangent of the latitudes in a Fieldset

thickness()

Compute the pressure thickness on model levels in a Fieldset (deprecated)

to_dataset()

Convert a Fieldset to an xndarray dataset

unipressure()

Compute the pressure on model levels in a Fieldset

unithickness()

Compute the pressure thickness of model levels in a Fieldset

univertint()

Perform a vertical integration for a Fieldset

valid_date()

Return the valid date(s) of a given Fieldset

values()

Return the values from a data object

var()

Return the variance of all the fields in a Fieldset

var_a()

Compute the area-weighted variance for each field in a Fieldset

vertint()

Perform a vertical integration for a Fieldset (deprecated)

vorticity()

Compute the relative vorticity of a vector Fieldset