# This script export matrices to an ASCII file. # # This script can export either all the matrices in a centroid configuration or # the matrices in a traffic demand. Matrices are saved in an ASCII file, one # after another and separated by a blank line. The format for each matrix is: # AIMSUN_ID AIMSUN_NAME # VEHICLE_ID VEHICLE_NAME # Initial Time # Duration # For each O/D a line with: # ORIGIN_CENTROID_ID DESTINATION_CENTROID_ID Trips # # If no vehicle has been assigned to a matrix the ID will be 0. # Note that names for vehicles and matrices can contain the space character # # (c) 2005-2006 TSS-Transport Simulation Systems # # Variables used to configure this script # # Full path to the file in where matrices will be written matrixFilePath = 'c:/tmp/matrices_bis.txt' # Identifier of the object that holds the matrices to export (either a traffic demand # or a centroid configuration) objectToExport = 111 # Write the matrix to the currently open file # Change this method if you want to change the format of the file (ie from a list of trips # to a matrix) def exportMatrix( file, matrix ): centroids = matrix.getCentroidConfiguration().getCentroidsInOrder() file.write( '%u %s\n' % (matrix.getId(), matrix.getName()) ) if matrix.getVehicle() != None: file.write( '%u %s\n' % (matrix.getVehicle().getId(), matrix.getVehicle().getName()) ) else: file.write( '0 None\n' ) file.write( '%s\n' % matrix.getFrom().toString( Qt.ISODate ) ) file.write( '%s\n' % matrix.getDuration().toString() ) for origin in centroids: for destination in centroids: if origin != destination: trips = matrix.getTrips( origin, destination ) if trips > 0: file.write( '%u %u %f\n' % (origin.getId(), destination.getId(), trips) ) file.write( '\n' ) # Export all the matrices in a centroid configuration def exportMatricesConf( model, file, centroidConf ): odMats = centroidConf.getODMatrices() if odMats != None: for matrix in odMats: if matrix.isA( "GKODMatrix" ): exportMatrix( file, matrix ) # Export all the matrices in a traffic demand def exportMatricesDemand( model, file, trafficDemand ): for demandItem in trafficDemand.getSchedule(): if demandItem.getTrafficDemand().isA( "GKODMatrix" ): exportMatrix( file, demandItem.getTrafficDemand() ) # Export matrices from object "entry". It can be either a traffic demand or a centroid configuration. # Change here the file name and path if required. def export( model, entry ): container = model.getCatalog().find( int(entry) ) if container != None: file = open( matrixFilePath, 'w' ) if file != None: if container.isA( "GKCentroidConfiguration" ): exportMatricesConf( model, file, container ) print 'Centroid configuration matrices exported' elif container.isA( "GKTrafficDemand" ): exportMatricesDemand( model, file, container ) print 'Traffic demand matrices exported' else: print 'Object is neither a centroid configuration not a traffic demand' file.close() else: print 'File cannot be opened' else: print 'No object to export' # Export the matrices. Set the right ID before using this script. export( model, objectToExport ) # Be sure that we reset the UNDO buffer after a non undoable modification model.getCommander().addCommand( None ) print "Done"