Procedural Python interface
From version 2.7 onwards Magics provides a procedural Python programming interface. This interface is identical to the Fortran and C interfaces in terms of functionality, with action routine names similar to the Fortran interface.
Converting From FORTRAN to Python
If you wish to copy an existing Fortran Magics program and change it to Python, then here are the things that you will need to change within the body of the program:
- The Magics interface needs to be imported at the start of your script using import Magics and all Magics commands need to be prefix with the namespace Magics. This is not strictly necessary but keeps a clear separation of Magics and other code.
- In order to derive the Python form of an action routine, take the FORTRAN routine name, change it to lower case and replace the initial P with the namespace Magics. There are few exception such as, POPEN becomes Magics.init() and PCLOSE becomes Magics.finalize().
- Strings in Python use double quotes, not single quotes (" not ').
- In Python, function names are case-sensitive, but the Magics parameter names are not.
- In FORTRAN, if a 2-dimensional array is declared as DIMENSION FIELD (NLON,NLAT), the equivalent in Python and follows the same convention as C. Please be aware that for arrays Magics uses NumPy.
Python Interface Example
The following example shows how to use Magics in a Python program.
import Magics Magics.init() Magics.setc("output_format", "ps") Magics.setc("output_name", "using_python01") Magics.setc("grib_input_type", "file") Magics.setc("grib_input_file_name", "data/z500.grb") Magics.grib() Magics.setc("map_coastline_colour", "khaki") Magics.setc("map_grid_colour", "grey") Magics.setc("contour", "on") Magics.setc("contour_line_colour", "sky") Magics.setc("contour_highlight_colour", "red") Magics.setc("contour_label", "on") Magics.cont() Magics.text () Magics.coast() Magics.new_page("super_page") Magics.setc("subpage_map_projection", "polar_stereographic") Magics.setr("subpage_lower_left_latitude", 18.51) Magics.setr("subpage_lower_left_longitude", -37.27) Magics.setr("subpage_upper_right_latitude", 51.28) Magics.setr("subpage_upper_right_longitude", 65.0) Magics.cont () Magics.text () Magics.coast() Magics.finalize()
Higher-level Python Interfaces
Additional to the procedural Python interface, Magics also offers since version 2.9 a higher level Python interface called MagMacro. This interface offers a more higher level and object-oriented interface, based on Metview Macro.
Python Interface Example
The following example shows how to use Magics in a Python program.
from magmacro import * #Definition of the output formats formats = output({'output_formats':['png','ps'], 'output_name':'using_python02'}) #Setting the coordinates of the geographical area europe = pmap({ "subpage_upper_right_longitude": 65., "subpage_map_projection": "polar_stereographic", "subpage_map_vertical_longitude": 0., "subpage_lower_left_longitude": -37.27, "subpage_lower_left_latitude": 18.51, "subpage_upper_right_latitude": 51.28}) coastlines = mcoast({'map_coastline_land_shade' : 'on', 'map_coastline_land_shade_colour' : 'grey', 'map_coastline_sea_shade' : 'on', 'map_coastline_sea_shade_colour' : 'white'}) #Import the z500 data z500 = pgrib({ "grib_input_file_name" : "data/z500.grb"}) #Define the simple contouring for z500 z500_contour = mcont({ "contour_level_selection_type": "interval", "contour_line_colour": "black", "contour_hilo_height": 0.25, "contour_line_thickness": 1, "contour_highlight_colour": "red", "contour_highlight_thickness": 2, "contour_interval": 5.}) #Do the plot plot(formats, europe, coastlines, z500, z500_contour)