# EMTG: Evolutionary Mission Trajectory Generator
# An open-source global optimization tool for preliminary mission design
# Provided by NASA Goddard Space Flight Center
#
# Copyright (c) 2013 - 2020 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Other Rights Reserved.
#
# Licensed under the NASA Open Source License (the "License"); 
# You may not use this file except in compliance with the License. 
# You may obtain a copy of the License at:
# https://opensource.org/licenses/NASA-1.3
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
# express or implied.   See the License for the specific language
# governing permissions and limitations under the License.

def make_PyEMTG_JourneyOptions(OptionsDefinitions, now, path = '.'):    
    with open(path + "/JourneyOptions.py","w") as file:
        
        file.write('#PyEMTG JourneyOptions class\n')
        file.write('#auto-generated by make_EMTG_missionoptions_journeyoptions.py\n')
        file.write('\n')

        file.write('class JourneyOptions(object):\n')
        file.write('\n')

        file.write('    #************************************************************************************constructor\n')
        file.write('    def __init__(self, inputFile = None, lineNumber = None):\n')

        for option in OptionsDefinitions:
            if 'string' in option['dataType'] and option['name'] != 'user_data':
                file.write('        self.' + option['name'] + ' = "' + str(option['defaultValue']) + '" #' + option['comment'] + '\n')
            else:
                file.write('        self.' + option['name'] + ' = ' + str(option['defaultValue']) + ' #' + option['comment'] + '\n')

        file.write('        \n')
        file.write('        #empty lists for constraint definitions and trialX\n')
        file.write('        self.ManeuverConstraintDefinitions = []\n')
        file.write('        self.BoundaryConstraintDefinitions = []\n')
        file.write('        self.PhaseDistanceConstraintDefinitions = []\n')
        file.write('        self.trialX = []\n')
        file.write('\n')
        file.write('        if inputFile != None:\n')
        file.write('            self.parse_journey(inputFile, lineNumber)\n')


        file.write('   \n')
        file.write('    #************************************************************************************parse\n')
        file.write('    def parse_journey(self, inputFile, lineNumber = 0):\n')
        file.write('        while True:\n')
        file.write('            line = inputFile.readline()\n')
        file.write('            if not line:\n')
        file.write('                break\n')
        file.write('            #strip off the newline character\n')
        file.write('            line = line.rstrip("\\n\\r ")\n')
        file.write('            \n')
        file.write('            lineNumber += 1\n')
        file.write('            if line == "END_JOURNEY":\n')
        file.write('                break\n')
        file.write('            \n')
        file.write('            #if we got this far, then this is a line worth reading\n')
        file.write('            #Note that unlike EMTG proper, PyEMTG does NOT length or bounds-check input files. But if you try to run an invalid .emtgopt, EMTG will notify you and help you fix it.\n')
        file.write('            \n')
        file.write('            if line.strip(\'\\r\') != "":\n')
        file.write('                if line[0] != "#":\n')
        file.write('                    #this is an active line, so it is space delimited\n')
        file.write('                    linecell = [entry.rstrip(" \\r\\n") for entry in line.split(" ")]\n')
        file.write('                    \n')

        ifelse = ''
        for option in OptionsDefinitions:
            length = 1
            if 'std::vector' in option['dataType']:
                length = len(option['defaultValue'])
            converter_in = ''
            converter_out = ''

            if 'double' in option['dataType']:
                converter_in = 'float('
                converter_out = ')'
            elif 'string' not in option['dataType']:#bool, size_t, int, time_t, or any enum
                converter_in = 'int('
                converter_out = ')'
            
            
            file.write('                    ' + ifelse + 'if linecell[0] == "' + option['name'] + '":\n')
            if length == 1:
                file.write('                        self.' + option['name'] + ' = ' + converter_in + 'linecell[1]' + converter_out + '\n')
            else:
                file.write('                        self.' + option['name'] + ' = [' + converter_in + 'entry' + converter_out + ' for entry in linecell[1:]]\n')
            file.write('                  \n')
            ifelse = 'el'

        file.write('                    elif linecell[0] == "BEGIN_MANEUVER_CONSTRAINT_BLOCK":\n')
        file.write('                        self.ManeuverConstraintDefinitions = []\n')
        file.write('                        while True:\n')
        file.write('                            entry = inputFile.readline()\n')
        file.write('                            lineNumber += 1\n')
        file.write('                            if "END_MANEUVER_CONSTRAINT_BLOCK" in entry:\n')
        file.write('                                break\n')
        file.write('                            self.ManeuverConstraintDefinitions.append(entry)\n')
        file.write('                    \n')

        file.write('                    elif linecell[0] == "BEGIN_BOUNDARY_CONSTRAINT_BLOCK":\n')
        file.write('                        self.BoundaryConstraintDefinitions = []\n')
        file.write('                        while True:\n')
        file.write('                            entry = inputFile.readline()\n')
        file.write('                            lineNumber += 1\n')
        file.write('                            if "END_BOUNDARY_CONSTRAINT_BLOCK" in entry:\n')
        file.write('                                break\n')
        file.write('                            self.BoundaryConstraintDefinitions.append(entry)\n')
        file.write('                    \n')

        file.write('                    elif linecell[0] == "BEGIN_PHASE_DISTANCE_CONSTRAINT_BLOCK":\n')
        file.write('                        self.PhaseDistanceConstraintDefinitions = []\n')
        file.write('                        while True:\n')
        file.write('                            entry = inputFile.readline()\n')
        file.write('                            lineNumber += 1\n')
        file.write('                            if "END_PHASE_DISTANCE_CONSTRAINT_BLOCK" in entry:\n')
        file.write('                                break\n')
        file.write('                            self.PhaseDistanceConstraintDefinitions.append(entry)\n')
        file.write('                    \n')

        file.write('                    elif linecell[0] == "BEGIN_TRIALX":\n')
        file.write('                        self.trialX = []\n')
        file.write('                        while True:\n')
        file.write('                            entry = inputFile.readline()\n')
        file.write('                            lineNumber += 1\n')
        file.write('                            if "END_TRIALX" in entry:\n')
        file.write('                                break\n')
        file.write('                            commalinecell = entry.split(\',\')\n')
        file.write('                            self.trialX.append(commalinecell)\n')
        file.write('                    \n')
        
        
        file.write('    #************************************************************************************write\n')
        file.write('    def write(self, optionsFileName, writeAll = False):\n')
        file.write('        with open(optionsFileName, "a+") as optionsFile:\n')
        file.write('            optionsFile.write("\\n")\n')
        file.write('            optionsFile.write("\\n")\n')
        file.write('            optionsFile.write("BEGIN_JOURNEY\\n")\n')
        file.write('            optionsFile.write("\\n")\n')
        file.write('            \n')

        for option in OptionsDefinitions:
            name = option['name']

            defaultValue = ''
            if option['dataType'] == 'std::string':
                defaultValue = '"' + str(option['defaultValue']) + '"'
            else:
                defaultValue = str(option['defaultValue'])

            file.write('            if (self.' + name + ' != ' + defaultValue + ' or writeAll):\n')
            file.write('                optionsFile.write("#' + option['description'] + '\\n")\n')
            if 'std::vector' in option['dataType']:
                elementType = option['dataType'].replace('std::vector<','').replace('>','')
                file.write('                optionsFile.write("' + name + '")\n')
                file.write('                for entry in self.' + name + ':\n')
                if 'bool' in option['dataType']:
                    file.write('                    optionsFile.write(" " + str(int(entry)))\n')
                else:
                    file.write('                    optionsFile.write(" " + str(entry))\n')
                file.write('                optionsFile.write("\\n")\n')
            else:
                if 'bool' in option['dataType']:
                    file.write('                optionsFile.write("' + name + ' " + str(int(self.' + name + ')) + "\\n")\n')
                else:
                    file.write('                optionsFile.write("' + name + ' " + str(self.' + name + ') + "\\n")\n')
            file.write('    \n')        
                
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            optionsFile.write("#Maneuver constraint code\\n")\n') 
        file.write('            optionsFile.write("#Works for absolute and relative epochs and also magnitudes\\n")\n') 
        file.write('            optionsFile.write("BEGIN_MANEUVER_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            for ManeuverConstraintDefinition in self.ManeuverConstraintDefinitions:\n') 
        file.write('                optionsFile.write(ManeuverConstraintDefinition + "\\n")\n') 
        file.write('            optionsFile.write("END_MANEUVER_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            \n') 
                
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            optionsFile.write("#Boundary constraint code\\n")\n') 
        file.write('            optionsFile.write("BEGIN_BOUNDARY_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            for BoundaryConstraintDefinition in self.BoundaryConstraintDefinitions:\n') 
        file.write('                optionsFile.write(BoundaryConstraintDefinition + "\\n")\n') 
        file.write('            optionsFile.write("END_BOUNDARY_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            \n') 
        
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            optionsFile.write("#Phase distance constraint code\\n")\n') 
        file.write('            optionsFile.write("BEGIN_PHASE_DISTANCE_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            for PhaseDistanceConstraintDefinition in self.PhaseDistanceConstraintDefinitions:\n') 
        file.write('                optionsFile.write(PhaseDistanceConstraintDefinition + "\\n")\n') 
        file.write('            optionsFile.write("END_PHASE_DISTANCE_CONSTRAINT_BLOCK\\n")\n') 
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            \n') 

        file.write('            if len(self.trialX) > 0:\n') 
        file.write('                optionsFile.write("#trial decision vector\\n")\n') 
        file.write('                optionsFile.write("BEGIN_TRIALX\\n")\n') 
        file.write('                for entry in self.trialX:\n') 
        file.write('                    optionsFile.write(entry[0] + "," + \'%17.20f\' % float(entry[1]) + "\\n")\n') 
        file.write('                optionsFile.write("END_TRIALX\\n")\n') 
        file.write('            optionsFile.write("\\n")\n') 
        file.write('            \n') 

        file.write('            optionsFile.write("END_JOURNEY")\n')
        file.write('            \n') 

        file.write('    #************************************************************************************convert decision vector\n')
        file.write('    def ConvertDecisionVector(self, ParallelShootingStateRepresentation, PeriapseBoundaryStateRepresentation):                          \n')
        file.write('        if ParallelShootingStateRepresentation == 1:#SphericalRADEC, so we need to convert any SphericalAZFPA segments to SphericalRADEC\n')
        file.write('            from math import sin, cos, atan2, asin                                                                          \n')
        file.write('            for Xindex in range(0, len(self.trialX)):                                                                       \n')
        file.write('                description = self.trialX[Xindex][0]                                                                        \n')
        file.write('                prefix = description.split(\':\')[0]                                                                          \n')
        file.write('                                                                                                                            \n')
        file.write('                if (\'PSFB_Step\' in description or \'PSBI_Step\' in description) and \'left state AZ\' in description:                                           \n')
        file.write('                    #extract the SphericalAZFPA state                                                                       \n')
        file.write('                    r = float(self.trialX[Xindex - 4][1])                                                                   \n')
        file.write('                    RA = float(self.trialX[Xindex - 3][1])                                                                  \n')
        file.write('                    DEC = float(self.trialX[Xindex - 2][1])                                                                 \n')
        file.write('                    v = float(self.trialX[Xindex - 1][1])                                                                   \n')
        file.write('                    AZ = float(self.trialX[Xindex][1])                                                                      \n')
        file.write('                    FPA = float(self.trialX[Xindex + 1][1])                                                                 \n')
        file.write('                    #convert to cartesian                                                                                   \n')
        file.write('                    cosRA = cos(RA)                                                                                         \n')
        file.write('                    sinRA = sin(RA)                                                                                         \n')
        file.write('                    cosDEC = cos(DEC)                                                                                       \n')
        file.write('                    sinDEC = sin(DEC)                                                                                       \n')
        file.write('                    cosAZ = cos(AZ)                                                                                         \n')
        file.write('                    sinAZ = sin(AZ)                                                                                         \n')
        file.write('                    cosFPA = cos(FPA)                                                                                       \n')
        file.write('                    sinFPA = sin(FPA)                                                                                       \n')
        file.write('                                                                                                                            \n')
        file.write('                    xdot = -v * (sinFPA*(sinAZ*sinRA + cosAZ * cosRA*sinDEC) - cosFPA * cosDEC*cosRA);                      \n')
        file.write('                    ydot =  v * (sinFPA*(cosRA*sinAZ - cosAZ * sinDEC*sinRA) + cosFPA * cosDEC*sinRA);                      \n')
        file.write('                    zdot =  v * (cosFPA*sinDEC + cosDEC * cosAZ*sinFPA);                                                    \n')
        file.write('                                                                                                                            \n')
        file.write('                    #compute vRA and vDEC                                                                                   \n')
        file.write('                    vRA = atan2(ydot, xdot)                                                                                 \n')
        file.write('                    vDEC = asin(zdot / v)                                                                                   \n')
        file.write('                                                                                                                            \n')
        file.write('                    #write vRA and vDEC into the decision vector                                                            \n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace(\'AZ\',\'vRA\')                                     \n')
        file.write('                    self.trialX[Xindex][1] = str(vRA)                                                                       \n')
        file.write('                    self.trialX[Xindex + 1][0] = self.trialX[Xindex + 1][0].replace(\'FPA\',\'vDEC\')                           \n')
        file.write('                    self.trialX[Xindex + 1][1] = str(vDEC)                                                                  \n')
        file.write('        elif ParallelShootingStateRepresentation == 2:#SphericalAZFPA - convert all SphericalRADEC to this format                       \n')
        file.write('            from math import sin, cos, atan2, asin, acos, pi                                                                \n')
        file.write('            import numpy                                                                                                    \n')
        file.write('            for Xindex in range(0, len(self.trialX)):                                                                       \n')
        file.write('                description = self.trialX[Xindex][0]                                                                        \n')
        file.write('                prefix = description.split(\':\')[0]                                                                          \n')
        file.write('                                                                                                                            \n')
        file.write('                if (\'PSFB_Step\' in description or \'PSBI_Step\' in description) and \'left state vRA\' in description:                                          \n')
        file.write('                    #extract the SphericalAZFPA state                                                                       \n')
        file.write('                    r = float(self.trialX[Xindex - 4][1])                                                                   \n')
        file.write('                    RA = float(self.trialX[Xindex - 3][1])                                                                  \n')
        file.write('                    DEC = float(self.trialX[Xindex - 2][1])                                                                 \n')
        file.write('                    v = float(self.trialX[Xindex - 1][1])                                                                   \n')
        file.write('                    vRA = float(self.trialX[Xindex][1])                                                                     \n')
        file.write('                    vDEC = float(self.trialX[Xindex + 1][1])                                                                \n')
        file.write('                    #convert to cartesian                                                                                   \n')
        file.write('                    cosRA = cos(RA)                                                                                         \n')
        file.write('                    sinRA = sin(RA)                                                                                         \n')
        file.write('                    cosDEC = cos(DEC)                                                                                       \n')
        file.write('                    sinDEC = sin(DEC)                                                                                       \n')
        file.write('                    cosvRA = cos(vRA)                                                                                       \n')
        file.write('                    sinvRA = sin(vRA)                                                                                       \n')
        file.write('                    cosvDEC = cos(vDEC)                                                                                     \n')
        file.write('                    sinvDEC = sin(vDEC)                                                                                     \n')
        file.write('                                                                                                                            \n')
        file.write('                    x = r * cosRA * cosDEC                                                                                  \n')
        file.write('                    y = r * sinRA * cosDEC                                                                                  \n')
        file.write('                    z = r * sinDEC;                                                                                         \n')
        file.write('                    xdot = v * cosvRA * cosvDEC                                                                             \n')
        file.write('                    ydot = v * sinvRA * cosvDEC                                                                             \n')
        file.write('                    zdot = v * sinvDEC;                                                                                     \n')
        file.write('                                                                                                                            \n')
        file.write('                    #compute AZ and FPA                                                                                     \n')
        file.write('                    FPA = acos( (x*xdot + y*ydot + z*zdot) / r / v )                                                        \n')
        file.write('                                                                                                                            \n')
        file.write('                    #azimuth is complicated                                                                                 \n')
        file.write('                    xhat = numpy.matrix([cos(RA)*cos(DEC), sin(RA)*cos(DEC), sin(DEC)]).T                                   \n')
        file.write('                    yhat = numpy.matrix([cos(RA + pi / 2.0), sin(RA + pi / 2.0), 0.0]).T                                    \n')
        file.write('                    zhat = numpy.matrix([-cos(RA)*sin(DEC), -sin(RA)*sin(DEC), cos(DEC)]).T                                 \n')
        file.write('                    R = numpy.hstack([xhat, yhat, zhat]).T                                                                  \n')
        file.write('                    V = numpy.matrix([xdot, ydot, zdot]).T                                                                  \n')
        file.write('                    Vprime = R * V                                                                                          \n')
        file.write('                    AZ = atan2(Vprime[1], Vprime[2])                                                                        \n')
        file.write('                                                                                                                            \n')
        file.write('                    #write vRA and vDEC into the decision vector                                                            \n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace(\'vRA\',\'AZ\')                                     \n')
        file.write('                    self.trialX[Xindex][1] = str(AZ)                                                                        \n')
        file.write('                    self.trialX[Xindex + 1][0] = self.trialX[Xindex + 1][0].replace(\'vDEC\',\'FPA\')                           \n')
        file.write('                    self.trialX[Xindex + 1][1] = str(FPA)                                                                   \n')
        file.write('                                                                                                                            \n')
        file.write('        if PeriapseBoundaryStateRepresentation == 1:#SphericalRADEC - convert all SphericalAZFPA to SphericalRADEC          \n')
        file.write('            from math import sin, cos, atan2, asin                                                                          \n')
        file.write('            for Xindex in range(0, len(self.trialX)):                                                                       \n')
        file.write('                description = self.trialX[Xindex][0]                                                                        \n')
        file.write('                prefix = description.split(\':\')[0]                                                                          \n')
        file.write('                                                                                                                            \n')
        file.write('                if \'Periapse\' in description and \'event left state AZ\' in description:                                      \n')
        file.write('                    #extract the SphericalAZFPA state                                                                       \n')
        file.write('                    r = float(self.trialX[Xindex - 4][1])                                                                   \n')
        file.write('                    RA = float(self.trialX[Xindex - 3][1])                                                                  \n')
        file.write('                    DEC = float(self.trialX[Xindex - 2][1])                                                                 \n')
        file.write('                    v = float(self.trialX[Xindex - 1][1])                                                                   \n')
        file.write('                    AZ = float(self.trialX[Xindex][1])                                                                      \n')
        file.write('                    FPA = float(self.trialX[Xindex + 1][1])                                                                 \n')
        file.write('                    #convert to cartesian                                                                                   \n')
        file.write('                    cosRA = cos(RA)                                                                                         \n')
        file.write('                    sinRA = sin(RA)                                                                                         \n')
        file.write('                    cosDEC = cos(DEC)                                                                                       \n')
        file.write('                    sinDEC = sin(DEC)                                                                                       \n')
        file.write('                    cosAZ = cos(AZ)                                                                                         \n')
        file.write('                    sinAZ = sin(AZ)                                                                                         \n')
        file.write('                    cosFPA = cos(FPA)                                                                                       \n')
        file.write('                    sinFPA = sin(FPA)                                                                                       \n')
        file.write('                                                                                                                            \n')
        file.write('                    xdot = -v * (sinFPA*(sinAZ*sinRA + cosAZ * cosRA*sinDEC) - cosFPA * cosDEC*cosRA);                      \n')
        file.write('                    ydot =  v * (sinFPA*(cosRA*sinAZ - cosAZ * sinDEC*sinRA) + cosFPA * cosDEC*sinRA);                      \n')
        file.write('                    zdot =  v * (cosFPA*sinDEC + cosDEC * cosAZ*sinFPA);                                                    \n')
        file.write('                                                                                                                            \n')
        file.write('                    #compute vRA and vDEC                                                                                   \n')
        file.write('                    vRA = atan2(ydot, xdot)                                                                                 \n')
        file.write('                    vDEC = asin(zdot / v)                                                                                   \n')
        file.write('                                                                                                                            \n')
        file.write('                    #write vRA and vDEC into the decision vector                                                            \n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace(\'AZ\',\'vRA\')                                     \n')
        file.write('                    self.trialX[Xindex][1] = str(vRA)                                                                       \n')
        file.write('                    self.trialX[Xindex + 1][0] = self.trialX[Xindex + 1][0].replace(\'FPA\',\'vDEC\')                           \n')
        file.write('                    self.trialX[Xindex + 1][1] = str(vDEC)                                                                  \n')
        file.write('        elif PeriapseBoundaryStateRepresentation == 2:#SphericalAZFPA - convert all SphericalRADEC to this format           \n')
        file.write('            from math import sin, cos, atan2, asin, acos, pi                                                                \n')
        file.write('            import numpy                                                                                                    \n')
        file.write('            for Xindex in range(0, len(self.trialX)):                                                                       \n')
        file.write('                description = self.trialX[Xindex][0]                                                                        \n')
        file.write('                prefix = description.split(\':\')[0]                                                                          \n')
        file.write('                                                                                                                            \n')
        file.write('                if \'Periapse\' in description and \'event left state vRA\' in description:                                     \n')
        file.write('                    #extract the SphericalAZFPA state                                                                       \n')
        file.write('                    r = float(self.trialX[Xindex - 4][1])                                                                   \n')
        file.write('                    RA = float(self.trialX[Xindex - 3][1])                                                                  \n')
        file.write('                    DEC = float(self.trialX[Xindex - 2][1])                                                                 \n')
        file.write('                    v = float(self.trialX[Xindex - 1][1])                                                                   \n')
        file.write('                    vRA = float(self.trialX[Xindex][1])                                                                     \n')
        file.write('                    vDEC = float(self.trialX[Xindex + 1][1])                                                                \n')
        file.write('                    #convert to cartesian                                                                                   \n')
        file.write('                    cosRA = cos(RA)                                                                                         \n')
        file.write('                    sinRA = sin(RA)                                                                                         \n')
        file.write('                    cosDEC = cos(DEC)                                                                                       \n')
        file.write('                    sinDEC = sin(DEC)                                                                                       \n')
        file.write('                    cosvRA = cos(vRA)                                                                                       \n')
        file.write('                    sinvRA = sin(vRA)                                                                                       \n')
        file.write('                    cosvDEC = cos(vDEC)                                                                                     \n')
        file.write('                    sinvDEC = sin(vDEC)                                                                                     \n')
        file.write('                                                                                                                            \n')
        file.write('                    x = r * cosRA * cosDEC                                                                                  \n')
        file.write('                    y = r * sinRA * cosDEC                                                                                  \n')
        file.write('                    z = r * sinDEC;                                                                                         \n')
        file.write('                    xdot = v * cosvRA * cosvDEC                                                                             \n')
        file.write('                    ydot = v * sinvRA * cosvDEC                                                                             \n')
        file.write('                    zdot = v * sinvDEC;                                                                                     \n')
        file.write('                                                                                                                            \n')
        file.write('                    #compute AZ and FPA                                                                                     \n')
        file.write('                    FPA = acos( (x*xdot + y*ydot + z*zdot) / r / v )                                                        \n')
        file.write('                                                                                                                            \n')
        file.write('                    #azimuth is complicated                                                                                 \n')
        file.write('                    xhat = numpy.matrix([cos(RA)*cos(DEC), sin(RA)*cos(DEC), sin(DEC)]).T                                   \n')
        file.write('                    yhat = numpy.matrix([cos(RA + pi / 2.0), sin(RA + pi / 2.0), 0.0]).T                                    \n')
        file.write('                    zhat = numpy.matrix([-cos(RA)*sin(DEC), -sin(RA)*sin(DEC), cos(DEC)]).T                                 \n')
        file.write('                    R = numpy.hstack([xhat, yhat, zhat]).T                                                                  \n')
        file.write('                    V = numpy.matrix([xdot, ydot, zdot]).T                                                                  \n')
        file.write('                    Vprime = R * V                                                                                          \n')
        file.write('                    AZ = atan2(Vprime[1], Vprime[2])                                                                        \n')
        file.write('                                                                                                                            \n')
        file.write('                    #write vRA and vDEC into the decision vector                                                            \n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace(\'vRA\',\'AZ\')                                     \n')
        file.write('                    self.trialX[Xindex][1] = str(AZ)                                                                        \n')
        file.write('                    self.trialX[Xindex + 1][0] = self.trialX[Xindex + 1][0].replace(\'vDEC\',\'FPA\')                           \n')
        file.write('                    self.trialX[Xindex + 1][1] = str(FPA)                                                                   \n')
        file.write('        \n')  
        file.write('        if len(self.trialX) > 0:\n')
        file.write('            if self.phase_type == 2 and "FBLT" in self.trialX[0][0]: #if this is an MGALT journey and the guess is from FBLT\n')
        file.write('                print("converting journey ", self.journey_name, " from FBLT to MGALT")\n')
        file.write('                for Xindex in range(0, len(self.trialX)):\n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace("FBLT","MGALT")\n')
        file.write('            \n')
        file.write('            elif self.phase_type == 3 and "MGALT" in self.trialX[0][0]: #if this is an FBLT journey and the guess is from MGALT\n')
        file.write('                print("converting journey ", self.journey_name, " from MGALT to FBLT")\n')
        file.write('                for Xindex in range(0, len(self.trialX)):\n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace("MGALT","FBLT")\n')
        file.write('            \n')
        file.write('            elif self.phase_type == 4 and "PSFB" in self.trialX[0][0]: #if this is a PSBI journey and the guess is from PSFB\n')
        file.write('                print("converting journey ", self.journey_name, " from PSFB to PSBI")\n')
        file.write('                for Xindex in range(0, len(self.trialX)):\n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace("PSFB","PSBI")\n')
        file.write('            \n')
        file.write('            elif self.phase_type == 5 and "PSBI" in self.trialX[0][0]: #if this is an PSFB journey and the guess is from PSBI\n')
        file.write('                print("converting journey ", self.journey_name, " from PSBI to PSFB")\n')
        file.write('                for Xindex in range(0, len(self.trialX)):\n')
        file.write('                    self.trialX[Xindex][0] = self.trialX[Xindex][0].replace("PSBI","PSFB")\n')
        file.write('        \n')       
        file.write('        return                                                                                                              \n')
