PyNGL example rectilinear grid contour plotΒΆ

Software requirements:

  • Python 2 or 3

  • PyNGL 1.6.1

  • xarray

Run the rectilinear grid contour example script:

python PyNGL_rectilinear_contour.py

Script PyNGL_rectilinear_contour.py:

'''
DKRZ PyNGL Script: PyNGL_rectilinear_contour.py

Description: Python script using PyNGL Python module

 - contour plot on map (rectilinear data)

09.02.21 meier-fleischer(at)dkrz.de
'''
import xarray as xr
import Ngl

#-----------------------------------------------------------------------
#-- Function: main
#-----------------------------------------------------------------------
def main():

    #-- define variables
    diri = '/Users/k204045/NCL/PyNGL/User_Guide_examples/'  #-- data directory
    fname = 'rectilinear_grid_2D.nc' #-- data file name

    minval = 250.                  #-- minimum contour level
    maxval = 315                   #-- maximum contour level
    inc = 5.                       #-- contour level spacing
    ncn = (maxval-minval)/inc + 1  #-- number of contour levels

    #-- open file and read variables

    ds = xr.open_dataset(diri + fname)

    temp = ds.tsurf[0,::-1,:]
    lat = ds.lat[::-1]
    lon = ds.lon[:]

    tempac = Ngl.add_cyclic(temp[:,:])  #-- add cyclic points

    #-- open a workstation
    wkres = Ngl.Resources()     #-- generate an res object for workstation
    wkres.wkWidth = 1024        #-- plot resolution 2500 pixel width
    wkres.wkHeight = 1024       #-- plot resolution 2500 pixel height
    wkres.wkColorMap = 'rainbow'  #-- choose colormap
    wks_type = 'png'            #-- graphics output type

    wks = Ngl.open_wks(wks_type,'Py_rectilinear_contour',wkres)  #-- open workstation

    #-- set resources
    res = Ngl.Resources()       #-- generate an resource object for plot

    res.tiMainString = ds.tsurf.long_name  #-- title

    #-- viewport resources
    res.vpXF = 0.1              #-- start x-position of viewport
    res.vpYF = 0.9              #-- start y-position of viewport
    res.vpWidthF = 0.7          #-- width of viewport
    res.vpHeightF = 0.7         #-- height of viewport

    #-- contour resources
    res.cnFillOn = True         #-- turn on contour fill
    res.cnLineLabelsOn = False  #-- turn off line labels
    res.cnInfoLabelOn = False   #-- turn off info label
    res.cnLevelSelectionMode = "ManualLevels" #-- select manual level selection mode
    res.cnMinLevelValF = minval #-- minimum contour value
    res.cnMaxLevelValF = maxval #-- maximum contour value
    res.cnLevelSpacingF = inc   #-- contour increment

    #-- grid data information resources
    res.sfXCStartV = float(min(lon)) #-- x-axis location of 1st element lon
    res.sfXCEndV = float(max(lon))   #-- x-axis location of last element lon
    res.sfYCStartV = float(min(lat)) #-- y-axis location of 1st element lat
    res.sfYCEndV = float(max(lat))   #-- y-axis location of last element lat

    #-- labelbar resources
    res.pmLabelBarDisplayMode = "Always" #-- turn on the label bar
    res.lbOrientation = "Horizontal"     #-- labelbar orientation

    #-- draw contours over a map
    map = Ngl.contour_map(wks,tempac,res)

    #-- done
    Ngl.end()

if __name__ == '__main__':
    main()

Result:

PyNGL rectilinear grid contour plot w400