source: issm/branches/trunk-larour-NatGeoScience2016/src/m/miscellaneous/fielddisplay.py@ 21243

Last change on this file since 21243 was 17480, checked in by cborstad, 11 years ago

CHG: cleaning up python imports. Use "m" prefix when using MatlabFuncs so that users can track down where these functions are defined rather than assuming they are mysterious python built-ins. Avoid importing * when possible, which cleans up the namespace and makes sure each module imports only what it needs (and that modules are not using functions imported by another module, which can break the code if modules are moved around)

File size: 3.2 KB
Line 
1#Module import
2import numpy
3from math import isnan
4import MatlabFuncs as m
5
6def fielddisplay(md,name,comment):
7 """
8 FIELDDISPLAY - display model field
9
10 Usage:
11 fielddisplay(md,name,comment)
12 """
13
14 #get field
15 field=getattr(md,name)
16
17 #disp corresponding line as a function of field type (offset set as 9 spaces)
18 return parsedisplay(" ",name,field,comment);
19
20def parsedisplay(offset,name,field,comment): # {{{
21
22 #string
23 if isinstance(field,(str,unicode)):
24
25 if len(field)>30:
26 string=displayunit(offset,name,"not displayed",comment)
27 else:
28 string=displayunit(offset,name,"'%s'" % field,comment)
29
30 #numeric
31 elif isinstance(field,(int,long,float)):
32 string=displayunit(offset,name,str(field),comment)
33
34 #matrix
35 elif isinstance(field,numpy.ndarray):
36 string=displayunit(offset,name,str(field.shape),comment)
37
38 #logical
39 elif isinstance(field,bool):
40 if field:
41 string=displayunit(offset,name,"True",comment)
42 else:
43 string=displayunit(offset,name,"False",comment)
44
45 #dictionary
46 elif isinstance(field,dict):
47 string=dict_display(offset,name,field,comment)
48
49 #list or tuple
50 elif isinstance(field,(list,tuple)):
51 string=list_display(offset,name,field,comment)
52
53 #None
54 elif field is None:
55 string=displayunit(offset,name,"None",comment)
56
57 else:
58 string=displayunit(offset,name,"not displayed",comment)
59
60 return string
61 # }}}
62
63def dict_display(offset,name,field,comment): # {{{
64
65 if field:
66 string =displayunit(offset,name,'{dictionary}',comment)+'\n'
67 offset+=' '
68
69 for structure_field,sfield in field.iteritems():
70 string+=parsedisplay(offset,str(structure_field),sfield,'')+'\n'
71
72 if string and string[-1]=='\n':
73 string=string[:-1]
74
75 else:
76 string=displayunit(offset,name,'N/A',comment)
77
78 return string
79 # }}}
80
81def list_display(offset,name,field,comment): # {{{
82
83 #initialization
84 if isinstance(field,list):
85 sbeg='['
86 send=']'
87 elif isinstance(field,tuple):
88 sbeg='('
89 send=')'
90 string=sbeg
91
92 #go through the cell and fill string
93 if len(field)<5:
94 for fieldi in field:
95 if isinstance(fieldi,(str,unicode)):
96 string+="'%s'," % fieldi
97 elif isinstance(fieldi,(bool,int,long,float)):
98 string+="%s," % str(fieldi)
99 else:
100 string=sbeg
101 break
102
103 if m.strcmp(string,sbeg):
104 string="%s%dx1%s" % (sbeg,len(field),send)
105 else:
106 string=string[:-1]+send
107
108 #call displayunit
109 return displayunit(offset,name,string,comment)
110 # }}}
111
112def displayunit(offset,name,characterization,comment): # {{{
113
114 #take care of name
115 if len(name)>23:
116 name="%s..." % name[:20]
117
118 #take care of characterization
119 if m.strcmp(characterization,"''") or m.strcmp(characterization,'""') or m.strcmpi(characterization,'nan'):
120 characterization="N/A"
121
122 if len(characterization)>15:
123 characterization="%s..." % characterization[:12]
124
125 #print
126 if not comment:
127 string="%s%-23s: %-15s" % (offset,name,characterization)
128 else:
129 if isinstance(comment,(str,unicode)):
130 string="%s%-23s: %-15s -- %s" % (offset,name,characterization,comment)
131 elif isinstance(comment,list):
132 string="%s%-23s: %-15s -- %s" % (offset,name,characterization,comment[0])
133 for commenti in comment:
134 string+="\n%s%-23s %-15s %s" % (offset,'','',commenti)
135 else:
136 raise RuntimeError("fielddisplay error message: format for comment not supported yet")
137
138 return string
139 # }}}
140
Note: See TracBrowser for help on using the repository browser.