#!/usr/bin/ksh
#------------------------------------------------------
file="HSURF_EUR-11_model_region.nc"
var="HSURF"

name="${0%.*}"
ncl_script="${name}.ncl"
prn="plot_${name}"
prn_type="png"

cat<<EOF>${ncl_script}
;-------------------------------------------------------
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"

begin
  f1         = addfile("${file}","r")
  var        = f1->${var}(0,:,:)
  lat2d      = f1->lat
  lon2d      = f1->lon
  nlat       = dimsizes(lat2d(:,0))
  nlon       = dimsizes(lon2d(0,:))

  wks  = gsn_open_wks("${prn_type}","${prn}")

  res                         = True
  res@gsnDraw                 = False
  res@gsnFrame                = False
  res@gsnMaximize             = True

;-- contour resources

  cnres                       = res
  cnres@cnFillOn              = True
  cnres@cnMissingValFillColor = "steelblue1"
  cnres@cnLinesOn             = False
  cnres@cnLineLabelsOn        = False
  cnres@cnLevelSelectionMode  = "ManualLevels"
  cnres@cnMinLevelValF        =  0.0
  cnres@cnMaxLevelValF        =  3000.
  cnres@cnLevelSpacingF       =  50.
  cnres@cnFillPalette         = "OceanLakeLandSnow"
  cnres@cnFillDrawOrder       = "Predraw"
  cnres@gsnRightString        = ""
  cnres@gsnLeftString         = ""
  cnres@trYReverse            = True
  cnres@tfDoNDCOverlay        = False
  cnres@sfXArray              = lon2d
  cnres@sfYArray              = lat2d

;-- map resources

  mpres                       = res
  mpres@mpLabelsOn            = False
  mpres@mpPerimOn             = True
  mpres@mpGridAndLimbOn       = False
  mpres@mpFillOn              = True
  mpres@mpOutlineOn           = True
  mpres@mpOutlineDrawOrder    = "PostDraw"
  mpres@mpFillDrawOrder       = "Predraw"
  mpres@mpOceanFillColor      = (/ 0.824, 0.961, 1.0 /)
  mpres@mpLandFillColor       = (/ 0.7, 0.7, 0.7 /)
  mpres@mpLimitMode           = "Corners"
  mpres@mpLeftCornerLonF      = lon2d(0,0) - 40
  mpres@mpLeftCornerLatF      = lat2d(0,nlon-1) - 6
  mpres@mpRightCornerLonF     = lon2d(nlat-1,nlon-1) + 1
  mpres@mpRightCornerLatF     = lat2d(nlat-1,0) + 15

  mpres@mpGridAndLimbDrawOrder= "Predraw"
  mpres@mpCenterLatF          = 0.
  mpres@tiMainString          = "Europe 0.11~S~o~N~ Orography (model region)"

  plot = gsn_csm_contour(wks,var,cnres)
  map = gsn_csm_map(wks,mpres)
  overlay(map,plot)

;-- polyline resources

  resl                        = True           ; polyline mods desired
  resl@gsLineThicknessF       = 2.0            ; line thickness   
  resl@gsLineColor            = "blue"         ; color of lines

;-- define edges

  lon_val_upper = lon2d(nlat-1,:)
  lat_val_upper = lat2d(nlat-1,:)
  lon_val_lower = lon2d(0,:)
  lat_val_lower = lat2d(0,:)
  lon_val_left = lon2d(:,0)
  lat_val_left = lat2d(:,0)
  lon_val_right = lon2d(:,nlon-1)
  lat_val_right = lat2d(:,nlon-1)

;-- draw edges

  upper = gsn_add_polyline(wks,plot, lon_val_upper, lat_val_upper, resl)
  lower = gsn_add_polyline(wks,plot, lon_val_lower, lat_val_lower, resl)
  left  = gsn_add_polyline(wks,plot, lon_val_left,  lat_val_left,  resl)
  right = gsn_add_polyline(wks,plot, lon_val_right, lat_val_right, resl)

;---Drawing the plot will draw the attached polyline too.

  draw(map)
  frame(wks)
end
;-------------------------------------------------------
EOF

#-- run NCL
ncl ${ncl_script}

#-- that's it
exit

