#!/usr/bin/env python ############################################################################ # # MODULE: v.in.marxan for GRASS 6.4 (2010-11-23) # # AUTHOR(S): Trevor Wiens # # PURPOSE: Updates Marxan best model and frequency columns from Marxan run ouptut files. # # REQUIREMENTS: PostgreSQL 8.x or above, Marxan 1.8.10 # # COPYRIGHT: (C) Trevor Wiens, 2010 GRASS Development Team # # This program is free software under the GNU General Public # License (>=v2). Read the file COPYING that comes with GRASS # for details. # # EXAMPLE: # v.in.marxan best_input=scenario001_best.dat ssoln_input=scenario001_ssoln.dat update_vector=test@PERMANENT key_field=cat # ############################################################################# #%Module #% description: Adds and populates columns of vector necessary for use of other GRASS Marxan modules #% keywords: vector #% keywords: marxan #% keywords: attribute table #%End #%option #% key: best_input #% type: string #% gisprompt: old_file,file,output #% answer: scenario001_best.dat #% label: 'Best' Solution file #% description: File for lowest cost solution #% required : yes #%end #%option #% key: ssoln_input #% type: string #% gisprompt: old_file,file,output #% answer: scenario001_ssoln.dat #% label: Frequency Solution File #% description: File with frequency of selection #% required : yes #%end #%option #% key: update_vector #% type: string #% gisprompt: old,vector,vector #% label: Planning Unit Vector Layer #% description: Layer containing planning units used in Marxan run. #% required: yes #%end #%option #% key: key_field #% type: string #% label: Planning Unit Key or ID Field #% description: Name of key field for planning unit #% answer: cat #% required: yes #%end import sys import os import subprocess import time import csv from grass.script import core as grass from grass.script import vector as gvect def main(): # record start time starttime = time.localtime() # extract sql table name table_name=gvect.vector_db(options['update_vector'])[1]['table'] # retrieve file information for best run tmpf = file(options['best_input'], 'rU') bestlist = tmpf.read().splitlines() beststr = ','.join(bestlist) tmpf.close() # create query qtext = 'Update %s set mxn_best = 0, mxn_freq = 0;' % table_name qtext = qtext + 'Update %s set mxn_best = 1 where %s in (%s);' % (table_name, options['key_field'], beststr) # update best run infomation errpipe = subprocess.PIPE outpipe = subprocess.PIPE try: r = grass.write_command('db.execute', stdin = qtext, stdout = outpipe, stderr = errpipe) if r <> 0: raise grass.message('Best run information updated') except: msgtext="An error occured when updating the best run information in the vector table. \n" + \ "Please review query and table structure for possible conflicts \n" + qtext grass.message(msgtext, flag='e') return(-1) # retrieve file information for frequency tmpf = file(options['ssoln_input'], 'rU') ssoln = tmpf.read().splitlines() # create query qtext = '' for i in range(0,len(ssoln)): cline = ssoln[i].split(' ') if int(cline[1]) > 0: qtext = qtext + \ 'Update %s set mxn_freq = %s where %s = %s;' % (table_name, cline[1], options['key_field'], cline[0]) #update frequency selection informaiton try: r = grass.write_command('db.execute', stdin = qtext, stdout = outpipe, stderr = errpipe) if r <> 0: raise grass.message('Frequency information updated') except: msgtext="An error occured when updating the frequency information in the vector table. \n" + \ "Please review query and table structure for possible conflicts \n" + qtext grass.message(msgtext, flag='e') return(-1) endtime = time.localtime() msgtext ='Done!\nStarted: '+ time.strftime("%Y.%m.%d %H:%M:%S",starttime) + \ '\nFinished:'+ time.strftime("%Y.%m.%d %H:%M:%S",endtime) grass.message(msgtext) return(0) if __name__ == "__main__": options, flags = grass.parser() sys.exit(main())