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

Compare with Current View Page History

« Previous Version 9 Next »

How to loop over all the fields in my fieldset?

Step-by-step guide

  1. 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.



  • No labels