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()

Computes the absolute value

absolute_vorticity()

Computes the absolute vorticity from a relative vorticity Fieldset

accumulate()

Adds up the values per field in a Fieldset

acos()

Computes the arc cosine

asin()

Computes the arc sine

atan()

Computes the arc tangent

atan2()

Computes the arc tangent of 2 Fieldset objects

average()

Computes the average per field in a Fieldset

average_ew()

Computes the zonal averages for each field in a Fieldset

average_ns()

Computes the meridional averages for each field in a Fieldset

base_date()

Returns the base date(s) of a given Fieldset

bearing()

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

bitmap()

Converts numbers to missing values in a Fieldset

convolve()

Performs spatial convolution on a Fieldset

corr_a()

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

cos()

Computes the cosine

coslat()

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

covar()

Returns the covariance of two Fieldset objects

covar_a()

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

datainfo()

Returns information on missing values in a Fieldset

dataset_to_fieldset()

Converts an xndarray dataset to a Fieldset

deacc()

De-accumulate values in a Fieldset

describe()

Prints summary of the contents of a Fieldset

direction()

Computes the wind direction

distance()

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

div()

Computes the integer part of a divison

divergence()

Computes the horizontal divergence of a vector Fieldset

exp()

Computes the exponential

fill_missing_values_ew()

Fills missing values along the horizontal line

find()

Find locations of values in a Fieldset

first_derivative_x()

Computes first West-East derivative of a Fieldset

first_derivative_y()

Computes first South-North derivative of a Fieldset

float()

Converts int GRIB to float GRIB

frequencies()

Computes the frequencies of a Fieldset

geostrophic_wind()

Computes the geostrophic wind on pressure levels in a Fieldset

gfind()

Finds values in field and returns the result as Geopoints

gradient()

Computes horizontal gradient of a Fieldset

grib_get()

Reads GRIB headers using ecCodes keys

grib_get_double()

Reads GRIB headers using ecCodes keys

grib_get_double_array()

Reads GRIB headers using ecCodes keys

grib_get_long()

Reads GRIB headers using ecCodes keys

grib_get_long_array()

Reads GRIB headers using ecCodes keys

grib_get_string()

Reads GRIB headers using ecCodes keys

grib_indexes()

Returns 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()

Sets GRIB packing bit width

grid_cell_area()

Computes the grid cell area in a Fieldset

indexes()

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

int()

Integer part

integer()

Converts float GRIB to int GRIB

integral()

Computes the surface integral of a Fieldset

integrate()

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

interpolate()

Interpolates Fieldset values to the specified location

laplacian()

Computes the horizontal Laplacian of Fieldset

latitudes()

Returns the latitudes of a Fieldset or Geopoints

log()

Computes the natural logarithm

log10()

Computes the base 10 logarithm

longitudes()

Returns the longitudes from a Fieldset or Geopoints

lookup()

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

ls()

Dumps metadata for each message in a Fieldset

mask()

Generates masks for a Fieldset or Geopoints

max()

Maximum

maxvalue()

Maximum value of a Fieldset

mean()

Returns the mean of the values in a Fieldset or Geopoints

mean_ew()

Generates 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()

Interpolates a model level Fieldset to height levels

mod()

Computes the integer remainder of a divison

mvl_geopotential_on_ml()

Computes the geopotential on model levels for a Fieldset

mvl_ml2hPa()

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

nearest_gridpoint()

Returns the nearest grid point value from a Fieldset

nearest_gridpoint_info()

Returns the nearest grid point value from a Fieldset

nobitmap()

Converts missing values to numbers in a Fieldset

pl_to_pl()

Interpolates pressure level fields to pressure levels

poly_mask()

Generates polygon masks for a Fieldset

pressure()

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

pressure_derivative()

Computes the verical pressure derivative

rmask()

Generates masks based on a radius around a point for Fieldset

rms()

Computes he pointwise root mean square for Fieldset

rms_a()

Computes the area-weighted root mean square for Fieldset

second_derivative_x()

Computes the second West-East derivative of a Fieldset

second_derivative_y()

Computes the econd South-North derivative of a Fieldset

select()

Filters Fieldset data by ecCodes keys with metadata indexing

set_latitudes()

Sets the latitudes in a Fieldset or Geopoints

set_longitudes()

Sets the longitudes in a Fieldset or Geopoints

set_values()

Sets the values in a Fieldset or Geopoints

sgn()

Computes the sign

shear_deformation()

Compute the shear deformation of a vector Fieldset

sin()

Computes the sine

sinlat()

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

smooth_gaussian()

Performs spatial smoothing on a Fieldset

smooth_n_points()

Performs spatial smoothing on a Fieldset

solar_zenith_angle()

Computes the solar zenith angle for a Fieldset

sort()

Sorts a Fieldset

sqrt()

Computes the square root

stdev()

Returns the standard deviation of all the fields in a Fieldset

stdev_a()

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

stretch_deformation()

Compute the stretch deformation of a vector Fieldset

sum()

Computes the sum of the values in a Fieldset or Geopoints

surrounding_points_indexes()

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

tan()

Computes the tangent

tanlat()

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

thickness()

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

to_dataset()

Converts a Fieldset to an xndarray dataset

unipressure()

Computes the pressure on model levels in a Fieldset

unithickness()

Computes the pressure thickness of model levels in a Fieldset

univertint()

Performs a vertical integration for a Fieldset

valid_date()

Returns the valid date(s) of a given Fieldset

values()

Returns the values from a data object

var()

Returns the variance of all the fields in a Fieldset

var_a()

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

vertint()

Performs a vertical integration for a Fieldset (deprecated)

vorticity()

Computes the relative vorticity of a vector Fieldset