# $CVSHeader: pal8e/src/DSSPContours.py,v 1.2 2005/02/15 01:10:25 indraneel Rel $

# calculates contours for different sec str types
# tags presence of sec str type for each 5x5 block for each type

import os
from PhiPsi import StatKeeper

class Parm:
    datdir = '../dat/stat/'
    datfilebase = 'dsspphipsi_'
    SStypes = ['B', 'E', 'G', 'H', 'I', 'S', 'T']
    gridsize = 10

class Contour:
    def __init__(self, parm):
        self.parm = parm
        self.SS = {}
        self.state = {}
        self.statkeeper = StatKeeper(parm)
        for phi in range(-180/parm.gridsize, 180/parm.gridsize):
            Phi = (phi*parm.gridsize)+(parm.gridsize/2.0)
            self.SS[Phi] = {}
            self.state[Phi] = {}
            for psi in range(-180/parm.gridsize, 180/parm.gridsize):
                Psi = (psi*parm.gridsize)+(parm.gridsize/2.0)
                self.SS[Phi][Psi] = {}
                self.state[Phi][Psi] = []
                for SS in parm.SStypes:
                    self.SS[Phi][Psi][SS] = 0
        #print self.SS

    def measure(self):
        parm = self.parm
        for SS in parm.SStypes:
            ss = None
            if SS in ['E']: ss = 'E'
            elif SS in ['G', 'H', 'I']: ss = 'H'
            fname = parm.datdir+parm.datfilebase+SS
            fh = file(fname)
            lines = fh.readlines()
            fh.close()
            for line in lines:
                line = line[:-1]
                [phi, psi] = map(float, line.split())
                if phi == 360.0: continue
                if psi == 360.0: continue
                if phi == 180.0: phi = 179.99
                if psi == 180.0: psi = 179.99
                if ss in ['H', 'E']:
                    self.statkeeper.addData([[phi, psi]], ss)
                Phi = int(phi/parm.gridsize) * parm.gridsize + \
                      (parm.gridsize/2.0)
                Psi = int(psi/parm.gridsize) * parm.gridsize + \
                      (parm.gridsize/2.0)
                #print phi, psi, Phi, Psi, SS
                self.SS[Phi][Psi][SS] += 1
        #print self.SS

    def mkstate(self):
        parm = self.parm
        total = {}
        for SS in parm.SStypes:
            total[SS] = 0
        for phi in range(-180/parm.gridsize, 180/parm.gridsize):
            Phi = (phi*parm.gridsize)+(parm.gridsize/2.0)
            for psi in range(-180/parm.gridsize, 180/parm.gridsize):
                Psi = (psi*parm.gridsize)+(parm.gridsize/2.0)
                for SS in parm.SStypes:
                    total[SS] += self.SS[Phi][Psi][SS]
        for phi in range(-180/parm.gridsize, 180/parm.gridsize):
            Phi = (phi*parm.gridsize)+(parm.gridsize/2.0)
            for psi in range(-180/parm.gridsize, 180/parm.gridsize):
                Psi = (psi*parm.gridsize)+(parm.gridsize/2.0)
                for SS in parm.SStypes:
                    ss = self.SS[Phi][Psi][SS]
                    if float(ss)/total[SS] == 0:
                        continue
                    self.state[Phi][Psi].append(SS)
                    if ss < 2: continue
                    #if SS in ['E']: print Phi, Psi
                    #if SS in ['G', 'H', 'I']: print Phi, Psi
                    #if SS in ['T']: print Phi, Psi

    def writeGrid(self, outfname):
        for typ in ['H', 'E']:
            griddata = self.statkeeper.getNormGridDataList(typ)
            lines = []
            for d in griddata:
                lines.append(str(d[0])+'\t'+str(d[1])+'\t'+str(d[2])+'\n')
            ofh = file(outfname+typ+'grid', 'w')
            ofh.writelines(lines)
            

    def run(self):
        parm = self.parm
        self.measure()
        #self.mkstate()
        self.writeGrid(parm.datdir+parm.datfilebase)
        

if __name__ == '__main__':
    parm = Parm()
    contour = Contour(parm)
    contour.run()
