source:
issm/oecreview/Archive/13393-13976/ISSM-13410-13411.diff
Last change on this file was 13980, checked in by , 12 years ago | |
---|---|
File size: 9.5 KB |
-
../trunk-jpl/src/m/classes/plotoptions.py
1 from collections import OrderedDict, Counter, defaultdict 2 from pairoptions import * 3 4 class plotoptions(object): 5 ''' 6 PLOTOPTIONS class definition 7 8 Usage: 9 plotoptions=plotoptions(*arg) 10 ''' 11 12 def __init__(self,*arg): 13 self.numberofplots = 0 14 self.figurenumber = 1 15 self.list = OrderedDict() 16 17 self.buildlist(*arg) 18 19 def __repr__(self): #{{{ 20 s="\n" 21 s+=" numberofplots: %i\n" % self.numberofplots 22 s+=" figurenumber: %i\n" % self.figurenumber 23 if self.list: 24 s+=" list: (%ix%i)\n" % (len(self.list),2) 25 for item in self.list.iteritems(): 26 #s+=" options of plot number %i\n" % item 27 if isinstance(item[1],(str,unicode)): 28 s+=" field: %-10s value: '%s'\n" % (item[0],item[1]) 29 elif isinstance(item[1],(bool,int,long,float)): 30 s+=" field: %-10s value: '%g'\n" % (item[0],item[1]) 31 else: 32 s+=" field: %-10s value: '%s'\n" % (item[0],item[1]) 33 else: 34 s+=" list: empty\n" 35 return s 36 #}}} 37 def buildlist(self,*arg): #{{{ 38 #check length of input 39 if len(arg) % 2: 40 raise TypeError('error: an even number of options is required') 41 42 #go through args and build list (like pairoptions) 43 rawoptions=pairoptions(*arg) 44 numoptions=len(arg)/2 45 rawlist=[] # cannot be a dict since they do not support duplicate keys 46 47 for i in xrange(numoptions): 48 if isinstance(arg[2*i],(str,unicode)): 49 rawlist.append([arg[2*i],arg[2*i+1]]) 50 else: 51 #option is not a string, ignore it 52 print "WARNING: option number %d is not a string and will be ignored." % (i+1) 53 54 #get figure number 55 self.figurenumber=rawoptions.getfieldvalue('figure',1) 56 57 #get number of subplots 58 numberofplots=Counter(x for sublist in rawlist for x in sublist if isinstance(x,(str,unicode)))['data'] 59 self.numberofplots=numberofplots 60 61 #figure out whether alloptions flag is on 62 if rawoptions.getfieldvalue('alloptions','off') is 'on': 63 allflag=1 64 else: 65 allflag=0 66 67 #initialize self.list (will need a list of dict's (or nested dict) for numberofplots>1) 68 #self.list=defaultdict(dict) 69 for i in xrange(numberofplots): 70 self.list[i]=pairoptions() 71 72 #process plot options 73 for i in xrange(len(rawlist)): 74 75 #if alloptions flag is on, apply to all plots 76 if (allflag and 'data' not in rawlist[i][0] and '#' not in rawlist[i][0]): 77 78 for j in xrange(numberofplots): 79 self.list[j].addfield(rawlist[i][0],rawlist[i][1]) 80 81 elif '#' in rawlist[i][0]: 82 83 #get subplots associated 84 string=rawlist[i][0].split('#') 85 plotnums=string[-1].split(',') 86 field=string[0] 87 88 #loop over plotnums 89 for k in xrange(len(plotnums)): 90 plotnum=plotnums[k] 91 92 #Empty 93 if not plotnum: continue 94 95 elif 'all' in plotnum: 96 for j in xrange(numberofplots): 97 self.list[j].addfield(field,rawlist[i][1]) 98 99 elif '-' in plotnum: 100 nums=plotnum.split('-') 101 if len(nums)!=2: continue 102 if False in [x.isdigit() for x in nums]: 103 raise ValueError('error: in option i-j both i and j must be integers') 104 for j in xrange(int(nums[0])-1,int(nums[1])): 105 self.list[j].addfield(field,rawlist[i][1]) 106 107 # Deal with #i 108 else: 109 #assign to subplot 110 if int(plotnum)>numberofplots: 111 raise ValueError('error: %s cannot be assigned %d which exceeds the number of subplots' % (field,plotnum)) 112 self.list[int(plotnum)-1].addfield(field,rawlist[i][1]) 113 else: 114 115 #go through all subplots and assign key-value pairs 116 j=0 117 while j <= numberofplots-1: 118 if not self.list[j].exist(rawlist[i][0]): 119 self.list[j].addfield(rawlist[i][0],rawlist[i][1]) 120 break 121 else: 122 j=j+1 123 if j+1>numberofplots: 124 print "WARNING: too many instances of '%s' in options" % rawlist[i][0] 125 #}}} -
../trunk-jpl/src/m/classes/pairoptions.py
40 40 self.list[arg[2*i]] = arg[2*i+1]; 41 41 else: 42 42 #option is not a string, ignore it 43 print "WARNING: option number %d '%s' is not a string and will be ignored." % (i+1,type(arg[2*i]))43 print "WARNING: option number %d is not a string and will be ignored." % (i+1) 44 44 # }}} 45 45 46 46 def addfield(self,field,value): # {{{ … … 127 127 return False 128 128 # }}} 129 129 130 # function num = fieldoccurences(obj,field), % {{{ 131 # %FIELDOCCURENCES - get number of occurence of a field 132 # 133 # %check input 134 # if ~ischar(field), 135 # error('fieldoccurences error message: field should be a string'); 136 # end 137 # 138 # %get number of occurence 139 # num=sum(strcmpi(field,obj.list(:,1))); 140 # end % }}} 130 #def fieldoccurences(self,field): #{{{ 131 # ''' 132 # FIELDOCCURENCES - get number of occurence of a field 133 # ''' 134 # 135 # #check input 136 # if not isinstance(field,(str,unicode)): 137 # raise TypeError("fieldoccurences error message: field should be a string") 141 138 139 # #get number of occurence 140 # # ?? 141 # #return num 142 # #% }}} 143 142 144 def getfieldvalue(self,field,default=None): # {{{ 143 145 """ 144 146 GETOPTION - get the value of an option -
../trunk-jpl/src/m/plot/plot_manager.py
1 from pairoptions import * 2 import pylab as p 3 from checkplotoptions import * 4 from plot_mesh import * 5 6 def plot_manager(md,options,subplotwidth,nlines,ncols,i): 7 ''' 8 PLOT_MANAGER - distribute the plots called by plotmodel 9 10 Usage: 11 plot_manager(md,options,subplotwidth,i); 12 13 See also: PLOTMODEL, PLOT_UNIT 14 ''' 15 16 #parse options and get a structure of options 17 options=checkplotoptions(md,options) 18 #print 'options:', options 19 20 #get data to be plotted 21 data=options.getfieldvalue('data'); 22 23 #figure out if this is a special plot 24 if isinstance(data,(str,unicode)): 25 26 if data=='mesh': plot_mesh(md,options,nlines,ncols,i) 27 else: 28 print "WARNING: '%s' is not implemented or is not a valid string for option 'data'" % data 29 30 else: 31 print "'data' not a string, plotting model properties yet to be implemented..." 32 -
../trunk-jpl/src/m/plot/checkplotoptions.py
1 def checkplotoptions(md,options): 2 ''' 3 CHECKPLOTOPTIONS - build a structure that holds all plot options 4 5 Usage: 6 options=checkplotoptions(md,options) 7 8 See also: PLOTMODEL 9 10 NOTE: not fully implemented yet 11 ''' 12 13 print "WARNING: checkplotoptions not implemented: options returned as passed" 14 return options 15 -
../trunk-jpl/src/m/plot/plot_mesh.py
1 import pylab as p 2 import matplotlib.tri as tri 3 4 def plot_mesh(md,options,nlines,ncols,i): 5 ''' 6 PLOT_MESH - plot model mesh 7 8 Usage: 9 plot_mesh(md,options,nlines,ncols,i) 10 11 See also: PLOTMODEL 12 ''' 13 14 #TODO: implement processmesh 15 x=md.mesh.x 16 y=md.mesh.y 17 elements=md.mesh.elements 18 elements=elements-1 #since python indexes from zero 19 20 p.subplot(nlines,ncols,i) 21 p.triplot(x,y,elements) 22 p.title('Mesh') 23 24 print 'WARNING: options passed to plot_mesh not implemented yet' -
../trunk-jpl/src/m/plot/plotmodel.py
1 import pylab as p 2 #from pairoptions import * 3 from plotoptions import * 4 from plot_manager import * 5 6 def plotmodel(md,*args): 7 ''' 8 at command prompt, type 'plotdoc' for additional documentation 9 ''' 10 11 #First process options 12 options=plotoptions(*args) 13 14 #get number of subplots 15 subplotwidth=math.ceil(math.sqrt(options.numberofplots)) 16 17 #if nlines and ncols specified, then bypass 18 if options.list[0].exist('nlines'): 19 nlines=options.list[0].getfieldvalue('nlines') 20 nl=True 21 else: 22 nlines=subplotwidth 23 nl=False 24 25 if options.list[0].exist('ncols'): 26 ncols=options.list[0].getfieldvalue('ncols') 27 nc=True 28 else: 29 ncols=subplotwidth 30 nc=False 31 32 #check that nlines and ncols were given at the same time! 33 if not nl==nc: 34 raise StandardError('error: nlines and ncols need to be specified together, or not at all') 35 36 #Get figure number and number of plots 37 figurenumber=options.figurenumber 38 numberofplots=options.numberofplots 39 40 #Go through plots 41 if numberofplots: 42 43 #Create figure 44 #plots will be visible by default if ipython is run in interactive mode (invoked by ipython --pylab) 45 #handling the 'visible' option will need some check on whether ipython is currently in interactive or non-interactive mode 46 p.figure(figurenumber) 47 48 try: 49 for i in xrange(numberofplots): 50 plot_manager(options.list[i].getfieldvalue('model',md),options.list[i],subplotwidth,nlines,ncols,i) 51 except StandardError: 52 print 'error in plot_manager' 53 else: 54 raise StandardError('plotmodel error message: no output data found.') 55
Note:
See TracBrowser
for help on using the repository browser.