Skip to end of metadata
Go to start of metadata

How to loop over all the fields in my fieldset?

Create a loop over the number of fields, and setting the parameter grib_field_position to the position of the field in your fieldset ( starting from 1) 

Python Example: Loop over over the fields in a grib fieldset
from Magics.macro import *

#Setting output
output = output(
    output_formats                = ['ps'],
    output_name                   = 'precip',
    output_name_first_page_number = 'off')
#Setting the coastlines
background = mcoast(
    map_coastline_land_shade        = 'on',
    map_coastline_land_shade_colour = 'cream')
#Defining the contour
contour = mcont(
)
#actions will contains the list of actions to pass to Magics
actions = []
# n is the number of filds in the fieldset
for i in range(1, n) :
	#Setting data
    grib = mgrib(grib_field_position = i,
          grib_input_file_name = "path/data.grib")
    actions.append(background)
    actions.append(grib)
    actions.append(contour)
	# Add an page() action to create a new page
    actions.append(page())
#Plotting
plot(output, actions)

To know the number of fields in your grib files, you can use the function grib_count of the grib_api package.

My field contains data from 0 to 100, but the contouring shows values below and above?

This is an effect of the interpolation, and easy way to get rid of it is to use the 2 following parameters contour_interpolation_floor and contour_interpolation_ceiling,

In this case mcont(contour_interpolation_floor = 0.,contour_interpolation_ceiling=100.). Check the mcont documentation.

How to remove the ECMWF Logo?

If you do not want to see the ECMWF logo on your plot, set the parameter page_id_line to off in the page object.

Python Example : Do not show theECMWF logo
from Magics.macro import *

#Setting output
output = output(
    output_formats                = ['ps'],
    output_name                   = 'precip',
    output_name_first_page_number = 'off')
#Setting the coastlines
background = mcoast(
    map_coastline_land_shade        = 'on',
    map_coastline_land_shade_colour = 'cream')
#Setting data
grib = mgrib(grib_input_file_name = "path/data.grib")
#Defining the contour
contour = mcont(
)
#Plotting
plot(output, background, grib, data)

How can I display some information of the GRIB header in my title?

When defining your text, you can use a special tag grib_info, Magics will then get the information from the grib header and replace the tag by the value read. You can use all the grib api keys, and Magics have a special one called valid-date that will compute the valid date for the data, taking into account the base date and the step.

Python Example : configure the title
from Magics.macro import *
#Setting output
output = output(
    output_formats                = ['ps'],
    output_name                   = 'precip',
    output_name_first_page_number = 'off')
#Setting the coastlines
background = mcoast(
    map_coastline_land_shade        = 'on',
    map_coastline_land_shade_colour = 'cream')
#Setting data
grib = mgrib(grib_input_file_name = "path/data.grib")
#Defining the contour
contour = mcont(
)
#Picking the grib metadata
title = mtext(
    text_lines                     = ["Parameter : <grib_info key='name'/> valid for  <grib_info key='valid-date'/>"],
	)     
#Plotting
plot(output, background, grib, data, title)

To get an idea of the available keys on your grib files, you can use the function grib_dump of the grib_api package.

Here is a short list of keys that we find useful, and are used in the generation of the automatioc Magics title

grib_api keydefinition
centreoriginating centre
namelong name of the parameter
shortNameshort name of the parameter
valid-datevalid date ( computed by Magics)
unitsunits
levellevel

How can I set the size of my PNG output?

Magics is internally working in cm. All the superpage, page and subpage dimension should be given in cm.  When working with png, you can set the parameter output_width to the number of pixel you want.  Magics will map this width with super_page_x_length to define a ratio and compute the height in pixel.

In the following example : we set the output_width to 1200, the page is the default A4 landscape. 1200 will match 29.7, the height will be computed to respect the aspect ratio ie, the height will be 848.

Python Example : resize a png
from Magics.macro import *
#Setting output
output = output(
    output_formats                = ['png'],
	output_name                   = 'size',
    output_name_first_page_number = 'off',
	output_width = 1200)
#Setting the coastlines
background = mcoast(
    map_coastline_land_shade        = 'on',
    map_coastline_land_shade_colour = 'cream')
#Setting data
grib = mgrib(grib_input_file_name = "path/data.grib")
#Defining the contour
contour = mcont(
)
#Plotting
plot(output, background, grib, data)

How can I set the DPI of my PNG output?

When printing, users are used to setting a DPI (dots per inch) for the quality of their output. Graphics on the web & screen, especially raster graphics, do not have the concept. There is only so much information which can be stored in a pixel. To get a higher quality raster plot you have to scale up the content and possibly increase the (pixel) size of you image (see above question). You can use image manipulation tools to set a DPI when you convert from vector to raster. So you could output your plot into PS or PDF and convert it using Imagemagick (see http://www.imagemagick.org/script/command-line-options.php#density)

For more information on this please read https://www.webdesignerdepot.com/2010/02/the-myth-of-dpi/.

Can I get a plot in in a portrait mode?

To do that, you will have to change the dimension of the super page ( the output) and of the page.

Let's say that you want an A4 in portrait mode. You will have to set  super_page_x_length and the page_x_length to 21., and super_page_y_length and the page_y_length to 29.7.

Python Example Portrait Mode
from Magics.macro import *

#Setting output
output = output(
    output_formats                = ['png'],
    output_name                   = 'portrait',
    output_name_first_page_number = 'off',
    super_page_x_length =  21.,
    super_page_y_length =  29.7,
    page_x_length =  21.,
    page_y_length =  29.7,
    )
area = mmap(subpage_lower_left_longitude = -30.,
        subpage_lower_left_latitude = -10.,
        subpage_upper_right_longitude = 50.,
        subpage_upper_right_latitude = 80.,
        )
#Setting the coastlines
background = mcoast(
    map_coastline_land_shade        = 'on',
    map_coastline_land_shade_colour = 'cream')

#Adding a title
title = mtext(text_lines = ["Plot in portrait mode"],
            text_font_size = 1. ,
            )

#Plotting
plot(output, area, background, title)

How can I apply a format to a number in Magics?

Several parameters accept a format : for historical reason Magics is using a fortran convention, with bracket around it,

Examples:

(I3) : Marks the place for an integer variable. It is followed by an integer indicating the number of columns to be reserved in the line for the integer. For example "i10", reserves 10 columns for output of the integer. If the integer contains less that 10 digits in this example, it is right justified in the reserved 10 columns and unused columns simply appear as spaces to the left of the integer.

(F5,2) : Marks the place for the decimal form of a real number. As with "i", it is followed by an integer reserving a number of columns for the number. This integer is followed immediately by a period ("."), acting as a separator rather than true decimal place. The period is immediately followed by another integer giving the number of digits to be printed (or read) to the right of the number's decimal. For example if the computer contains the number 11.23456, then "f10.3" in an output format results in a printed number " 11.234" (four spaces to the left of the number).



2 Comments

  1. hi, I'm a newbie.

    Data fields are passed to MAGICS using the routine PSET2R as follows.
    Contouring Fields:CALL PSET2R ('INPUT_FIELD', C, M, N)    

    This is written in FORTRAN,and how in python?

    1. "set2r" should do it. Question tracked as SUP-1475 - Getting issue details... STATUS