Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Python Interfaces

Magics offers since version 2.9.0 a high-level Python (2 and 3) interface called Magics.macro. This interface offers a object-oriented interface, based on Metview METV Macro. You can use pip or conda to install it.

Python Interface Example

The following example shows how to use Magics in a Python program. 

Code Block
languagepython
titleSimple Python program
linenumberstrue
from Magics.macro import *

#Definition of the output formats
formats = output({'
	output_formats':=['png','ps'],
	'output_name':='using_python02'})

#Setting the coordinates of the geographical area
europe = pmapmmap({
	"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 = pgribmgrib({ "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)

 

 

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:

...

,

...

Python Interface Example

The following example shows how to use Magics in a Python program.

 

 

Code Block
languagepython
titleSimple Python example
linenumberstrue
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()

 

...