import numpy,os
import Nio,Ngl

#---Open the WRF data file
dir      = os.path.join("..","..","Data","WRF")
filename = "wrfout_d01_2008-08-09_00:00:00.nc"
a        = Nio.open_file(os.path.join(dir,filename))

#---Read the height values variable
hgt  = a.variables["HGT"]

#---This is so we can set up map projection parameters later.
lat  = a.variables["XLAT"][0,:,:]
lon  = a.variables["XLONG"][0,:,:]
nlat = lat.shape[0]
nlon = lat.shape[1]

#---Open x11 window or png file
wks = Ngl.open_wks("png","wrf_ngl_hgt")
Ngl.define_colormap(wks,"OceanLakeLandSnow")

#---Set some resources
res = Ngl.Resources()

#---Setting this to True instead of using lat/lon arrays can be much faster!
res.tfDoNDCOverlay        = True
 
res.cnFillOn              = True         # Turn on contour fill
res.cnLinesOn             = False        # Turn off contour lines
res.cnLineLabelsOn        = False        # Turn off contour line labels

#---Use map projection info from WRF file
res.mpProjection          = "LambertConformal"
res.mpLambertParallel1F   = a.TRUELAT1
res.mpLambertParallel2F   = a.TRUELAT2
res.mpLambertMeridianF    = a.STAND_LON

#---Use map limits from WRF file
res.mpLimitMode           = "Corners"
res.mpLeftCornerLatF      = lat[0,0]
res.mpLeftCornerLonF      = lon[0,0]
res.mpRightCornerLatF     = lat[nlat-1,nlon-1]
res.mpRightCornerLonF     = lon[nlat-1,nlon-1]

res.mpDataBaseVersion     = "MediumRes"    # Better resolution
res.mpOutlineBoundarySets = "GeophysicalAndUSStates"

res.mpOutlineOn           = True      # Turn on map outlines
res.mpGridAndLimbOn       = False     # Turn off map grid lines
res.mpFillOn              = False     # Turn off map fill

res.tiMainString          = hgt.description + " (" + hgt.units + ")"

res.lbLabelFontHeightF    = 0.015     # Make various labels smaller
res.tmXBLabelFontHeightF  = 0.015
res.tmYLLabelFontHeightF  = 0.015

res.lbOrientation         = "Vertical"   # Vertical labelbar
res.pmLabelBarWidthF      = 0.10         # Make labelbar thinner

plot = Ngl.contour_map(wks,hgt[0,:,:],res)

Ngl.end()
