
########################################################################################################################
# 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 os
import numpy as np
import matplotlib.pyplot as plt

BORDERS = [-37, -25, 70, 24]
STEPWIDTH = 1
TRANSMITTER = [67, 20, 5]

#Execute simulation:
transmitterstring = "".join(str(TRANSMITTER).split())  #remove whitespaces
borderstring = "".join(str(BORDERS).split())
print "executing simulation, may take a few minutes the first time"
command = './waf --pyrun "scratch/ralans_example.py --transmitter=' + transmitterstring + ' --borders=' + borderstring + ' --stepwidth=' + str(
    STEPWIDTH) + '" 2>powerlog'
os.system(command)


#The powerlog file now contains the receiver power for all sent packets.
values = []
for l in open("powerlog", "r"):
    try:
        val = l.split("rxPower=")[-1].split("dbm")[0]
        values.append(float(val))
    except:
        #print all other lines, they may contain valuable debugging information
        print l
        pass #automatically skipping all the lines that don't contain an rxPower

#Plot the rxPower values:
xlen = (BORDERS[2] - BORDERS[0]) / STEPWIDTH
ylen = (BORDERS[3] - BORDERS[1]) / STEPWIDTH
values = np.reshape(values, (xlen, ylen))

    
values = np.transpose(values)
plt.imshow(values, vmin=-100, vmax=0, interpolation="nearest", cmap=plt.get_cmap("gnuplot2"),
           origin="lower", extent=np.array(BORDERS)[[0, 2, 1, 3]])
cb = plt.colorbar()
plt.ylabel("y [m]")
plt.xlabel("x [m]")
cb.set_label("received power [dBm]")
plt.show()


