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.
...
More examples are available in the Metview Python Gallery (Redundant).
Variable types
When calling Macro functions from Python, variables passed as input to or output from those functions undergo a conversion as described in this table:
...
Any Metview function that normally returns a vector will return a numPy array when called from Python. For example, the follownig fieldset functions return numPy arrays:
Code Block | ||
---|---|---|
| ||
a = mv.read('my_data.grib') # returns a Fieldset lats = mv.latitudes(a) # returns a numPy array lons = mv.longitudes(a) # returns a numPy array vals = mv.values(a) # returns a numPy array |
Pandas Dataframes
The Using Metview's Python Interface Geopoints data type has an additional function, to_dataframe()
, which can be used to produce a Pandas Dataframe object as shown:
...
- 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]
. - 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 Using Metview's Python Interface Layoutx3 In Python gallery example.