;**********************************
; taylor_4.ncl
;**********************************
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"   
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "./taylor_diagram.ncl"
;**********************************
procedure add_vector(wks, plot, xBegin, yBegin, xEnd, yEnd, opt)
; draw a vector spanning (xBegin, yBegin) to (xEnd, yEnd)
local dx, dy
begin
 ;if (opt) then
 ;end if

  x_in = (/0.5,1.0/)
  y_in = (/.1,.1/)
  x_out = new(dimsizes(x_in),float)
  y_out = new(dimsizes(y_in),float)
  datatondc(plot,x_in,y_in,x_out,y_out)
  ndc_length = x_out(1)-x_out(0)
  vec_length = x_in(1) - x_in(0)

  dx = xEnd-xBegin
  dy = yEnd-yBegin
  
  wmsetp("vrs",vec_length)  ; reference vector size
  wmsetp("vrn",ndc_length)  ; NDC size of a reference vector size
  
  wmvect(wks, xBegin, yBegin, dx, dy)
end
;**********************************
; Assume the following have already been computed:
;   _ratio are the ratio:  Case_Variance/Reference_Variance
;   _cc    are the cross correlation coef of Case to Reference
; In this example, these are derived for annual mean climatologies.
;**********************************

; Cases [Model]
  case      = (/ "Case A", "Case B" /) 
  nCase     = dimsizes(case )                 ; # of Cases [Cases]

; variables compared
  var       = (/ "SLP","Tsfc" ,"Prc","Prc 30S-30N","LW","SW", "U300", "Guess" \ 
               , "RH" ,"LHFLX","TWP","CLDTOT"     ,"O3","Q" , "PBLH", "Omega" /) 
  nVar      = dimsizes(var)                   ; # of Variables

; more info to be added [all are bogus]
  source    = (/ "ERA40", "ERA40","GPCP" , "GPCP", "ERS" , "ERS", "ERA40", "BOGUS" \
               , "NCEP",  "ERA40","ERA40", "NCEP", "NASA", "JMA", "JMA"  , "CAS"  /)

; "Case A"                        
  CA_ratio   = (/1.230, 0.988, 1.092, 1.172, 1.064, 0.966, 1.079, 0.781 \
                ,1.122, 1.000, 0.998, 1.321, 0.842, 0.978, 0.998, 0.811 /)
  CA_cc      = (/0.958, 0.973, 0.740, 0.743, 0.922, 0.982, 0.952, 0.433 \
                ,0.971, 0.831, 0.892, 0.659, 0.900, 0.933, 0.912, 0.633 /)

; "Case B" 
  CB_ratio   = (/1.129, 0.996, 1.016, 1.134, 1.023, 0.962, 1.048, 0.852 \ 
                ,0.911, 0.835, 0.712, 1.122, 0.956, 0.832, 0.900, 1.311 /)
  CB_cc      = (/0.963, 0.975, 0.801, 0.814, 0.946, 0.984, 0.968, 0.647 \ 
                ,0.832, 0.905, 0.751, 0.822, 0.932, 0.901, 0.868, 0.697 /)

; arrays to be passed to taylor_diagram. It will calculate the x xnd y coordinates.
  ratio      = new ((/nCase, nVar/),typeof(CA_cc) )  
  cc         = new ((/nCase, nVar/),typeof(CA_cc) ) 

  ratio(0,:) = CA_ratio 
  ratio(1,:) = CB_ratio

  cc(0,:)    = CA_cc 
  cc(1,:)    = CB_cc

;**********************************
; create plot
;**********************************
  varSource = var +"_"+ source              ; add extra info [*not* required]

  ty_opt   = True                           ; taylor diagram with options
        
  ty_opt@Markers       = (/16, 16/)         ; make all solid fill
  ty_opt@Colors        = (/"red", "blue" /)          
  ty_opt@varLabels     = varSource
  ty_opt@caseLabels    = case

  ty_opt@varLabelsYloc = 1.5                ; Move location of variable labels [default 0.45]
  ty_opt@caseLabelsFontHeightF = 0.14       ; make slight larger   [default=0.12 ]
  ty_opt@varLabelsFontHeightF  = 0.011      ; make slight smaller  [default=0.013]
 
  ty_opt@tiMainString  = "Annual"           ; title
  ty_opt@ccRays        = (/ 0.6, 0.9 /)     ; correlation rays
  ty_opt@centerDiffRMS = True               ; RMS 'circles'

  wks   = gsn_open_wks("ps","taylor_4_arrow")
  gsn_define_colormap(wks, "hlu_default")

  ty_opt@taylorFrame      = False

  plot  = taylor_diagram(wks,ratio,cc,ty_opt)


  xBegin = (/0.75, 0.30/)
  xEnd   = (/1.25, 0.10/)
  yBegin = (/0.60, 0.40/) 
  yEnd   = (/0.30, 1.10/) 

 wmsetp("vcc",2)    ; vector color
 wmsetp("vcw",4.)   ; vector linewidth scale factor
 ;wmsetp("vva",25.)  ; angle (degrees) that vector arrow head makes with tail.
 
  opt    = False
  add_vector(wks, plot, xBegin, yBegin, xEnd, yEnd, opt)
  frame(wks)

