source: issm/oecreview/Archive/13393-13976/ISSM-13410-13411.diff@ 13980

Last change on this file since 13980 was 13980, checked in by Mathieu Morlighem, 12 years ago

preparing oecreview for 13393-13976'

File size: 9.5 KB
RevLine 
[13980]1Index: ../trunk-jpl/src/m/classes/plotoptions.py
2===================================================================
3--- ../trunk-jpl/src/m/classes/plotoptions.py (revision 0)
4+++ ../trunk-jpl/src/m/classes/plotoptions.py (revision 13411)
5@@ -0,0 +1,125 @@
6+from collections import OrderedDict, Counter, defaultdict
7+from pairoptions import *
8+
9+class plotoptions(object):
10+ '''
11+ PLOTOPTIONS class definition
12+
13+ Usage:
14+ plotoptions=plotoptions(*arg)
15+ '''
16+
17+ def __init__(self,*arg):
18+ self.numberofplots = 0
19+ self.figurenumber = 1
20+ self.list = OrderedDict()
21+
22+ self.buildlist(*arg)
23+
24+ def __repr__(self): #{{{
25+ s="\n"
26+ s+=" numberofplots: %i\n" % self.numberofplots
27+ s+=" figurenumber: %i\n" % self.figurenumber
28+ if self.list:
29+ s+=" list: (%ix%i)\n" % (len(self.list),2)
30+ for item in self.list.iteritems():
31+ #s+=" options of plot number %i\n" % item
32+ if isinstance(item[1],(str,unicode)):
33+ s+=" field: %-10s value: '%s'\n" % (item[0],item[1])
34+ elif isinstance(item[1],(bool,int,long,float)):
35+ s+=" field: %-10s value: '%g'\n" % (item[0],item[1])
36+ else:
37+ s+=" field: %-10s value: '%s'\n" % (item[0],item[1])
38+ else:
39+ s+=" list: empty\n"
40+ return s
41+ #}}}
42+ def buildlist(self,*arg): #{{{
43+ #check length of input
44+ if len(arg) % 2:
45+ raise TypeError('error: an even number of options is required')
46+
47+ #go through args and build list (like pairoptions)
48+ rawoptions=pairoptions(*arg)
49+ numoptions=len(arg)/2
50+ rawlist=[] # cannot be a dict since they do not support duplicate keys
51+
52+ for i in xrange(numoptions):
53+ if isinstance(arg[2*i],(str,unicode)):
54+ rawlist.append([arg[2*i],arg[2*i+1]])
55+ else:
56+ #option is not a string, ignore it
57+ print "WARNING: option number %d is not a string and will be ignored." % (i+1)
58+
59+ #get figure number
60+ self.figurenumber=rawoptions.getfieldvalue('figure',1)
61+
62+ #get number of subplots
63+ numberofplots=Counter(x for sublist in rawlist for x in sublist if isinstance(x,(str,unicode)))['data']
64+ self.numberofplots=numberofplots
65+
66+ #figure out whether alloptions flag is on
67+ if rawoptions.getfieldvalue('alloptions','off') is 'on':
68+ allflag=1
69+ else:
70+ allflag=0
71+
72+ #initialize self.list (will need a list of dict's (or nested dict) for numberofplots>1)
73+ #self.list=defaultdict(dict)
74+ for i in xrange(numberofplots):
75+ self.list[i]=pairoptions()
76+
77+ #process plot options
78+ for i in xrange(len(rawlist)):
79+
80+ #if alloptions flag is on, apply to all plots
81+ if (allflag and 'data' not in rawlist[i][0] and '#' not in rawlist[i][0]):
82+
83+ for j in xrange(numberofplots):
84+ self.list[j].addfield(rawlist[i][0],rawlist[i][1])
85+
86+ elif '#' in rawlist[i][0]:
87+
88+ #get subplots associated
89+ string=rawlist[i][0].split('#')
90+ plotnums=string[-1].split(',')
91+ field=string[0]
92+
93+ #loop over plotnums
94+ for k in xrange(len(plotnums)):
95+ plotnum=plotnums[k]
96+
97+ #Empty
98+ if not plotnum: continue
99+
100+ elif 'all' in plotnum:
101+ for j in xrange(numberofplots):
102+ self.list[j].addfield(field,rawlist[i][1])
103+
104+ elif '-' in plotnum:
105+ nums=plotnum.split('-')
106+ if len(nums)!=2: continue
107+ if False in [x.isdigit() for x in nums]:
108+ raise ValueError('error: in option i-j both i and j must be integers')
109+ for j in xrange(int(nums[0])-1,int(nums[1])):
110+ self.list[j].addfield(field,rawlist[i][1])
111+
112+ # Deal with #i
113+ else:
114+ #assign to subplot
115+ if int(plotnum)>numberofplots:
116+ raise ValueError('error: %s cannot be assigned %d which exceeds the number of subplots' % (field,plotnum))
117+ self.list[int(plotnum)-1].addfield(field,rawlist[i][1])
118+ else:
119+
120+ #go through all subplots and assign key-value pairs
121+ j=0
122+ while j <= numberofplots-1:
123+ if not self.list[j].exist(rawlist[i][0]):
124+ self.list[j].addfield(rawlist[i][0],rawlist[i][1])
125+ break
126+ else:
127+ j=j+1
128+ if j+1>numberofplots:
129+ print "WARNING: too many instances of '%s' in options" % rawlist[i][0]
130+ #}}}
131Index: ../trunk-jpl/src/m/classes/pairoptions.py
132===================================================================
133--- ../trunk-jpl/src/m/classes/pairoptions.py (revision 13410)
134+++ ../trunk-jpl/src/m/classes/pairoptions.py (revision 13411)
135@@ -40,7 +40,7 @@
136 self.list[arg[2*i]] = arg[2*i+1];
137 else:
138 #option is not a string, ignore it
139- print "WARNING: option number %d '%s' is not a string and will be ignored." % (i+1,type(arg[2*i]))
140+ print "WARNING: option number %d is not a string and will be ignored." % (i+1)
141 # }}}
142
143 def addfield(self,field,value): # {{{
144@@ -127,18 +127,20 @@
145 return False
146 # }}}
147
148-# function num = fieldoccurences(obj,field), % {{{
149-# %FIELDOCCURENCES - get number of occurence of a field
150-#
151-# %check input
152-# if ~ischar(field),
153-# error('fieldoccurences error message: field should be a string');
154-# end
155-#
156-# %get number of occurence
157-# num=sum(strcmpi(field,obj.list(:,1)));
158-# end % }}}
159+ #def fieldoccurences(self,field): #{{{
160+ # '''
161+ # FIELDOCCURENCES - get number of occurence of a field
162+ # '''
163+ #
164+ # #check input
165+ # if not isinstance(field,(str,unicode)):
166+ # raise TypeError("fieldoccurences error message: field should be a string")
167
168+ # #get number of occurence
169+ # # ??
170+ # #return num
171+ # #% }}}
172+
173 def getfieldvalue(self,field,default=None): # {{{
174 """
175 GETOPTION - get the value of an option
176Index: ../trunk-jpl/src/m/plot/plot_manager.py
177===================================================================
178--- ../trunk-jpl/src/m/plot/plot_manager.py (revision 0)
179+++ ../trunk-jpl/src/m/plot/plot_manager.py (revision 13411)
180@@ -0,0 +1,32 @@
181+from pairoptions import *
182+import pylab as p
183+from checkplotoptions import *
184+from plot_mesh import *
185+
186+def plot_manager(md,options,subplotwidth,nlines,ncols,i):
187+ '''
188+ PLOT_MANAGER - distribute the plots called by plotmodel
189+
190+ Usage:
191+ plot_manager(md,options,subplotwidth,i);
192+
193+ See also: PLOTMODEL, PLOT_UNIT
194+ '''
195+
196+ #parse options and get a structure of options
197+ options=checkplotoptions(md,options)
198+ #print 'options:', options
199+
200+ #get data to be plotted
201+ data=options.getfieldvalue('data');
202+
203+ #figure out if this is a special plot
204+ if isinstance(data,(str,unicode)):
205+
206+ if data=='mesh': plot_mesh(md,options,nlines,ncols,i)
207+ else:
208+ print "WARNING: '%s' is not implemented or is not a valid string for option 'data'" % data
209+
210+ else:
211+ print "'data' not a string, plotting model properties yet to be implemented..."
212+
213Index: ../trunk-jpl/src/m/plot/checkplotoptions.py
214===================================================================
215--- ../trunk-jpl/src/m/plot/checkplotoptions.py (revision 0)
216+++ ../trunk-jpl/src/m/plot/checkplotoptions.py (revision 13411)
217@@ -0,0 +1,15 @@
218+def checkplotoptions(md,options):
219+ '''
220+ CHECKPLOTOPTIONS - build a structure that holds all plot options
221+
222+ Usage:
223+ options=checkplotoptions(md,options)
224+
225+ See also: PLOTMODEL
226+
227+ NOTE: not fully implemented yet
228+ '''
229+
230+ print "WARNING: checkplotoptions not implemented: options returned as passed"
231+ return options
232+
233Index: ../trunk-jpl/src/m/plot/plot_mesh.py
234===================================================================
235--- ../trunk-jpl/src/m/plot/plot_mesh.py (revision 0)
236+++ ../trunk-jpl/src/m/plot/plot_mesh.py (revision 13411)
237@@ -0,0 +1,24 @@
238+import pylab as p
239+import matplotlib.tri as tri
240+
241+def plot_mesh(md,options,nlines,ncols,i):
242+ '''
243+ PLOT_MESH - plot model mesh
244+
245+ Usage:
246+ plot_mesh(md,options,nlines,ncols,i)
247+
248+ See also: PLOTMODEL
249+ '''
250+
251+ #TODO: implement processmesh
252+ x=md.mesh.x
253+ y=md.mesh.y
254+ elements=md.mesh.elements
255+ elements=elements-1 #since python indexes from zero
256+
257+ p.subplot(nlines,ncols,i)
258+ p.triplot(x,y,elements)
259+ p.title('Mesh')
260+
261+ print 'WARNING: options passed to plot_mesh not implemented yet'
262Index: ../trunk-jpl/src/m/plot/plotmodel.py
263===================================================================
264--- ../trunk-jpl/src/m/plot/plotmodel.py (revision 0)
265+++ ../trunk-jpl/src/m/plot/plotmodel.py (revision 13411)
266@@ -0,0 +1,55 @@
267+import pylab as p
268+#from pairoptions import *
269+from plotoptions import *
270+from plot_manager import *
271+
272+def plotmodel(md,*args):
273+ '''
274+ at command prompt, type 'plotdoc' for additional documentation
275+ '''
276+
277+ #First process options
278+ options=plotoptions(*args)
279+
280+ #get number of subplots
281+ subplotwidth=math.ceil(math.sqrt(options.numberofplots))
282+
283+ #if nlines and ncols specified, then bypass
284+ if options.list[0].exist('nlines'):
285+ nlines=options.list[0].getfieldvalue('nlines')
286+ nl=True
287+ else:
288+ nlines=subplotwidth
289+ nl=False
290+
291+ if options.list[0].exist('ncols'):
292+ ncols=options.list[0].getfieldvalue('ncols')
293+ nc=True
294+ else:
295+ ncols=subplotwidth
296+ nc=False
297+
298+ #check that nlines and ncols were given at the same time!
299+ if not nl==nc:
300+ raise StandardError('error: nlines and ncols need to be specified together, or not at all')
301+
302+ #Get figure number and number of plots
303+ figurenumber=options.figurenumber
304+ numberofplots=options.numberofplots
305+
306+ #Go through plots
307+ if numberofplots:
308+
309+ #Create figure
310+ #plots will be visible by default if ipython is run in interactive mode (invoked by ipython --pylab)
311+ #handling the 'visible' option will need some check on whether ipython is currently in interactive or non-interactive mode
312+ p.figure(figurenumber)
313+
314+ try:
315+ for i in xrange(numberofplots):
316+ plot_manager(options.list[i].getfieldvalue('model',md),options.list[i],subplotwidth,nlines,ncols,i)
317+ except StandardError:
318+ print 'error in plot_manager'
319+ else:
320+ raise StandardError('plotmodel error message: no output data found.')
321+
Note: See TracBrowser for help on using the repository browser.