#!/usr/bin/env/python

import math
import random

class sources:
    def __init__(self):
        pass

    def gen_sine(self, freq, len, type):
        '''freq is frequency in cycles per packet.  len is the length of the packet.  type is the data type to return (e.g., "float", "short", "int")'''

        count = 0
        sine = []
        while count < len:
            sine.append(math.sin(freq*2*math.pi*count/len))
            count = count + 1

        sine = self.convert_data_type(sine, type)

        return sine


    def gen_cosine(self, freq, len, type):
        '''freq is frequency in cycles per packet.  len is the length of the packet.  type is the data type to return (e.g., "float", "short", "int")'''

        count = 0
        cosine = []
        while count < len:
            cosine.append(math.cos(freq*2*math.pi*count/len))
            count = count + 1

        cosine = self.convert_data_type(cosine, type)

        return cosine

    def read_file(self, file_name, delimiter, type):
        #TODO: test this method

        data_from_file = open(file_name, 'r')
        data_from_file = data_from_file.parse(delimiter)

        data_from_file = convert_data_type(data_from_file, type)

        return data_from_file

    def gen_random_data(self, len, rand_type, type):
        '''the rand_type variable defines which function call to use in the random module'''

        data = []
        rand_type = rand_type.lower()  #makes comparison case insensitive
        if rand_type == 'random':
            count = 0
            while count < len:
                data.append(random.random())
                count = count + 1
        elif rand_type == 'uniform':
            count = 0
            while count < len:
                data.append(random.uniform())
                count = count + 1
        else:
            print "random methods other than random and uniform are not supported in sources.random_data"

        data = self.convert_data_type(data, type)

        return data

    def convert_data_type(self, data, type):
        type = type.lower()  #makes comparisons case-insensitive
        
        if type == "short" or type == "int":   #short and int are the same in python
            #TODO: test the robustness of the max() module
            #data_max = max(data)
            data_max = 1
            data = [int(round(x*(32767/data_max))) for x in data]  #conver to integer format
        elif type == "float":
            data = [float(x) for x in data]
        else:
            print "types other than float, short, and int are not yet supported in sources.convert"
        
        return data








