Sie sind hier: Startseite / Services / Data Analysis and Visualization / Visualization / Software / NCL / source codes / PyNGL example rectilinear grid contour plot
Info
Alle Inhalte des Nutzerportal sind nur auf Englisch verfügbar.

PyNGL example rectilinear grid contour plot

The script uses the PyNGL python package to generate a contour plot of rectilinear gridded data. The data is read from a netCDF file with PyNIO Nio.open_file function.

Software requirements:

  • Python 2.7.x
  • Numpy 1.9.2
  • PyNGL/PyNIO 1.5.0

 

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)

 2015-06-04  meier-fleischer(at)dkrz.de
'''
import numpy,sys,os
import Nio
import Ngl

#--  define variables
diri   = "./"                                  #-- 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
f      =  Nio.open_file(diri + fname,"r")      #-- open data file
temp   =  f.variables["tsurf"][0,::-1,:]       #-- first time step, reverse latitude
lat    =  f.variables["lat"][::-1]             #-- reverse latitudes
lon    =  f.variables["lon"][:]                #-- all longitudes

tempac =  Ngl.add_cyclic(temp[:,:])

#-- 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

if hasattr(f.variables["tsurf"],"long_name"):
   res.tiMainString = f.variables["tsurf"].long_name  #-- if long_name attribute exists use it as 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()

Plot result:

PyNGL rectilinear grid contour plot w400

 

 

Artikelaktionen