def perc_compare(pc, winlen, season, obfile, modfile, intitle):
	import numpy as np
	from grid_tools import interp_togrid
	
	#Extract model and obs data
	odata, olat, olon = rcs_gpcp(winlen)
	mdata, mlat, mlon = rcs_gpcp(winlen, modfile)
	
	#Shift data to start of required season
	rlen = -1*(season - 1)
	odata = np.roll(odata, rlen, axis=0)
	odata = odata[0::12, :, :]
	mdata = np.roll(mdata, rlen, axis=0)
	mdata = mdata[0::12, :, :]
	
	#Calculate thresholds
	opc = np.percentile(odata, pc, axis=0)
	opc[opc < 0.0] = 0.0 #ensure percentile is not below zero, if below zero then set to 0
	mpc = np.percentile(mdata, pc, axis=0)
	mpc[mpc < 0.0] = 0.0 #ensure percentile is not below zero, if below zero then set to 0
	
	#Interpolate to the same grid spacing (coarsest) and compute difference as a percentage
	ob, mod, lon, lat = interp_togrid(opc, olat, olon, mpc, mlat, mlon)
	d = ((mod - ob)/ob)*100
	
	plot = plot_perc_compare (d, lat, lon, pc, winlen, season, intitle)

	return d, lat, lon
def perc_compare_bsoblen(pc, winlen, season, obfile, modfile, intitle):
	import numpy as np
	from grid_tools import interp_togrid
	
	#Extract model and obs data
	odata, olat, olon = rcs_gpcp(winlen)
	mdata, mlat, mlon = rcs_gpcp(winlen, modfile)
	
	#Shift data to start of required season and trim
	rlen = -1*(season - 1)
	odata = np.roll(odata, rlen, axis=0)
	odata = odata[0::12, :, :]
	mdata = np.roll(mdata, rlen, axis=0)
	mdata = mdata[0::12, :, :]
	
	#Calculate thresholds
	#Set the length of the bootstrapped data set
	n = mdata.shape[0]  #length is the length of the model data
	print("Commencing bootstrap...")
	opc, bspc = bootstrap_perc_obs(odata, n, pc)
	print("Bootstrap completed...")
	
	mpc = np.percentile(mdata, pc, axis=0)
	mpc[mpc < 0.0] = 0.0 #ensure percentile is not below zero, if below zero then set to 0
	
	#Interpolate to the same grid spacing (coarsest) and compute difference as a percentage
	ob, mod, lon, lat = interp_togrid(opc, olat, olon, mpc, mlat, mlon)
	d = ((mod - ob)/ob)*100
	
	#Calculate significance
	bslow = np.percentile(bspc, 2.5, axis=0)
	bshi = np.percentile(bspc, 97.5, axis=0)
	
	sig = np.zeros((lat.size, lon.size))-1
	sig[np.logical_and(mod >= bslow, mod <= bshi)] = 1
	
	plot = plot_perc_compare_bs (d, sig, lat, lon, pc, winlen, season, intitle)

	return d, sig, lat, lon
def mmm_perc_compare(pc, winlen, season, obfile, thresh):
	import numpy as np
	from grid_tools import interp_togrid
	
	#Extract model and obs data
	odata, olat, olon = rcs_gpcp(winlen)
	
	#Begin at correct time 
	rlen = -1*(season - 1)
	odata = np.roll(odata, rlen, axis=0)
	odata = odata[0::12, :, :]

	#Calculate thresholds
	opc = np.percentile(odata, pc, axis=0)
	opc[opc < 0.0] = 0.0 #ensure percentile is not below zero, if below zero then set to 0
	
	cmipfile = ['CMIP5/ACCESS1-0/r1i1p1/pr/pr_Amon_ACCESS1-0_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/CanESM2/r1i1p1/pr/pr_Amon_CanESM2_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/GFDL-CM3/r1i1p1/pr/pr_Amon_GFDL-CM3_historical_r1i1p1_186001-200512.nc',\
				'CMIP5/HadGEM2-CC/r1i1p1/pr/pr_Amon_HadGEM2-CC_historical_r1i1p1_185912-200511.nc',\
				'CMIP5/MPI-ESM-P/r1i1p1/pr/pr_Amon_MPI-ESM-P_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/CCSM4/r1i1p1/pr/pr_Amon_CCSM4_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/FGOALS-s2/r1i1p1/pr/pr_Amon_FGOALS-s2_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/GISS-E2-R/r6i1p1/pr/pr_Amon_GISS-E2-R_historical_r6i1p1_185001-200512.nc',\
				'CMIP5/NorESM1-M/r1i1p1/pr/pr_Amon_NorESM1-M_historical_r1i1p1_185001-200512.nc',\
				'CMIP5/IPSL-CM5B-LR/r1i1p1/pr/pr_Amon_IPSL-CM5B-LR_historical_r1i1p1_185001-200512.nc']
				
	amipfile = ['AMIP/FGOALS-s2/r1i1p1/pr/pr_Amon_FGOALS-s2_amip_r1i1p1_197901-200812.nc',\
				'AMIP/GFDL-CM3/r1i1p1/pr/pr_Amon_GFDL-CM3_amip_r1i1p1_197901_200812.nc',\
				'AMIP/HadGEM2-A/r1i1p1/pr/pr_Amon_HadGEM2-A_amip_r1i1p1_197809-200811.nc',\
				'AMIP/NorESM1-M/r1i1p1/pr/pr_Amon_NorESM1-M_amip_r1i1p1_197901-200512.nc']
	
	modpath = '/Users/ailieg/Data/drought_model_eval_data/data/'
	modfile = cmipfile
	
	d = np.zeros((len(modfile), olat.size, olon.size))
	
	for i in range(0,len(modfile)):
		
		#Extract model data
		mdata, mlat, mlon = rcs_gpcp(winlen, modfile)
	
		#Shift data to start of required season and trim
		mdata = np.roll(mdata, rlen, axis=0)
		mdata = mdata[0::12, :, :]
	
		#Calculate thresholds
		mpc = np.percentile(mdata, pc, axis=0)
		mpc[mpc < 0.0] = 0.0 #ensure percentile is not below zero, if below zero then set to 0
	
		#Interpolate to the same grid spacing (coarsest) and compute difference as a percentage
		ob, mod, lon, lat = interp_togrid(opc, olat, olon, mpc, mlat, mlon)
		d[i,:,:] = ((mod - ob)/ob)*100
		print("Model "+str(i+1)+" of "+str(len(modfile))+" completed.")
		
	#Compute multi-model mean
	mmm = np.median(d, axis=0)
	
	#Compute the agreement in the sign of the models
	sign = d
	sign[d < 0.0] = -1
	sign[d > 0.0] = 1
	
	sign = abs(np.sum(sign, axis=0))
	
	plot = plot_perc_compare_mmm (mmm, sign, lat, lon, pc, winlen, season, thresh)
	
	return mmm, sign, lat, lon