#-------------------------------------------------------------------------------
# Name:        Convert Portable Targets to Intersense Configuration Files
# Purpose:
#
# Author:      Andrew Britton
#
# Created:     12/09/2011
# Copyright:   (c) Andrew 2011
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import sys
import math
import datetime
from time import strftime
import time

##############################################################################
################################################ File and Version Checks #####
##############################################################################
#Check for python version 2.7 to ensure this code will run
def PythonVersionQuery():
    var_version = sys.version_info[:3]
    float_version = float(var_version[0]) + (0.1 * var_version[1])
    if float_version < 2.7:
        s_errorCode = "==========================="
        s_errorCode += "This script was developed using Python 2.7.1.\n"
        s_errorCode += "You're using an earlier version and may need to update."
        s_errorCode += "===========================\n\n"
        s_tmp = raw_input( s_errorCode)




def ChooseFromOneSurveyFile(l_files):
    i_Check = -1
    s_Msg = ""
    i_size = int(len(l_files))
    while(i_Check == -1):
        for k in range(0,50):
            print "\n"
        s_Msg = "Type the number of the File you would like to use from the list below...\n"
        for i in range(0,i_size):
             s_Msg += "(%d): %s\n" % (i, l_files[i])
        val = raw_input( s_Msg)
        if str(val).isdigit():
            i_Check = int(val)
        if not val.isdigit():
            i_Check = -1
        if (i_Check > (i_size-1)):
            i_Check = -1

    return l_files[i_Check]




def LoadSurveyFile():
    l_files =[]
    l_vals = []
    i_FloorTargetSurvey = 0
    l_surveyTXTFile = []
    s_errorCode = ""
    for string_root, list_dirs, list_files in os.walk(".", True):
        del list_dirs[:]

        for string_file in list_files:
            l_vals = os.path.splitext(string_file)
            i_size = len(l_vals)
            if l_vals[i_size-1] == ".txt":
                if not("Floor_Grid_" in string_file):
                    i_FloorTargetSurvey += 1
                    l_surveyTXTFile.append(string_file)
        del list_files

    if(i_FloorTargetSurvey == 0):
        s_errorCode = "Survey File Does Not Exist in this Directory.\nExpected a file of type \"*.txt\""
    elif (i_FloorTargetSurvey >1):
        s_errorCode = ChooseFromOneSurveyFile(l_surveyTXTFile)
    else:
        s_errorCode = l_surveyTXTFile[0]

    return i_FloorTargetSurvey, s_errorCode




def ErrorReporting_WhichBoardsAreSurveyed(l_boardsUsed):
    #return list as [errorCode, String description]
    sizelist = len(l_boardsUsed)
    l_errorCode = [0,"",[]]
    l_boardCheck = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    l_boardTranslate = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"]
    if l_boardsUsed[0] != 3 and l_boardsUsed[1] != 3 and l_boardsUsed[2] != 3 and l_boardsUsed[3] != 3  and l_boardsUsed[4] != 3  and l_boardsUsed[5] != 3  and l_boardsUsed[6] != 3  and l_boardsUsed[7] != 3 and l_boardsUsed[8] != 3 and l_boardsUsed[9] != 3 and l_boardsUsed[10] != 3 and l_boardsUsed[11] != 3  and l_boardsUsed[12] != 3  and l_boardsUsed[13] != 3  and l_boardsUsed[14] != 3  and l_boardsUsed[15] != 3:
        l_errorCode[0] = 0
        l_errorCode[1] = "Not enough survey points or too many survey points taken per board.\nEach board needs exactly 3, and only 3, survey points to solve properly.\nCheck your survey data and try again."
        return l_errorCode

    #print l_boardsUsed
    #print len(l_boardsUsed)
    #print len(l_boardTranslate)
    for i in range(0, len(l_boardsUsed)):
        #print "This is the index %d and value at that index in l_usedBoards is %d\n" % (i, l_boardsUsed[i])
        if l_boardsUsed[i] is 3:
            l_boardCheck[i] = 1
            l_errorCode[1] += "Floor_Grid_%s: is being used in this survey\n" % (l_boardTranslate[i])
        else:
            l_errorCode[1] += "Floor_Grid_%s: WILL NOT be used in this survey. There are %d unique surveyed points for this board.\n" % (l_boardTranslate[i], l_boardsUsed[i])
        l_errorCode[0] = 1
        l_errorCode[2] = l_boardCheck
    return l_errorCode






def DefWhichBoardsAreSurveyed(s_surveyFile, l_pointsA, l_pointsB, l_pointsC, l_pointsD, l_pointsE, l_pointsF, l_pointsG, l_pointsH, l_pointsI, l_pointsJ, l_pointsK, l_pointsL, l_pointsM, l_pointsN, l_pointsO, l_pointsP, l_GM):
    #print s_surveyFile
    i_boardA = i_boardB = i_boardC = i_boardD = i_boardE = i_boardF = i_boardG = i_boardH = i_boardI = i_boardJ = i_boardK = i_boardL = i_boardM = i_boardN = i_boardO = i_boardP = 0
    l_A = [0,0,0]
    l_B = [0,0,0]
    l_C = [0,0,0]
    l_D = [0,0,0]
    l_E = [0,0,0]
    l_F = [0,0,0]
    l_G = [0,0,0]
    l_H = [0,0,0]
    l_I = [0,0,0]
    l_J = [0,0,0]
    l_K = [0,0,0]
    l_L = [0,0,0]
    l_M = [0,0,0]
    l_N = [0,0,0]
    l_O = [0,0,0]
    l_P = [0,0,0]
    l_tmp2 = [0,0,0,""]
    l_tmp = []
    l_GMtmp = []
    i_count = 0
    f_survey = open(s_surveyFile)
    l_Lines = f_survey.readlines()
    f_survey.close
    for i in range (0,len(l_Lines)):
        l_currentLine = l_Lines[i].split(',')
        l_tmp = l_currentLine[3].split()
        #print l_tmp
        #print l_tmp[3]
        #Counts unique instances of each of the 3 points required for the proper survey orientation
        if(l_tmp[0] == "A1" or l_tmp[0] == "100"):
            if l_A[0] is 0:
                i_boardA += 1
                l_A[0] = 1
                l_pointsA[0] = float(l_currentLine[0])
                l_pointsA[1] = float(l_currentLine[1]) #[2] and [1] are flipped to set elevation in the index[2] and easting in index[1]
                l_pointsA[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "A2" or l_tmp[0] == "700"):
            if l_A[1] is 0:
                i_boardA += 1
                l_A[1] = 1
                l_pointsA[3] = float(l_currentLine[0])
                l_pointsA[4] = float(l_currentLine[1])
                l_pointsA[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "A3" or l_tmp[0] == "900"):
            if l_A[2] is 0:
                i_boardA += 1
                l_A[2] = 1
                l_pointsA[6] = float(l_currentLine[0])
                l_pointsA[7] = float(l_currentLine[1])
                l_pointsA[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "B1" or l_tmp[0] == "1000"):
            if l_B[0] is 0:
                i_boardB += 1
                l_B[0] = 1
                l_pointsB[0] = float(l_currentLine[0])
                l_pointsB[1] = float(l_currentLine[1])
                l_pointsB[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "B2" or l_tmp[0] == "1600"):
            if l_B[1] is 0:
                i_boardB += 1
                l_B[1] = 1
                l_pointsB[3] = float(l_currentLine[0])
                l_pointsB[4] = float(l_currentLine[1])
                l_pointsB[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "B3" or l_tmp[0] == "1800"):
            if l_B[2] is 0:
                i_boardB += 1
                l_B[2] = 1
                l_pointsB[6] = float(l_currentLine[0])
                l_pointsB[7] = float(l_currentLine[1])
                l_pointsB[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "C1" or l_tmp[0] == "1900"):
            if l_C[0] is 0:
                i_boardC += 1
                l_C[0] = 1
                l_pointsC[0] = float(l_currentLine[0])
                l_pointsC[1] = float(l_currentLine[1])
                l_pointsC[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "C2" or l_tmp[0] == "2500"):
            if l_C[1] is 0:
                i_boardC += 1
                l_C[1] = 1
                l_pointsC[3] = float(l_currentLine[0])
                l_pointsC[4] = float(l_currentLine[1])
                l_pointsC[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "C3" or l_tmp[0] == "2700"):
            if l_C[2] is 0:
                i_boardC += 1
                l_C[2] = 1
                l_pointsC[6] = float(l_currentLine[0])
                l_pointsC[7] = float(l_currentLine[1])
                l_pointsC[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "D1" or l_tmp[0] == "2800"):
            if l_D[0] is 0:
                i_boardD += 1
                l_D[0] = 1
                l_pointsD[0] = float(l_currentLine[0])
                l_pointsD[1] = float(l_currentLine[1])
                l_pointsD[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "D2" or l_tmp[0] == "3400"):
            if l_D[1] is 0:
                i_boardD += 1
                l_D[1] = 1
                l_pointsD[3] = float(l_currentLine[0])
                l_pointsD[4] = float(l_currentLine[1])
                l_pointsD[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "D3" or l_tmp[0] == "3600"):
            if l_D[2] is 0:
                i_boardD += 1
                l_D[2] = 1
                l_pointsD[6] = float(l_currentLine[0])
                l_pointsD[7] = float(l_currentLine[1])
                l_pointsD[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "E1" or l_tmp[0] == "3700"):
            if l_E[0] is 0:
                i_boardE += 1
                l_E[0] = 1
                l_pointsE[0] = float(l_currentLine[0])
                l_pointsE[1] = float(l_currentLine[1])
                l_pointsE[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "E2" or l_tmp[0] == "4300"):
            if l_E[1] is 0:
                i_boardE += 1
                l_E[1] = 1
                l_pointsE[3] = float(l_currentLine[0])
                l_pointsE[4] = float(l_currentLine[1])
                l_pointsE[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "E3" or l_tmp[0] == "4500"):
            if l_E[2] is 0:
                i_boardE += 1
                l_E[2] = 1
                l_pointsE[6] = float(l_currentLine[0])
                l_pointsE[7] = float(l_currentLine[1])
                l_pointsE[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "F1" or l_tmp[0] == "4600"):
            if l_F[0] is 0:
                i_boardF += 1
                l_F[0] = 1
                l_pointsF[0] = float(l_currentLine[0])
                l_pointsF[1] = float(l_currentLine[1])
                l_pointsF[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "F2" or l_tmp[0] == "5200"):
            if l_F[1] is 0:
                i_boardF += 1
                l_F[1] = 1
                l_pointsF[3] = float(l_currentLine[0])
                l_pointsF[4] = float(l_currentLine[1])
                l_pointsF[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "F3" or l_tmp[0] == "5400"):
            if l_F[2] is 0:
                i_boardF += 1
                l_F[2] = 1
                l_pointsF[6] = float(l_currentLine[0])
                l_pointsF[7] = float(l_currentLine[1])
                l_pointsF[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "G1" or l_tmp[0] == "5500"):
            if l_G[0] is 0:
                i_boardG += 1
                l_G[0] = 1
                l_pointsG[0] = float(l_currentLine[0])
                l_pointsG[1] = float(l_currentLine[1])
                l_pointsG[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "G2" or l_tmp[0] == "6100"):
            if l_G[1] is 0:
                i_boardG += 1
                l_G[1] = 1
                l_pointsG[3] = float(l_currentLine[0])
                l_pointsG[4] = float(l_currentLine[1])
                l_pointsG[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "G3" or l_tmp[0] == "6300"):
            if l_G[2] is 0:
                i_boardG += 1
                l_G[2] = 1
                l_pointsG[6] = float(l_currentLine[0])
                l_pointsG[7] = float(l_currentLine[1])
                l_pointsG[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "H1" or l_tmp[0] == "6400"):
            if l_H[0] is 0:
                i_boardH += 1
                l_H[0] = 1
                l_pointsH[0] = float(l_currentLine[0])
                l_pointsH[1] = float(l_currentLine[1])
                l_pointsH[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "H2" or l_tmp[0] == "7000"):
            if l_H[1] is 0:
                i_boardH += 1
                l_H[1] = 1
                l_pointsH[3] = float(l_currentLine[0])
                l_pointsH[4] = float(l_currentLine[1])
                l_pointsH[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "H3" or l_tmp[0] == "7200"):
            if l_H[2] is 0:
                i_boardH += 1
                l_H[2] = 1
                l_pointsH[6] = float(l_currentLine[0])
                l_pointsH[7] = float(l_currentLine[1])
                l_pointsH[8] = float(l_currentLine[2])
##Boards I - Q

        elif(l_tmp[0] == "I1" or l_tmp[0] == "7300"):
            if l_I[0] is 0:
                i_boardI += 1
                l_I[0] = 1
                l_pointsI[0] = float(l_currentLine[0])
                l_pointsI[1] = float(l_currentLine[1]) #[2] and [1] are flipped to set elevation in the index[2] and easting in index[1]
                l_pointsI[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "I2" or l_tmp[0] == "7900"):
            if l_I[1] is 0:
                i_boardI += 1
                l_I[1] = 1
                l_pointsI[3] = float(l_currentLine[0])
                l_pointsI[4] = float(l_currentLine[1])
                l_pointsI[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "I3" or l_tmp[0] == "8100"):
            if l_I[2] is 0:
                i_boardI += 1
                l_I[2] = 1
                l_pointsI[6] = float(l_currentLine[0])
                l_pointsI[7] = float(l_currentLine[1])
                l_pointsI[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "J1" or l_tmp[0] == "8200"):
            if l_J[0] is 0:
                i_boardJ += 1
                l_J[0] = 1
                l_pointsJ[0] = float(l_currentLine[0])
                l_pointsJ[1] = float(l_currentLine[1])
                l_pointsJ[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "J2" or l_tmp[0] == "8800"):
            if l_J[1] is 0:
                i_boardJ += 1
                l_J[1] = 1
                l_pointsJ[3] = float(l_currentLine[0])
                l_pointsJ[4] = float(l_currentLine[1])
                l_pointsJ[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "J3" or l_tmp[0] == "9000"):
            if l_J[2] is 0:
                i_boardJ += 1
                l_J[2] = 1
                l_pointsJ[6] = float(l_currentLine[0])
                l_pointsJ[7] = float(l_currentLine[1])
                l_pointsJ[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "K1" or l_tmp[0] == "9100"):
            if l_K[0] is 0:
                i_boardK += 1
                l_K[0] = 1
                l_pointsK[0] = float(l_currentLine[0])
                l_pointsK[1] = float(l_currentLine[1])
                l_pointsK[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "K2" or l_tmp[0] == "9700"):
            if l_K[1] is 0:
                i_boardK += 1
                l_K[1] = 1
                l_pointsK[3] = float(l_currentLine[0])
                l_pointsK[4] = float(l_currentLine[1])
                l_pointsK[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "K3" or l_tmp[0] == "9900"):
            if l_K[2] is 0:
                i_boardK += 1
                l_K[2] = 1
                l_pointsK[6] = float(l_currentLine[0])
                l_pointsK[7] = float(l_currentLine[1])
                l_pointsK[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "L1" or l_tmp[0] == "10000"):
            if l_L[0] is 0:
                i_boardL += 1
                l_L[0] = 1
                l_pointsL[0] = float(l_currentLine[0])
                l_pointsL[1] = float(l_currentLine[1])
                l_pointsL[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "L2" or l_tmp[0] == "10600"):
            if l_L[1] is 0:
                i_boardL += 1
                l_L[1] = 1
                l_pointsL[3] = float(l_currentLine[0])
                l_pointsL[4] = float(l_currentLine[1])
                l_pointsL[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "L3" or l_tmp[0] == "10800"):
            if l_L[2] is 0:
                i_boardL += 1
                l_L[2] = 1
                l_pointsL[6] = float(l_currentLine[0])
                l_pointsL[7] = float(l_currentLine[1])
                l_pointsL[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "M1" or l_tmp[0] == "10900"):
            if l_M[0] is 0:
                i_boardM += 1
                l_M[0] = 1
                l_pointsM[0] = float(l_currentLine[0])
                l_pointsM[1] = float(l_currentLine[1])
                l_pointsM[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "M2" or l_tmp[0] == "11500"):
            if l_M[1] is 0:
                i_boardM += 1
                l_M[1] = 1
                l_pointsM[3] = float(l_currentLine[0])
                l_pointsM[4] = float(l_currentLine[1])
                l_pointsM[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "M3" or l_tmp[0] == "11700"):
            if l_M[2] is 0:
                i_boardM += 1
                l_M[2] = 1
                l_pointsM[6] = float(l_currentLine[0])
                l_pointsM[7] = float(l_currentLine[1])
                l_pointsM[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "N1" or l_tmp[0] == "11800"):
            if l_N[0] is 0:
                i_boardN += 1
                l_N[0] = 1
                l_pointsN[0] = float(l_currentLine[0])
                l_pointsN[1] = float(l_currentLine[1])
                l_pointsN[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "N2" or l_tmp[0] == "12400"):
            if l_N[1] is 0:
                i_boardN += 1
                l_N[1] = 1
                l_pointsN[3] = float(l_currentLine[0])
                l_pointsN[4] = float(l_currentLine[1])
                l_pointsN[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "N3" or l_tmp[0] == "12600"):
            if l_N[2] is 0:
                i_boardN += 1
                l_N[2] = 1
                l_pointsN[6] = float(l_currentLine[0])
                l_pointsN[7] = float(l_currentLine[1])
                l_pointsN[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "O1" or l_tmp[0] == "12700"):
            if l_O[0] is 0:
                i_boardO += 1
                l_O[0] = 1
                l_pointsO[0] = float(l_currentLine[0])
                l_pointsO[1] = float(l_currentLine[1])
                l_pointsO[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "O2" or l_tmp[0] == "13300"):
            if l_O[1] is 0:
                i_boardO += 1
                l_O[1] = 1
                l_pointsO[3] = float(l_currentLine[0])
                l_pointsO[4] = float(l_currentLine[1])
                l_pointsO[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "O3" or l_tmp[0] == "13500"):
            if l_O[2] is 0:
                i_boardO += 1
                l_O[2] = 1
                l_pointsO[6] = float(l_currentLine[0])
                l_pointsO[7] = float(l_currentLine[1])
                l_pointsO[8] = float(l_currentLine[2])

        elif(l_tmp[0] == "P1" or l_tmp[0] == "13600"):
            if l_P[0] is 0:
                i_boardP += 1
                l_P[0] = 1
                l_pointsP[0] = float(l_currentLine[0])
                l_pointsP[1] = float(l_currentLine[1])
                l_pointsP[2] = float(l_currentLine[2])
        elif(l_tmp[0] == "P2" or l_tmp[0] == "14200"):
            if l_P[1] is 0:
                i_boardP += 1
                l_P[1] = 1
                l_pointsP[3] = float(l_currentLine[0])
                l_pointsP[4] = float(l_currentLine[1])
                l_pointsP[5] = float(l_currentLine[2])
        elif(l_tmp[0] == "P3" or l_tmp[0] == "14400"):
            if l_P[2] is 0:
                i_boardP += 1
                l_P[2] = 1
                l_pointsP[6] = float(l_currentLine[0])
                l_pointsP[7] = float(l_currentLine[1])
                l_pointsP[8] = float(l_currentLine[2])
        else:
            print "GM Point!"
            l_GMtmp.append([0,0,0,0])
            l_GMtmp[i_count][0] = l_tmp[0]
            l_GMtmp[i_count][1] = float(l_currentLine[0])
            l_GMtmp[i_count][2] = float(l_currentLine[1])
            l_GMtmp[i_count][3] = float(l_currentLine[2])
            i_count += 1
            print l_GMtmp


    if i_boardA is 3:
        print "You're using Floor Grid A."
    if i_boardB is 3:
        print "You're using Floor Grid B."
    if i_boardC is 3:
        print "You're using Floor Grid C."
    if i_boardD is 3:
        print "You're using Floor Grid D."
    if i_boardE is 3:
        print "You're using Floor Grid E."
    if i_boardF is 3:
        print "You're using Floor Grid F."
    if i_boardG is 3:
        print "You're using Floor Grid G."
    if i_boardH is 3:
        print "You're using Floor Grid H."
    if i_boardI is 3:
        print "You're using Floor Grid I."
    if i_boardJ is 3:
        print "You're using Floor Grid J."
    if i_boardK is 3:
        print "You're using Floor Grid K."
    if i_boardL is 3:
        print "You're using Floor Grid L."
    if i_boardM is 3:
        print "You're using Floor Grid M."
    if i_boardN is 3:
        print "You're using Floor Grid N."
    if i_boardO is 3:
        print "You're using Floor Grid O."
    if i_boardP is 3:
        print "You're using Floor Grid P."
    if len(l_GMtmp) > 0:
        print "You're using Additional Survey Points."
        l_GM.append(l_GMtmp)

    return i_boardA, i_boardB, i_boardC, i_boardD, i_boardE, i_boardF, i_boardG, i_boardH, i_boardI, i_boardJ, i_boardK, i_boardL, i_boardM, i_boardN, i_boardO, i_boardP




def NecessaryFileCheckCalibrationBoards():
    l_files =[]
    l_vals = []
    i_checkBoard1 = 0
    i_checkBoard2 = 0
    i_checkBoard3 = 0
    i_checkBoard4 = 0
    i_checkBoard5 = 0
    i_checkBoard6 = 0
    i_checkBoard7 = 0
    i_checkBoard8 = 0
    i_checkBoard9 = 0
    i_checkBoard10 = 0
    i_checkBoard11 = 0
    i_checkBoard12 = 0
    i_checkBoard13 = 0
    i_checkBoard14 = 0
    i_checkBoard15 = 0
    i_checkBoard16 = 0
    for string_root, list_dirs, list_files in os.walk(".", True):
        del list_dirs[:]

        for string_file in list_files:
            l_vals = os.path.splitext(string_file)
            if string_file == "Floor_Grid_A.txt":
                i_checkBoard1 += 1
            if string_file == "Floor_Grid_B.txt":
                i_checkBoard2 += 1
            if string_file == "Floor_Grid_C.txt":
                i_checkBoard3 += 1
            if string_file == "Floor_Grid_D.txt":
                i_checkBoard4 += 1
            if string_file == "Floor_Grid_E.txt":
                i_checkBoard5 += 1
            if string_file == "Floor_Grid_F.txt":
                i_checkBoard6 += 1
            if string_file == "Floor_Grid_G.txt":
                i_checkBoard7 += 1
            if string_file == "Floor_Grid_H.txt":
                i_checkBoard8 += 1
            if string_file == "Floor_Grid_I.txt":
                i_checkBoard9 += 1
            if string_file == "Floor_Grid_J.txt":
                i_checkBoard10 += 1
            if string_file == "Floor_Grid_K.txt":
                i_checkBoard11 += 1
            if string_file == "Floor_Grid_L.txt":
                i_checkBoard12 += 1
            if string_file == "Floor_Grid_M.txt":
                i_checkBoard13 += 1
            if string_file == "Floor_Grid_N.txt":
                i_checkBoard14 += 1
            if string_file == "Floor_Grid_O.txt":
                i_checkBoard15 += 1
            if string_file == "Floor_Grid_P.txt":
                i_checkBoard16 += 1

        del list_files
    return i_checkBoard1, i_checkBoard2, i_checkBoard3, i_checkBoard4, i_checkBoard5, i_checkBoard6, i_checkBoard7, i_checkBoard8, i_checkBoard9, i_checkBoard10, i_checkBoard11, i_checkBoard12, i_checkBoard13, i_checkBoard14, i_checkBoard15, i_checkBoard16




def CreateErrorReportCalibrationBoardsFiles(l_files, l_boardsUsed):
    l_errorCode = [1,""]
    l_boardLetters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"]
    for aa in range(16):
        if l_files[aa] is 0 and l_boardsUsed[aa] is 1:
            if l_errorCode[0] is 1:
                l_errorCode[1] += "::::TERMINAL EXECUTION ERROR::::\n\n"
            l_errorCode[1] += "Missing:: \"Floor_Grid_%s.txt.\" Please find and relocate to current directory.\n\n" % (l_boardLetters[aa])
            l_errorCode[0] = 0

    if l_errorCode[0] is 0:
        l_errorCode[1] += "...Press enter to continue..."
        s_tmp = raw_input( l_errorCode[1])
    return l_errorCode

##############################################################################
################################################### Function Definitions #####
##############################################################################


########################################################
########################### Vector and Matrix Math #####
########################################################
def ToXYZfromNorthEastElev(l_points):
    for i in range(0,len(l_points)):
        l_p1 = l_points[i][0:3]
        l_p2 = l_points[i][3:6]
        l_p3 = l_points[i][6:9]
        l_points[i][0:3] = [l_p1[0], l_p1[2], l_p1[1]]
        l_points[i][3:6] = [l_p2[0], l_p2[2], l_p2[1]]
        l_points[i][6:9] = [l_p3[0], l_p3[2], l_p3[1]]
        #return l_points
    return l_points




def ToNorthEastNegativeElevfromXYZ(l_data):
    l_p1 = [0,0,0]
    for i in range(0,len(l_data)):
        #Start in XYZ
        l_p1[0] = l_data[i][3]
        l_p1[1] = l_data[i][7]
        l_p1[2] = l_data[i][11]

        #Covnert to North(X), East(Z), -Elev(Y)
        l_data[i][3] = l_p1[0]
        l_data[i][7] = l_p1[2]
        l_data[i][11] = l_p1[1] * -1
    return l_data




def crossProd(l_a, l_b):
    l_cross = [0, 0, 0]
    l_cross[0] = l_a[1]*l_b[2] - l_a[2]*l_b[1]
    l_cross[1] = l_a[2]*l_b[0] - l_a[0]*l_b[2]
    l_cross[2] = l_a[0]*l_b[1] - l_a[1]*l_b[0]

    return l_cross




def normalize(l_v):
    f_mag = pow((l_v[0]*l_v[0] + l_v[1]*l_v[1] + l_v[2]*l_v[2]), 0.5)
    if f_mag != 0:
        return [l_v[0]/f_mag, l_v[1]/f_mag, l_v[2]/f_mag]
    else:
        return l_v




def multMTX(list_leftMTx, list_rightMTX):
    c = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
    a = list_leftMTx
    b = list_rightMTX
    for r in range(0,16,4):
        for i in range(r,r+4):
            c[i] = a[r]*b[i-r] + a[r+1]*b[i+4-r] + a[r+2]*b[i+8-r] + a[r+3]*b[i+12-r]
    return c



def GetRotationMatrixIntersense(l_a, l_b, l_c):
    l_ab = [l_b[0] - l_a[0], l_b[1] - l_a[1], l_b[2] - l_a[2]]
    l_ac = [l_c[0] - l_a[0], l_c[1] - l_a[1], l_c[2] - l_a[2]]
    l_x = normalize(crossProd(l_ab, l_ac))
    l_y = normalize(l_ab)
    l_z = normalize(crossProd(l_x, l_y))

    return [l_x[0], l_y[0], l_z[0], l_x[1], l_y[1], l_z[1], l_x[2], l_y[2], l_z[2]]




def transformPreIntersense(l_P1, l_P2, l_P3, l_dataPoint):
    l_P1minusP2 = [l_P1[0] - l_P2[0], l_P1[1] - l_P2[1], l_P1[2] - l_P2[2]]
    l_P3minusP2 = [l_P3[0] - l_P2[0], l_P3[1] - l_P2[1], l_P3[2] - l_P2[2]]
    l_P1P2crossP3P2 = crossProd(l_P1minusP2, l_P3minusP2)

    l_x = normalize(l_P1minusP2)
    l_z = normalize(l_P3minusP2)
    l_y = normalize(l_P1P2crossP3P2)
    l_y[0] *= -1.0
    l_y[1] *= -1.0
    l_y[2] *= -1.0
    l_z = normalize(crossProd(l_x, l_y))

    l_surveyMtx = [l_x[0], l_y[0], l_z[0], l_P2[0], l_x[1], l_y[1], l_z[1], l_P2[1], l_x[2], l_y[2], l_z[2], l_P2[2], 0.0, 0.0, 0.0, 1.0 ]
    l_baseMtx = l_dataPoint[0:16]
    return multMTX(l_surveyMtx, l_baseMtx)




########################################################
######################################### File I/O #####
########################################################
def WriteCFGFile(l_data, s_filename):
    now = datetime.datetime.now()
    s_body = ""
    s_header = "# File Rev. 3\n"
    s_header += "# Created: %s \n" % (strftime("%a, %d %b %Y %H:%M:%S"))
    s_header += "#\tdrvN\tdevN\tbtype\tstype\tmember\tsNum\tAtrib[0]\tAtrib[1]\tAtrib[2]\tAtrib[3]\tAtrib[4]\tAtrib[5]\tAtrib[6]\tAtrib[7]\tpos[0]\t\tpos[1]\t\tpos[2]\t\tpUnc[0]\tpUnc[1]\tpUnc[2]\tr[0][0]\t\tr[0][1]\t\tr[0][2]\t\tr[1][0]\t\tr[1][1]\t\tr[1][2]\t\tr[2][0]\t\tr[2][1]\t\tr[2][2]\t\trUnc[0]\trUnc[1]\trUnc[2]\tkPar[0]\tkPar[1]\tkPar[2]\tkPar[3]\tkPar[4]\tkPar[5]\tkPar[6]\tkPar[7]\tkPar[8]\n"
    for l_item in l_data:
        s_body += "\t0\t%d\t4\t14010\t0\t%d\t?\t\t%f" % (l_item[17],l_item[17],l_item[16])
        s_body += "\t?\t"*6
        s_body += "\t%f\t%f\t%f" % (l_item[3], l_item[7], l_item[11])
        s_body += "\t?"*3
        s_body += "\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f" % (l_item[0], l_item[1], l_item[2], l_item[4], l_item[5], l_item[6], l_item[8], l_item[9], l_item[10])
        s_body += "\t?"*12
        s_body += "\n"
    f = open(s_filename+".cfg", 'w')
    f.write(s_header+s_body)
    f.close




def WriteAdditionalSurveyPoints(l_data, s_filename):
    s_outData = ""

    s_outData = "# Lightcraft Additional Survey Points file\n"
    s_outData += "# Created: %s \n" % (strftime("%a, %d %b %Y %H:%M:%S"))
    s_outData += "# ID\tX\t\tY\t\tZ\n"
    for l in l_data:
        s_outData += "%s\t%f\t%f\t%f\n" % (l[0], l[1], l[2], l[3])
    f_out = open(s_filename+"_AdditionalSurveyPoints.txt",'w')
    f_out.write(s_outData)
    f_out.close()




########################################################
################################# Custom Functions #####
########################################################
def FindNewTransform(l_surveyPoints, l_dataPoint):
    l_newMtx = transformPreIntersense(l_surveyPoints[0:3], l_surveyPoints[3:6], l_surveyPoints[6:9], l_dataPoint)
    l_dataPoint[0:16] = l_newMtx #indices are listed as [northing, elevation, easting] but will need to be swapped to standard surveying [northing, easting, elevation] before being calculated for the final intersense constellation configuration
    #print l_dataPoint




def TransformConstellationsFilesBySurveyOrientation(l_activeTargets, l_surveyData):
    #A description of the data in l_startData
        #The first dimension of the array equals the number of targets per board times the number of boards being used. So, a min lenth of this array is 21 and a max lenth is 84, with size in between defined in increments of 21
        #The 2nd dimension in this array is always 18 indices long and breaks down as follows: [0->15] are linear 4x4 matrix, [16] is the fiducial width in meters, [17] is the unique fiducial ID

    #for Each Active Target transform fiducial targets
    f_scaleConversion = 1.0
    l_startData = []
    l_fileNames = ["Floor_Grid_A.txt", "Floor_Grid_B.txt", "Floor_Grid_C.txt", "Floor_Grid_D.txt", "Floor_Grid_E.txt", "Floor_Grid_F.txt", "Floor_Grid_G.txt", "Floor_Grid_H.txt", "Floor_Grid_I.txt", "Floor_Grid_J.txt", "Floor_Grid_K.txt", "Floor_Grid_L.txt", "Floor_Grid_M.txt", "Floor_Grid_N.txt", "Floor_Grid_O.txt", "Floor_Grid_P.txt"]
    i_counter = 0

    for cc in range(16):
        if l_activeTargets[cc] is 1:
            f_survey = open(l_fileNames[cc])
            l_Lines = f_survey.readlines()
            f_survey.close()
            for i in range(2, len(l_Lines)):
                l_tmp = l_Lines[i].split()
                l_startData.append([0]*18)
                l_startData[i_counter][16] = float(l_tmp[3]) * f_scaleConversion
                l_startData[i_counter][17] = int(l_tmp[0])
                l_startData[i_counter][0] = l_startData[i_counter][5] = l_startData[i_counter][10] = l_startData[i_counter][15] = float(1.0)
                l_startData[i_counter][3] = float(l_tmp[2]) #set Y (of 2d space) to X (of 3D space)
                l_startData[i_counter][11] = float(l_tmp[1]) #set X (of 2d space) to Z (of 3D space)
                FindNewTransform(l_surveyData[cc], l_startData[i_counter])
                i_counter +=1

    return l_startData




def PutIntersenseRotMtxIntoCurrentData(l_mtx, l_point):
    #l_mtx comes in with [9] indices
    #l_point comes in with [18] indices. The first 16 are a linear form of 4x4 matrix, column major
    l_point[0] = l_mtx[0]
    l_point[1] = l_mtx[1]
    l_point[2] = l_mtx[2]

    l_point[4] = l_mtx[3]
    l_point[5] = l_mtx[4]
    l_point[6] = l_mtx[5]

    l_point[8] = l_mtx[6]
    l_point[9] = l_mtx[7]
    l_point[10] = l_mtx[8]




def ConvertToIntersense(l_activeBoards, l_allData):
    #all target data in l_allData is segmented into segments of 21 points per board. This means the smallest this list will be is 21 and the largest it will be is 84 (based on the existence of four boards)
    i_P1A = i_P2A = iP3A = i_P1B = i_P2B = iP3B = i_P1C = i_P2C = iP3C = i_P1D = i_P2D = iP3D = i_tmp = 0
    i_P1E = i_P2E = iP3E = i_P1F = i_P2F = iP3F = i_P1G = i_P2G = iP3G = i_P1H = i_P2H = iP3H = 0
    i_P1I = i_P2I = iP3I = i_P1J = i_P2J = iP3J = i_P1K = i_P2K = iP3K = i_P1L = i_P2L = iP3L = 0
    i_P1M = i_P2M = iP3M = i_P1N = i_P2N = iP3N = i_P1O = i_P2O = iP3O = i_P1P = i_P2P = iP3P = 0
    l_mtxA = []
    l_mtxB = []
    l_mtxC = []
    l_mtxD = []
    l_mtxE = []
    l_mtxF = []
    l_mtxG = []
    l_mtxH = []
    l_mtxI = []
    l_mtxJ = []
    l_mtxK = []
    l_mtxL = []
    l_mtxM = []
    l_mtxN = []
    l_mtxO = []
    l_mtxP = []
    l_p1 = [0,0,0]
    l_p2 = [0,0,0]
    l_p3 = [0,0,0]

            #find corner targets
    for i in range(0, len(l_allData)):
        i_tmp = l_allData[i][17]
        if i_tmp == 100:
            i_P1A = i
        if i_tmp == 700:
            i_P2A = i
        if i_tmp == 900:
            i_P3A = i
        if i_tmp == 1000:
            i_P1B = i
        if i_tmp == 1600:
            i_P2B = i
        if i_tmp == 1800:
            i_P3B = i
        if i_tmp == 1900:
            i_P1C = i
        if i_tmp == 2500:
            i_P2C = i
        if i_tmp == 2700:
            i_P3C = i
        if i_tmp == 2800:
            i_P1D = i
        if i_tmp == 3400:
            i_P2D = i
        if i_tmp == 3600:
            i_P3D = i
        if i_tmp == 3700:
            i_P1E = i
        if i_tmp == 4300:
            i_P2E = i
        if i_tmp == 4500:
            i_P3E = i
        if i_tmp == 4600:
            i_P1F = i
        if i_tmp == 5200:
            i_P2F = i
        if i_tmp == 5400:
            i_P3F = i
        if i_tmp == 5500:
            i_P1G = i
        if i_tmp == 6100:
            i_P2G = i
        if i_tmp == 6300:
            i_P3G = i
        if i_tmp == 6400:
            i_P1H = i
        if i_tmp == 7000:
            i_P2H = i
        if i_tmp == 7200:
            i_P3H = i
        if i_tmp == 7300:
            i_P1I = i
        if i_tmp == 7900:
            i_P2I = i
        if i_tmp == 8100:
            i_P3I = i
        if i_tmp == 8200:
            i_P1J = i
        if i_tmp == 8800:
            i_P2J = i
        if i_tmp == 9000:
            i_P3J = i
        if i_tmp == 9100:
            i_P1K = i
        if i_tmp == 9700:
            i_P2K = i
        if i_tmp == 9900:
            i_P3K = i
        if i_tmp == 10000:
            i_P1L = i
        if i_tmp == 10600:
            i_P2L = i
        if i_tmp == 10800:
            i_P3L = i
        if i_tmp == 10900:
            i_P1M = i
        if i_tmp == 11500:
            i_P2M = i
        if i_tmp == 11700:
            i_P3M = i
        if i_tmp == 11800:
            i_P1N = i
        if i_tmp == 12400:
            i_P2N = i
        if i_tmp == 12600:
            i_P3N = i
        if i_tmp == 12700:
            i_P1O = i
        if i_tmp == 13300:
            i_P2O = i
        if i_tmp == 13500:
            i_P3O = i
        if i_tmp == 13600:
            i_P1P = i
        if i_tmp == 14200:
            i_P2P = i
        if i_tmp == 14400:
            i_P3P = i

            #send three points to get rotation matrix
            #apply rotation matrix to all points in this board
    if l_activeBoards[0] is 1:
        l_p1[0] = l_allData[i_P1A][3]
        l_p1[1] = l_allData[i_P1A][7]
        l_p1[2] = l_allData[i_P1A][11]

        l_p2[0] = l_allData[i_P2A][3]
        l_p2[1] = l_allData[i_P2A][7]
        l_p2[2] = l_allData[i_P2A][11]

        l_p3[0] = l_allData[i_P3A][3]
        l_p3[1] = l_allData[i_P3A][7]
        l_p3[2] = l_allData[i_P3A][11]
        l_mtxA = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1A, i_P1A+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxA, l_allData[j])

    if l_activeBoards[1] is 1:
        l_p1[0] = l_allData[i_P1B][3]
        l_p1[1] = l_allData[i_P1B][7]
        l_p1[2] = l_allData[i_P1B][11]

        l_p2[0] = l_allData[i_P2B][3]
        l_p2[1] = l_allData[i_P2B][7]
        l_p2[2] = l_allData[i_P2B][11]

        l_p3[0] = l_allData[i_P3B][3]
        l_p3[1] = l_allData[i_P3B][7]
        l_p3[2] = l_allData[i_P3B][11]
        l_mtxB = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1B, i_P1B+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxB, l_allData[j])

    if l_activeBoards[2] is 1:
        l_p1[0] = l_allData[i_P1C][3]
        l_p1[1] = l_allData[i_P1C][7]
        l_p1[2] = l_allData[i_P1C][11]

        l_p2[0] = l_allData[i_P2C][3]
        l_p2[1] = l_allData[i_P2C][7]
        l_p2[2] = l_allData[i_P2C][11]

        l_p3[0] = l_allData[i_P3C][3]
        l_p3[1] = l_allData[i_P3C][7]
        l_p3[2] = l_allData[i_P3C][11]
        l_mtxC = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1C, i_P1C+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxC, l_allData[j])

    if l_activeBoards[3] is 1:
        l_p1[0] = l_allData[i_P1D][3]
        l_p1[1] = l_allData[i_P1D][7]
        l_p1[2] = l_allData[i_P1D][11]

        l_p2[0] = l_allData[i_P2D][3]
        l_p2[1] = l_allData[i_P2D][7]
        l_p2[2] = l_allData[i_P2D][11]

        l_p3[0] = l_allData[i_P3D][3]
        l_p3[1] = l_allData[i_P3D][7]
        l_p3[2] = l_allData[i_P3D][11]
        l_mtxD = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1D, i_P1D+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxD, l_allData[j])

    if l_activeBoards[4] is 1:
        l_p1[0] = l_allData[i_P1E][3]
        l_p1[1] = l_allData[i_P1E][7]
        l_p1[2] = l_allData[i_P1E][11]

        l_p2[0] = l_allData[i_P2E][3]
        l_p2[1] = l_allData[i_P2E][7]
        l_p2[2] = l_allData[i_P2E][11]

        l_p3[0] = l_allData[i_P3E][3]
        l_p3[1] = l_allData[i_P3E][7]
        l_p3[2] = l_allData[i_P3E][11]
        l_mtxE = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1E, i_P1E+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxE, l_allData[j])

    if l_activeBoards[5] is 1:
        l_p1[0] = l_allData[i_P1F][3]
        l_p1[1] = l_allData[i_P1F][7]
        l_p1[2] = l_allData[i_P1F][11]

        l_p2[0] = l_allData[i_P2F][3]
        l_p2[1] = l_allData[i_P2F][7]
        l_p2[2] = l_allData[i_P2F][11]

        l_p3[0] = l_allData[i_P3F][3]
        l_p3[1] = l_allData[i_P3F][7]
        l_p3[2] = l_allData[i_P3F][11]
        l_mtxF = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1F, i_P1F+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxF, l_allData[j])

    if l_activeBoards[6] is 1:
        l_p1[0] = l_allData[i_P1G][3]
        l_p1[1] = l_allData[i_P1G][7]
        l_p1[2] = l_allData[i_P1G][11]

        l_p2[0] = l_allData[i_P2G][3]
        l_p2[1] = l_allData[i_P2G][7]
        l_p2[2] = l_allData[i_P2G][11]

        l_p3[0] = l_allData[i_P3G][3]
        l_p3[1] = l_allData[i_P3G][7]
        l_p3[2] = l_allData[i_P3G][11]
        l_mtxG = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1G, i_P1G+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxG, l_allData[j])

    if l_activeBoards[7] is 1:
        l_p1[0] = l_allData[i_P1H][3]
        l_p1[1] = l_allData[i_P1H][7]
        l_p1[2] = l_allData[i_P1H][11]

        l_p2[0] = l_allData[i_P2H][3]
        l_p2[1] = l_allData[i_P2H][7]
        l_p2[2] = l_allData[i_P2H][11]

        l_p3[0] = l_allData[i_P3H][3]
        l_p3[1] = l_allData[i_P3H][7]
        l_p3[2] = l_allData[i_P3H][11]
        l_mtxH = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1H, i_P1H+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxH, l_allData[j])
##Boards I - P
    if l_activeBoards[8] is 1:
        l_p1[0] = l_allData[i_P1I][3]
        l_p1[1] = l_allData[i_P1I][7]
        l_p1[2] = l_allData[i_P1I][11]

        l_p2[0] = l_allData[i_P2I][3]
        l_p2[1] = l_allData[i_P2I][7]
        l_p2[2] = l_allData[i_P2I][11]

        l_p3[0] = l_allData[i_P3I][3]
        l_p3[1] = l_allData[i_P3I][7]
        l_p3[2] = l_allData[i_P3I][11]
        l_mtxI = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1I, i_P1I+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxI, l_allData[j])

    if l_activeBoards[9] is 1:
        l_p1[0] = l_allData[i_P1J][3]
        l_p1[1] = l_allData[i_P1J][7]
        l_p1[2] = l_allData[i_P1J][11]

        l_p2[0] = l_allData[i_P2J][3]
        l_p2[1] = l_allData[i_P2J][7]
        l_p2[2] = l_allData[i_P2J][11]

        l_p3[0] = l_allData[i_P3J][3]
        l_p3[1] = l_allData[i_P3J][7]
        l_p3[2] = l_allData[i_P3J][11]
        l_mtxJ = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1J, i_P1J+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxJ, l_allData[j])

    if l_activeBoards[10] is 1:
        l_p1[0] = l_allData[i_P1K][3]
        l_p1[1] = l_allData[i_P1K][7]
        l_p1[2] = l_allData[i_P1K][11]

        l_p2[0] = l_allData[i_P2K][3]
        l_p2[1] = l_allData[i_P2K][7]
        l_p2[2] = l_allData[i_P2K][11]

        l_p3[0] = l_allData[i_P3K][3]
        l_p3[1] = l_allData[i_P3K][7]
        l_p3[2] = l_allData[i_P3K][11]
        l_mtxK = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1K, i_P1K+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxK, l_allData[j])

    if l_activeBoards[11] is 1:
        l_p1[0] = l_allData[i_P1L][3]
        l_p1[1] = l_allData[i_P1L][7]
        l_p1[2] = l_allData[i_P1L][11]

        l_p2[0] = l_allData[i_P2L][3]
        l_p2[1] = l_allData[i_P2L][7]
        l_p2[2] = l_allData[i_P2L][11]

        l_p3[0] = l_allData[i_P3L][3]
        l_p3[1] = l_allData[i_P3L][7]
        l_p3[2] = l_allData[i_P3L][11]
        l_mtxL = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1L, i_P1L+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxL, l_allData[j])

    if l_activeBoards[12] is 1:
        l_p1[0] = l_allData[i_P1M][3]
        l_p1[1] = l_allData[i_P1M][7]
        l_p1[2] = l_allData[i_P1M][11]

        l_p2[0] = l_allData[i_P2M][3]
        l_p2[1] = l_allData[i_P2M][7]
        l_p2[2] = l_allData[i_P2M][11]

        l_p3[0] = l_allData[i_P3M][3]
        l_p3[1] = l_allData[i_P3M][7]
        l_p3[2] = l_allData[i_P3M][11]
        l_mtxM = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1M, i_P1M+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxM, l_allData[j])

    if l_activeBoards[13] is 1:
        l_p1[0] = l_allData[i_P1N][3]
        l_p1[1] = l_allData[i_P1N][7]
        l_p1[2] = l_allData[i_P1N][11]

        l_p2[0] = l_allData[i_P2N][3]
        l_p2[1] = l_allData[i_P2N][7]
        l_p2[2] = l_allData[i_P2N][11]

        l_p3[0] = l_allData[i_P3N][3]
        l_p3[1] = l_allData[i_P3N][7]
        l_p3[2] = l_allData[i_P3N][11]
        l_mtxN = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1N, i_P1N+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxN, l_allData[j])

    if l_activeBoards[14] is 1:
        l_p1[0] = l_allData[i_P1O][3]
        l_p1[1] = l_allData[i_P1O][7]
        l_p1[2] = l_allData[i_P1O][11]

        l_p2[0] = l_allData[i_P2O][3]
        l_p2[1] = l_allData[i_P2O][7]
        l_p2[2] = l_allData[i_P2O][11]

        l_p3[0] = l_allData[i_P3O][3]
        l_p3[1] = l_allData[i_P3O][7]
        l_p3[2] = l_allData[i_P3O][11]
        l_mtxO = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1O, i_P1O+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxO, l_allData[j])

    if l_activeBoards[15] is 1:
        l_p1[0] = l_allData[i_P1P][3]
        l_p1[1] = l_allData[i_P1P][7]
        l_p1[2] = l_allData[i_P1P][11]

        l_p2[0] = l_allData[i_P2P][3]
        l_p2[1] = l_allData[i_P2P][7]
        l_p2[2] = l_allData[i_P2P][11]

        l_p3[0] = l_allData[i_P3P][3]
        l_p3[1] = l_allData[i_P3P][7]
        l_p3[2] = l_allData[i_P3P][11]
        l_mtxP = GetRotationMatrixIntersense(l_p2, l_p3, l_p1)
        for j in range(i_P1P, i_P1P+21):
            PutIntersenseRotMtxIntoCurrentData(l_mtxP, l_allData[j])

    return l_allData




def GenerateGarbageMattePoints(l_points):
    l_tmp = [0,0,0]
    f_unitConversion = 100.0
    print l_points
    for i in range(0,len(l_points)):
        l_tmp[0] = l_points[0][i][1]
        l_tmp[1] = l_points[0][i][2]
        l_tmp[2] = l_points[0][i][3]

        l_points[0][i][1] = l_tmp[0] * f_unitConversion
        l_points[0][i][2] = l_tmp[2] * f_unitConversion
        l_points[0][i][3] = l_tmp[1] * f_unitConversion



def WriteGarbageMattePoints(l_data, s_filename):
    s_outData = ""

    s_outData = "# Lightcraft Additional Survey Points file\n"
    s_outData += "# Created: %s \n" % (strftime("%a, %d %b %Y %H:%M:%S"))
    s_outData += "# ID\tX\t\tY\t\tZ\n"
    for l in l_data[0]:
        #Keep in mind that l_data comes in as 3D array with the first dimension of length 1, so only l_data[0] is required
        s_outData += "%s\t%f\t%f\t%f\n" % (l[0], l[1], l[2], l[3])
    f_out = open(s_filename+"_AdditionalSurveyPoints.txt",'w')
    f_out.write(s_outData)
    f_out.close()




##############################################################################
########################################################## Main Function #####
##############################################################################
def main():
    #variables
    l_filesCheck = []
    l_errorReport = []
    l_usedBoards = []
    l_activeTargets = []
    l_pointsA = [0,0,0,0,0,0,0,0,0]
    l_pointsB = [0,0,0,0,0,0,0,0,0]
    l_pointsC = [0,0,0,0,0,0,0,0,0]
    l_pointsD = [0,0,0,0,0,0,0,0,0]
    l_pointsE = [0,0,0,0,0,0,0,0,0]
    l_pointsF = [0,0,0,0,0,0,0,0,0]
    l_pointsG = [0,0,0,0,0,0,0,0,0]
    l_pointsH = [0,0,0,0,0,0,0,0,0]
    l_pointsI = [0,0,0,0,0,0,0,0,0]
    l_pointsJ = [0,0,0,0,0,0,0,0,0]
    l_pointsK = [0,0,0,0,0,0,0,0,0]
    l_pointsL = [0,0,0,0,0,0,0,0,0]
    l_pointsM = [0,0,0,0,0,0,0,0,0]
    l_pointsN = [0,0,0,0,0,0,0,0,0]
    l_pointsO = [0,0,0,0,0,0,0,0,0]
    l_pointsP = [0,0,0,0,0,0,0,0,0]
    l_GM = []
    l_data = []
    s_filename = ""
    #print l_usedBoards

    #File and Version Checks
    PythonVersionQuery()
    l_errorReport = LoadSurveyFile()
    if(l_errorReport[0] == 0):
        s_tmp = raw_input(l_errorReport[1] + "\n...press enter to continue...")
        return 0
    else:
        l_usedBoards = DefWhichBoardsAreSurveyed(l_errorReport[1], l_pointsA, l_pointsB, l_pointsC, l_pointsD, l_pointsE, l_pointsF, l_pointsG, l_pointsH, l_pointsI, l_pointsJ, l_pointsK, l_pointsL, l_pointsM, l_pointsN, l_pointsO, l_pointsP, l_GM )
        s_filename = l_errorReport[1].split('.')[0]

        l_data.append(l_pointsA)
        l_data.append(l_pointsB)
        l_data.append(l_pointsC)
        l_data.append(l_pointsD)
        l_data.append(l_pointsE)
        l_data.append(l_pointsF)
        l_data.append(l_pointsG)
        l_data.append(l_pointsH)
        l_data.append(l_pointsI)
        l_data.append(l_pointsJ)
        l_data.append(l_pointsK)
        l_data.append(l_pointsL)
        l_data.append(l_pointsM)
        l_data.append(l_pointsN)
        l_data.append(l_pointsO)
        l_data.append(l_pointsP)
        l_data = ToXYZfromNorthEastElev(l_data)
        l_errorReport = ErrorReporting_WhichBoardsAreSurveyed(l_usedBoards)  #returns l_errorReport with 3 indices, where the [2] index is the list of used floor board targets.
        l_activeTargets = l_errorReport[2]

        print l_GM
        if(len(l_GM)>0):
            print "Preparing to Generate Garbage Mattes"
            GenerateGarbageMattePoints(l_GM)
            WriteGarbageMattePoints(l_GM, s_filename)
        #print l_activeTargets
        #print l_errorReport

    if(l_errorReport[0] == 0):
        s_tmp = raw_input(l_errorReport[1] + "\n...press enter to continue...")
    else:
        l_filesCheck = NecessaryFileCheckCalibrationBoards()
        l_errorReport = CreateErrorReportCalibrationBoardsFiles(l_filesCheck, l_errorReport[2])
        #print l_errorReport


    if(l_errorReport[0] is 0):
        print l_errorReport[1]
        print "This process has been terminated..."
    else:
        #print l_pointsA, l_pointsB, l_pointsC, l_pointsD

        l_allData = TransformConstellationsFilesBySurveyOrientation(l_activeTargets, l_data)
        l_allData = ToNorthEastNegativeElevfromXYZ(l_allData)
        l_allData = ConvertToIntersense(l_activeTargets, l_allData)

        WriteCFGFile(l_allData, s_filename)

if __name__ == '__main__':
    main()

