#! /usr/bin/env python
#
# ===========================================================
#
# Alexander R Davies
#
# Ocean Exploration, Remote Sensing, and Biogeography Lab
# School of Marine Science and Policy
# College of Earth, Ocean, and Environment
# University of Delaware
# ardavies@udel.edu
#
# Latest Update: 10/09/13
#
# NOTES: 	1) Reads Processed Data, Linearly Interpolates 
#				each for each profile to a known grid
#
#			2) Calculates vertical velocities for density
#				and chlorphyll. 
#
#			3) Plots the vertical velocities over contoured 
#				gridded, interpolated w data
#				
#
# ===========================================================
# Import Modules
# ===========================================================
#
import numpy as np
import csv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import glob
import os
from matplotlib.ticker import MaxNLocator
from matplotlib import rc, rcParams
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
from numpy import arange,array,ones#,random,linalg
from pylab import plot,show
from scipy import interpolate
from matplotlib.colors import LogNorm
from matplotlib.backends.backend_pdf import PdfPages
#
# ===========================================================
# Function to read csv files
# ===========================================================
#
def csvread(filename):
	with open(filename, 'rb') as f:
	    reader = csv.reader(f, delimiter=',')
	    nrow = 0;
	    for row in reader:
	    	nrow = nrow +1
	    	ncol = len(row)
	array = np.zeros([ncol,nrow]) 
	with open(filename, 'rb') as f:
	    reader = csv.reader(f, delimiter=',')
	    counter = 0
	    for row in reader:
	    	for jj in range(0,ncol):
	    		array[jj,counter] = row[jj]
	    	counter = counter + 1
	return array, nrow, ncol   
#
# ===========================================================
# Function to Find the nearest value
# ===========================================================
#
def find_nearest(array,value):
    idx = (np.abs(array-value)).argmin()
    return array[idx], idx
#
# ===========================================================
# Sort Data and Descriptive Arrays
# ===========================================================
#
os.chdir('C:/Documents and Settings/Alex/Documents/Research/Delaware/MUSTACHE/Jan01_Jun04_2013/data/type')
fullnames = glob.glob('Full*')
gradnames = glob.glob('Grad*')
infonames = glob.glob('Text*')
arraylen =  len(fullnames)
floatdate = np.zeros(arraylen)
lat = [None]*arraylen
lon = [None]*arraylen
day = np.zeros(arraylen)
month=[None]*arraylen
year=[None]*arraylen
daystr=[None]*arraylen
#
# ===========================================================
# Build the Arrays as days past 1/1/13
# ===========================================================
#
for d in range(0,int(arraylen)):
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:4] == "Date":
			month[d] = str(line[5:7])
			day[d] = int(line[8:10])
			daystr[d] = str(line[8:10])
			year[d] = str(line[11:15])
			break
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:3] == "Lat":
			lat[d] = str(line[5:12])
			break
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:3] == "Lon":
			lon[d] = str(line[5:12])
			break
	
	if (year[d] == '2013'):
		yr = 0
	elif (year[d] == '2014'):
		yr = 365
	elif (year[d] == '2015'):
		yr = 365*2
	if(month[d] == '01'):
		mon = 0
	elif(month[d] == '02'):
		mon = 31
	elif(month[d] == '03'):
		mon = 31 + 28
	elif(month[d] == '04'):
		mon = (31 + 28 + 31)
	elif(month[d] == '05'):
		mon = (31 + 28 + 31 + 30)
	elif(month[d] == '06'):
		mon = (31 + 28 + 31 + 30 + 31)
	elif(month[d] == '07'):
		mon = (31 + 28 + 31 + 30 + 31 + 30)
	elif(month[d] == '08'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31)
	elif(month[d] == '09'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31)
	elif(month[d] == '10'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30)
	elif(month[d] == '11'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
	elif(month[d] == '12'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
	else:
		print 'invalid month'

	floatdate[d] = yr + mon + day[d]
#
# ===========================================================
# Read the Data and Interpolate for Gridding
# ===========================================================
#
Ygrid = np.linspace(-1800,-10,1791*2)
interplength = len(Ygrid)
GridData = np.zeros([interplength,7,arraylen])
ChlyDumby = np.zeros([interplength,arraylen])
for i in range(0,arraylen):
	dataout, rows, cols = csvread(fullnames[i])
	#
	TemInterp = interpolate.interp1d(dataout[0,:], dataout[1,:],kind='linear')
	DenInterp = interpolate.interp1d(dataout[0,:], dataout[2,:],kind='linear')
	SalInterp = interpolate.interp1d(dataout[0,:], dataout[3,:],kind='linear')
	PreInterp = interpolate.interp1d(dataout[0,:], dataout[4,:],kind='linear')
	OxyInterp = interpolate.interp1d(dataout[0,:], dataout[7,:],kind='linear')
	ChlInterp = interpolate.interp1d(dataout[0,:], dataout[11,:],kind='linear')
	BskInterp = interpolate.interp1d(dataout[0,:], dataout[12,:],kind='linear')
	CDMInterp = interpolate.interp1d(dataout[0,:], dataout[13,:],kind='linear')
	#
	for j in range(0,interplength):
		GridData[j,0,i] = TemInterp(Ygrid[j])
		GridData[j,1,i] = DenInterp(Ygrid[j])
		GridData[j,2,i] = SalInterp(Ygrid[j])
		GridData[j,3,i] = OxyInterp(Ygrid[j])			 
		ChlyDumby[j,i] = ChlInterp(Ygrid[j])	
		GridData[j,5,i] = BskInterp(Ygrid[j])	
		GridData[j,6,i] = CDMInterp(Ygrid[j])	
		if (ChlInterp(Ygrid[j]) < 0):
			GridData[j,4,i] = GridData[j-1,4,i]
		else: 
			GridData[j,4,i] = ChlyDumby[j,i]	
#
# ===========================================================
# Find Gridded Change Velocities Based on Nearest Values
# ===========================================================
#
GridWData = np.zeros([interplength,7,arraylen-1])
GridWDepths = np.zeros([interplength,7,arraylen-1])
GridWDates = np.zeros([interplength,7,arraylen-1])
for k in range(0,7):
	for i in range (0,arraylen-1):
		for j in range(0,interplength):
			value, index = find_nearest(GridData[:,k,i+1], GridData[j,k,i])
			startdepth = Ygrid[j]
			newdepth = Ygrid[index]
			GridWDepths[j,k,i] = (newdepth + startdepth)/2
			dz = newdepth-startdepth
			GridWData[j,k,i] = dz/172800
			GridWDates[j,k,i] = (floatdate[i] + floatdate[i+1])/2
#
# ===========================================================
# Find Avg Gridded Change Velocities Based on Nearest Values
# ===========================================================
#
GridWDataAvg = np.zeros([interplength,7,arraylen-3])
GridWDepthsAvg = np.zeros([interplength,7,arraylen-3])
GridWDatesAvg = np.zeros([interplength,7,arraylen-3])
for k in range(0,7):
	for i in range (0,arraylen-3):
		for j in range(0,interplength):
			GridWDataAvg[j,k,i] = (GridWData[j,k,i] + GridWData[j,k,i+1] + GridWData[j,k,i+2])/3
			GridWDepthsAvg[j,k,i] = (GridWDepths[j,k,i] + GridWDepths[j,k,i+1] + GridWDepths[j,k,i+2])/3
			GridWDatesAvg[j,k,i] = (GridWDates[j,k,i] + GridWDates[j,k,i+1] + GridWDates[j,k,i+2])/3
#
# ===========================================================
# Find Depth of Isochly Contours
# ===========================================================
#
isobiolines = np.linspace(.1,.40,20)
numbiolines = len(isobiolines)
Biodepths = np.zeros([numbiolines,arraylen])
for i in range(0,arraylen):
	#print i
	for j in range(0,numbiolines):
		#print j
		count = 0
		while (GridData[count,4,i] < isobiolines[j]):
				count = count + 1 
		Biodepths[j,i] = Ygrid[count]
#
# ===========================================================
# Find Depth of Isocdom Contours
# ===========================================================
#
isocdomlines = np.linspace(1.7,2.2,21)
print isocdomlines
numcdomlines = len(isocdomlines)
print numcdomlines
cdomdepths = np.zeros([numcdomlines,arraylen])
for i in range(0,arraylen):
	#print i
	for j in range(0,numcdomlines):
		#print j
		count = 0
		while (GridData[count,6,i] > isocdomlines[j]):
				count = count + 1 
		cdomdepths[j,i] = Ygrid[count]

#
# ===========================================================
# Find Isochly Contour Change Velocities
# ===========================================================
#
IsoBioWData = np.zeros([numbiolines,arraylen-1])
IsoBioWDepths = np.zeros([numbiolines,arraylen-1])
IsoBioWDates = np.zeros([numbiolines,arraylen-1])
IsoBioFake = np.zeros([numbiolines,arraylen-1])
for i in range(0,arraylen-1):
	for k in range(0,numbiolines):
		j = i
		IsoBioWData[k,i] = (Biodepths[k,j+1] - Biodepths[k,j])/172800
		IsoBioFake[k,i] = 0
		IsoBioWDepths[k,i] = (Biodepths[k,j+1] + Biodepths[k,j])/2
		IsoBioWDates[k,i] = (floatdate[j+1] + floatdate[j])/2
#
# ===========================================================
# Find Isochly Contour Change Velocities
# ===========================================================
#
IsoCdomWData = np.zeros([numcdomlines,arraylen-1])
IsoCdomWDepths = np.zeros([numcdomlines,arraylen-1])
IsoCdomWDates = np.zeros([numcdomlines,arraylen-1])
IsoCdomFake = np.zeros([numcdomlines,arraylen-1])
for i in range(0,arraylen-1):
	for k in range(0,numcdomlines):
		j = i
		IsoCdomWData[k,i] = (cdomdepths[k,j+1] - cdomdepths[k,j])/172800
		IsoCdomFake[k,i] = 0
		IsoCdomWDepths[k,i] = (cdomdepths[k,j+1] + cdomdepths[k,j])/2
		IsoCdomWDates[k,i] = (floatdate[j+1] + floatdate[j])/2
#
# ===========================================================
# Find Avg Isoline Change Velocities
# ===========================================================
#
IsoBioWDataAvg = np.zeros([numbiolines,arraylen-3])
IsoBioWDepthsAvg = np.zeros([numbiolines,arraylen-3])
IsoBioWDatesAvg = np.zeros([numbiolines,arraylen-3])
IsoBioFakeAvg = np.zeros([numbiolines,arraylen-3])
for i in range (0,arraylen-3):
	for j in range(0,numbiolines):
			IsoBioWDataAvg[j,i] = (IsoBioWData[j,i] + IsoBioWData[j,i+1] + IsoBioWData[j,i+2])/3
			IsoBioWDepthsAvg[j,i] = (IsoBioWDepths[j,i] + IsoBioWDepths[j,i+1] + IsoBioWDepths[j,i+2])/3
			IsoBioWDatesAvg[j,i] = (IsoBioWDates[j,i] + IsoBioWDates[j,i+1] + IsoBioWDates[j,i+2])/3
			IsoBioFakeAvg[j,i] = (IsoBioFake[j,i] + IsoBioFake[j,i+1] + IsoBioFake[j,i+2])/3
#
# ===========================================================
# Find Avg IsoCDOM Change Velocities
# ===========================================================
#
IsoCdomWDataAvg = np.zeros([numcdomlines,arraylen-3])
IsoCdomWDepthsAvg = np.zeros([numcdomlines,arraylen-3])
IsoCdomWDatesAvg = np.zeros([numcdomlines,arraylen-3])
IsoCdomFakeAvg = np.zeros([numcdomlines,arraylen-3])
for i in range (0,arraylen-3):
	for j in range(0,numcdomlines):
			IsoCdomWDataAvg[j,i] = (IsoCdomWData[j,i] + IsoCdomWData[j,i+1] + IsoCdomWData[j,i+2])/3
			IsoCdomWDepthsAvg[j,i] = (IsoCdomWDepths[j,i] + IsoCdomWDepths[j,i+1] + IsoCdomWDepths[j,i+2])/3
			IsoCdomWDatesAvg[j,i] = (IsoCdomWDates[j,i] + IsoCdomWDates[j,i+1] + IsoCdomWDates[j,i+2])/3
			IsoCdomFakeAvg[j,i] = (IsoCdomFake[j,i] + IsoCdomFake[j,i+1] + IsoCdomFake[j,i+2])/3
#
# ===========================================================
# Find Depth Averaged Avg Isoline Change Velocities
# ===========================================================
#
IsoBioWDataDepthAvgAvg = np.zeros(arraylen-3)
IsoBioWDepthsDepthAvgAvg = np.zeros(arraylen-3)
IsoBioWDatesDepthAvgAvg = np.zeros(arraylen-3)
IsoBioFakeDepthAvgAvg = np.zeros(arraylen-3)
for i in range (0,arraylen-3):
	IsoBioWDataDepthAvgAvg[i] = np.mean(IsoBioWDataAvg[:,i])
	IsoBioWDepthsDepthAvgAvg[i] = np.mean(IsoBioWDepthsAvg[:,i])
	IsoBioWDatesDepthAvgAvg[i] = np.mean(IsoBioWDatesAvg[:,i])
	IsoBioFakeDepthAvgAvg[i] = np.mean(IsoBioFakeAvg[:,i])
#
# ===========================================================
# Find Depth Averaged Avg Isoline Change Velocities
# ===========================================================
#
IsoCdomWDataDepthAvgAvg = np.zeros(arraylen-3)
IsoCdomWDepthsDepthAvgAvg = np.zeros(arraylen-3)
IsoCdomWDatesDepthAvgAvg = np.zeros(arraylen-3)
IsoCdomFakeDepthAvgAvg = np.zeros(arraylen-3)
for i in range (0,arraylen-3):
	IsoCdomWDataDepthAvgAvg[i] = np.mean(IsoCdomWDataAvg[:,i])
	IsoCdomWDepthsDepthAvgAvg[i] = np.mean(IsoCdomWDepthsAvg[:,i])
	IsoCdomWDatesDepthAvgAvg[i] = np.mean(IsoCdomWDatesAvg[:,i])
	IsoCdomFakeDepthAvgAvg[i] = np.mean(IsoCdomFakeAvg[:,i])
#
# ===========================================================
# Find Sink Vel
# ===========================================================
#
NearestWGridded  = np.zeros([numbiolines,arraylen-1])
SinkWData = np.zeros([numbiolines,arraylen-1])
for i in range (0,arraylen-1):
	for k in range(0,numbiolines):
		value, index = find_nearest(GridWDepths[:,1,i], IsoBioWDepths[k,i])
		SinkWData[k,i] = IsoBioWData[k,i] - GridWData[index,1,i]
		NearestWGridded[k,i] = GridWData[index,1,i]
#
# ===========================================================
# Find Avg Sink Vel
# ===========================================================
#
SinkWDataAvg = np.zeros([numbiolines,arraylen-3])
NearestWGriddedAvg  = np.zeros([numbiolines,arraylen-3])
for i in range (0,arraylen-3):
	for k in range(0,numbiolines):
		value, index = find_nearest(GridWDepthsAvg[:,1,i], IsoBioWDepthsAvg[k,i])
		SinkWDataAvg[k,i] = IsoBioWDataAvg[k,i] - GridWDataAvg[index,1,i]
		NearestWGriddedAvg[k,i] = GridWDataAvg[index,1,i]
#
# ===========================================================
# Find Depth Averaged Avg Sink Vel
# ===========================================================
#
SinkWDataDepthAvgAvg = np.zeros(arraylen-3)
NearestWGriddedAvgAvg  = np.zeros(arraylen-3)
for i in range (0,arraylen-3):
	SinkWDataDepthAvgAvg[i] = np.mean(SinkWDataAvg[:,i])
	NearestWGriddedAvgAvg[i] = np.mean(NearestWGriddedAvg[:,i])
#
# Change Path for Plotting
figdirpath ='C:/Documents and Settings/Alex/Documents/Research/Delaware/MUSTACHE/Jan01_Jun04_2013/plots/contours_with_Ws/'
#
# ===========================================================
# Contour CDOM  w/ IsoCDOM Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,6,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)

	contlevels= np.linspace(1,3,100)
	conticks = [1.5, 2.0, 2.5, 3.0]
	figtit = 'CDOM (ppb)'
	savetit = "CDOM_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels)
	plt.colorbar(cs, spacing='proportional', ticks = conticks)
	# Plotting
	figtit2 = ' and CDOM Quivers'
	savetit2 = "_IsoCdomW_Quivers"
	from pylab import *
	#Q = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10])
	#plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$5 \times 10^{-3} m/s$', labelpos='W')

	Q2 = plt.quiver(IsoCdomWDates[1::10], IsoCdomWDepths[1::10], IsoCdomFake[1::10], IsoCdomWData[1::10], color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +"2.png")

#
# ===========================================================
# Contour CDOM  w/ Averaged IsoCDOM Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,6,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)

	contlevels= np.linspace(1,3,100)
	conticks = [1.5, 2.0, 2.5, 3.0]
	figtit = 'CDOM (ppb)'
	savetit = "CDOM_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels)
	plt.colorbar(cs, spacing='proportional', ticks = conticks)
	# Plotting
	figtit2 = ' and CDOM 3 Day Average Quivers'
	savetit2 = "_IsoCdomW_Quivers_3Avg"
	from pylab import *
	#Q = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10])
	#plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$5 \times 10^{-3} m/s$', labelpos='W')

	Q2 = plt.quiver(IsoCdomWDatesAvg[1::10], IsoCdomWDepthsAvg[1::10], IsoCdomFakeAvg[1::10], IsoCdomWDataAvg[1::10], color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +".png")
#
# ===========================================================
# Contour CDOM  w/ Depth Averaged Averaged IsoCDOM Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,6,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)

	contlevels= np.linspace(1,3,100)
	conticks = [1.5, 2.0, 2.5, 3.0]
	figtit = 'CDOM (ppb)'
	savetit = "CDOM_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels)
	plt.colorbar(cs, spacing='proportional', ticks = conticks)
	# Plotting
	figtit2 = ' and CDOM Depth Averaged 3 Day Average Quivers'
	savetit2 = "_IsoCdomW_Quivers_3Avg_Averaged"
	from pylab import *
	#Q = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10])
	#plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$5 \times 10^{-3} m/s$', labelpos='W')

	Q2 = plt.quiver(IsoCdomWDatesDepthAvgAvg, IsoCdomWDepthsDepthAvgAvg, IsoCdomFakeDepthAvgAvg, IsoCdomWDataDepthAvgAvg, color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +".png")

#
# ===========================================================
# Contour Chly  w/ Isochly Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,4,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)


   	contlevels= np.logspace(-2.5,.65,100)
	conticks = [0.01,0.25,0.50, 1.00, 1.50, 2.00, 3.00]
	figtit = 'Chlorophyll (micro gram/liter)'
	savetit = "Chlorophyll_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels, norm = LogNorm())
	cbar = plt.colorbar(cs, spacing='proportional', norm = LogNorm())
	cbar.set_ticks(conticks)
	cbar.set_ticklabels(conticks)
	# Plotting
	figtit2 = ' and Chlorophyll Quivers'
	savetit2 = "_IsoChlyW_Quivers"
	from pylab import *
	Q2 = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10], color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +".png")
#
# ===========================================================
# Contour Chly  w/ Averaged Isochly Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,4,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)


   	contlevels= np.logspace(-2.5,.65,100)
	conticks = [0.01,0.25,0.50, 1.00, 1.50, 2.00, 3.00]
	figtit = 'Chlorophyll (micro gram/liter)'
	savetit = "Chlorophyll_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels, norm = LogNorm())
	cbar = plt.colorbar(cs, spacing='proportional', norm = LogNorm())
	cbar.set_ticks(conticks)
	cbar.set_ticklabels(conticks)
	# Plotting
	figtit2 = ' and Chlorophyll 3 Day Avg Quivers'
	savetit2 = "_IsoChlyW_Quivers_3Avg"
	from pylab import *
	Q2 = plt.quiver(IsoBioWDatesAvg[1::10], IsoBioWDepthsAvg[1::10], IsoBioFakeAvg[1::10], IsoBioWDataAvg[1::10], color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +".png")
#
# ===========================================================
# Contour Chly  w/ Averaged Averaged Isochly Ws
# ===========================================================
#
contplt = 2
if contplt == 1:
	#
	# Plotting correct data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,4,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)


   	contlevels= np.logspace(-2.5,.65,100)
	conticks = [0.01,0.25,0.50, 1.00, 1.50, 2.00, 3.00]
	figtit = 'Chlorophyll (micro gram/liter)'
	savetit = "Chlorophyll_Contour"
	cs = plt.contourf(floatdate,Ygrid,contourdat, levels= contlevels, norm = LogNorm())
	cbar = plt.colorbar(cs, spacing='proportional', norm = LogNorm())
	cbar.set_ticks(conticks)
	cbar.set_ticklabels(conticks)
	# Plotting
	figtit2 = ' and Chlorophyll Depth Avg 3 Day Avg Quivers'
	savetit2 = "_IsoChlyW_Quivers_3Avg_Averaged"
	from pylab import *
	Q2 = plt.quiver(IsoBioWDatesDepthAvgAvg, IsoBioWDepthsDepthAvgAvg, IsoBioFakeDepthAvgAvg, IsoBioWDataDepthAvgAvg, color='k')
	plt.quiverkey(Q2, 0.22, 0.96, 0.0005, r'$ 5 \times 10^{-4} m/s$', labelpos='W')
	ax = plt.gca()

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 +".png")


#
# ===========================================================
# Contour Plotting Grided W Data
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWplt = 1, No: else
gridWplt = 2
if gridWplt == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-1])
		wcontourdep = np.zeros([interplength,arraylen-1])
		wcontourtim = np.zeros([interplength,arraylen-1])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-1):
				wcontourdat[ci,cj] = GridWData[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepths[ci,k,cj]
				wcontourtim[ci,cj] = GridWDates[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		#
		# Plot Set-up
		fig = plt.figure()
		ax = fig.add_subplot(1, 1, 1)
		#
		# Which Plotting Information?
		if k == 0: #temperature
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(-1.5,3,100)
			conticks = [-1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
			figtit = 'Isothermal Gridded W Contour x 10^4 (m/s)'
			savetit = "TemperatureGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		if k == 1: # density
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = 'Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		if k == 2: # sal
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(33.5,34.75,100)
			conticks = [33.25, 33.50, 33.75, 34.00, 34.25, 34.50, 34.75, 35.00]
			figtit = 'Isosaline Gridded W Contour x 10^4 (m/s)'
			savetit = "SalinityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		if k == 3: #oxygen
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(4,10,100)
			conticks = [4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
			figtit = 'Isooxy Gridded W Contour x 10^4 (m/s)'
			savetit = "OxygenGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		if k == 4: # chl
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.logspace(-2.5,.65,100)
			conticks = [0.01,0.25,0.50, 1.00, 1.50, 2.00, 3.00]
			figtit = 'Isochly Gridded W Contour x 10^4 (m/s)'
			savetit = "ChlorophyllGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		if k == 5: #backscatter
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.logspace(-4,-3.3,100)
			conticks = [0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0004, 0.0005, 0.00075, 0.001, 0.0015]
			figtit = 'Isobackscatter Gridded W Contour x 10^4 (m/s)'
			savetit = "BackscatterGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
	
		#
		# Plotting
		ax = plt.gca()
		ax.set_xlabel("Days since 01/01/2013")
		ax.set_ylabel("Depth (m)")
		ax.set_ylim([-900,0])
		# plt.title(figtit)
		fig.suptitle(figtit) 
		plt.savefig(figdirpath + savetit+".png")
#
# ===========================================================
# Contour Plotting Grided W Data Zoomed
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWplt = 1, No: else
gridWplt = 2
if gridWplt == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-1])
		wcontourdep = np.zeros([interplength,arraylen-1])
		wcontourtim = np.zeros([interplength,arraylen-1])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-1):
				wcontourdat[ci,cj] = GridWData[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepths[ci,k,cj]
				wcontourtim[ci,cj] = GridWDates[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		#
		# Plot Set-up
		fig = plt.figure()
		ax = fig.add_subplot(1, 1, 1)
		#
		# Which Plotting Information?
		if k == 1: # density
			Wcontlevels= np.linspace(-6,6,101)
			Wconticks = [-5,-2.5, 0,2.5, 5]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = 'Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
			#
			# Plotting
			ax = plt.gca()
			ax.set_xlabel("Days since 01/01/2013")
			ax.set_ylabel("Depth (m)")
			ax.set_ylim([-900,0])
			ax.set_xlim([70,110])
			# plt.title(figtit)
			fig.suptitle(figtit) 
			plt.savefig(figdirpath + savetit+ "_zoom" +".png")
		if k == 2: # sal
			Wcontlevels= np.linspace(-6,6,101)
			Wconticks = [-5,-2.5, 0,2.5, 5]
			#contlevels= np.linspace(33.5,34.75,100)
			conticks = [33.25, 33.50, 33.75, 34.00, 34.25, 34.50, 34.75, 35.00]
			figtit = 'Isosaline Gridded W Contour x 10^4 (m/s)'
			savetit = "SalinityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
			#
			# Plotting
			ax = plt.gca()
			ax.set_xlabel("Days since 01/01/2013")
			ax.set_ylabel("Depth (m)")
			ax.set_ylim([-900,0])
			ax.set_xlim([70,110])
			# plt.title(figtit)
			fig.suptitle(figtit) 
			plt.savefig(figdirpath + savetit+ "_zoom" +".png")
#
# ===========================================================
# Contour Plotting Averaged Grided W Data
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltavg = 1, No: else
gridWpltavg = 2
if gridWpltavg == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)	
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-3])
		wcontourdep = np.zeros([interplength,arraylen-3])
		wcontourtim = np.zeros([interplength,arraylen-3])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-3):
				wcontourdat[ci,cj] = GridWDataAvg[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepthsAvg[ci,k,cj]
				wcontourtim[ci,cj] = GridWDatesAvg[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		if k == 1: # density
			#
			# Plot Set-up
			fig = plt.figure()
			ax = fig.add_subplot(1, 1, 1)
			#
			# Which Plotting Information?
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = '3 Profile Avg Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour_3Avg"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
			#
			# Plotting
			ax = plt.gca()
			ax.set_xlabel("Days since 01/01/2013")
			ax.set_ylabel("Depth (m)")
			ax.set_ylim([-900,0])
			# plt.title(figtit)
			fig.suptitle(figtit) 
			plt.savefig(figdirpath + savetit+".png")
#
# ===========================================================
# Contour Plotting Averaged Grided W Data Zoomed
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltavg = 1, No: else
gridWpltavg = 1
if gridWpltavg == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)	
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-3])
		wcontourdep = np.zeros([interplength,arraylen-3])
		wcontourtim = np.zeros([interplength,arraylen-3])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-3):
				wcontourdat[ci,cj] = GridWDataAvg[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepthsAvg[ci,k,cj]
				wcontourtim[ci,cj] = GridWDatesAvg[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		if k == 1: # density
			#
			# Plot Set-up
			fig = plt.figure()
			ax = fig.add_subplot(1, 1, 1)
			#
			# Which Plotting Information?
			Wcontlevels= np.linspace(-6,6,101)
			Wconticks = [-5,-2.5, 0,2.5, 5]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = '3 Profile Avg Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour_3Avg"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
			#
			# Plotting
			ax = plt.gca()
			ax.set_xlabel("Days since 01/01/2013")
			ax.set_ylabel("Depth (m)")
			ax.set_ylim([-900,0])
			ax.set_xlim([70,110])
			# plt.title(figtit)
			fig.suptitle(figtit) 
			plt.savefig(figdirpath + savetit+ "_zoom" +".png")
#
# ===========================================================
# Contour Plotting Averaged Grided W Data w/ Isoline avg Ws
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltIsoLineWavg = 1, No: else
gridWpltIsoLineWavg = 2
if gridWpltIsoLineWavg == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-3])
		wcontourdep = np.zeros([interplength,arraylen-3])
		wcontourtim = np.zeros([interplength,arraylen-3])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-3):
				wcontourdat[ci,cj] = GridWDataAvg[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepthsAvg[ci,k,cj]
				wcontourtim[ci,cj] = GridWDatesAvg[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		#
		# Plot Set-up
		fig = plt.figure()
		ax = fig.add_subplot(1, 1, 1)
		#
		# Which Plotting Information?
		# Which Plotting Information?
		if k == 1: # density
			Wcontlevels= np.linspace(-30,30,101)
			Wconticks = [-25, -10, 0, 10, 25]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = '3 Profile Avg Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		figtit2 = ' and Isochly W Quivers'
		savetit2 = "_ChlorophyllW_Quivers_3Avg"
		from pylab import *
		Q = plt.quiver(IsoBioWDatesAvg[1::10], IsoBioWDepthsAvg[1::10], IsoBioFakeAvg[1::10], IsoBioWDataAvg[1::10])
		plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$1 \times 10^{-3} m/s$', labelpos='W',
		               fontproperties={'weight': 'bold'})
		#
		# Plotting
		ax = plt.gca()
		ax.set_xlabel("Days since 01/01/2013")
		ax.set_ylabel("Depth (m)")
		ax.set_ylim([-900,0])
		# plt.title(figtit)
		fig.suptitle(figtit + " with " + figtit2 ) 
		plt.savefig(figdirpath + savetit+ savetit2 + ".png")

# ===========================================================
# Contour Plotting Grided W Data w/ Isoline Ws
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltIsoLineW = 1, No: else
gridWpltIsoLineW = 2
if gridWpltIsoLineW == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	for k in range (0,7):
		#
		# Plotting correct W data
		wcontourdat = np.zeros([interplength,arraylen-1])
		wcontourdep = np.zeros([interplength,arraylen-1])
		wcontourtim = np.zeros([interplength,arraylen-1])
		for ci in range(0,interplength):
			for cj in range(0,arraylen-1):
				wcontourdat[ci,cj] = GridWData[ci,k,cj]
				wcontourdep[ci,cj] = GridWDepths[ci,k,cj]
				wcontourtim[ci,cj] = GridWDates[ci,k,cj]
		#
		# Plotting correct contour data
		contourdat = np.zeros([interplength,arraylen])
		for ci in range(0,interplength):
			for cj in range(0,arraylen):
				#cjj = cj + 1
				contourdat[ci,cj] = GridData[ci,k,cj]
		#
		# Plot Set-up
		fig = plt.figure()
		ax = fig.add_subplot(1, 1, 1)
		#
		# Which Plotting Information?
		if k == 1: # density
			Wcontlevels= np.linspace(-50,50,101)
			Wconticks = [-50, -25, 0, 25, 50]
			#contlevels= np.linspace(1026.75,1027.8,100)
			conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
			figtit = 'Isopycnal Gridded W Contour x 10^4 (m/s)'
			savetit = "DensityGriddedW_Contour"
			cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
			plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
			plt.set_cmap('BlueRedAlpha')
			cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
			plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
		# Plotting
		figtit2 = ' and Isochly W Quivers'
		savetit2 = "_ChlorophyllW_Quivers"
		from pylab import *
		Q = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10])
		plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$1 \times 10^{-3} m/s$', labelpos='W',
		               fontproperties={'weight': 'bold'})

		ax = plt.gca()
		ax.set_xlabel("Days since 01/01/2013")
		ax.set_ylabel("Depth (m)")
		ax.set_ylim([-900,0])
		# plt.title(figtit)
		fig.suptitle(figtit + " with " + figtit2 ) 
		plt.savefig(figdirpath + savetit+ savetit2 + ".png")
#
# ===========================================================
# Contour Plotting Grided W Data w/ Isoline Ws and Sink Ws
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltIsoLineW = 1, No: else
gridWpltIsoLineSinkW = 2
if gridWpltIsoLineSinkW == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	#
	# Plotting correct W data
	wcontourdat = np.zeros([interplength,arraylen-1])
	wcontourdep = np.zeros([interplength,arraylen-1])
	wcontourtim = np.zeros([interplength,arraylen-1])
	for ci in range(0,interplength):
		for cj in range(0,arraylen-1):
			wcontourdat[ci,cj] = GridWData[ci,1,cj]
			wcontourdep[ci,cj] = GridWDepths[ci,1,cj]
			wcontourtim[ci,cj] = GridWDates[ci,1,cj]
	#
	# Plotting correct contour data
	contourdat = np.zeros([interplength,arraylen])
	for ci in range(0,interplength):
		for cj in range(0,arraylen):
			#cjj = cj + 1
			contourdat[ci,cj] = GridData[ci,1,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)
	#
	# Which Plotting Information?

	Wcontlevels= np.linspace(-30,30,101)
	Wconticks = [-25, -10, 0, 10, 25]
	#contlevels= np.linspace(1026.75,1027.8,100)
	conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
	figtit = 'Isopycnal Gridded W Contour x 10^4 (m/s)'
	savetit = "DensityGriddedW_Contour"
	cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
	plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
	plt.set_cmap('BlueRedAlpha')
	cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
	plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
	#
	# Plotting
	figtit2 = ' and "Sinking" Quivers'
	savetit2 = "_SinkW_Quivers"
	from pylab import *
	#Q = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], IsoBioWData[1::10])
	#plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$5 \times 10^{-3} m/s$', labelpos='W')

	Q2 = plt.quiver(IsoBioWDates[1::10], IsoBioWDepths[1::10], IsoBioFake[1::10], SinkWData[1::10], color='g')
	plt.quiverkey(Q2, 0.22, 0.96, 0.001, r'$ 1 \times 10^{-3} m/s$', labelpos='W')
	ax = plt.gca()
	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + " with " + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 + ".png")
#
# ===========================================================
# Contour Plotting Grided W Data w/ Isoline Ws
# ===========================================================
#
# Do you want to plot Gridded Ws?  Yes: gridWpltIsoLineW = 1, No: else
gridWpltIsoLineSinkWAvg = 2
if gridWpltIsoLineSinkWAvg == 1:
	#
	# ===========================================================
	# Making Blue-Red Contour Map for Vertical Velocities
	# ===========================================================
	#
	# Make Blue-Red Color Scheme
	from matplotlib.colors import LinearSegmentedColormap
	cdict3 = {'red':  ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.8, 1.0),
	                   (0.75,1.0, 1.0),
	                   (1.0, 0.4, 1.0)),

	         'green': ((0.0, 0.0, 0.0),
	                   (0.25,0.0, 0.0),
	                   (0.5, 0.9, 0.9),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0)),

	         'blue':  ((0.0, 0.0, 0.4),
	                   (0.25,1.0, 1.0),
	                   (0.5, 1.0, 0.8),
	                   (0.75,0.0, 0.0),
	                   (1.0, 0.0, 0.0))
	        }
	#
	# Make a modified version of cdict3 with some transparency
	# in the middle of the range.
	cdict4 = cdict3.copy()
	cdict4['alpha'] = ((0.0, 1.0, 1.0),
	                #   (0.25,1.0, 1.0),
	                   (0.5, 0.3, 0.3),
	                #   (0.75,1.0, 1.0),
	                   (1.0, 1.0, 1.0))
	plt.register_cmap(name='BlueRedAlpha', data=cdict4)
	#
	# Plotting correct W data
	wcontourdat = np.zeros([interplength,arraylen-3])
	wcontourdep = np.zeros([interplength,arraylen-3])
	wcontourtim = np.zeros([interplength,arraylen-3])
	for ci in range(0,interplength):
		for cj in range(0,arraylen-3):
			wcontourdat[ci,cj] = GridWDataAvg[ci,1,cj]
			wcontourdep[ci,cj] = GridWDepthsAvg[ci,1,cj]
			wcontourtim[ci,cj] = GridWDatesAvg[ci,1,cj]
	#
	# Plot Set-up
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)
	#
	# Which Plotting Information?
	Wcontlevels= np.linspace(-50,50,101)
	Wconticks = [-50, -25, 0, 25, 50]
	#contlevels= np.linspace(1026.75,1027.8,100)
	conticks = [1026.50, 1026.75, 1027.00, 1027.25, 1027.50, 1027.75, 1028.00]
	figtit = '3 Profile Avg Isopycnal Gridded W Contour x 10^4 (m/s)'
	savetit = "DensityGriddedW_Contour"
	cs = plt.contourf(wcontourtim,wcontourdep,wcontourdat*10**(4), levels = Wcontlevels)
	plt.colorbar(cs, spacing='proportional', ticks = Wconticks)
	plt.set_cmap('BlueRedAlpha')
	cs2 = plt.contour(floatdate,Ygrid,contourdat, colors='k',levels = conticks)
	plt.clabel(cs2, conticks[1::2], inline=1, fmt='%1.1f', fontsize=8)
	#
	# Plotting
	figtit2 = ' and "Sinking" Quivers'
	savetit2 = "_SinkW_Quivers_3Avg"
	from pylab import *
	#Q = plt.quiver(IsoBioWDatesAvg[1::10], IsoBioWDepthsAvg[1::10], IsoBioFakeAvg[1::10], IsoBioWDataAvg[1::10], scale = 1)
	#plt.quiverkey(Q, 0.22, 0.95, 0.001, r'$1 \times 10^{-3} m/s$', labelpos='W')

	Q2 = plt.quiver(IsoBioWDatesAvg[1::10], IsoBioWDepthsAvg[1::10], IsoBioFakeAvg[1::10], SinkWDataAvg[1::10], color='g')
	plt.quiverkey(Q2, 0.22, 0.96, 0.001, r'$ 1 \times 10^{-3} m/s$', labelpos='W')
	ax = plt.gca()
	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Depth (m)")
	ax.set_ylim([-900,0])
	# plt.title(figtit)
	fig.suptitle(figtit + " with " + figtit2 ) 
	plt.savefig(figdirpath + savetit+ savetit2 + ".png")

lineplotting = 2
if lineplotting == 1:
	from pylab import *
	numbiolinesavg = len(IsoBioWDataAvg)
	#
	# Plot Set-up
	fig = plt.figure()
	from matplotlib.font_manager import FontProperties

	fontP = FontProperties()
	fontP.set_size('x-small')
	ax = fig.add_subplot(2, 1, 1)
	#
	# Which Plotting Information?
	axhline(0, color='k')
	for j in range(0,numbiolinesavg):
		if j == 0:
			plt.plot(IsoBioWDatesAvg[j], IsoBioWDataAvg[j]*10**4, '0.75',label='3 Profile Avg. (3PA) Chlorophyll Vert. Vel.')
		else:
			plt.plot(IsoBioWDatesAvg[j], IsoBioWDataAvg[j]*10**4, '0.75')
	plt.plot(IsoBioWDatesDepthAvgAvg, IsoBioWDataDepthAvgAvg*10**4, 'k',label='Depth Avg. 3PA Chlorophyll Vert. Vel.',linewidth = 2)
	plt.plot(IsoBioWDatesDepthAvgAvg, SinkWDataDepthAvgAvg*10**4, 'g',label='Depth Avg. 3PA Sinking Vel.',linewidth = 2)
	plt.plot(IsoBioWDatesDepthAvgAvg, NearestWGriddedAvgAvg*10**4, 'b',label='Depth Avg. 3PA Isopycnal Vel. Vel. ',linewidth = 2)
	plt.plot(IsoBioWDatesDepthAvgAvg, IsoCdomWDataDepthAvgAvg*10**4, 'r', label = 'Depth Avg. 3PA CDOM Vert. Vel.',linewidth = 2)


	l = legend(loc = 2,prop = fontP)

	ax.set_xlabel("Days since 01/01/2013")
	ax.set_ylabel("Vert. Vel. x 10^4 (m/s)")
	ax.set_xlim([70,110])
	ax.set_ylim([-13,18])
	#
	#
	#
	ax2 = fig.add_subplot(2, 1, 2)
	#
	# Which Plotting Information?

	axhline(0, color='k')
	for j in range(0,numbiolinesavg):
		if j == 0:
			plt.plot(IsoBioWDatesAvg[j], IsoBioWDepthsAvg[j], '0.75',label='3 Profile Avg. (3PA) Chlorophyll Vert. Vel.')
		else:
			plt.plot(IsoBioWDatesAvg[j], IsoBioWDepthsAvg[j], '0.75')
	plt.plot(IsoBioWDatesDepthAvgAvg, IsoBioWDepthsDepthAvgAvg, 'k',label='Depth Avg. 3PA Chlorophyll, Sinking, and Isopycnal Vert. Vel.',linewidth = 2)
	plt.plot(IsoBioWDatesDepthAvgAvg, IsoCdomWDepthsDepthAvgAvg, 'r', label = 'Depth Avg. 3PA CDOM Vert. Vel.',linewidth = 2)
	#
	#
	l = legend(loc = 3,prop = fontP)

	ax2.set_xlabel("Days since 01/01/2013")
	ax2.set_ylabel("Avg. Vert. Vel. Depth (m)")
	ax2.set_xlim([70,110])



	#ax.set_yscale('log')

	savetit = "VertVelocity_Depth_TimeSeries"
	plt.savefig(figdirpath + savetit  + ".png")



os.chdir('C:/Documents and Settings/Alex/Documents/Research/Delaware/MUSTACHE/Jan01_Jun04_2013/code/interpolate_profiles')