1 | __all__ = ["rc", "list_rcparameters", "rcParams", "rcParamsDefault"]
|
---|
2 |
|
---|
3 | import os
|
---|
4 |
|
---|
5 | def _validate_bool(b):
|
---|
6 | 'Convert b to a boolean or raise'
|
---|
7 | bl = b.lower()
|
---|
8 | if bl in ('f', 'no', 'false', '0', 0): return False
|
---|
9 | elif bl in ('t', 'yes', 'true', '1', 1): return True
|
---|
10 | else:
|
---|
11 | raise ValueError('Could not convert "%s" to boolean' % b)
|
---|
12 |
|
---|
13 | def _validate_int(s):
|
---|
14 | 'convert s to int or raise'
|
---|
15 | try: return int(s)
|
---|
16 | except ValueError:
|
---|
17 | raise ValueError('Could not convert "%s" to int' % s)
|
---|
18 |
|
---|
19 | def _asap_fname():
|
---|
20 | """
|
---|
21 | Return the path to the rc file
|
---|
22 |
|
---|
23 | Search order:
|
---|
24 |
|
---|
25 | * current working dir
|
---|
26 | * environ var ASAPRC
|
---|
27 | * HOME/.asaprc
|
---|
28 |
|
---|
29 | """
|
---|
30 | fname = os.path.join( os.getcwd(), '.asaprc')
|
---|
31 | if os.path.exists(fname): return fname
|
---|
32 |
|
---|
33 | if os.environ.has_key('ASAPRC'):
|
---|
34 | path = os.environ['ASAPRC']
|
---|
35 | if os.path.exists(path):
|
---|
36 | fname = os.path.join(path, '.asaprc')
|
---|
37 | if os.path.exists(fname):
|
---|
38 | return fname
|
---|
39 |
|
---|
40 | if os.environ.has_key('HOME'):
|
---|
41 | home = os.environ['HOME']
|
---|
42 | fname = os.path.join(home, '.asaprc')
|
---|
43 | if os.path.exists(fname):
|
---|
44 | return fname
|
---|
45 | return None
|
---|
46 |
|
---|
47 |
|
---|
48 | defaultParams = {
|
---|
49 | # general
|
---|
50 | 'verbose' : [True, _validate_bool],
|
---|
51 | 'useplotter' : [True, _validate_bool],
|
---|
52 | 'insitu' : [True, _validate_bool],
|
---|
53 |
|
---|
54 | # plotting
|
---|
55 | 'plotter.gui' : [True, _validate_bool],
|
---|
56 | 'plotter.stacking' : ['p', str],
|
---|
57 | 'plotter.panelling' : ['s', str],
|
---|
58 | 'plotter.colours' : ['', str],
|
---|
59 | 'plotter.linestyles' : ['', str],
|
---|
60 | 'plotter.decimate' : [False, _validate_bool],
|
---|
61 | 'plotter.ganged' : [True, _validate_bool],
|
---|
62 | 'plotter.histogram' : [False, _validate_bool],
|
---|
63 | 'plotter.papertype' : ['A4', str],
|
---|
64 | ## for older Matplotlib version
|
---|
65 | #'plotter.axesformatting' : ['mpl', str],
|
---|
66 | 'plotter.axesformatting' : ['asap', str],
|
---|
67 |
|
---|
68 | # scantable
|
---|
69 | 'scantable.save' : ['ASAP', str],
|
---|
70 | 'scantable.autoaverage' : [True, _validate_bool],
|
---|
71 | 'scantable.freqframe' : ['LSRK', str], #default frequency frame
|
---|
72 | 'scantable.verbosesummary' : [False, _validate_bool],
|
---|
73 | 'scantable.storage' : ['memory', str],
|
---|
74 | 'scantable.history' : [True, _validate_bool],
|
---|
75 | 'scantable.reference' : ['.*(e|w|_R)$', str],
|
---|
76 | 'scantable.parallactify' : [False, _validate_bool]
|
---|
77 | # fitter
|
---|
78 | }
|
---|
79 |
|
---|
80 | def list_rcparameters():
|
---|
81 |
|
---|
82 | print """
|
---|
83 | # general
|
---|
84 | # print verbose output
|
---|
85 | verbose : True
|
---|
86 |
|
---|
87 | # preload a default plotter
|
---|
88 | useplotter : True
|
---|
89 |
|
---|
90 | # apply operations on the input scantable or return new one
|
---|
91 | insitu : True
|
---|
92 |
|
---|
93 | # plotting
|
---|
94 |
|
---|
95 | # do we want a GUI or plot to a file
|
---|
96 | plotter.gui : True
|
---|
97 |
|
---|
98 | # default mode for colour stacking
|
---|
99 | plotter.stacking : Pol
|
---|
100 |
|
---|
101 | # default mode for panelling
|
---|
102 | plotter.panelling : scan
|
---|
103 |
|
---|
104 | # push panels together, to share axis labels
|
---|
105 | plotter.ganged : True
|
---|
106 |
|
---|
107 | # decimate the number of points plotted by a factor of
|
---|
108 | # nchan/1024
|
---|
109 | plotter.decimate : False
|
---|
110 |
|
---|
111 | # default colours/linestyles
|
---|
112 | plotter.colours :
|
---|
113 | plotter.linestyles :
|
---|
114 |
|
---|
115 | # enable/disable histogram plotting
|
---|
116 | plotter.histogram : False
|
---|
117 |
|
---|
118 | # ps paper type
|
---|
119 | plotter.papertype : A4
|
---|
120 |
|
---|
121 | # The formatting style of the xaxis
|
---|
122 | plotter.axesformatting : 'mpl' (default) or 'asap' (for old versions of matplotlib)
|
---|
123 |
|
---|
124 | # scantable
|
---|
125 |
|
---|
126 | # default storage of scantable ('memory'/'disk')
|
---|
127 | scantable.storage : memory
|
---|
128 |
|
---|
129 | # write history of each call to scantable
|
---|
130 | scantable.history : True
|
---|
131 |
|
---|
132 | # default ouput format when saving
|
---|
133 | scantable.save : ASAP
|
---|
134 |
|
---|
135 | # auto averaging on read
|
---|
136 | scantable.autoaverage : True
|
---|
137 |
|
---|
138 | # default frequency frame to set when function
|
---|
139 | # scantable.set_freqframe is called
|
---|
140 | scantable.freqframe : LSRK
|
---|
141 |
|
---|
142 | # Control the level of information printed by summary
|
---|
143 | scantable.verbosesummary : False
|
---|
144 |
|
---|
145 | # Control the identification of reference (off) scans
|
---|
146 | # This is has to be a regular expression
|
---|
147 | scantable.reference : .*(e|w|_R)$
|
---|
148 |
|
---|
149 | # Indicate whether the data was parallactified (total phase offest == 0.0)
|
---|
150 | scantable.parallactify : False
|
---|
151 |
|
---|
152 | # Fitter
|
---|
153 | """
|
---|
154 |
|
---|
155 | def rc_params():
|
---|
156 | 'Return the default params updated from the values in the rc file'
|
---|
157 | fname = _asap_fname()
|
---|
158 |
|
---|
159 | if fname is None or not os.path.exists(fname):
|
---|
160 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
161 | #print message
|
---|
162 | #message = 'could not find rc file; returning defaults'
|
---|
163 | return ret
|
---|
164 |
|
---|
165 | cnt = 0
|
---|
166 | for line in file(fname):
|
---|
167 | cnt +=1
|
---|
168 | line = line.strip()
|
---|
169 | if not len(line): continue
|
---|
170 | if line.startswith('#'): continue
|
---|
171 | tup = line.split(':',1)
|
---|
172 | if len(tup) !=2:
|
---|
173 | print ('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
|
---|
174 | #asaplog.push('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
|
---|
175 | #print_log('WARN')
|
---|
176 | continue
|
---|
177 |
|
---|
178 | key, val = tup
|
---|
179 | key = key.strip()
|
---|
180 | if not defaultParams.has_key(key):
|
---|
181 | print ('Bad key "%s" on line %d in %s' % (key, cnt, fname))
|
---|
182 | #asaplog.push('Bad key "%s" on line %d in %s' % (key, cnt, fname))
|
---|
183 | #print_log('WARN')
|
---|
184 | continue
|
---|
185 |
|
---|
186 | default, converter = defaultParams[key]
|
---|
187 |
|
---|
188 | ind = val.find('#')
|
---|
189 | if ind>=0: val = val[:ind] # ignore trailing comments
|
---|
190 | val = val.strip()
|
---|
191 | try: cval = converter(val) # try to convert to proper type or raise
|
---|
192 | except ValueError, msg:
|
---|
193 | print ('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg))
|
---|
194 | #asaplog.push('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, str(msg)))
|
---|
195 | #print_log('WARN')
|
---|
196 | continue
|
---|
197 | else:
|
---|
198 | # Alles Klar, update dict
|
---|
199 | defaultParams[key][0] = cval
|
---|
200 |
|
---|
201 | # strip the conveter funcs and return
|
---|
202 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
203 | print ('loaded rc file %s'%fname)
|
---|
204 |
|
---|
205 | return ret
|
---|
206 |
|
---|
207 |
|
---|
208 | # this is the instance used by the asap classes
|
---|
209 | rcParams = rc_params()
|
---|
210 |
|
---|
211 | rcParamsDefault = dict(rcParams.items()) # a copy
|
---|
212 |
|
---|
213 | def rc(group, **kwargs):
|
---|
214 | """
|
---|
215 | Set the current rc params. Group is the grouping for the rc, eg
|
---|
216 | for scantable.save the group is 'scantable', for plotter.stacking, the
|
---|
217 | group is 'plotter', and so on. kwargs is a list of attribute
|
---|
218 | name/value pairs, eg
|
---|
219 |
|
---|
220 | rc('scantable', save='SDFITS')
|
---|
221 |
|
---|
222 | sets the current rc params and is equivalent to
|
---|
223 |
|
---|
224 | rcParams['scantable.save'] = 'SDFITS'
|
---|
225 |
|
---|
226 | Use rcdefaults to restore the default rc params after changes.
|
---|
227 | """
|
---|
228 |
|
---|
229 | aliases = {}
|
---|
230 |
|
---|
231 | for k,v in kwargs.items():
|
---|
232 | name = aliases.get(k) or k
|
---|
233 | if len(group):
|
---|
234 | key = '%s.%s' % (group, name)
|
---|
235 | else:
|
---|
236 | key = name
|
---|
237 | if not rcParams.has_key(key):
|
---|
238 | raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' % (key, group, name))
|
---|
239 |
|
---|
240 | rcParams[key] = v
|
---|
241 |
|
---|
242 |
|
---|
243 | def rcdefaults():
|
---|
244 | """
|
---|
245 | Restore the default rc params - the ones that were created at
|
---|
246 | asap load time
|
---|
247 | """
|
---|
248 | rcParams.update(rcParamsDefault)
|
---|