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 | from asap.logging import asaplog, print_log
|
---|
158 | fname = _asap_fname()
|
---|
159 |
|
---|
160 | if fname is None or not os.path.exists(fname):
|
---|
161 | message = 'could not find rc file; returning defaults'
|
---|
162 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
163 | #print message
|
---|
164 | return ret
|
---|
165 |
|
---|
166 | cnt = 0
|
---|
167 | for line in file(fname):
|
---|
168 | cnt +=1
|
---|
169 | line = line.strip()
|
---|
170 | if not len(line): continue
|
---|
171 | if line.startswith('#'): continue
|
---|
172 | tup = line.split(':',1)
|
---|
173 | if len(tup) !=2:
|
---|
174 | #print ('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
|
---|
175 | asaplog.push('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
|
---|
176 | print_log('WARN')
|
---|
177 | continue
|
---|
178 |
|
---|
179 | key, val = tup
|
---|
180 | key = key.strip()
|
---|
181 | if not defaultParams.has_key(key):
|
---|
182 | #print ('Bad key "%s" on line %d in %s' % (key, cnt, fname))
|
---|
183 | asaplog.push('Bad key "%s" on line %d in %s' % (key, cnt, fname))
|
---|
184 | print_log('WARN')
|
---|
185 | continue
|
---|
186 |
|
---|
187 | default, converter = defaultParams[key]
|
---|
188 |
|
---|
189 | ind = val.find('#')
|
---|
190 | if ind>=0: val = val[:ind] # ignore trailing comments
|
---|
191 | val = val.strip()
|
---|
192 | try: cval = converter(val) # try to convert to proper type or raise
|
---|
193 | except ValueError, msg:
|
---|
194 | #print ('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg))
|
---|
195 | asaplog.push('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, str(msg)))
|
---|
196 | print_log('WARN')
|
---|
197 | continue
|
---|
198 | else:
|
---|
199 | # Alles Klar, update dict
|
---|
200 | defaultParams[key][0] = cval
|
---|
201 |
|
---|
202 | # strip the conveter funcs and return
|
---|
203 | ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
|
---|
204 | print ('loaded rc file %s'%fname)
|
---|
205 |
|
---|
206 | return ret
|
---|
207 |
|
---|
208 |
|
---|
209 | # this is the instance used by the asap classes
|
---|
210 | rcParams = rc_params()
|
---|
211 |
|
---|
212 | rcParamsDefault = dict(rcParams.items()) # a copy
|
---|
213 |
|
---|
214 | def rc(group, **kwargs):
|
---|
215 | """
|
---|
216 | Set the current rc params. Group is the grouping for the rc, eg
|
---|
217 | for scantable.save the group is 'scantable', for plotter.stacking, the
|
---|
218 | group is 'plotter', and so on. kwargs is a list of attribute
|
---|
219 | name/value pairs, eg
|
---|
220 |
|
---|
221 | rc('scantable', save='SDFITS')
|
---|
222 |
|
---|
223 | sets the current rc params and is equivalent to
|
---|
224 |
|
---|
225 | rcParams['scantable.save'] = 'SDFITS'
|
---|
226 |
|
---|
227 | Use rcdefaults to restore the default rc params after changes.
|
---|
228 | """
|
---|
229 |
|
---|
230 | aliases = {}
|
---|
231 |
|
---|
232 | for k,v in kwargs.items():
|
---|
233 | name = aliases.get(k) or k
|
---|
234 | if len(group):
|
---|
235 | key = '%s.%s' % (group, name)
|
---|
236 | else:
|
---|
237 | key = name
|
---|
238 | if not rcParams.has_key(key):
|
---|
239 | raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' % (key, group, name))
|
---|
240 |
|
---|
241 | rcParams[key] = v
|
---|
242 |
|
---|
243 |
|
---|
244 | def rcdefaults():
|
---|
245 | """
|
---|
246 | Restore the default rc params - the ones that were created at
|
---|
247 | asap load time
|
---|
248 | """
|
---|
249 | rcParams.update(rcParamsDefault)
|
---|