00001 """This module provides functions to set up resource parameters (rc).
00002 These can be set in a file .asaprc or using functions.
00003 """
00004 __all__ = ["rc", "list_rcparameters", "rcParams", "rcParamsDefault"]
00005
00006 import os
00007
00008 def _validate_bool(b):
00009 'Convert b to a boolean or raise'
00010 bl = b.lower()
00011 if bl in ('f', 'no', 'false', '0', 0): return False
00012 elif bl in ('t', 'yes', 'true', '1', 1): return True
00013 else:
00014 raise ValueError('Could not convert "%s" to boolean' % b)
00015
00016 def _validate_int(s):
00017 'convert s to int or raise'
00018 try: return int(s)
00019 except ValueError:
00020 raise ValueError('Could not convert "%s" to int' % s)
00021
00022 def _asap_fname():
00023 """
00024 Return the path to the rc file
00025
00026 Search order:
00027
00028 * current working dir
00029 * environ var ASAPRC
00030 * HOME/.asaprc
00031
00032 """
00033 fname = os.path.join( os.getcwd(), '.asaprc')
00034 if os.path.exists(fname): return fname
00035
00036 if os.environ.has_key('ASAPRC'):
00037 path = os.environ['ASAPRC']
00038 if os.path.exists(path):
00039 fname = os.path.join(path, '.asaprc')
00040 if os.path.exists(fname):
00041 return fname
00042
00043 if os.environ.has_key('HOME'):
00044 home = os.environ['HOME']
00045 fname = os.path.join(home, '.asaprc')
00046 if os.path.exists(fname):
00047 return fname
00048 return None
00049
00050
00051 defaultParams = {
00052
00053 'verbose' : [True, _validate_bool],
00054 'useplotter' : [True, _validate_bool],
00055 'insitu' : [True, _validate_bool],
00056
00057
00058 'plotter.gui' : [True, _validate_bool],
00059 'plotter.stacking' : ['p', str],
00060 'plotter.panelling' : ['s', str],
00061 'plotter.colours' : ['', str],
00062 'plotter.linestyles' : ['', str],
00063 'plotter.decimate' : [False, _validate_bool],
00064 'plotter.ganged' : [True, _validate_bool],
00065 'plotter.histogram' : [False, _validate_bool],
00066 'plotter.papertype' : ['A4', str],
00067
00068
00069 'plotter.axesformatting' : ['asap', str],
00070
00071
00072 'scantable.save' : ['ASAP', str],
00073 'scantable.autoaverage' : [True, _validate_bool],
00074 'scantable.freqframe' : ['LSRK', str],
00075 'scantable.verbosesummary' : [False, _validate_bool],
00076 'scantable.storage' : ['memory', str],
00077 'scantable.history' : [True, _validate_bool],
00078 'scantable.reference' : ['.*(e|w|_R)$', str],
00079 'scantable.parallactify' : [False, _validate_bool]
00080
00081 }
00082
00083 def list_rcparameters():
00084
00085 print """
00086 # general
00087 # only valid in asap standard mode not in scripts or casapy
00088 # It will disable exceptions and just print the messages
00089 verbose : True
00090
00091 # preload a default plotter
00092 useplotter : True
00093
00094 # apply operations on the input scantable or return new one
00095 insitu : True
00096
00097 # plotting
00098
00099 # do we want a GUI or plot to a file
00100 plotter.gui : True
00101
00102 # default mode for colour stacking
00103 plotter.stacking : Pol
00104
00105 # default mode for panelling
00106 plotter.panelling : scan
00107
00108 # push panels together, to share axis labels
00109 plotter.ganged : True
00110
00111 # decimate the number of points plotted by a factor of
00112 # nchan/1024
00113 plotter.decimate : False
00114
00115 # default colours/linestyles
00116 plotter.colours :
00117 plotter.linestyles :
00118
00119 # enable/disable histogram plotting
00120 plotter.histogram : False
00121
00122 # ps paper type
00123 plotter.papertype : A4
00124
00125 # The formatting style of the xaxis
00126 plotter.axesformatting : 'mpl' (default) or 'asap' (for old versions of matplotlib)
00127
00128 # scantable
00129
00130 # default storage of scantable ('memory'/'disk')
00131 scantable.storage : memory
00132
00133 # write history of each call to scantable
00134 scantable.history : True
00135
00136 # default ouput format when saving
00137 scantable.save : ASAP
00138
00139 # auto averaging on read
00140 scantable.autoaverage : True
00141
00142 # default frequency frame to set when function
00143 # scantable.set_freqframe is called
00144 scantable.freqframe : LSRK
00145
00146 # Control the level of information printed by summary
00147 scantable.verbosesummary : False
00148
00149 # Control the identification of reference (off) scans
00150 # This is has to be a regular expression
00151 scantable.reference : .*(e|w|_R)$
00152
00153 # Indicate whether the data was parallactified (total phase offest == 0.0)
00154 scantable.parallactify : False
00155
00156 # Fitter
00157 """
00158
00159 def rc_params():
00160 'Return the default params updated from the values in the rc file'
00161 fname = _asap_fname()
00162
00163 if fname is None or not os.path.exists(fname):
00164 ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
00165
00166
00167 return ret
00168
00169 cnt = 0
00170 for line in file(fname):
00171 cnt +=1
00172 line = line.strip()
00173 if not len(line): continue
00174 if line.startswith('#'): continue
00175 tup = line.split(':',1)
00176 if len(tup) !=2:
00177 print ('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
00178
00179
00180 continue
00181
00182 key, val = tup
00183 key = key.strip()
00184 if not defaultParams.has_key(key):
00185 print ('Bad key "%s" on line %d in %s' % (key, cnt, fname))
00186
00187
00188 continue
00189
00190 default, converter = defaultParams[key]
00191
00192 ind = val.find('#')
00193 if ind>=0: val = val[:ind]
00194 val = val.strip()
00195 try: cval = converter(val)
00196 except ValueError, msg:
00197 print ('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg))
00198
00199
00200 continue
00201 else:
00202
00203 defaultParams[key][0] = cval
00204
00205
00206 ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
00207 print ('loaded rc file %s'%fname)
00208
00209 return ret
00210
00211
00212
00213 rcParams = rc_params()
00214
00215 rcParamsDefault = dict(rcParams.items())
00216
00217 def rc(group, **kwargs):
00218 """
00219 Set the current rc params. Group is the grouping for the rc, eg
00220 for scantable.save the group is 'scantable', for plotter.stacking, the
00221 group is 'plotter', and so on. kwargs is a list of attribute
00222 name/value pairs, eg
00223
00224 rc('scantable', save='SDFITS')
00225
00226 sets the current rc params and is equivalent to
00227
00228 rcParams['scantable.save'] = 'SDFITS'
00229
00230 Use rcdefaults to restore the default rc params after changes.
00231 """
00232
00233 aliases = {}
00234
00235 for k,v in kwargs.items():
00236 name = aliases.get(k) or k
00237 if len(group):
00238 key = '%s.%s' % (group, name)
00239 else:
00240 key = name
00241 if not rcParams.has_key(key):
00242 raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' % (key, group, name))
00243
00244 rcParams[key] = v
00245
00246
00247 def rcdefaults():
00248 """
00249 Restore the default rc params - the ones that were created at
00250 asap load time
00251 """
00252 rcParams.update(rcParamsDefault)