Convert Macros to Python

Note

The Macro to Python converter is available from Metview version 5.22.0

How to use the converter?

There are two different ways to perform the conversion.

Conversion from the icon context menu

In the user interface if we right-click on any Macro icon we can use the Convert to Python action.

../../_images/macro_convert_context_menu.png

It will generate a Python script in the same folder where the Macro is located. The name of the Python script is formed by adding the “.py” suffix to the file name or if the Macro name contains the “.mv” suffix it is replaced by “.py”. Subsequent calls will always generate a new Python script with an increment added to the file name.

../../_images/macro_convert_result_1.png

The conversion also works for multiple selected Macros:

../../_images/macro_convert_context_menu_multi.png

It the conversion fails for a given Macro its name turns red and the error message will be avialable in the icon Log (you can launch it from the icon context menu):

../../_images/macro_convert_error_log.png

Conversion from the Macro Editor

In the Macro editor we can use the Convert to Python action from the File menu in the menubar:

../../_images/macro_convert_from_editor.png

It will generate a Python script in the same folder where the Macro is located. The name of the Python script is generated by adding the “.py” suffix to the file name or if the Macro name contains the “.mv” suffix it is replaced by “.py”. Subsequent calls will always generate a new Python script with an increment added to the file name.

On success a popup dialog appears showing the path to the generated Python script. If an error happens the popup will contain the error message as to why the conversion failed.

How to adjust the results?

For simple Macros you should expect to get a correctly formatted and fully functional Python script. However, the converter cannot handle certain code structures and data types and it does not have any run-time information, so the generated code most probably will require further adjustments. If this happens a set of warnings will be added to the top of the Python script and in certain cases next to the affected code lines. Please check these warnings and modify your script accordingly. The following list gives you detailed advice on how to do it:

Unsupported features

Some features are unsupported in Metview Python and it is not possible to generate a working Python code out of them:

Feature

Comment

Inline C/Fortran

Macro supports embedded C and Fortran code via the inline keyword. In Metview Python this is unsupported. The converter simply puts the C or Fortran code inside a triple quoted string resulting in a correctly formatted but non-functional Python script.

matrix()

This method does not exists in Metview Python. You need to use 2D numpy arrays to convert your code.

Testing the generated code

Once the adjustments are done try to run the Python script and see if it generates the same output as your Macro. If it does then congratulations you can start using your nice new Python script. However, if the results differ or the script fails please let the developers know about it by reporting it via the ECMWF Support Portal.