#----------------------------------------------------------------------
# This script plots uocn and vocn from a HYCOM model file using
# vectors.
#----------------------------------------------------------------------
import os,sys,numpy
from numpy import ma
import Ngl,Nio


#---Open NetCDF file
dir      = os.path.join("..","..","Data","HYCOM")
filename = "hycom-cice_ARCu0.08_035_UOA_2012010100_t000.nc"
a = Nio.open_file(os.path.join(dir,filename))

#---Apply scale_factor and add_offset attributes to data
u       = a.variables["uocn"]
v       = a.variables["vocn"]
uoffset = u.attributes['add_offset']
voffset = v.attributes['add_offset']
uscale  = u.attributes['scale_factor']
vscale  = v.attributes['scale_factor']

uf = numpy.array(u[:],'f')
vf = numpy.array(v[:],'f')
uf = numpy.where(uf!=u._FillValue,(uf*uscale)+uoffset,u._FillValue)
vf = numpy.where(vf!=v._FillValue,(vf*vscale)+voffset,v._FillValue)

umask = ma.masked_values(uf[0,:,:],u._FillValue)
vmask = ma.masked_values(vf[0,:,:],v._FillValue)


#---Get lat/lon and fix the longitudes
lat     = a.variables["lat"][:]
lon     = a.variables["lon"][:]
lon     = numpy.where((lon < 0),lon+360,lon)

#---Start the graphics
wks = Ngl.open_wks("png","vector_ngl_ocn_4")   # "ps", "pdf", "png"

#---Set some resources for drawing vectors over a map
res          = Ngl.Resources()
res.vfXArray = lon
res.vfYArray = lat

#---Zoom in on map.
res.mpLimitMode          = "LatLon"
res.mpMinLatF            = numpy.min(lat)
res.mpMaxLatF            = numpy.max(lat)
res.mpMinLonF            = numpy.min(lon)
res.mpMaxLonF            = numpy.max(lon)
res.mpCenterLonF         = (res.mpMinLonF + res.mpMaxLonF) / 2.
res.mpLandFillColor      = "Gray"
res.mpGridAndLimbOn      = False
res.vcMinDistanceF       = 0.005     # thin the vectors
res.vcRefLengthF         = 0.03
res.vcRefMagnitudeF      = 0.70

vector = Ngl.vector_map(wks,umask,vmask,res)
Ngl.end()


