SCDzine
Spring 1999, Vol. 20, No. 1
S I D E B A R

GSUN: Graphics made easier in NCL

Example 2: How to draw contour plots using GSUN

The NCL script below is a more complex example using GSUN functions. The script reads in data from a netCDF file, draws a contour plot in the first frame, and draws the same contour plot over a map in the second frame.

This NCL script sets many resources for changing the size of a plot in the viewport, changing the foreground and background colors, and customizing and annotating the contour and map plots.


load "$NCARG_ROOT/lib/ncarg/nclex/gsun/gsn_code.ncl"  

begin

  data_dir = "$NCARG_ROOT/lib/ncarg/data/cdf/" ; Directory containing
                                               ; netCDF file.
  filename = "sstdata_netcdf.nc"               ; netCDF file name

  ncfile = addfile(data_dir + filename,"r")    ; Open netCDF file.

  sst = ncfile->sst(0,:,:)  ; Store first month of data into variable "sst".
  lat = ncfile->lat         ; Store latitude values into variable "lat".
  lon = ncfile->lon         ; Store longitude values into variable "lon"

  wks = gsn_open_wks("x11","contour_example")   ; Open a workstation.

  setvalues wks
    "wkColorMap"        : "temp1"   ; Change color map to predefined one
                                    ; called "temp1".
    "wkBackgroundColor" : (/1.,1.,1./)  ; Change background to white.
    "wkForegroundColor" : (/0.,0.,0./)  ; Change foreground to black.  
  end setvalues

  cnres              = True                  ; Indicate we want to set
                                             ; some resources.

  cnres@tiMainString = "STR SST Climatology (:S:o:N:C)" ; Create a main title.

  cnres@cnFillOn     = True            ; Turn on contour fill.
  cnres@cnFillColors = ispan(4,64,3)   ; Use colors 4,7,10,...,64 for fill.

  contour = gsn_contour(wks,sst,cnres) ; Create and draw contour plot. This
                                       ; function also advances the frame.

; This next section of NCL code sets additional resources necessary
; to overlay a contour plot on a map, add a labelbar, and change the
; size of the plot in the viewport.  Note that the previous resources
; that were set will still apply.

  cnres@vpXF      = 0.10     ; X position of upper left corner of plot.
  cnres@vpYF      = 0.85     ; Y position of upper left corner of plot.
  cnres@vpWidthF  = 0.70     ; Make the width and height
  cnres@vpHeightF = 0.70     ; of plot larger.

  cnres@sfXCStartV = min(lon)    ; Indicate where to overlay contour
  cnres@sfXCEndV   = max(lon)    ; plot on map, by using lat/lon 
  cnres@sfYCStartV = min(lat)    ; values read in above.
  cnres@sfYCEndV   = max(lat)

  cnres@pmLabelBarDisplayMode = "Always"   ; Turn on a labelbar.
  cnres@lbPerimOn             = False      ; Turn off labelbar perimeter.

  cnres@cnInfoLabelOn         = False      ; Turn off contour info label.
  cnres@cnLineLabelsOn        = False      ; Turn off contour line labels.
  cnres@cnLinesOn             = False      ; Turn off contour lines.

  cnres@mpProjection          = "Orthographic"  ; Change map projection.
  cnres@mpGridAndLimbOn       = False           ; Turn off grid lines.

  contour = gsn_contour_map(wks,sst,cnres) ; Create and draw the contour
                                           ; plot over a map. This function
                                           ; also advances the frame.
end


The above NCL script should produce the following two frames of graphical output. (Click on either frame to see it enlarged.)

graph graph


Back Back to "GSUN: Graphics made easier in NCL"

rule
Contents || SCDzine || Search || Article index || Subscribe || Contact us || SCD