
########################################################################################################################
# Copyright (c) 2015, University of Osnabrueck                                                                         #
#   All rights reserved.                                                                                               #
#                                                                                                                      #
#   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the   #
#   following conditions are met:                                                                                      #
#                                                                                                                      #
#   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following#
#       disclaimer.                                                                                                    #
#                                                                                                                      #
#   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       #
#       following disclaimer in the documentation and/or other materials provided with the distribution.               #
#                                                                                                                      #
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, #
#   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  #
#   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, #
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR    #
#   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  #
#   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE   #
#   USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                           #
########################################################################################################################
import numpy as np
import marshal
import sys
import StringIO

from src.launcher import wrapLauncher as wl



def prepare4ns3(coveragefolder, outfilename, borders, config):

    outfile = open(outfilename, "wb")

    transmitters = config["transmitters"]

    #write Heads
    headt = createTransmitterMeta(config)
    headr = wl.createTypeString(config) + "\n"
    outfile.write(str(headt))
    outfile.write(str(headr))

    for transmitter in transmitters:
        x,y,z = transmitter
        string = "[{:g}, {:g}, {:g}].txt".format(x,y,z)
        filename = coveragefolder + string
        file = open(filename, 'r')
        input = file.read().split("\n")

        result = []

        if config["receiverType"] == 0:
            signal = float(input[0])
            result.append(signal)
            if result == 0:
                result = sys.float_info.min
            result = 10.0 * np.log10(result)
            np.savetxt(outfile, [result])

        else:
            for l in input:
                if len(l.strip()) > 0:
                    tmp = np.genfromtxt(StringIO.StringIO(l), delimiter=" ").tolist()
                    result.append(tmp)

            storeValues(result, outfile)
        print "Prepared result of transmitter:", transmitter
        file.close()

    outfile.close()

def createTransmitterMeta(config):

    type = config["transmitterType"]
    transmitters = config["transmitters"]

    out = str(type)

    if type == 0:
        out += " " + str(transmitters[0][0]) + " " + str(transmitters[0][1]) + " " + str(transmitters[0][2])
    if type == 2 or type == 3:
        head = wl.createTypeString(config)
        #print head
        out += head[1:]
    if type == 1:
        # UGLY HACK, interpret line as list
        out = str(4)
        type = 4
        """
        line = config["transmitters"][0]
        start, end, step = line
        out += " " + str(start[0]) + " " + str(start[1]) + " " + str(start[2]) + " " + str(end[0]) + " " + str(end[1]) + " " + str(end[2]) + " " + str(step)
        """
    if type == 4:
        out += " " + str(len(transmitters))
        for transmitter in transmitters:
            out += " " + str(transmitter[0]) + " " + str(transmitter[1]) + " " + str(transmitter[2])


    out += "\n"
    return out


def storeValues(res, outfile):
    for row in res:
        row = np.array(row).flatten()
        row += (row == 0) * sys.float_info.min
        row = 10.0 * np.log10(row)
        np.savetxt(outfile, [row])


if __name__ == "__main__":
    coveragefolder = "coverage/"
    filename = 'coverage4ns3.txt'
    borders = [-50, -30, 50, 30]
    config = {"stepSize": 2}

    prepare4ns3(coveragefolder, filename, borders, config)


