text: import sys
  sys.path.append("../")
  
  import seabird
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  import json,sys
  import logging
  from sqlalchemy import create_engine
  import pickle
  import fnmatch
  import os
  
  def plotSegment(mySeabird, site = None, year = None, folder = '/Users/wenzhaoxu/Developer/Seabird/output/meta/',legendLoc = 4):
  	from seabird.seabird_class import seabird
  
  	fname = "%s/%s_%d_%d.p" %(folder,mySeabird.site,int(mySeabird.time.year),int(mySeabird.fileId))
  	print(fname)
  
  	mySeabird
  	pt = plt.figure(figsize=(4.5,8))
  	ax1 = pt.add_subplot(111)
  	
  	ax1.plot(mySeabird.cleanData.Temperature, -mySeabird.cleanData.Depth, "r",linewidth=1)
  	ax1.plot(mySeabird.downCastRawData.Temperature, -mySeabird.downCastRawData.Depth, "r--", alpha=0.5,linewidth=1)
  	ax1.set_xlabel("Temperature (C)")
  	ax1.set_ylabel("Depth (m)")
  	for seg in mySeabird.thermocline.models["segmentation"].segmentList:
  		ax1.plot(seg[0],-np.array(mySeabird.cleanData.Depth[seg[1]]),linewidth=5)
  	
  	plt.savefig("/Users/wenzhaoxu/Developer/Seabird/output/focus/segment_%s_%s.pdf" %(os.path.splitext(os.path.basename(fname))[0],config["Algorithm"]["segment"]["max_error"]),
  		bbox_inches='tight', dpi=350)
  	plt.close()
  
  
  def plotHistoryProfile(site, years = range(1996,2014), var = "TRM", targetYear = 2000):
  	# site is site
  	# year is a list
  	folder = '/Users/wenzhaoxu/Developer/Seabird/output/meta/'
  
  	depths = []
  	values = []
  	pt = plt.figure(figsize = (3.5, 4.5))
  
  	for year in years:
  		# if year == targetYear:
  			# continue
  		fname = None
  		for file in os.listdir(folder):
  			if fnmatch.fnmatch(file, '%s_%d_*.p' %(site,year)):
  				fname = "%s/%s" %(folder,file)
  				print(fname)
  				break
  		if fname is None:
  			continue
  		mySeabird = pickle.load(open(fname,"rb"), encoding="latin1")
  		features = mySeabird.features
  		depths.append(max(mySeabird.cleanData.Depth))
  
  		if var in ["TRM","UHY","LEP"]:
  			plt.plot(mySeabird.downCastRawData.Temperature, -mySeabird.downCastRawData.Depth, ls = "--", alpha=1, color = '0.75', label='_nolegend_')
  			values.append(max(mySeabird.downCastRawData.Temperature))
  		elif var == "DCL":
  			plt.plot(mySeabird.downCastRawData.Fluorescence, -mySeabird.downCastRawData.Depth, ls = "--", alpha=1, color = '0.75',  label='_nolegend_')
  			values.append(max(mySeabird.downCastRawData.Fluorescence))
  
  	if targetYear:
  		for file in os.listdir(folder):
  			if fnmatch.fnmatch(file, '%s_%d_*.p' %(site,targetYear)):
  				fname = "%s/%s" %(folder,file)
  				break
  		mySeabird = pickle.load(open(fname,"rb"), encoding="latin1")
  		features = mySeabird.features
  
  
  		if var in ["TRM","UHY","LEP"]:
  			depth_algorithm = mySeabird.features[var+"_segment"]
  			plt.plot(mySeabird.downCastRawData.Temperature, -mySeabird.downCastRawData.Depth, ls = "--", alpha=1, color = '0.75', label = "Historical Profile")
  			plt.plot(mySeabird.downCastRawData.Temperature, -mySeabird.downCastRawData.Depth, "r", alpha=1, label = "%d Profile" % targetYear)
  			plt.axhline(y = -1*depth_algorithm if depth_algorithm is not None else -999,color = 'b',label = "%d %s" %(targetYear, var))
  		elif var == "DCL":
  			depth_algorithm = mySeabird.features["DCL_depth"]
  			plt.plot(mySeabird.downCastRawData.Fluorescence, -mySeabird.downCastRawData.Depth, ls = "--", alpha=1, color = '0.75', label = "Historical Profile")
  			plt.plot(mySeabird.downCastRawData.Fluorescence, -mySeabird.downCastRawData.Depth, "r", alpha=1, label = "%d Profile" % targetYear)
  			plt.axhline(y = -1*depth_algorithm if depth_algorithm is not None else -999,color = 'b',label = "%d %s" %(targetYear, var))
  
  	plt.ylim((-max(depths) - 5, 0))
  	plt.xlim((0, max(values)*1.1))
  	plt.ylabel("Depth (m)")
  
  	if var in ["TRM", "UHY", "LEP"]:
  		plt.xlabel("Temperature (C)")
  	elif var == "DCL":
  		plt.xlabel("Fluorescence (ug/L)")
  
  	# plt.show()
  	plt.legend(loc = 4)
  	plt.savefig("/Users/wenzhaoxu/Developer/Seabird/finalResults/focus/History_%s_%d_%s.png" %(site, targetYear, var), bbox_inches='tight', dpi = 350)
  	plt.close()
  
  
  
  def plotProfile(fid=None, fileName=None, var="DCL", legendLoc=4, expertNote=None):
  	from seabird.seabird_class import seabird
  
  	if fileName is None and fid is not None:
  		folder='/Users/wenzhaoxu/Developer/Seabird/output/meta/'
  		for file in os.listdir(folder):
  			if fnmatch.fnmatch(file, '*_%d.p' %(fid,)):
  				fileName = "%s/%s" %(folder,file)
  				break
  		mySeabird = pickle.load(open(fileName,"rb"), encoding="latin1") # set encoding to deal with incompatible of pickle file generated by python 2
  	elif fileName is not None:
  		if fileName.endswith(".cnv"):
  			config=json.load(open('/Users/WenzhaoXu/Developer/Seabird/SeabirdCode/config.json'))
  			mySeabird = seabird(config=config)
  			mySeabird.loadData(dataFile=fileName)
  
  			# set expert data
  			if var != "None":
  				for k in expertNote.keys():
  					mySeabird.expert[k] = expertNote[k]
  		else:
  			mySeabird = pickle.load(open(fileName,"rb"), encoding="latin1") # set encoding to deal with incompatible of pickle file generated by python 2
  		
  	
  	# refit data
  	mySeabird.preprocessing()
  	mySeabird.identify()
  	
  	features = mySeabird.features
  
  	
  	pt = plt.figure(figsize=(3,4))
  	ax1 = pt.add_subplot(111)
  	ax2 = ax1.twiny()
  
  	ax1.plot(mySeabird.downCastRawData.Temperature, -mySeabird.downCastRawData.Depth, "r--", alpha=0.5,label = "Raw Temperature")
  	xlimRange = (0,np.percentile(mySeabird.downCastRawData["Fluorescence"][mySeabird.downCastRawData.Depth > 2],99) * 1.3)
  
  	ax2.set_xlim(xlimRange)
  	ax2.set_xlabel("Fluorescence (ug/L)")
  	ax2.plot(mySeabird.downCastRawData.Fluorescence, -mySeabird.downCastRawData.Depth, "g--", alpha=0.5,label = "Raw Fluorescence")
  	
  	ax1.set_xlabel("Temperature (C)")
  	ax1.set_ylabel("Depth (m)")
  	
  	ax1.set_ylim((-max(mySeabird.cleanData.Depth) - 5, 0))
  
  	if var == "DCL":
  		depth_algorithm = mySeabird.features["DCL_depth"]
  		ax2.axhline(y = -1*depth_algorithm if depth_algorithm is not None else -999,color = 'b',label = "Algorithm")
  
  		depth_expert = mySeabird.expert["DCL"]
  		ax2.axhline(y = -1*depth_expert if depth_expert is not None else -999,color = 'b',ls="--",label = "Operator")
  
  	elif var in ["TRM","LEP","UHY"]:
  		depth_algorithm = mySeabird.features[var+"_segment"]
  		ax2.axhline(y = -1*depth_algorithm if depth_algorithm is not None else -999,color = 'b',label = "Algorithm")
  
  		depth_expert = mySeabird.expert[var]
  		ax2.axhline(y = -1*depth_expert if depth_expert is not None else -999,color = 'b',ls="--", label = "Operator")
  	else:
  		pass
  
  	lines, labels = ax1.get_legend_handles_labels()
  	lines2, labels2 = ax2.get_legend_handles_labels()
  	if legendLoc >-1:
  		ax1.legend(lines + lines2, labels + labels2, loc=legendLoc, prop={'size':10})
  	plt.savefig("%s/%s_%s.png" %(outputFolder, os.path.splitext(os.path.basename(fileName))[0],var),
  		bbox_inches='tight', dpi = 350)
  	plt.close()
  
  if __name__ == '__main__':
  	dataFolder = "./data/sampleData/"
  	outputFolder = "./paperResults/plot9and11/"
  
  	expertNote = {
  
  	}
  
  	# Plot figure 9
  	plotProfile(fileName=dataFolder + "SU09_2011.cnv", var = "DCL",legendLoc = -1, expertNote={"DCL": 47.1}) # SU09 2011 for DCL mislabled
  	plotProfile(fileName=dataFolder + "ER15_2009.cnv", var = "LEP",legendLoc = -1, expertNote={"LEP": 12.4}) # ER15_2009_1444 for LEP algorithm limitations
  	plotProfile(fileName=dataFolder + "SU09_2002.cnv", var = "TRM",legendLoc = -1, expertNote={"TRM": 25.4}) # SU09 2002 for TRM definition
  	plotProfile(fileName=dataFolder + "HU32_2011.cnv", var = "DCL",legendLoc = -1, expertNote={"DCL": 16}) # HU32 2011 for DCL definition
  	plotProfile(fileName=dataFolder + "MI40_2005.cnv", var = "DCL",legendLoc = -1, expertNote={"DCL": 23.5})
  	plotProfile(fileName=dataFolder + "ON55_1999.cnv", var = "UHY",legendLoc = -1, expertNote={"UHY": 39.8}) # ON55_1999 for large transition zone
  	
  	# plot figure 11
  	plotProfile(fileName=dataFolder + "SU17_2008.cnv", var = "None",legendLoc = 4)# 2008 SU17 for DCL asymesstry shape 1.459844
  	plotProfile(fileName=dataFolder + "HU12_2000.cnv", var = "None",legendLoc = 4)# 2008 SU17 for DCL asymesstry shape 1.459844
  	plotProfile(fileName=dataFolder + "SU11_2008.cnv", var = "None",legendLoc = 4)# 2008 SU17 for DCL asymesstry shape 1.459844
  
  	# not run. The following code requires all data from SU15, SU21, SU07 and SU05
  	# plotHistoryProfile("SU15",var = "TRM", targetYear=2009)
  	# plotHistoryProfile("SU21",var = "TRM", targetYear=2010)
  	# plotHistoryProfile("SU07",var = "DCL", targetYear=2011)
  	# plotHistoryProfile("SU05",var = "DCL", targetYear=2004)