############################################################################################################
##################################################        Strg+Shift+1
################### import stuff, datearray stuff, multiPDF plot stuff
############################################################################################################

# This scripts is doing...
# Patrick Kuehl, date
import time
from pylab import *
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
from matplotlib.colors import LogNorm
from scipy.optimize import leastsq
from matplotlib import dates
import time
import datetime as date
from datetime import datetime
from datetime import timedelta
import os
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
from matplotlib.backends.backend_pdf import PdfPages

#datearray=[(datetime(year[date],1,1,0)+timedelta(doy[date]-1,sec[date])) for date in range(len(year))]
#pp= PdfPages('contour_relative.pdf')
#  pp.savefig()
#  plt.close()
#pp.close()

#def runningMeanFast(x, N):
#    return np.convolve(x, np.ones((N,))/N)[(N-1):]

#plt.figure(figsize=(8.27,11.69))
#plt.subplots_adjust(top=0.96,bottom=0.05,hspace=0.2,right=0.86)


############################################################################################################
##################################################        Strg+Shift+2
################### histo maker and plotter
############################################################################################################

bins=np.linspace(0,100,101)
hist,binedges=np.histogram(data,bins)
bincenter=np.ones(len(binedges)-1)
for i in range(len(binedges)-1):
  bincenter[i]=0.5*(binedges[i]+binedges[i+1])
plt.figure()
plt.step(bincenter,hist,where='mid')
plt.xlabel("Data value")
plt.ylabel("counts")
plt.savefig("histo.eps")

############################################################################################################
##################################################        Strg+Shift+3
################### fit routine
############################################################################################################

fitfunc= lambda p, x: p[0]+p[1]*x
errfunc= lambda p,x,y: fitfunc(p,x)-y
p0=[2.,1.]
para,success,infodict,mesg,ier=leastsq(errfunc, p0[:], args=(x,y),full_output=1,warning=True)
chisq=(infodict['fvec']**2).sum()
fitx=np.linspace(np.min(x),np.max(x),1000)

plt.figure()
plt.plot(x,y,'r+',label='data')
plt.plot(fitx,fitfunc(para,fitx),'k-',label='fit')
plt.legend()
plt.xlabel("this is the x-axis")
plt.ylabel("this is the y-axis")
plt.savefig("fit.eps")


############################################################################################################
##################################################        Strg+Shift+4
################### plot routine
############################################################################################################

plt.figure()
plt.plot(x,y,'k+',label='myLabel')
plt.legend()
#plt.xscale('log')
#plt.yscale('log')
#plt.xlim([0,1])
#plt.ylim([0,1])
plt.xlabel("this is the x-axis")
plt.ylabel("this is the y-axis")
plt.savefig("plotname.eps")

############################################################################################################
##################################################        Strg+Shift+5
################### 2d hist routine
############################################################################################################

H,xedges,yedges=np.histogram2d(np.log10(etot*ea),np.log10(etot/ea),bins=(200,200),range=[[-3,3],[0,3]])

xs=np.ones(len(xedges)-1)
ys=np.ones(len(yedges)-1)
for i in range(len(xs)):
  xs[i]=10**(0.5*(xedges[i]+xedges[i+1]))
for i in range(len(ys)):
  ys[i]=10**(0.5*(yedges[i]+yedges[i+1]))

levels=np.logspace(-1,3,10)
plt.contourf(xs,ys,np.transpose(H)+0.001,levels=levels,norm=LogNorm())
cbar=plt.colorbar()
cbar.set_label('Counts per cell')


