Sie sind hier: Startseite / Services / Data Analysis and Visualization / Visualization / Software / NCL / examples / source_code / DKRZ NCL Workshop: caption and annotation
Info
Alle Inhalte des Nutzerportal sind nur auf Englisch verfügbar.

DKRZ NCL Workshop: caption and annotation

All caption and annotation exercises from the DKRZ NCL Workshop. Requirement: NCL version 6.2.0 and higher

 

1. Map annotations:

begin
;-- read the data and define var
  f   =  addfile("$NCL_TUT/data/ECHAM5_OM_A1B_2001_0101-1001_2D.nc","r")
  var =  f->tsurf(0,:,:)

;-- define the workstation (plot type and name)
  wks = gsn_open_wks("png","part_VII_map_annotation")

;-- set resources
  mres1                       =  True
  mres1@gsnDraw               =  False                  ;-- don't draw the plot yet
  mres1@gsnFrame              =  False                  ;-- don't advance the frame
  mres1@gsnMaximize           =  True                   ;-- maximize the plot output

  mres1@cnFillOn              =  True                   ;-- filled contours
  mres1@cnLinesOn             =  False                  ;-- don't draw contour lines

  mres1@mpDataBaseVersion     = "MediumRes"             ;-- set map data base
  mres1@mpOutlineOn           =  True                   ;-- turn map outline on
  mres1@mpGeophysicalLineThicknessF = 2                 ;-- increase coastline thickness
  mres1@mpMinLonF             = -30.                    ;-- min lon
  mres1@mpMaxLonF             =  30.                    ;-- max lon
  mres1@mpMinLatF             =  30.                    ;-- min lat
  mres1@mpMaxLatF             =  80.                    ;-- max lat

  mres1@lbBoxMinorExtentF     =  0.15                   ;-- decrease height of labelbar

  mres1@tiMainString          = "DKRZ NCL workshop:  map annotation"   ;-- title string
  mres1@tiMainFontHeightF     =  0.02                   ;-- main title font size

;-- draw the main map
  map1 = gsn_csm_contour_map(wks, var, mres1)

;-- set resources for the smaller world map
  mres2                       =  True
  mres2@gsnDraw               =  False                  ;-- don't draw the plot yet
  mres2@gsnFrame              =  False                  ;-- don't advance the frame
  mres2@gsnLeftString         =  ""                     ;-- don't draw left string
  mres2@gsnRightString        =  ""                     ;-- don't draw right string

  mres2@vpWidthF              =  0.3                    ;-- set viewport width
  mres2@vpHeightF             =  0.3                    ;-- set viewport height

  mres2@mpLandFillColor       = "Gray75"                ;-- land fill color
  mres2@mpOceanFillColor      = "White"                 ;-- ocean fill color
  mres2@mpInlandWaterFillColor = "White"                ;-- inland water color 

  mres2@tmXBOn                =  False                  ;-- turn off tickmarks at bottom
  mres2@tmXTOn                =  False                  ;-- turn off tickmarks at top
  mres2@tmYLOn                =  False                  ;-- turn off tickmarks at left side
  mres2@tmYROn                =  False                  ;-- turn off tickmarks at right side

;-- draw the smaller world map
  map2 = gsn_csm_map(wks, mres2)

;-- set polyline resources
  pres                      =  True
  pres@gsLineThicknessF     =  2.0                      ;-- polyline thickness
  pres@gsLineColor          = "blue"                    ;-- polyline color

;-- attach polyline to map2
  pbox = gsn_add_polyline(wks, map2, (/-30,30,30,-30,-30/), (/30,30,80,80,30/), pres)

;-- set annotation resources                                      
  amres                     =  True
  amres@amJust              = "TopLeft"                ;-- labelbar justification
  amres@amParallelPosF      = -0.5                     ;-- move labelbar to the right of plot
  amres@amOrthogonalPosF    = -0.5                     ;-- move labelbar to the top of plot

;-- atach the labelbar to the plot
  annoid = gsn_add_annotation(map1,map2,amres)

;-- set text resources
  txres1                     =  True
  txres1@txFontHeightF       =  0.03
  txres1@txAngleF            =  40.                     ;-- rotate text string 40 degrees

;-- attach text to map1
  text1 = gsn_add_text(wks,map1,"Atlantic",-20,45,txres1)

;-- set text resources
  txres2                     =  True
  txres2@txFontHeightF       =  0.03
  txres2@txAngleF            =  -50.                     ;-- rotate text string 40 degrees

;-- attach text to map1
  text2 = gsn_add_text(wks,map1,"North Sea",1.0,61,txres2)

;-- draw the plot and advance the frame
  draw(map1)
  frame(wks)

end 

 

2. Labelbar/Legend annotation:

begin
;-- create x-, y-values, label and color lists
  llab = (/"a","b","c","d","e","f","g","h","i","j"/)
  cols = (/"firebrick4","red","orange","yellow","green","darkgreen", \
                                "cyan","DeepSkyBlue","blue","midnightblue"/)
  nlb  = dimsizes(cols)                       ;-- number of labelbar boxes

  x    = (/1,2,3,4,5,6,7,8,9,10/)
  y    = (/32,25,35,21,28,31,11,19,16,14/)

;-- open a workstation
  wks = gsn_open_wks("png","part_VII_legend_annotation")

;-- set plot resources
  res                       =  True      
  res@gsnMaximize           =  True           ;-- maximize plot output
  res@gsnDraw               =  False          ;-- don't draw plot yet
  res@gsnFrame              =  False          ;-- don't advance frame yet
  res@gsnXYBarChart         =  True           ;-- turn on bar chart
  res@gsnXYBarChartBarWidth =  0.4            ;-- set width of bins
  res@gsnXYBarChartColors   =  cols           ;-- set the colors for the bins
  res@trYMinF               =  0              ;-- start bins from bottom
  res@trYMaxF               =  max(y)+5       ;-- start bins from bottom
  res@trXMinF               =  0              ;-- start at x-value 0
  res@trXMaxF               =  11             ;-- end at x-value 11

  res@tiMainString          = "DKRZ NCL workshop:  Labelbar Annotation"

;-- create the plot, but don't draw it yet
  plot = gsn_csm_xy (wks,x,y,res)

;-- get viewport width and height of plot
  getvalues plot
    "vpWidthF"  :    vpw
    "vpHeightF" :    vph
  end getvalues

;-- set labelbar resources
  lres                      =  True
  lres@lbAutoManage         =  True             ;-- necessary to control sizes
  lres@lbFillColors         =  cols             ;-- labelbar colors
  lres@vpWidthF             =  0.2 * vpw        ;-- labelbar width
  lres@vpHeightF            =  0.5 * vph        ;-- labelbar height
  lres@lbBoxMajorExtentF    =  0.7              ;-- insert white space between labelbar boxes
  lres@lbMonoFillPattern    =  True             ;-- solid fill pattern
  lres@lbLabelFontHeightF   =  0.08             ;-- font height
  lres@lbLabelJust          = "CenterLeft"      ;-- label justification

;-- create the labelbar, but don't draw it yet
  lbid = gsn_create_labelbar(wks,nlb,llab,lres)
         
;-- set annotation resources                                      
  amres                     =  True
  amres@amJust              = "TopRight"        ;-- labelbar justification
  amres@amParallelPosF      =  0.5              ;-- move labelbar to the right of plot
  amres@amOrthogonalPosF    = -0.5              ;-- move labelbar to the top of plot

;-- attach the labelbar to the plot
  annoid = gsn_add_annotation(plot,lbid,amres)

;-- set text resources
  txres                     =  True
  txres@txPerimOn           =  True             ;-- draw a box around the text
  txres@txBackgroundFillColor = "cornsilk"      ;-- box fill color
  txres@txFontHeightF       =  0.03             ;-- smaller text font size

;-- create text, but don't draw it yet
  txid = gsn_create_text(wks, "Text string, e.g. data description", txres)

  amres@amJust              = "TopLeft"         ;-- labelbar justification
  amres@amParallelPosF      = -0.5              ;-- move labelbar to the right of plot
  amres@amOrthogonalPosF    = -0.5              ;-- move labelbar to the top of plot

;-- attach text to the plot
  annoid = gsn_add_annotation(plot, txid, amres)
           
;-- draw second plot and advance the frame
  draw(plot)
  frame(wks)

end 

 

3. Text settings:

begin
;-- define german "Umlaute"
  Auml = "A~H-15V6F35~H~FV-6H3~"
  auml = "a~H-13V2F35~H~FV-2H3~"
  Ouml = "O~H-16V6F35~H~FV-6H3~"
  ouml = "o~H-14V2F35~H~FV-2H3~"
  Uuml = "U~H-15V6F35~H~FV-6H3~"
  uuml = "u~H-13V2F35~H~FV-2H3~"

;-- define super- and subscript variable
  super = "m~S~3~N~  [m s~S~-1~N~]  [kg m~S~-2~N~]  30~S~o~N~C"
  sub   = "Schwefels"+auml+"ure:   H~B~2~N~SO~B~4~N~"

  data  = (/ "1.",      "2.",     "3.",    "4."/)
  diff  = (/ 16.25,    -0.93,    0.43,     3.5/)
  var   = (/  0.06,     0.02,    0.04,     0.05/)
  ratio = (/     2,      2.4,     1.1,     0.9/)

  ntext = dimsizes(data)

;-- open workstation
  wks = gsn_open_wks("png","part_VII_text_settings")   

;-- x, y start point for writing
  x   = 0.1
  y   = 0.95
  inc = 0.07

;-- text resources
  txres                   = True
  txres@txFont            = "helvetica-bold"   ;-- change font
  txres@txFontHeightF     = 0.03               ;-- set font size
  txres@txJust            = "CenterCenter"     ;-- set text justification
  str = "DKRZ NCL workshop: Text settings"     ;-- title string
  gsn_text_ndc(wks,str,0.5,y,txres)            ;-- draw title string

  txres@txFont            = "helvetica"        ;-- change font
  txres@txJust            = "CenterLeft"       ;-- change text justification
  str1 = "Umlaute:"
  gsn_text_ndc(wks,str1,x,y-2*inc,txres)

  str2 = Auml+"  "+auml+"   "+Ouml+"  "+ouml+"   "+Uuml+" "+uuml
  gsn_text_ndc(wks,str2,x+0.3,y-2*inc,txres)

  str1 = "Superscript:"
  gsn_text_ndc(wks,str1,x,y-3*inc,txres)
  str2 = super
  gsn_text_ndc(wks,str2,x+0.3,y-3*inc,txres)

  str1 = "Subscript:"
  gsn_text_ndc(wks,str1,x,y-4*inc,txres)
  str2 = sub
  gsn_text_ndc(wks,str2,x+0.3,y-4*inc,txres)

;-- nice formated text output using sprintf
  str = "Format:"
  gsn_text_ndc(wks,str,x,y-5*inc,txres)

  xpos = 0.4
  do i=0,ntext-1
    ypos = y-5*inc-i*0.05
    gsn_text_ndc(wks,data(i),xpos,ypos,txres)
  end do

  txres@txJust = "CenterRight"

  do i=0,ntext-1
    xpos = 0.65                             ;-- column 1
    ypos = y-5*inc-i*0.05
    str = sprintf("%6.2f",diff(i))
    gsn_text_ndc(wks,str,xpos,ypos,txres)
 
    xpos = xpos + 0.12                      ;-- column 2
    str = sprintf("%5.2f",var(i))
    gsn_text_ndc(wks,str,xpos,ypos,txres)

    xpos = xpos + 0.12                      ;-- column 3
    str = sprintf("%3.1f",ratio(i))
    gsn_text_ndc(wks,str,xpos,ypos,txres)
  end do

;-- greek characters
  xpos = 0.3
  ypos = 0.35

  str1 = "Greek font:"
  gsn_text_ndc(wks,str1,xpos,ypos,txres)

  str2 = "alpha = ~F33~a~N~"
  gsn_text_ndc(wks,str2,xpos+0.27,ypos,txres)

  str2 = "beta   = ~F33~b~N~"
  gsn_text_ndc(wks,str2,xpos+0.55,ypos,txres)

  str2 = "sigma = ~F33~s~N~"
  gsn_text_ndc(wks,str2,xpos+0.27,ypos-0.05,txres)

;-- decrease the font
  str1 = "Font size 100%"
  gsn_text_ndc(wks,str1,xpos+0.08,ypos-3*inc,txres)

  str2 = "~Z70~Font size 70%~N~"
  gsn_text_ndc(wks,str2,xpos+0.3,ypos-3*inc,txres)

  str3 = "~Z40~Font size 40%~N~"
  gsn_text_ndc(wks,str3,xpos+0.45,ypos-3*inc,txres)

;-- change Helvetica font to Helvetica-bold
  txres1                  =  True
  txres1@txFont           = "helvetica-bold"
  txres1@txFontHeightF     = 0.03
  txres1@txJust            = "CenterLeft"
  bold = "Helvetica-bold font size 0.03"
  gsn_text_ndc(wks,bold,x,0.08,txres1)

  frame(wks)

end 

 

Artikelaktionen