Info |
---|
Complete documentation for Metview's Python interface is now available on readthedocs! |
Metview's Python interface provides access to all of Metview's Macro language functions, with automatic translations between data types. Here's an example that shows how to retrieve model and observation data and compute their difference, and the weighted mean value of that difference, both in Macro and in Python.
...
Macro | Python | Notes |
---|---|---|
number | number | |
string | string | |
list | list | Can also pass a tuple to Macro, and it will be converted to a Macro list |
fieldset | Fieldset | Lightweight wrapper class in Meview-Python |
geopoints | Geopoints | Lightweight wrapper class in Meview-Python |
observations | Bufr | Lightweight wrapper class in Meview-Python |
netcdf | NetCDF | Lightweight wrapper class in Meview-Python |
odb | Odb | Lightweight wrapper class in Meview-Python |
table | Table | Lightweight wrapper class in Meview-Python |
vector | numPy array | |
date | datetime | Can also pass a date or a datetime64 to Macro |
definition | dictionary | |
nil | None |
Working with Fieldsets
Fieldsets work much the same as they do in the Macro language, but watch out for these things:
- length of a fieldset can be found with the
len
function:num_fields = len(my_fieldset)
- indexing starts at 0:
first_field = my_fieldset[0]
- slicing works:
my_fields = fs[0:6:2]
- you can pass a numpy array of indexes:
my_fields = fs[np.array([1.0, 2.0, 0.0, 5.0])]
- comparison operators work the same as in Macro, i.e. they return a fieldset of 1s and 0s:
smaller = fs1 < fs2
- equality and non-equality operators are
==
and!=
- Fieldsets can be directly constructed either as empty, or with a path to a GRIB file:
f = mv.Fieldset()
f = mv.Fieldset(path='test.grib')
- concatenation can be done like this:
my_fieldset.append(my_other_fieldset)
- iteration works:
for f in my_fieldset: #do something
Working with Geopoints
Geopoints also work much the same as they do in Macro, but be aware of these points:
- in Macro, we use
geo_missing_value
to denote missing data values; in Python, we usenumpy.nan
Working with dates
There are several differences between the usage of the date object in Macro and the datetime object in Python. You will find a few examples below to compare the various date manipulation techniques used in Macro and Python, respectively.
Macro | Python | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Additional data export features
...
Any Metview function that normally returns a vector will return a numPy array when called from Python. dtypes of float32 and float64 are supported. For example, the follownig following fieldset functions return numPy arrays:
...
Code Block | ||
---|---|---|
| ||
<xarray.Dataset> Dimensions: (latitude: 181, longitude: 360, step: 9, time: 1) Coordinates: * time (time) datetime64[ns] 2018-05-10T12:00:00 * step (step) timedelta64[ns] 0 days 00:00:00 0 days 06:00:00 ... * latitude (latitude) float64 90.0 89.0 88.0 87.0 86.0 85.0 84.0 83.0 ... * longitude (longitude) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ... Data variables: 2t (time, step, latitude, longitude) float32 ... 2d (time, step, latitude, longitude) float32 ... Attributes: Conventions: CF-1.7 comment: GRIB to CF translation performed by xarray-grib |
This is based on the xarray GRIB driver, which is not yet available outside of Metview, but is planned to be released and ultimately integrated into xarray.Open Source cfgrib package which is planned to be ultimately integrated into xarray.
From version 0.8.6 of Metview's Python interface, it is also possible to convert an xarray dataset to a Metview Fieldset, and also to pass one directly to Metview functions. Note that this uses an experimental feature of cfgrib and will only work for a small subsset of xarray datasets. Examples:
Code Block | ||
---|---|---|
| ||
grib = mv.read('temperature_on_pl.grib')
x = grib.to_dataset() # convert from Fieldset to dataset
f = mv.dataset_to_fieldset(x) # convert back to Fieldset |
Code Block | ||
---|---|---|
| ||
grib = mv.read('temperature_on_pl.grib')
x = grib.to_dataset() # x is now an xarray dataset
fs = mv.mean(x*2) # *2 is done by xarray, mean() is done by Metview |
Icon functions
Macro functions which correspond to icons, such as retrieve()
, which corresponds to the Mars Retrieval icon, can take their arguments in a number of ways:
...
- Macro indexing starts at 1, but Python indexing starts at 0. Aside from the standard Python structures such as lists, this is also true for Metview Python classes such as Fieldset. Given a fieldset
fs
, the first field is obtained in Macro byfs[1]
, but in Python it is obtained byfs[0]
. - Some MARS requests require a class parameter. As class is a restricted keyword in Python, please use class_ in its place.
- In order to support the more interactive coding environments provided by Python, any call to the
plot()
command will immediately produce a plot. This is different from Macro, where plots are delayed until the end. The result of this is that multipleplot()
commands in Python will result in multiple plot windows. Fortunately, a singleplot()
command can be given any number of items, including multiple pages. To see how to produce a multi-plot layout, please see the Layoutx3 Example gallery example. - To produce multiple physical pages in a PostScript file, add calls to
newpage()
within theplot()
command, e.g.plot(dw, data1, visdef1, newpage(), data2, visdef2)
.