[GRASS-SVN] r44442 - in grass-addons/vector: . v.in.marxan

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Nov 26 17:22:05 EST 2010


Author: tsw
Date: 2010-11-26 14:22:05 -0800 (Fri, 26 Nov 2010)
New Revision: 44442

Added:
   grass-addons/vector/v.in.marxan/
   grass-addons/vector/v.in.marxan/Makefile
   grass-addons/vector/v.in.marxan/description.html
   grass-addons/vector/v.in.marxan/v.in.marxan
Log:
New script for importing Marxan output for display

Added: grass-addons/vector/v.in.marxan/Makefile
===================================================================
--- grass-addons/vector/v.in.marxan/Makefile	                        (rev 0)
+++ grass-addons/vector/v.in.marxan/Makefile	2010-11-26 22:22:05 UTC (rev 44442)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.in.marxan
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/vector/v.in.marxan/description.html
===================================================================
--- grass-addons/vector/v.in.marxan/description.html	                        (rev 0)
+++ grass-addons/vector/v.in.marxan/description.html	2010-11-26 22:22:05 UTC (rev 44442)
@@ -0,0 +1,32 @@
+<H2>DESCRIPTION</H2>
+
+<EM>v.in.marxan</EM> is a python script reads output from marxan and updates the planning grid vector file 
+prepared using <EM>v.out.marxan</EM>. 
+
+<H2>NOTES</H2>
+
+Use of the DOS output format will create a second file with '.dos' appended to the output file name.  
+Consistent specification of the Planning Unit Key field is necessary in all Marxan scripts to ensure a working set of ouptut files. 
+All GRASS Marxan modules require Python, PostgreSQL 8.x or higher and are intended for use with Marxan version 1.8.10. 
+
+<h2>SEE ALSO</h2>
+
+<em><a HREF="v.out.marxan.html">v.out.marxan</a>,
+<a HREF="v.mkhexgrid">v.mkhexgrid</a>,
+</em>
+<p><a HREF="http://www.uq.edu.au/marxan/">Marxan Home Page</a></p>
+
+<H2>AUTHOR</H2>
+Trevor Wiens
+
+<H2>REFERENCES</H2>
+
+<p><a href="http://www.uq.edu.au/marxan/docs/marxan_manual_1_8_2.pdf" target="_blank">Ball, I.R., and H.P. Possingham, 2000. MARXAN (V1.8.2): Marine Reserve Design Using Spatially Explicit Annealing, a Manual.</a></p>
+
+<p><a href="http://www.uq.edu.au/marxan/docs/Marxan_User_Manual_2008.pdf" target="_blank">Game, E.T. and H.S. Grantham, 2008. Marxan User Manual: For Marxan version 1.8.10. University of Queensland, St. Lucia, Queensland, Australia, and Pacific Marine Analysis and Research Association, Vancouver, British Columbia, Canada.</a></p>
+
+<p><a href="http://www.uq.edu.au/marxan/docs/Possingham_etal_2000_Mathematical.pdf" target="_blank">Possingham, H. P., I. R. Ball and S. Andelman (2000) Mathematical methods for identifying representative reserve networks. In: S. Ferson and M. Burgman (eds) Quantitative methods for conservation biology. Springer-Verlag, New York, pp. 291-305.</a></p>
+
+<p><a href="http://www.uq.edu.au/marxan/docs/Marxan%20Good%20Practices%20Handbook%20v2%202010.pdf" target="_blank">Ardron, J. H.P. Possingham and C.J. Klein (Eds.),Version 2, 2010. Marxan good practices handbook. University of Queensland, St. Lucia, Queensland, Australia, and Pacific Marine Analysis and Research Association, Vancouver, British Columbia, Canada.</a> </p>
+
+<p><i>Last changed: $Date: 2010-11-24 $</i>

Added: grass-addons/vector/v.in.marxan/v.in.marxan
===================================================================
--- grass-addons/vector/v.in.marxan/v.in.marxan	                        (rev 0)
+++ grass-addons/vector/v.in.marxan/v.in.marxan	2010-11-26 22:22:05 UTC (rev 44442)
@@ -0,0 +1,129 @@
+#!/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 at 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())


Property changes on: grass-addons/vector/v.in.marxan/v.in.marxan
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list