croposplot.py 8.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
# this is <croposplot.py>
# ----------------------------------------------------------------------------
# 
# Copyright (c) 2019 by Thomas Forbriger (BFO Schiltach) 
# 
# create graphical diagram from output of croposp
#
# ----
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ----
#
# REVISIONS and CHANGES 
#    31/01/2019   V1.0   Thomas Forbriger
26
#    14/02/2019   V1.1   first version providing reasonable functionality
27
28
# 
# ============================================================================
29
__version__ = "croposplot 2019-02-15 V1.2"
30
31
32
33
34
35
36
37
38
39
40
41
42
43
__author__ = "Thomas Forbriger"
# ----------------------------------------------------------------------------
# backend
# -------
# For screen-plots an appropriate backend must be chosen. This should be done
# by an entry in ~/.config/matplotlib/matplotlibrc like
# backend : qt5agg
# 
# Alternatively the first two statements may be set to:
#
#   import matplotlib
#    matplotlib.use('qt5agg')
#
# However, the backend then would be hardcoded, which is not desired.
44
#
45
46
47
48
49
50
# ----------------------------------------------------------------------------

import sys
import os
import getopt
import string
51
import re
52
53
54
55
56
57
58
59
60
import numpy as np
import matplotlib.pyplot as plt

# ============================================================================
def usage():
  """
  print usage information
  """
  print(__version__)
61
62
  print('Usage: '+sys.argv[0]+' [-v]')
  print('       [-o file] [-g|--grid] [--nologlog] [--nologx] [--nology]')
63
  print('       [--legfontsize s] [--legpos p] [--title t]')
64
65
  print('       [--xlabel l] [--ylabel l] [--titfontsize t]')
  print('       [--xlim l] [--ylim l]')
66
  print('       [--usemarkers] [--match re]')
67
  print('       file [file [file ...]]')
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  print('   or: '+sys.argv[0]+' --help|-h')


# ----------------------------------------------------------------------------
def help():
  """
  print online help
  """
  usage()
  print(
  """
  Read multicolumn output files of spectral values as written by croposp and
  create graphical diagrams.

82
83
84
85
  file ...        multicolumn spectral data output of croposp
  -v              be verbose
  -o file         write to file
  --xlabel l      set label "l" on x-axis
86
  --ylabel l      set label "l" on y-axis
87
88
89
90
  -g|--grid       plot grid
  --nologlog      plot on linear scales
  --nologx        use linear scale for x-axis
  --nology        use linear scale for y-axis
91
  --usemarkers    use graph markers
92
93
  --legfontsize s set font size for legend
  --legpos p      set position of legend
94
  --title t       set plot title
95
96
97
  --titfontsize s set font size for legend
  --xlim min:max  set x-axis limits
  --ylim min:max  set y-axis limits
98
99
  --match re      only display curve for which the label matches the regular
                  expression "re"
100
101
  """)

102
103
104
105
106
107
108
109
110
# -----------------------------------------------------------------------------
class Error(Exception):

  def __init__(self, msg=""):
    self.msg = str(msg)

  def display(self):
    sys.stderr.write("croposplot ERROR: " + self.msg + "\n")

111
112
113
114
115
116
117
118
119
120
121
# ============================================================================

def main(argv=None):
  """
  main body of program
  evaluate command line options and control processing
  """
  if argv is None:
    argv=sys.argv

  try:
122
    opts, args=getopt.getopt(sys.argv[1:], 'hvo:g', ['help', 'nologlog',
123
    'nologx', 'nology', 'grid', 'xlabel=', 'legfontsize=', 'title=',
124
    'legpos=', 'titfontsize=', 'ylabel=', 'xlim=', 'ylim=',
125
    'usemarkers', 'match='])
126
127
128
129
130
131
132
133
  except getopt.GetoptError as err:
    print(err.msg)
    exit(0)

  verbose=False
  global DEBUG
  DEBUG=False
  outfile='x11'
134
135
136
137
  nologlog = False
  nologx = False
  nology = False
  grid = False
138
  xlabel = 'frequency / Hz'
139
  ylabel = None
140
  opt_legendfontsize='xx-small'
141
  opt_titlefontsize='large'
142
  opt_title = None
143
  opt_legposition = 'best'
144
145
  opt_xlim = None
  opt_ylim = None
146
  opt_usemarkers = False
147
  opt_match = None
148
149
150
151
152
153
154
155

  for (opt, arg) in opts:
    if opt == '-v':
      verbose=True
    elif opt == '-D':
      DEBUG=True
    elif opt == '-o':
      outfile=arg
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    elif opt in ("--xlim"):
      try:
        opt_xlim = [float(x) for x in arg.split(":")]
        if 2 != len(opt_xlim):
          raise
      except:
        raise Error("Invalid 'xlim' argument.")
    elif opt in ("--ylim"):
      try:
        opt_ylim = [float(y) for y in arg.split(":")]
        if 2 != len(opt_ylim):
          raise
      except:
        raise Error("Invalid 'ylim' argument.")
170
171
    elif opt in ('--match'):
      opt_match=arg
172
173
    elif opt in ('--xlabel'):
      xlabel=arg
174
175
    elif opt in ('--ylabel'):
      ylabel=arg
176
    elif opt in ('--legfontsize'):
177
      opt_legendfontsize=arg
178
179
    elif opt in ('--titfontsize'):
      opt_titlefontsize=arg
180
181
    elif opt in ('--legpos'):
      opt_legposition=arg
182
183
    elif opt in ('--title'):
      opt_title=arg
184
185
186
187
188
189
190
191
    elif opt in ("-g", "--grid"):
      grid = True
    elif opt in ("--nologlog"):
      nologlog = True
    elif opt in ("--nologx"):
      nologx = True
    elif opt in ("--nology"):
      nology = True
192
193
    elif opt in ("--usemarkers"):
      opt_usemarkers = True
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    elif (opt == '-h') or (opt == '--help'):
      help()
      exit(0)
    else :
      usage()
      exit(0)

  if DEBUG:
    print(opts)
    print(args)

  if len(args) < 1 and len(opts) < 1:
    usage()
    exit(0)

  print(__version__)
  print("Read croposp output files and create diagram")

  Figure=plt.figure()

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  plt.grid(grid)
  # using logscale if desired
  if nologx and nology:
    nologlog = True

  if nology and not nologlog:
    if verbose:
      sys.stdout.write("specplot: Using log scaling for x-axis ... \n")
    plt.semilogx()
  elif nologx and not nologlog:
    if verbose:
      sys.stdout.write("specplot: Using log scaling for y-axis ... \n")
    plt.semilogy()
  elif not nologlog:
    if verbose:
      sys.stdout.write("specplot: Using loglog scaling  ... \n")
    plt.loglog()
  else:
    if verbose:
      sys.stdout.write("specplot: Disable logscale ... \n")

235
  plt.xlabel(xlabel)
236
237
238
239
240
241
  if ylabel is not None:
    plt.ylabel(ylabel)
  if opt_xlim is not None:
    plt.xlim(opt_xlim[0], opt_xlim[1]) 
  if opt_ylim is not None:
    plt.ylim(opt_ylim[0], opt_ylim[1]) 
242

243
# support curve style cycling
244
  mymarkers=['o','*','p','v','<','>','d','D','.']
245
246
247
248
249
250
251
252
253
254
255
  mycolors=['#000000',
      '#0000ff',
      '#ff0000',
      '#ff00ff',
      '#555555',
      '#880000',
      '#999999',
      '#22ffff',
      '#007777',
      '#ffff22',
      '#997700']
256
257
  icurve=0

258
259
260
  for specfile in args:
    if verbose:
      print('read file %s' % specfile)
261
262
263
264
# read trace labels
    labellines=list(filter((lambda x: re.match('^# #',x)), 
      open(specfile).readlines()))
# read trace data
265
    data=np.loadtxt(specfile, unpack=False)
266
    for i in range(1,len(data[0,:])):
267
      label=re.sub('^# #\d+: ', '', labellines[i-1].strip())
268
269
270
271
272
273
274
275
276
      match=True
      if opt_match is not None:
        match=(re.search(opt_match, label) is not None)
      if match:
        if verbose:
          print('plot curve %d: %s' % (i, label))
        themarker=None
        if opt_usemarkers:
          themarker=mymarkers[icurve % len(mymarkers)]
277
278
279
        thecolor=mycolors[icurve % len(mycolors)]
        plt.plot(data[:,0], data[:,i], label=label,
            color=thecolor, marker=themarker)
280
281
        plt.legend(fontsize=opt_legendfontsize, loc=opt_legposition)
        icurve=1+icurve
282

283
    if opt_title is not None:
284
      plt.title(re.sub('\\\\n','\n',opt_title), fontsize=opt_titlefontsize)
285

286
287
288
289
290
291
292
293
294
295
  if outfile=='x11':
    plt.show(True)
  else:
    plt.savefig(outfile, papertype='a4', orientation='landscape')

  return 0

# ============================================================================
if __name__ == '__main__':
  sys.exit(main())
296
297

# ----- END OF croposplot.py -----