# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 12:59:28 2012

@author: SBrink
"""
from struct import unpack
from pylab import figure, show
from collections import deque

class SGM_Chunk:
    chunk_size = 2000*60 #1 second ~ 2kb -> 1 minute
    lta_sum = 0
    sta_sum = 0
    lta_buffer = deque()
    sta_buffer = deque()
    lta_buffer_full = False
    lta_length = 256
    sta_length = 32
    lta_shift = 8
    sta_shift = 5
    this_chunk = []
    previous_chunk = []
    persistance = 0
    persistance_threshold = 10
    
    def input_chunk(self, chunk):
        self.previous_chunk = self.this_chunk
        self.this_chunk = list(unpack(">%dh" % (len(chunk) / 2), chunk)) 
    
    def process_chunk(self):
        
        position = 0
        lta=0
        sta=0
        
        
        for value in self.this_chunk:
            abs_value = abs(value)        
            
            self.lta_sum += abs_value        
            self.sta_sum += abs_value
            
            self.lta_buffer.append(abs_value)                
            self.sta_buffer.append(abs_value)
            
            if len(self.lta_buffer) == self.lta_length:
                self.lta_sum -= self.lta_buffer.popleft()
                self.lta_buffer_full = True
            if len(self.sta_buffer) == self.sta_length:
                self.sta_sum -= self.sta_buffer.popleft() 
            
            if self.lta_buffer_full == True:
                lta = self.lta_sum >> self.lta_shift
                sta = self.sta_sum >> self.sta_shift
            
            ratio = 0
            while (sta > lta):
                sta -= lta
                ratio += 1
    
            if ratio == 2:
                print "SMALL EVENT"
                
            if ratio >= 4:
                if self.persistance < self.persistance_threshold :
                    self.persistance += 1
                
                if self.persistance == self.persistance_threshold :
                    self.persistance = 0
                    print "EVENT"
                    
            elif self.persistance > 0:
                self.persistance -= 1
            
            position += 1  
                
            
def read_in_chunks(file_object, chunk_size=1024):
    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data



#path = "D:\SGM Data\Continuous 007\wave\CNT_20120417_151503.raw"
path = "D:\SGM Data\Continuous 008\wave\CNT_20120417_163753.raw"
file_name = open( path, "rb" )


i = 0
chunk = SGM_Chunk()

for piece in read_in_chunks(file_name, chunk.chunk_size):
    chunk.input_chunk(piece) 
    chunk.process_chunk()
    
    i = i + 1  
    if i == 86400*2/60: # 2 days @ 3600 seconds/hour
        break
    if (i%60) == 0: #Every hour
        print "Hour: " + str(i/60)

file_name.close()
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 12:59:28 2012

@author: SBrink
"""
from struct import unpack
from pylab import figure, show
from collections import deque

class SGM_Chunk:
    chunk_size = 2000*60 #1 second ~ 2kb -> 1 minute
    lta_sum = 0
    sta_sum = 0
    lta_buffer = deque()
    sta_buffer = deque()
    lta_buffer_full = False
    lta_length = 256
    sta_length = 32
    lta_shift = 8
    sta_shift = 5
    this_chunk = []
    previous_chunk = []
    persistance = 0
    persistance_threshold = 10
    
    def input_chunk(self, chunk):
        self.previous_chunk = self.this_chunk
        self.this_chunk = list(unpack(">%dh" % (len(chunk) / 2), chunk)) 
    
    def process_chunk(self):
        
        position = 0
        lta=0
        sta=0
        
        
        for value in self.this_chunk:
            abs_value = abs(value)        
            
            self.lta_sum += abs_value        
            self.sta_sum += abs_value
            
            self.lta_buffer.append(abs_value)                
            self.sta_buffer.append(abs_value)
            
            if len(self.lta_buffer) == self.lta_length:
                self.lta_sum -= self.lta_buffer.popleft()
                self.lta_buffer_full = True
            if len(self.sta_buffer) == self.sta_length:
                self.sta_sum -= self.sta_buffer.popleft() 
            
            if self.lta_buffer_full == True:
                lta = self.lta_sum >> self.lta_shift
                sta = self.sta_sum >> self.sta_shift
            
            ratio = 0
            while (sta > lta):
                sta -= lta
                ratio += 1
    
            if ratio == 2:
                print "SMALL EVENT"
                
            if ratio >= 4:
                if self.persistance < self.persistance_threshold :
                    self.persistance += 1
                
                if self.persistance == self.persistance_threshold :
                    self.persistance = 0
                    print "EVENT"
                    
            elif self.persistance > 0:
                self.persistance -= 1
            
            position += 1  
                
            
def read_in_chunks(file_object, chunk_size=1024):
    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data



#path = "D:\SGM Data\Continuous 007\wave\CNT_20120417_151503.raw"
path = "D:\SGM Data\Continuous 008\wave\CNT_20120417_163753.raw"
file_name = open( path, "rb" )


i = 0
chunk = SGM_Chunk()

for piece in read_in_chunks(file_name, chunk.chunk_size):
    chunk.input_chunk(piece) 
    chunk.process_chunk()
    
    i = i + 1  
    if i == 86400*2/60: # 2 days @ 3600 seconds/hour
        break
    if (i%60) == 0: #Every hour
        print "Hour: " + str(i/60)

file_name.close()
# -*- coding: utf-8 -*- Created on Wed Apr 18 12:59:28 2012  @author: SBrink #1 second ~ 2kb -> 1 minute #path = "D:\SGM Data\Continuous 007\wave\CNT_20120417_151503.raw" # 2 days @ 3600 seconds/hour #Every hour