import numpy, os
import Ngl
import Nio
import sys

filename = "nos.ngofs.fields.nowcast.20120322.t03z.nc"

file = Nio.open_file(filename, 'r', None, '', '')

minLon = -97.5
maxLon = -85.5
minLat = 25.5
maxLat = 31

temp = file.variables["temp"]
lat = file.variables["lat"]
lon = file.variables["lon"]
lon_fixed = lon[:] - 360
nv = file.variables["nv"] # has the wrong dimensions to be used to for sfElementNodes

wkres = Ngl.Resources()
wkres.wkColorMap = "rainbow+gray"
wks_type = "png"
wks = Ngl.open_wks(wks_type, "ngofs-test", wkres)

resources = Ngl.Resources()

resources.sfXArray = lon_fixed
resources.sfYArray = lat[:]
# resources.sfElementNodes = nv_transposed
# resources.sfFirstNodeIndex = 1

# Define which colors will be used in the spread of the contour
resources.nglSpreadColorStart = 15 # First color used for contour is 16th color from beginning
resources.nglSpreadColorEnd = -2   # Last color used for contour is 10th from end

# Determine the contour levels
min_temp = 0.0
max_temp = 30.0
interval = 0.25
contour_levels = [0.0] * (int)(((max_temp - min_temp) / interval) + 1)
v = min_temp
for i in range(len(contour_levels)):
	contour_levels[i] = v
	v = v + interval

# Define the min and max values for the contouring
resources.cnLevelSelectionMode = "ExplicitLevels"
resources.cnLevels = contour_levels

resources.cnFillOn = True
resources.cnLinesOn = True
resources.cnLineLabelsOn = True
resources.cnLineLabelFontHeightF = 0.0095
resources.cnLabelMasking = True
resources.cnLineLabelBackgroundColor = "transparent"

# Label bar
resources.lbOrientation = "Vertical"      # Orientation
resources.lbTitleString= "~S~o~N~Celcius" # Title: ~S~ represents superscript, ~N~ represents normal text
resources.lbTitlePosition = "Top"         # Title position
resources.lbTitleDirection = "Across"     # Title direction
resources.lbLabelStride = 8               # Interval of boxes in label bar to be labeled
resources.lbBoxLinesOn = False            # Turns off label bar lines
resources.lbTitleExtentF = 0.04
resources.lbLabelFontHeightF = 0.02

resources.cnFillMode = "RasterFill"
resources.cnRasterSmoothingOn = True

resources.mpProjection = "Mercator"

resources.mpLimitMode = "Corners"
resources.mpLeftCornerLatF = minLat
resources.mpLeftCornerLonF = minLon
resources.mpRightCornerLatF = maxLat
resources.mpRightCornerLonF = maxLon
# Don't draw the default land/ocean boundaries:
#resources.mpOutlineBoundarySets = "NoBoundaries"
#resources.mpDataBaseVersion = "MediumRes"

tempa = temp[0,0,:]

plot = Ngl.contour_map(wks, tempa, resources)

Ngl.end()

file.close()

