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 |
---|---|
# Metview Macro t2m_fc48 = retrieve( type : "fc", levtype : "sfc", param : "2t", date : -5, step : 48, grid : "o1280") synop = retrieve( type : "ob", repres : "bu", date : -3) synop_t2m = obsfilter( output : "geopoints", parameter : 012004, data : synop) diff = t2m_fc48 - synop_t2m print(integrate(diff)) | import metview as mv t2m_fc48 = mv.retrieve( type = "fc", levtype = "sfc", param = "2t", date = -5, step = 48, grid = "o1280") synop = mv.retrieve( type = "ob", repres = "bu", date = -3) synop_t2m = mv.obsfilter( output = "geopoints", parameter = "012004", data = synop) diff = t2m_fc48 - synop_t2m print(mv.integrate(diff)) |
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.
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:
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 |
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:
Method | Example |
---|---|
Named arguments | a = mv.retrieve(type = 'fc', date = -5) |
Dictionary |
|
Combination |
|
Object-oriented calling
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 |
---|---|
fs = mv.retrieve(grid = [1,1]) locs = mv.find(fs, 273.15) | fs = mv.retrieve(grid = [1,1]) locs = fs.find(273.15) |