#!/usr/bin/env python

from PIL import Image,ImageTk
from Fabric import file_io
import numpy as n

class plothst:
    def __init__(self, direc='.', stem=None,grainno = 0, reflno = 0):
        import time
        self.reflno = reflno
        self.grainno = grainno
        self.setno = 0
        self.stem = stem
        self.direc = direc
        self.read_data()
        
    def read_data(self):
        #a = n.random.poisson(n.ones((32,32))*160)
        #a = a.astype(n.uint16)
        direc =  self.direc.get()
        stem = self.stem.get()
        try:
            self.ref = file_io.readref(direc,stem)
            self.ref._open(self.grainno,self.setno)
            self.ref.read(self.grainno,self.setno)
        except:
            raise IOError, 'Could not open ref file'
            


        try:
            hst = file_io.readhst(direc,stem)
            hst._open(self.grainno,self.setno)
        except:
            raise IOError, 'Error'
        self.hst = hst
        self.hst.get_file_pos()
        self.hst.read(self.grainno,
                      self.setno,
                      self.hst.spotpos[self.ref.refinfo[self.reflno,self.ref.colinfo['spot_id']]])
        #        self.hst.read(self.grainno,self.setno,self.hst.filepos[0])
        self.shoebox = n.clip(self.hst.shoebox,0, 2**16-1)
        self.shoebox = self.shoebox.astype(n.uint16)
        
