import time, copy
import os
import sys
import numpy
import h5py
#from PnSC_ui import *
#from PnSC_dataimport import *
from PnSC_SCui import *
#from PnSC_math import *
from PnSC_h5io import *
from PnSC_main import *
from matplotlib.ticker import FuncFormatter
import scipy.integrate

celllist=[1, 2]+range(4, 21)+[22]+[24, 25]
for selectcell in celllist:
    print selectcell
    p='C:/Users/JohnnyG/Documents/PythonCode/Vlassak/NanoCalorimetry/AuSiCu_pnsc_all.h5'

    def myexpformat(x, pos):
        for ndigs in range(2):
            lab=(('%.'+'%d' %ndigs+'e') %x).replace('e+0','e').replace('e+','e').replace('e0','').replace('e-0','e-')
            if eval(lab)==x:
                return lab
        return lab
    ExpTickLabels=FuncFormatter(myexpformat)

    def make_ticklabels_invisible(ax, x=True, y=True):
        if x:
            for tl in ax.get_xticklabels():
                tl.set_visible(False)
        if y:
            for tl in ax.get_yticklabels():
                tl.set_visible(False)

    def heatrate_T(d, T, Twin=10.):
        #i=numpy.argmin((T-d['sampletemperature'])**2)
        Ta=d['sampletemperature'][cycleindex]
        x=numpy.where((Ta>=T-Twin)&(Ta<=T+Twin))[0]
        prev=numpy.array([not (t-1 in x) for t in x])
        previ=numpy.where(prev)[0]
        if len(previ)==0:
            return 0.
        stopi=numpy.append(previ[1:],len(x))
        longestbunchind=numpy.argmax(stopi-previ)
        inds=x[previ[longestbunchind]:stopi[longestbunchind]]
        return d['sampleheatrate'][cycleindex][inds].mean()
        
        

    def findenthalpyandpinacles(segdict, critenth=1.e-5, dTmin=.4, Tmeanmin=100.):
        T=segdict['sampletemperature'][cycleindex]
        C=segdict['sampleheatcapacity'][cycleindex]
        nci=numpy.where((C[:-1]>0.)&(C[1:]<=0.))[0]#neg crossings
        pci=numpy.where((C[1:]>0.)&(C[:-1]<=0.))[0]#pos crossings
        ci=numpy.sort(numpy.concatenate([nci, pci]))
        ans=[]
        for i, j in zip(ci[:-1], ci[1:]):
            enth=scipy.integrate.trapz(C[i:j], T[i:j])
            if numpy.abs(enth)>critenth and (T[j]-T[i])>dTmin:
                itemp=numpy.argmax(numpy.abs(C[i:j]))
                Tmean=scipy.integrate.trapz(C[i:j]*T[i:j], T[i:j])/scipy.integrate.trapz(C[i:j], T[i:j])
                if Tmean<Tmeanmin:
                    continue
                ans+=[dict([('enthalpy', enth), ('T_Cmax', T[i:j][itemp]), ('Cmax', C[i:j][itemp]), ('Tweightedmean', Tmean), ('cycindstart', i), ('cycindstop', j)])]
        return ans
        
        
        
        
    nskip=100

    cycleindex=0
    #p='C:/Users/JohnnyG/Documents/PythonCode/Vlassak/NanoCalorimetry/AuSiCu_pnsc_all.h5'
    #p=mm.h5path
    #f=h5py.File(p, mode='r+')
    #f=h5py.File(p, mode='r')
    savef='C:/Users/JohnnyG/Documents/HarvardWork/MG/PnSCplots/batchplotbycell_June2'

    plotTlim=(50., 700.)

    expdicts=[\
    dict([('name', 'heat1a'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat1b'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat1c'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat1d'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat2'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat3'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat4a'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat4b'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat4c'), ('heatseg', 2), ('coolseg', 3)]), #never partitioned\
    dict([('name', 'heat6a'), ('heatseg', 2), ('coolseg', 4)]), #never partitioned\
    dict([('name', 'heat6b'), ('heatseg', 2), ('coolseg', 4)]), \
    dict([('name', 'heat6c'), ('heatseg', 2), ('coolseg', 3)]), #never partitioned\
    dict([('name', 'heat7'), ('heatseg', 2), ('coolseg', 3)]), #never partitioned\
    dict([('name', 'heat8'), ('heatseg', 2), ('coolseg', 4)]), \
    ]

    metadictlist=[]
    allsegdict=[]
    for ed in expdicts:
        exp=ed['name']
        f, hppaths=experimenthppaths(p, exp)
        f.close()
        hpsdl=None
        saveh5hpname=None
        for hpp in hppaths:
            h5hpname=hpp.rpartition('/')[2]
            f, g=gethpgroup(p, exp, h5hpname=h5hpname)
            cell=g.attrs['CELLNUMBER']
            f.close()
            if cell!=selectcell:
                continue
            hpsdl=CreateHeatProgSegDictList(p, exp, h5hpname)
            saveh5hpname=h5hpname
        if not hpsdl is None:
            allsegdict+=[hpsdl]
            ed['h5hpname']=saveh5hpname
            metadictlist+=[copy.deepcopy(ed)]

    for i, (metadict, hpsdl) in enumerate(zip(metadictlist, allsegdict)):
        if hpsdl is None:
            continue
        prevhpsdl=None
        for tempmetadict, prevhpsdl in zip(metadictlist[:i][::-1], allsegdict[:i][::-1]):
            if not prevhpsdl is None:
                prevmetadict=tempmetadict
                #prevmetadict=metadictlist[allsegdict.index(prevhpsdl)]
                break
        if prevhpsdl is None:
            metadict['prevcoolrate_180C']=-1.e2
            metadict['prevcoolrate_250C']=-1.e2
            metadict['prevcoolrate_320C']=-1.e2
            metadict['prevcoolrate_400C']=-1.e2
            metadict['prevname']='heat0'
        else:
            metadict['prevcoolrate_180C']=heatrate_T(prevhpsdl[prevmetadict['coolseg']], 180.)
            metadict['prevcoolrate_250C']=heatrate_T(prevhpsdl[prevmetadict['coolseg']], 250.)
            metadict['prevcoolrate_320C']=heatrate_T(prevhpsdl[prevmetadict['coolseg']], 320.)
            metadict['prevcoolrate_400C']=heatrate_T(prevhpsdl[prevmetadict['coolseg']], 400.)
            metadict['prevname']=prevmetadict['name']
        print metadict['name'], ', previous scan ', metadict['prevname']
        metadict['heatrate_170C500C']=heatrate_T(hpsdl[metadict['heatseg']], 335., Twin=165.)
        if not 'sampleheatcapacity' in hpsdl[metadict['heatseg']].keys():
            continue
        enthdlist=findenthalpyandpinacles(hpsdl[metadict['heatseg']], critenth=1.e-6, dTmin=10.)
        if len(enthdlist)>0:
            for k in enthdlist[0].keys():
                metadict['Cpregions_'+k]=[]
                for d in enthdlist:
                    metadict['Cpregions_'+k]+=[d[k]]
                metadict['Cpregions_'+k]=numpy.array(metadict['Cpregions_'+k])

        metadict['Cpregions_glassind']=-1
        metadict['Cpregions_xtalind']=-1
        metadict['Cpregions_meltind']=-1
        metadict['Cpregions_melt2ind']=-1
        ctemp=0.
        for enthind, d in enumerate(enthdlist):
            if d['Tweightedmean']<100. or d['Cmax']<0:
                continue
            if d['Tweightedmean']>250.:
                break
            if metadict['Cpregions_glassind']<0 or numpy.abs(d['enthalpy'])>ctemp:
                metadict['Cpregions_glassind']=enthind
                ctemp=numpy.abs(d['enthalpy'])
        ctemp=0.
        for enthind, d in enumerate(enthdlist):
            if d['Tweightedmean']<180. or d['Cmax']>0:
                continue
            #if metadict['Cpregions_glassind']<0 or d['Tweightedmean']>450.:
            if d['Tweightedmean']>450.:
                break
            if metadict['Cpregions_xtalind']<0 or numpy.abs(d['enthalpy'])>ctemp:
                metadict['Cpregions_xtalind']=enthind
                ctemp=numpy.abs(d['enthalpy'])
        ctemp=0.
        ctemp2=0.
        for enthind, d in enumerate(enthdlist):
            if d['Tweightedmean']<300. or d['Cmax']<0:
                continue
            if d['Tweightedmean']>600.:
                break
            if metadict['Cpregions_meltind']<0:
                metadict['Cpregions_meltind']=enthind
                ctemp=numpy.abs(d['enthalpy'])
            else:
                if numpy.abs(d['enthalpy'])>ctemp:
                    if metadict['Cpregions_meltind']<0 or ctemp>ctemp2:
                        metadict['Cpregions_melt2ind']=metadict['Cpregions_meltind']
                        ctemp2=ctemp
                    metadict['Cpregions_meltind']=enthind
                    ctemp=numpy.abs(d['enthalpy'])
                elif numpy.abs(d['enthalpy'])>ctemp2:
                    metadict['Cpregions_melt2ind']=enthind
                    ctemp2=numpy.abs(d['enthalpy'])
                
        if selectcell==1 and metadict['name']=='heat8':
            metadict['Cpregions_glassind']=-1
        if selectcell==7 and metadict['name']=='heat1a':
            metadict['Cpregions_xtalind']=-1
        if selectcell==12 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1
        if selectcell==13 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1
        if selectcell==14 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1
        if selectcell==16 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1
        if selectcell==17 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1
        if selectcell==18 and metadict['name']=='heat8':
            metadict['Cpregions_glassind']=-1
        if selectcell==22 and metadict['name']=='heat1a':
            metadict['Cpregions_glassind']=-1
            metadict['Cpregions_xtalind']=-1   
        if selectcell==25 and metadict['name']=='heat8':
            metadict['Cpregions_glassind']=-1
    savebool=True
    if savebool:
        f=h5py.File(p, mode='r+')
        if 'calbycellmetadata' in f:
            g=f['calbycellmetadata']
        else:
            g=f.create_group('calbycellmetadata')
        if `selectcell` in g:
            cg=g[`selectcell`]
        else:
            cg=g.create_group(`selectcell`)
        for metadict in metadictlist:
            if selectcell==1 and metadict['name'].startswith('heat1'):
                continue
            if selectcell==6 and metadict['name']=='heat6a':
                continue
            if selectcell==16 and (metadict['name']=='heat2' or metadict['name']=='heat8'):