# Path: Modules/read_dat.py
# def loaddata(filename):
# 
# Path: Modules/read_wav.py
# def loaddata(filename):
# 
# Path: Modules/selectfile.py
# class ImportScreen(QtGui.QDialog, SelectScreen.Ui_Dialog):
#     def __init__(self):
#     def initialize(self):
#     def choosefile(self):
#     def importvalues(self):
# def select():
# 
# Path: Modules/SignalData.py
# class Signal(object):
#     def __init__(self, filename=None, filetype=None, filedata=None, Fsample=None, Fcentre=None):
#     def setvalues(self, filename, filetype, Fsample, Fcentre, filedata=None):
#     def setdatavalue(self, filedata):
#     def getvalues(self):
# 
# Path: Modules/importfile.py
# def loadfile(filename):
# 
# Path: Modules/fourier.py
# def CalcFourier(signal):
# def CalcFourierPower(signal, fs, fc):
# def CalcIFourier(signal):
#     N = transform.shape[0]
# 
# Path: Modules/bandpass.py
# def calc_parameter(flow, fhigh, fc, fs):
# def butter_bandpass_filter(data, width, fs):
# def filter(signal, SignalInfo, Flow, Fhigh, chunksize):
# def filter_box(SignalInfo, Flow, Fhigh, chunksize):
# 
# Path: Modules/freqbands.py
# def getbands(SignalInfo, filename):
# 
# Path: Modules/waterfall.py
# def plot_waterfall(SignalInfo, chunksize, number):
# 
# Path: Modules/selectfolder.py
# class ImportScreen(QtGui.QDialog, FolderScreen.Ui_Dialog):
#     def __init__(self):
#     def initialize(self):
#     def choosefile(self):
#     def importvalues(self):
# def select():
# 
# Path: Track/detect.py
# def find_segs(samples, threshold, min_dur, merge_dur, fs, n, signal_filtered):
# def calc_average(signal_filtered, n):
# def re_check(signal, point, threshold, m):
# def calc_threshold(signal):
# 
# Path: Track/aprs.py
# def radar_detect(diff, refLength, threshold_min=1.2, threshold_ratio=10, threshold_max=1.5):
# def cfar(signal_chunk, refLength=10000, guardLength=10, p=0.001):
# def check(SignaIInfo, signal_chunk):
#     N = 2 * refLength
# 
# Path: Track/beacon.py
# def merge(peak, signal_chunk):
# def radar_detect(radar, signal_chunk):
# def cfar(signal_chunk, refLength=1000, guardLength=100, p=0.01):
# def check(SignaIInfo, signal_chunk):
#     N = 2 * refLength
# 
# Path: Track/apt.py
# def stat_check(values, threshold, chunknumber):
# def check(SignaIInfo, signal):


import numpy as np
import os
import time
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join, getmtime
from scipy.fftpack import fft, fftshift
from Modules import read_dat
from Modules import read_wav
from Modules import selectfile
from Modules import SignalData
from Modules import importfile
from Modules import fourier
from Modules import bandpass
from Modules import freqbands
from Modules import waterfall
from Modules import selectfolder
from Track import detect
from Track import aprs
from Track import beacon
from Track import apt
        SignalInfo.filedata = read_dat.loaddata(SignalInfo.filename)
    else:
        SignalInfo.filedata = read_wav.loaddata(SignalInfo.filename)

    peaks, is_peaks = analysis(SignalInfo)
    print(peaks)


def folderwatch():
    foldername = selectfolder.select()

    #laststamp = -1
    while True:
        contents = os.listdir(foldername)
        process = []
        for content in contents:
            if(content.endswith('.dat') or content.endswith('.wav')):
                duplicate = 0
                for content1 in contents:
                    if content + ".png" == content1:
                        # we assume that as soon as the waterfall graph image is there, it was already processed
                        duplicate = 1

                if duplicate == 0:
                    process.append(join(foldername, content))

        for file in process:
            SignalInfo = importfile.loadfile(file)

            if SignalInfo.filetype == ".dat":

# Path: Modules/read_dat.py
# def loaddata(filename):
# 
# Path: Modules/read_wav.py
# def loaddata(filename):
# 
# Path: Modules/selectfile.py
# class ImportScreen(QtGui.QDialog, SelectScreen.Ui_Dialog):
#     def __init__(self):
#     def initialize(self):
#     def choosefile(self):
#     def importvalues(self):
# def select():
# 
# Path: Modules/SignalData.py
# class Signal(object):
#     def __init__(self, filename=None, filetype=None, filedata=None, Fsample=None, Fcentre=None):
#     def setvalues(self, filename, filetype, Fsample, Fcentre, filedata=None):
#     def setdatavalue(self, filedata):
#     def getvalues(self):
# 
# Path: Modules/importfile.py
# def loadfile(filename):
# 
# Path: Modules/fourier.py
# def CalcFourier(signal):
# def CalcFourierPower(signal, fs, fc):
# def CalcIFourier(signal):
#     N = transform.shape[0]
# 
# Path: Modules/bandpass.py
# def calc_parameter(flow, fhigh, fc, fs):
# def butter_bandpass_filter(data, width, fs):
# def filter(signal, SignalInfo, Flow, Fhigh, chunksize):
# def filter_box(SignalInfo, Flow, Fhigh, chunksize):
# 
# Path: Modules/freqbands.py
# def getbands(SignalInfo, filename):
# 
# Path: Modules/waterfall.py
# def plot_waterfall(SignalInfo, chunksize, number):
# 
# Path: Modules/selectfolder.py
# class ImportScreen(QtGui.QDialog, FolderScreen.Ui_Dialog):
#     def __init__(self):
#     def initialize(self):
#     def choosefile(self):
#     def importvalues(self):
# def select():
# 
# Path: Track/detect.py
# def find_segs(samples, threshold, min_dur, merge_dur, fs, n, signal_filtered):
# def calc_average(signal_filtered, n):
# def re_check(signal, point, threshold, m):
# def calc_threshold(signal):
# 
# Path: Track/aprs.py
# def radar_detect(diff, refLength, threshold_min=1.2, threshold_ratio=10, threshold_max=1.5):
# def cfar(signal_chunk, refLength=10000, guardLength=10, p=0.001):
# def check(SignaIInfo, signal_chunk):
#     N = 2 * refLength
# 
# Path: Track/beacon.py
# def merge(peak, signal_chunk):
# def radar_detect(radar, signal_chunk):
# def cfar(signal_chunk, refLength=1000, guardLength=100, p=0.01):
# def check(SignaIInfo, signal_chunk):
#     N = 2 * refLength
# 
# Path: Track/apt.py
# def stat_check(values, threshold, chunknumber):
# def check(SignaIInfo, signal):


# Path: main.py
import numpy as np
import os
import time
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join, getmtime
from scipy.fftpack import fft, fftshift
from Modules import read_dat
from Modules import read_wav
from Modules import selectfile
from Modules import SignalData
from Modules import importfile
from Modules import fourier
from Modules import bandpass
from Modules import freqbands
from Modules import waterfall
from Modules import selectfolder
from Track import detect
from Track import aprs
from Track import beacon
from Track import apt

        SignalInfo.filedata = read_dat.loaddata(SignalInfo.filename)
    else:
        SignalInfo.filedata = read_wav.loaddata(SignalInfo.filename)

    peaks, is_peaks = analysis(SignalInfo)
    print(peaks)


def folderwatch():
    foldername = selectfolder.select()

    #laststamp = -1
    while True:
        contents = os.listdir(foldername)
        process = []
        for content in contents:
            if(content.endswith('.dat') or content.endswith('.wav')):
                duplicate = 0
                for content1 in contents:
                    if content + ".png" == content1:
                        # we assume that as soon as the waterfall graph image is there, it was already processed
                        duplicate = 1

                if duplicate == 0:
                    process.append(join(foldername, content))

        for file in process:
            SignalInfo = importfile.loadfile(file)

            if SignalInfo.filetype == ".dat":

                SignalInfo.filedata = read_dat.loaddata(SignalInfo.filename)