How to plot NetCDF Files with Magics

This tutorial will give view an overview of the possibilities offered by Magics to interpret and plot some NetCDF data.

You will have to create a netcdf object to help Magics to extract and interpret the data you want to plot. A list of the available parameters can be found in the NetCDF Action Documentation

This action will be combined with a projection definition, and a visualisation action to create a plot.

Magics can handle 2 type of geo-referenced NetCDF:

  • geomatrix: A geomatrix is a regular matrix, defined by 2 vectors [ 1 for latitudes, 1 for longitudes]
  • complex_matrix: A complex_matrix is defined by 2 matrices [1 2D-Matrix describing the latitudes, and 1 2D-Matrix defining the longitudes]
  • But, let's try an example.

    We have NetCDF file : A simple ncdump will give the following information

    netcdf data {
    dimensions:
        longitude = 360 ;
        latitude = 181 ;
        time = 1 ;
    variables:
        float longitude(longitude) ;
            longitude:units = "degrees_east" ;
            longitude:long_name = "longitude" ;
        float latitude(latitude) ;
            latitude:units = "degrees_north" ;
            latitude:long_name = "latitude" ;
        int time(time) ;
            time:units = "hours since 1900-01-01 00:00:0.0" ;
            time:long_name = "time" ;
            time:calendar = "gregorian" ;
        short t(time, latitude, longitude) ;
            t:scale_factor = 0.00100381797148002 ;
            t:add_offset = 267.106813154491 ;
            t:_FillValue = -32767s ;
            t:missing_value = -32767s ;
            t:units = "K" ;
            t:long_name = "Temperature" ;
            t:standard_name = "air_temperature" ;
    
    // global attributes:
            :Conventions = "CF-1.6" ;
            :history = "2016-09-16 16:52:53 GMT by grib_to_netcdf-1.15.0" ;
    }
    

    This simple example contains a regular field t, the geo-referencement is described by the 2 variables latitude and longitude . Let's pass this information to Magics through a mnetcdf object.

    First we import the Magics.macro package.

    In [2]:
    from Magics.macro import *
    

    Now, we have to define the geographical area we want to see in our final plot. Let's say that we focus on Europe in a geographical projection .. We need to create :

  • a mmap object to setup the geographical projection
  • a mcoast object to plot some coastlines
  • In [3]:
    area = mmap(
          subpage_map_projection = "cylindrical",
          subpage_lower_left_latitude = 30.00,
          subpage_lower_left_longitude = -20.00,
          subpage_upper_right_latitude = 60.00,
          subpage_upper_right_longitude = 20.00
    )
    
    coast = mcoast(map_coastline_land_shade = "on",
              map_coastline_land_shade_colour = "rgb(0.86,0.89,0.8)"
            )
    

    Now that these 2 objects have been created, we combine them in a plot command :

    In [4]:
    myplot(area, coast)
    
    Out[4]:

    Note, that you have to define the area before anything else, this will allow Magics to setup its working environment and extract the relevant information from the data. You can experiment setting the area, and playing with the different options of the mcoast object.

    Now that you are happy, let's add our data: It important to define the name of the variable you want to extract from the NetCDF and the names of the variables that describe the geo-referencement.

    In [5]:
    data = mnetcdf(netcdf_type = "geomatrix",
      netcdf_filename = "data.nc",
      netcdf_value_variable = "t",
      netcdf_latitude_variable = "latitude",
      netcdf_longitude_variable = "longitude"
    )
    

    Next step is to define some visual attributes. To apply a contour, you have to create a <a href ='https://software.ecmwf.int/wiki/display/MAGP/Contouring' > mcont object </a>. This object comes with a long list of options to setup contour intervals, colour, shading, etc. We start simple by just changing the colour of the isolines.

    In [6]:
    contour = mcont(contour_line_colour = "red")
    

    To get a plot, we add these new objects into the plot command.

    In [7]:
    myplot(area, coast, data, contour)
    
    Out[7]:

    What are this blue lines ? we asked for red! Magics has a concept of highlight isolines. Every nth isolines, Magics will use a different style/colour/thickness toplot the line. You can :

  • Turn that option off
  • In [8]:
    contour = mcont(contour_line_colour = "red",
                   contour_highlight = "off")
    myplot(area, coast, data, contour)
    
    Out[8]:
  • Or define your own highlight style
  • In [9]:
    contour = mcont(contour_line_colour = "red",
                   contour_highlight_colour = "red",
                   contour_highlight_thickness = 2)
    myplot(area, coast, data, contour)
    
    Out[9]:

    Nice, but what about a title ?

    Easy, you can use an mtext object .

    By default, the text will be positioned at the top of your plot.

    In [10]:
    text = mtext(text_lines = ["A Netcdf Data is plotted", "The plot is <font color='red'> red </font>"])
    

    Add this new object to the plot command..

    In [11]:
    myplot(area, coast, data, contour, text)
    
    Out[11]:

    Good, but what if you want to extract some information from the NetCDF data ?

    Magics has a special tag netcdf_info that you can use to do that ..

    Imagine you want to display the standard name and the units of your variable, you can redefine your text object

    In [12]:
    text = mtext(text_lines = ["Using <b>netcdf_info</b> to tailor my title", 
                  "The field <netcdf_info variable='t' attribute='standard_name'/> is displayed in <netcdf_info variable='t' attribute='units'/>"]
                )
    myplot(area, coast, data, contour, text)
    
    Out[12]:

    Let's now explore another functionality : you can extract a subset of your data using netcdf_dimension_setting .

    In [13]:
    data = mnetcdf(netcdf_type = "geomatrix",
      netcdf_filename = "data.nc",
      netcdf_value_variable = "t",
      netcdf_latitude_variable = "latitude",
      netcdf_longitude_variable = "longitude",
      netcdf_dimension_setting = ["latitude:40:50"]
    )
    myplot(area, coast, data, contour, text)
    
    Out[13]:
    In [ ]:
     
    
    In [ ]: