Metview's documentation is now on readthedocs!

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

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.

MacroPython
# 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:

MacroPythonNotes
numbernumber
stringstring
listlistCan also pass a tuple to Macro, and it will be converted to a Macro list
fieldsetFieldsetLightweight wrapper class in Meview-Python
geopointsGeopointsLightweight wrapper class in Meview-Python
observationsBufrLightweight wrapper class in Meview-Python
netcdfNetCDFLightweight wrapper class in Meview-Python
odbOdbLightweight wrapper class in Meview-Python
tableTableLightweight wrapper class in Meview-Python
vectornumPy array
datedatetimeCan also pass a date or a datetime64 to Macro
definitiondictionary
nilNone

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:

MethodExample
Named argumentsa = mv.retrieve(type = 'fc', date = -5)
Dictionary

params = {'type' : 'fc', 'date' : -5}
a = mv.retrieve(params)

Combination

common_params = {'type' : 'fc'}
a = mv.retrieve(common_params, date = -5)

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:

FunctionObject method

# find the locations where t is > 310K

t = mv.retrieve(param = 't', grid = [1,1])
hot = t > 310
locs = find(hot, 1)

# find the locations where t is > 310K

t = mv.retrieve(param = 't', grid = [1,1])
hot = t > 310
locs = hot.find(1)



  • No labels