Files
OpenUxAS-SoI/resources/AutomationDiagramDataService/PlotAutomationDiagram.code
2017-12-04 15:30:56 -05:00

172 lines
11 KiB
Plaintext

file << "#! /usr/bin/env python" << std::endl;
file << "" << std::endl;
file << "import pandas as pd" << std::endl;
file << "import os\t#os.path.exists" << std::endl;
file << "import matplotlib" << std::endl;
file << "import matplotlib.pyplot as plt" << std::endl;
file << "import matplotlib.mlab as mlab" << std::endl;
file << "from matplotlib.backends.backend_pdf import PdfPages" << std::endl;
file << "from matplotlib.dates import YEARLY, DateFormatter, rrulewrapper, RRuleLocator, drange" << std::endl;
file << "import matplotlib.patches as mpatches" << std::endl;
file << "from itertools import cycle " << std::endl;
file << "import LocalCoords\t# lat/long conversion" << std::endl;
file << "import ProcessTasks\t# process task messages" << std::endl;
file << "import ProcessUniqueAutomationResponse\t# process UniqueAutomationResponse message" << std::endl;
file << "import ProcessEntityStates\t# process vehicle states" << std::endl;
file << "import ProcessZones\t# process keep-in/out zones" << std::endl;
file << "" << std::endl;
file << "def main():" << std::endl;
file << "" << std::endl;
file << "\tprint('')\t# add a line return" << std::endl;
file << "\tprint('*** LOADING pkls ***')" << std::endl;
file << "\tprint('')\t# add a line return" << std::endl;
file << "" << std::endl;
file << "\ttasksPickle = 'Tasks.pkl'\t\t\t" << std::endl;
file << "\tif not os.path.exists(tasksPickle):" << std::endl;
file << "\t\tProcessTasks.main()" << std::endl;
file << "\ttasksPd = pd.read_pickle(tasksPickle)" << std::endl;
file << "" << std::endl;
file << "\tmissionCommandPickle = 'MissionCommands.pkl'\t\t\t" << std::endl;
file << "\tif not os.path.exists(missionCommandPickle):" << std::endl;
file << "\t\tProcessUniqueAutomationResponse.main()" << std::endl;
file << "\tmissionCommandArrayPd = pd.read_pickle(missionCommandPickle)" << std::endl;
file << "" << std::endl;
file << "\tvehicleStatesPickle = 'VehicleStates.pkl'\t\t\t" << std::endl;
file << "\tif not os.path.exists(vehicleStatesPickle):" << std::endl;
file << "\t\tProcessEntityStates.main()" << std::endl;
file << "\tvehicleStatesPd = pd.read_pickle(vehicleStatesPickle)" << std::endl;
file << "" << std::endl;
file << "\tzonesPickle = 'Zones.pkl'\t\t\t" << std::endl;
file << "\tif not os.path.exists(zonesPickle):" << std::endl;
file << "\t\tProcessZones.main()" << std::endl;
file << "\tzoneArrayPd = pd.read_pickle(zonesPickle)" << std::endl;
file << "" << std::endl;
file << "\tprint('')\t# add a line return" << std::endl;
file << "\tprint('*** PLOTTING ***')" << std::endl;
file << "\tprint('')\t# add a line return" << std::endl;
file << "" << std::endl;
file << "\tfig = plt.figure(10000)" << std::endl;
file << "\tfig.clf()" << std::endl;
file << "" << std::endl;
file << "\t#############################################" << std::endl;
file << "\t# the waypoint files" << std::endl;
file << "\tplotLines = []" << std::endl;
file << "\tlabelStrings = []" << std::endl;
file << "\tlastVehicleId = -1" << std::endl;
file << "" << std::endl;
file << "\tnormVehicles = matplotlib.colors.Normalize(vmin=missionCommandArrayPd.vehicleID.min(), vmax=missionCommandArrayPd.vehicleID.max())" << std::endl;
file << "\tcmapVehicles = matplotlib.cm.get_cmap('plasma')" << std::endl;
file << "" << std::endl;
file << "\t# THE PLANS/VEHICLES" << std::endl;
file << "\tfor missionCommand in missionCommandArrayPd.itertuples():" << std::endl;
file << "\t\tvehicleID = missionCommand.vehicleID" << std::endl;
file << "\t\tlabelStrings.append(str(vehicleID))" << std::endl;
file << "\t\tfirstWaypoint = missionCommand.firstWaypoint" << std::endl;
file << "\t\tcommandID = missionCommand.commandID" << std::endl;
file << "\t\tstatus = missionCommand.status" << std::endl;
file << "\t\tvehicleColor = cmapVehicles(normVehicles(vehicleID))" << std::endl;
file << "\t\tfor vehicleState in vehicleStatesPd.itertuples():" << std::endl;
file << "\t\t\tif vehicleID == vehicleState.vehicleID:" << std::endl;
file << "\t\t\t\t# print('[' + str(vehicleState) + ']')" << std::endl;
file << "\t\t\t\tvehicleLabel = 'V[' + str(vehicleState.vehicleID) + ']'" << std::endl;
file << "\t\t\t\tlabelStrings.append(vehicleLabel)" << std::endl;
file << "\t\t\t\tNorthEast_m = LocalCoords.LatLong_degToNorthEast_m(vehicleState.latitude,vehicleState.longitude)" << std::endl;
file << "\t\t\t\tplt.text(NorthEast_m[1],NorthEast_m[0],vehicleLabel,horizontalalignment='center',verticalalignment='top',color=[0.1,0.1,0.1],size=15)" << std::endl;
file << "\t\t\t\tline, = plt.plot(NorthEast_m[1],NorthEast_m[0],'*',markersize=18,color=vehicleColor)" << std::endl;
file << "\t\t\t\tplotLines.append(line)" << std::endl;
file << "\t\t\t\tbreak;" << std::endl;
file << "" << std::endl;
file << "\t\tNorth_m = []" << std::endl;
file << "\t\tEast_m = []" << std::endl;
file << "\t\tfor waypoint in missionCommand.waypointListPd.itertuples():" << std::endl;
file << "\t\t\tnumber = waypoint.number" << std::endl;
file << "\t\t\taltitude = waypoint.altitude" << std::endl;
file << "\t\t\tNorthEast_m = LocalCoords.LatLong_degToNorthEast_m(waypoint.latitude,waypoint.longitude)" << std::endl;
file << "\t\t\tNorth_m.append(NorthEast_m[0])" << std::endl;
file << "\t\t\tEast_m.append(NorthEast_m[1])" << std::endl;
file << "\t\t\tline, = plt.plot(NorthEast_m[1],NorthEast_m[0],'sg',markersize=5,color=vehicleColor)" << std::endl;
file << "\t\t\tplotLines.append(line)" << std::endl;
file << "\t\t\t# labelString = '[' + str(number) + ']'" << std::endl;
file << "\t\t\t# plt.text(NorthEast_m[0],NorthEast_m[1],labelString,horizontalalignment='right',verticalalignment='top')" << std::endl;
file << "\t\tline, = plt.plot(East_m,North_m,linestyle='-',color=vehicleColor)" << std::endl;
file << "\t\tplotLines.append(line)" << std::endl;
file << "\t# THE TASKS" << std::endl;
file << "\tfor task in tasksPd.itertuples():" << std::endl;
file << "\t\ttaskId = task.taskID" << std::endl;
file << "\t\ttaskLabel = task.label" << std::endl;
file << "\t\tlabelStrings.append(str(taskLabel))" << std::endl;
file << "\t\tNorth_m = []" << std::endl;
file << "\t\tEast_m = []" << std::endl;
file << "\t\t# print('task.searchBoundaryPd.size[' + str(task.searchBoundaryPd.size) + ']')" << std::endl;
file << "\t\tif task.searchBoundaryPd.shape[0] != 1 and task.searchBoundaryPd.shape[0] != 0:" << std::endl;
file << "\t\t\tfor boundary in task.searchBoundaryPd.itertuples():" << std::endl;
file << "\t\t\t\tNorthEast_m = LocalCoords.LatLong_degToNorthEast_m(boundary.latitude,boundary.longitude)" << std::endl;
file << "\t\t\t\tNorth_m.append(NorthEast_m[0])" << std::endl;
file << "\t\t\t\tEast_m.append(NorthEast_m[1])" << std::endl;
file << "\t\t\tplt.text(East_m[0],North_m[0],taskLabel,horizontalalignment='right',verticalalignment='top',color=[0.1,0.1,0.1],size=15)" << std::endl;
file << "\t\t\tline, = plt.plot(East_m,North_m,linewidth=3,linestyle='--',color=[0.5,0.5,0.5])" << std::endl;
file << "\t\t\tplotLines.append(line)" << std::endl;
file << "\t\telif task.searchBoundaryPd.shape[0] != 0:" << std::endl;
file << "\t\t\tNorthEast_m = LocalCoords.LatLong_degToNorthEast_m(task.searchBoundaryPd.latitude[0],task.searchBoundaryPd.longitude[0])" << std::endl;
file << "\t\t\tplt.text(NorthEast_m[1],NorthEast_m[0],taskLabel,horizontalalignment='right',verticalalignment='top',color=[0.1,0.1,0.1],size=15)" << std::endl;
file << "\t\t\tline, = plt.plot(NorthEast_m[1],NorthEast_m[0],'o',markersize=12,color=[0.5,0.5,0.5])" << std::endl;
file << "\t\t\tplotLines.append(line)" << std::endl;
file << "\t\telse:" << std::endl;
file << "\t\t\tprint('Missing data: Could not plot task ' + taskLabel)" << std::endl;
file << "\t# THE ZONES" << std::endl;
file << "\tfor zone in zoneArrayPd.itertuples():" << std::endl;
file << "\t\tzoneId = zone.zoneID" << std::endl;
file << "\t\tzoneLabel = zone.label" << std::endl;
file << "\t\tlabelStrings.append(str(zoneLabel))" << std::endl;
file << "\t\tzoneColor = [0.1,0.1,0.1]" << std::endl;
file << "\t\tzoneLineStyle = '-'" << std::endl;
file << "\t\tzoneLinewidth = 1" << std::endl;
file << "\t\tif zone.zoneType == 'KeepInZone':" << std::endl;
file << "\t\t\tzoneColor = [0.4,0.4,0.4]" << std::endl;
file << "\t\t\tzoneLineStyle = ':'" << std::endl;
file << "\t\t\tzoneLinewidth = 4" << std::endl;
file << "\t\telif zone.zoneType == 'KeepOutZone':" << std::endl;
file << "\t\t\tzoneColor = [0.1,0.1,0.1]" << std::endl;
file << "\t\t\tzoneLineStyle = '-'" << std::endl;
file << "\t\t\tzoneLinewidth = 4" << std::endl;
file << "" << std::endl;
file << "\t\tNorth_m = []" << std::endl;
file << "\t\tEast_m = []" << std::endl;
file << "\t\t# print('zone.boundaryPd.size[' + str(zone.boundaryPd.shape) + ']')" << std::endl;
file << "\t\tfor boundary in zone.boundaryPd.itertuples():" << std::endl;
file << "\t\t\tNorthEast_m = LocalCoords.LatLong_degToNorthEast_m(boundary.latitude,boundary.longitude)" << std::endl;
file << "\t\t\tNorth_m.append(NorthEast_m[0])" << std::endl;
file << "\t\t\tEast_m.append(NorthEast_m[1])" << std::endl;
file << "\t\tNorth_m.append(North_m[0])\t#close the boundary" << std::endl;
file << "\t\tEast_m.append(East_m[0])" << std::endl;
file << "\t\tplt.text(East_m[0],North_m[0],zoneLabel,horizontalalignment='right',verticalalignment='top',color=[0.1,0.1,0.1],size=15)" << std::endl;
file << "\t\tline, = plt.plot(East_m,North_m,linewidth=zoneLinewidth,linestyle=zoneLineStyle,color=zoneColor)" << std::endl;
file << "\t\tplotLines.append(line)" << std::endl;
file << "" << std::endl;
file << "\t#plt.title(stringTitle)" << std::endl;
file << "\tplt.ylabel('postion north (m)')" << std::endl;
file << "\tplt.xlabel('position east (m)')" << std::endl;
file << "\tplt.grid(True)" << std::endl;
file << "\tplt.axis('equal')" << std::endl;
file << "\t# LEGEND STUFF" << std::endl;
file << "\t# plt.legend(plotLines,labelStrings,shadow=True,fancybox=True,bbox_to_anchor=(0.9, 0.1), loc=2, borderaxespad=0.)" << std::endl;
file << "\t# leg = plt.gca().get_legend()" << std::endl;
file << "\t# ltext = leg.get_texts() # all the text.Text instance in the legend" << std::endl;
file << "\t# llines = leg.get_lines() # all the lines.Line2D instance in the legend" << std::endl;
file << "\t# frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend" << std::endl;
file << "\t# frame.set_facecolor('0.80') # set the frame face color to light gray" << std::endl;
file << "\t# plt.setp(ltext, fontsize='small') # the legend text fontsize" << std::endl;
file << "\t# plt.setp(llines, linewidth=1.5) # the legend linewidth" << std::endl;
file << "\t#leg.draw_frame(False) # don't draw the legend frame" << std::endl;
file << "\tplt.draw()" << std::endl;
file << "\t#save a pdf file" << std::endl;
file << "\tpdfFileName = 'AutomationDiagram.pdf'" << std::endl;
file << "\tpp = PdfPages(pdfFileName)" << std::endl;
file << "\tpp.savefig()" << std::endl;
file << "\tpp.close()" << std::endl;
file << "\tplt.show()" << std::endl;
file << "" << std::endl;
file << "" << std::endl;
file << "if __name__ == '__main__':" << std::endl;
file << " main()" << std::endl;