#----------------------------------------------------------------------
# Read and plot a variable read off the cfdda_1985013123.v2.nc NetCDF 
# file using PyNIO and PyNGL.
#
# This example was coded to plot "Temp", which is a "packed" variable.
# You must unpack it before plotting it.
#
# Data is rectilinear, 1 x 28 x 450 x 900   (time x lev x lat x lon)
#
# See cfdda_plot_var_mpl.py for a matplotlib/basemap example.
#----------------------------------------------------------------------
import numpy as np
import Nio, Ngl

# Read data
dir      = "../Data/"
filename = "cfdda_1985013123.v2.nc"
a   = Nio.open_file("%s/%s" %(dir,filename))

var_name = "Temp"
var = a.variables[var_name]    # var will be a 'NioVariable'
lat = a.variables["y0"][:]
lon = a.variables["x0"][:]

# Unpack the variable
var_unpack =  var.scale_factor * (var[:] - var.add_offset)

#----------------------------------------------------------------------
# Start the graphics
#----------------------------------------------------------------------

#---Verify that the min/max of the variable looks good.
print("var_unpack: %g / %g" % (var_unpack.min(),var_unpack.max()))
 
wks = Ngl.open_wks("png","cfdda_plot_%s_ngl" % var_name)

#---Set some plot options
res                   = Ngl.Resources()

#---Contour options
res.cnFillOn           = True          # turn on contour fill
res.cnLinesOn          = False         # turn off contour lines
res.cnLineLabelsOn     = False         # turn off line labels
res.cnFillPalette      = "MPL_jet"
res.cnFillMode         = "RasterFill"  # Can be much faster than "AreaFill"

res.sfXArray           = lon
res.sfYArray           = lat

res.lbOrientation      = "horizontal"
res.lbLabelFontHeightF = 0.01

#---Main Title
res.tiMainString          = ("%s : %s") % (filename,var_name)
res.tiMainFontHeightF     = 0.015

nt = 0     # "Temp" is a 4D array. Only plot first 
nl = 0     # time step and level
plot = Ngl.contour_map(wks,var_unpack[nt,nl,:,:],res)

Ngl.end()

