Python script for Vortex

Vortex is used on the Conservation Biology MSc course at UCT to simulate the extinction process of a species stochastically. Its user interface in managing/renaming different scenarios isn’t great, and I was a bit lazy and ended up with a single vortex project file containing a huge number of scenarios that were redundant for my project. I certainly wasn’t going to create them all again from scratch, so I manually removed the chunks in my .vpj pertaining to the simulations I no longer wanted, and wrote a quick script which would fix all of the numbering and references throughout the .vpj program using Python and regex. I hope this will help someone else:

import re
btm = open('C:/yourpathto/vortexproject.vpj', 'r')
i = 0
outputdata = []

# Iterate through all of the scenario notes, keeping track of what number we are on and changing numbers as necessary
for line in btm:
 match = re.match('^\[(\d\d?\d?) Scenario Notes\]$', line)
 if match:
   i = i + 1
 match = re.search('^\[\d\d?\d?\d? ', line)
 if match:
   line = re.sub('^\[\d\d?\d?\d? ','[' + str(i) + ' ', line)
 outputdata.append(line)

# Create a new file to output the data
outputfile = open('C:/replacementvortexproject.vpj', 'w')
# Loop through each line in outputdata and add it to the new file
for line in outputdata:
  outputfile.write(line.lstrip('\r\n'))
# Close the file and print to screen to know the program has ended
btm.close()
outputfile.close()
print 'Finished'

Leave a Reply

Your email address will not be published. Required fields are marked *