[GRASS-SVN] r33741 - grass/trunk/scripts/i.spectral

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Oct 7 20:02:06 EDT 2008


Author: glynn
Date: 2008-10-07 20:02:05 -0400 (Tue, 07 Oct 2008)
New Revision: 33741

Modified:
   grass/trunk/scripts/i.spectral/i.spectral.py
Log:
Allow use of d.linegraph instead of gnuplot


Modified: grass/trunk/scripts/i.spectral/i.spectral.py
===================================================================
--- grass/trunk/scripts/i.spectral/i.spectral.py	2008-10-07 22:48:46 UTC (rev 33740)
+++ grass/trunk/scripts/i.spectral/i.spectral.py	2008-10-08 00:02:05 UTC (rev 33741)
@@ -60,6 +60,10 @@
 #%key: c
 #%description: label with coordinates instead of numbering
 #%end
+#%flag 
+#%key: g
+#%description: use gnuplot for display
+#%end
 
 import sys
 import os
@@ -73,12 +77,101 @@
 	if name[5:].isdigit():
 	   grass.try_remove(name)
 
+    grass.try_remove('data_x')
+    for name in glob.glob('data_y_[0-9]*'):
+	if name[7:].isdigit():
+	   grass.try_remove(name)
+
+def draw_gnuplot(what, xlabels, output, label):
+    xrange = 0
+
+    for i, row in enumerate(what):
+	outfile = 'data_%d' % i
+	outf = file(outfile, 'w')
+	xrange = max(xrange, len(row) - 2)
+	for j, val in enumerate(row[3:]):
+	    outf.write("%d %s\n" % (j + 1, val))
+	outf.close()
+
+    # build gnuplot script
+    lines = []
+    if output:
+	lines += [
+	    "set term png large",
+	    "set output '%s'" % output
+	    ]
+    lines += [
+	"set xtics (%s)" % xlabels,
+	"set grid",
+	"set title 'Spectral signatures'",
+	"set xrange [0:%d]" % xrange,
+	"set noclabel",
+	"set xlabel 'Bands'",
+	"set ylabel 'DN Value'",
+	"set data style lines"
+	]
+
+
+    cmd = []
+    for i, row in enumerate(what):
+	if not label:
+	    title = str(i + 1)
+	else:
+	    title = str(tuple(row[0:2]))
+	cmd.append("'data_%d' title '%s'" % (i, title))
+    cmd = ','.join(cmd)
+    cmd = ' '.join(['plot', cmd, "with linespoints pt 779"])
+    lines.append(cmd)
+
+    plotfile = 'spectrum.gnuplot'
+    plotf = file(plotfile, 'w')
+    for line in lines:
+	plotf.write(line + '\n')
+    plotf.close()
+
+    if output:
+	grass.call(['gnuplot', plotfile])
+    else:
+	grass.call(['gnuplot', '-persist', plotfile])
+
+def draw_linegraph(what):
+    yfiles = []
+
+    xfile = 'data_x'
+    xf = file(xfile, 'w')
+    for j, val in enumerate(what[0][3:]):
+	xf.write("%d\n" % (j + 1))
+    xf.close()
+
+    for i, row in enumerate(what):
+	yfile = 'data_y_%d' % i
+	yf = file(yfile, 'w')
+	for j, val in enumerate(row[3:]):
+	    yf.write("%s\n" % val)
+	yf.close()
+	yfiles.append(yfile)
+
+    sienna = '#%02x%02x%02x' % (160,  82,  45)
+    coral  = '#%02x%02x%02x' % (255, 127,  80)
+    gp_colors = ['red', 'green', 'blue', 'magenta', 'cyan', sienna, 'orange', coral]
+
+    colors = gp_colors
+    while len(what) > len(colors):
+	colors += gp_colors
+    colors = colors[0:len(what)]
+
+    grass.run_command('d.linegraph', x_file = xfile, y_file = yfiles,
+		      y_color = colors, title = 'Spectral signatures',
+		      x_title = 'Bands', y_title = 'DN Value')
+
+
 def main():
     group = options['group']
     raster = options['raster']
     output = options['output']
     coords = options['coords']
     label = flags['c']
+    gnuplot = flags['g']
 
     if not group and not raster:
 	grass.fatal("Either group= or raster= is required")
@@ -87,7 +180,7 @@
 	grass.fatal("group= and raster= are mutually exclusive")
 
     #check if present
-    if not grass.find_program('gnuplot', ['-V']):
+    if gnuplot and not grass.find_program('gnuplot', ['-V']):
 	grass.fatal("gnuplot required, please install first")
 
     tmp1 = grass.tempfile()
@@ -114,7 +207,7 @@
 	# ## get data from list of files and set the x-axis labels
 	rastermaps = raster.split(',')
 
-    xlabels = ["'%s' %d" % (n, i) for i, n in enumerate(rastermaps)]
+    xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)]
     xlabels = ','.join(xlabels)
     numbands = len(rastermaps)
 
@@ -129,61 +222,11 @@
 		f[i] = float(v)
 	what.append(f)
 
-    numclicks = len(what)
-    start = 0
-
-    num = numbands
-
     # build data files
-    for i, row in enumerate(what):
-	outfile = 'data_%d' % i
-	outf = file(outfile, 'w')
-	for j, val in enumerate(row[3:]):
-	    outf.write("%s %s\n" % (j, val))
-	outf.close()
-
-    xrange = numbands + 1
-
-    # build gnuplot script
-    lines = []
-    if output:
-	lines += [
-	    "set term png large",
-	    "set output '%s'" % output
-	    ]
-    lines += [
-	"set xtics (%s)" % xlabels,
-	"set grid",
-	"set title 'Spectral signatures'",
-	"set xrange [0:%s]" % xrange,
-	"set noclabel",
-	"set xlabel 'Bands'",
-	"set ylabel 'DN Value'",
-	"set data style lines"
-	]
-
-
-    cmd = []
-    for i, row in enumerate(what):
-	if not label:
-	    title = str(i)
-	else:
-	    title = str(tuple(row[0:2]))
-	cmd.append("'data_%d' title '%s'" % (i, title))
-    cmd = ','.join(cmd)
-    cmd = ' '.join(['plot', cmd, "with linespoints pt 779"])
-    lines.append(cmd)
-
-    plotfile = 'spectrum.gnuplot'
-    plotf = file(plotfile, 'w')
-    for line in lines:
-	plotf.write(line + '\n')
-    plotf.close()
-
-    if output:
-	grass.call(['gnuplot', plotfile])
+    if gnuplot:
+	draw_gnuplot(what, xlabels, output, label)
     else:
-	grass.call(['gnuplot', '-persist', plotfile])
+	draw_linegraph(what)
 
 if __name__ == "__main__":
     options, flags = grass.parser()



More information about the grass-commit mailing list