![]() |
| Script Doctor 3: Barcelona, Clooney |
|
If you have been in Barcelona you will for sure remember Las Ramblas, one of the most popular tourist attractions in town. Seems that George Clooney has been in Barcelona too and was so impressed by this area that has decided to open a hotel and casino in Las Vegas called, guess it, Las Ramblas. A nice idea from a fine actor and director (have you seen Good Night, and Good Luck?). Todays example continues with the O/D matrix manipulations. This time we will import, export and copy a matrix to Excel. OutThe code that we will present is more on Pythons file management than on AIMSUN Scripting. Basically you can adapt the example from the last column (Script Doctor 2) and add this code: The code writes all the trips in a matrix in a three columns ASCII file. The first column has the origin centroid identifier, the second the destination centroid identifier and the last one the trips. You can also write, before the trips, any extra information as, for example, the vehicle type: or the initial time and duration: You can put all the code in a function and export more than one matrix at the same time. In this case you will open the file before calling these functions and you will close it after exporting all the matrices. For example, to export all the matrices in a Traffic Demand: or all the matrices in a Centroid Configuration: You can change the format from a list of trips to a matrix, you can add more information, etc. InWe will also code functions to read back matrices as exported from the previous example. We will only present the more relevant part of the code. Look in the References for the complete script. The reader function will be programmed as a state machine with five states. In the first state it looks for the matrix name, in the second one looks for the vehicle name, then for the initial time, then for the duration and then for all the trips until it founds an empty line. Then it goes back to the first state, looking for a new matrix. The matrix is located using the id. If the id does not correspond to an existing matrix, a new matrix is created. Note that, in this case, the matrix id will be different from the specified in the file. The more tricky part is the split of a line in different parts. To split the line with the identifier and name we look for the first space (line.index(' ')) and then we take the initial part as the identifier (int( line[:line.index(' ')] )) and the remain as the name (line[line.index(' ')+1:]). Python includes a nice function to split a line using a token (line.split( " " )) that can be used to separate in three strings the trips line but cannot be used for the identifier and name lanes, as the separator, is a space and the name can contains spaces too. Note the use of int and float methods to convert a string to a number (int( line[:line.index(' ')] ) and trips = float(entry) ). ExcelWhat makes Python great is not only the quality as a programming language but also the big catalog of solutions that are available: from numerical manipulation to database access. One nice available module allows Python to talk with Windows applications that support COM as Microsoft Excel, its called PyWin32. Using PyWin32 we can launch Excel and write a matrix into it. The code is: References |