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 | ||
---|---|---|---|
|
|
You can see that the Macro functions are all available through the namespace defined in the import
command.
More examples are available in the Python Gallery.
When calling Macro functions from Python, variables passed as input to or output from those functions undergo a conversion as described in this table:
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 |
Any Metview function that normally returns a vector will return a numPy array when called from Python. For example:
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 |
The Geopoints data type as an additional function, to_dataframe()
, which can be used as shown:
import metview as mv import pandas as pd gpt = mv.read("gpts.gpt") # returns a Geopoints df = gpt.to_dataframe() # returns a Pandas Dataframe print(df.head()) |
Output:
date latitude level longitude value 0 2018-01-14 12:00:00 30.0 1000.0 -24.0 288.736 1 2018-01-14 12:00:00 30.0 1000.0 -18.0 288.736 2 2018-01-14 12:00:00 30.0 1000.0 -12.0 286.736 3 2018-01-14 12:00:00 30.0 1000.0 -6.0 NaN 4 2018-01-14 12:00:00 30.0 1000.0 0.0 NaN |
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:
Method | Example |
---|---|
Named arguments | a = mv.retrieve(type = 'fc', date = -5) |
Dictionary |
|
Combination |
|
Metview's Python interface also provides a more object-oriented way of using the wrapper classes such as Fieldset. The following two snippets of code are equivalent:
Function | Object method |
---|---|
|
|
Any user-defined Macro function can be called from Python as long as it satisfies both of these criteria:
METVIEW_MACRO_PATH
environment variable; this variable can be modified by the user, but by default it will include $HOME/metview/System/Macros
.If these are satisfied, then the function may be invoked using the call()
command as shown here:
import metview as mv mv.call('my_func', 4, 1, 'my_string_arg')) |
The first argument is the name of the function, the subsequent arguments will be passed to the function.
Although it's easy to convert an existing macro into a Python script, there are some 'gotchas' to be aware of.
fs
, the first field is obtained in Macro by fs[1]
, but in Python it is obtained by fs[0]
.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 multiple plot()
commands in Python will result in multiple plot windows. Fortunately, a single plot()
command can be given any number of items, including multiple pages. To see how to produce a multi-plot layout, please see the Layoutx3 In Python gallery example.