import os, sys
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
#from creader import ClusterFileReader
import creader as cr
import clustersFunctions as cf

fname = "/mnt/myData/230914_30s_star_100um_nofi/star_"
fnameff = "/mnt/myData/230914_30s_flat_100um_nofi/flat_"
xmin=161+20
xmax=xmin+40
ymin=161+20
ymax=ymin+40
emin=0
emax=30
ecutmin=8
ecutmax=12
etabins=251
csize=3
gain=150
nbins=100
indmin=1
indmax=20


subpix=5


im=None
intim=None
etas=None
sp=None
ietax=None
ietay=None

for i in range(indmin,indmax+1):
    #ff=fname
    ff=fnameff+str(i)+".clust"
    print(ff)
    r = cr.ClusterFileReader(ff)
    im, sp, ebins, etas, etabinsx, etabinsy=cf.analyze_clusters(r, emin, emax, ecutmin, ecutmax, xmin, xmax, ymin, ymax,   ietax, ietay, im, sp, etas, intim,csize, gain, nbins, etabins)
    print(np.sum(im))

ietax, ietay=cf.prepare_interpolation(etas)




im=None
intim=None
etas=None
sp=None


for i in range(indmin,indmax+1):
    #ff=fname
    ff=fname+str(i)+".clust"
    print(ff)
    r = cr.ClusterFileReader(ff)
    im, intim, sp, ebins, etas, etabinsx, etabinsy=cf.analyze_clusters(r, emin, emax, ecutmin, ecutmax, xmin, xmax, ymin, ymax,   ietax, ietay, im, sp, etas, intim, csize, gain, nbins, etabins, subpix)
  


imff=None
intimff=None
etasff=None
spff=None

for i in range(indmin,indmax):
    ff=fnameff+str(i)+".clust"
    print(ff)
    r = cr.ClusterFileReader(ff)
    imff, intimff, spff, ebins, etasff, etabinsx, etabinsy=cf.analyze_clusters(r, emin, emax, ecutmin, ecutmax, xmin, xmax, ymin, ymax,ietax, ietay, imff, spff, etasff, intimff, csize, gain, nbins, etabins, subpix)
  



fig, ax = plt.subplots()
ax.plot(ebins[:-1],sp)
ax.plot(ebins[:-1],spff)
#ax.set_yscale('log')
fig.show()
"""
fig1, axs1 = plt.subplots()
vv=axs1.imshow(intim/intimff,vmax=1.,origin='upper',cmap=plt.cm.jet)
fig1.colorbar(vv, ax=axs1)
fig1.show()
"""
cf.plot_colz(im/imff,1.1)
cf.plot_colz(intim/intimff,1.1)

cf.plot_colz(etas,np.max(etas))
cf.plot_colz(ietax,1.1)
cf.plot_colz(ietay,1.1)
