# $CVSHeader: pal8e/src/Extparm.py,v 1.4 2005/02/15 01:10:26 indraneel Rel $

import math
import Extparm_c

class extparm:
    def __init__(self, conf):
        datdir = conf.get('Data', 'datdir')
        self._store_prob(datdir+'syn-bond-prob2', 'sblp')
        self._store_prob(datdir+'anti-bond-prob2', 'ablp')
        self._store_prob(datdir+'syn-torsion-arrow-prob2', 'stlp')
        self._store_prob(datdir+'anti-torsion-arrow-prob2', 'atlp')

    def _store_prob(self, fname, tag):
        probfile = open(fname);
        lines = probfile.readlines()
        probfile.close()
        stor = {}
        for line in lines:
            if line[0] == '#': continue
            (sd, prob) = line.split()
            # change 0 to least probable value locally in prob file
            # do not globally change here
            # set <-6 and >6 sigma to 6 sigma
            #if (float(prob) == 0): prob = 0.0001
            stor[sd] = -1 * math.log(float(prob))
        setattr(self, tag, stor)

    def get_val(self, tag, sd):
        if tag == 'lpg':
            return Extparm_c.lpg(sd)
        sd = '%.1f' % sd
        if sd == '-0.0': sd = '0.0'
        if hasattr(self, tag):
            stor = getattr(self, tag)
            if stor.has_key(sd):
                return stor[sd]

