3
############################################################################
6
# AUTHOR(S): Markus Neteler, 18. August 1998
7
# Converted to Python by Glynn Clements
8
# PURPOSE: Displays spectral response at user specified locations in
9
# group or raster images
10
# COPYRIGHT: (C) 1999-2013 by the GRASS Development Team
12
# This program is free software under the GNU General Public
13
# License (>=v2). Read the file COPYING that comes with GRASS
16
#############################################################################
18
# this script needs gnuplot for pretty rendering
19
# TODO: use PyPlot like the wxGUI Profiling tool
21
# written by Markus Neteler 18. August 1998
22
# neteler geog.uni-hannover.de
24
# bugfix: 25. Nov.98/20. Jan. 1999
25
# 3 March 2006: Added multiple images and group support by Francesco Pirotti - CIRGEO
29
#% description: Displays spectral response at user specified locations in group or images.
33
#% keyword: multispectral
35
#%option G_OPT_I_GROUP
39
#%option G_OPT_R_INPUTS
44
#%option G_OPT_M_COORDS
49
#%option G_OPT_F_OUTPUT
51
#% description: Name for output image
58
#% description: Graphics format for output file
59
#% options: png,eps,svg
66
#% description: Show sampling coordinates instead of numbering in the legend
70
#% description: Use gnuplot for display
75
from grass.script.utils import try_rmdir
76
from grass.script import core as grass
83
def draw_gnuplot(what, xlabels, output, img_format, coord_legend):
86
for i, row in enumerate(what):
87
outfile = os.path.join(tmp_dir, 'data_%d' % i)
88
outf = open(outfile, 'w')
89
xrange = max(xrange, len(row) - 2)
90
for j, val in enumerate(row[3:]):
91
outf.write("%d %s\n" % (j + 1, val))
94
# build gnuplot script
97
if img_format == 'png':
98
term_opts = "png truecolor large size 825,550"
99
elif img_format == 'eps':
100
term_opts = "postscript eps color solid size 6,4"
101
elif img_format == 'svg':
102
term_opts = "svg size 825,550 dynamic solid"
104
grass.fatal(_("Programmer error (%s)") % img_format)
107
"set term " + term_opts,
108
"set output '%s'" % output
112
"set xtics (%s)" % xlabels,
114
"set title 'Spectral signatures'",
115
"set xrange [0.5 : %d - 0.5]" % xrange,
117
"set xlabel 'Bands'",
118
"set xtics rotate by -40",
119
"set ylabel 'DN Value'",
120
"set style data lines"
124
for i, row in enumerate(what):
126
title = 'Pick ' + str(i + 1)
128
title = str(tuple(row[0:2]))
130
x_datafile = os.path.join(tmp_dir, 'data_%d' % i)
131
cmd.append(" '%s' title '%s'" % (x_datafile, title))
134
cmd = ' '.join(['plot', cmd, "with linespoints pt 779"])
137
plotfile = os.path.join(tmp_dir, 'spectrum.gnuplot')
138
plotf = open(plotfile, 'w')
140
plotf.write(line + '\n')
144
grass.call(['gnuplot', plotfile])
146
grass.call(['gnuplot', '-persist', plotfile])
149
def draw_linegraph(what):
152
xfile = os.path.join(tmp_dir, 'data_x')
154
xf = open(xfile, 'w')
155
for j, val in enumerate(what[0][3:]):
156
xf.write("%d\n" % (j + 1))
159
for i, row in enumerate(what):
160
yfile = os.path.join(tmp_dir, 'data_y_%d' % i)
161
yf = open(yfile, 'w')
162
for j, val in enumerate(row[3:]):
163
yf.write("%s\n" % val)
167
sienna = '#%02x%02x%02x' % (160, 82, 45)
168
coral = '#%02x%02x%02x' % (255, 127, 80)
169
gp_colors = ['red', 'green', 'blue', 'magenta', 'cyan', sienna, 'orange',
173
while len(what) > len(colors):
175
colors = colors[0:len(what)]
177
grass.run_command('d.linegraph', x_file=xfile, y_file=yfiles,
178
y_color=colors, title='Spectral signatures',
179
x_title='Bands', y_title='DN Value')
183
group = options['group']
184
raster = options['raster']
185
output = options['output']
186
coords = options['coordinates']
187
img_fmt = options['format']
188
coord_legend = flags['c']
192
tmp_dir = grass.tempdir()
194
if not group and not raster:
195
grass.fatal(_("Either group= or raster= is required"))
198
grass.fatal(_("group= and raster= are mutually exclusive"))
200
# check if gnuplot is present
201
if gnuplot and not grass.find_program('gnuplot', '-V'):
202
grass.fatal(_("gnuplot required, please install first"))
204
# get data from group listing and set the x-axis labels
206
# Parse the group list output
207
s = grass.read_command('i.group', flags='g', group=group, quiet=True)
208
rastermaps = s.splitlines()
210
# get data from list of files and set the x-axis labels
211
rastermaps = raster.split(',')
213
xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)]
214
xlabels = ','.join(xlabels)
216
# get y-data for gnuplot-data file
218
s = grass.read_command('r.what', map=rastermaps, coordinates=coords,
219
null='0', quiet=True)
221
grass.fatal(_('No data returned from query'))
223
for l in s.splitlines():
225
for i, v in enumerate(f):
234
draw_gnuplot(what, xlabels, output, img_fmt, coord_legend)
238
if __name__ == "__main__":
239
options, flags = grass.parser()
240
atexit.register(cleanup)