source: branches/Release2.0/python/__init__.py@ 1403

Last change on this file since 1403 was 1057, checked in by mar637, 18 years ago

Version bump for minor bug release

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.5 KB
Line 
1"""
2This is the ATNF Single Dish Analysis package.
3
4"""
5import os,sys
6
7def _validate_bool(b):
8 'Convert b to a boolean or raise'
9 bl = b.lower()
10 if bl in ('f', 'no', 'false', '0', 0): return False
11 elif bl in ('t', 'yes', 'true', '1', 1): return True
12 else:
13 raise ValueError('Could not convert "%s" to boolean' % b)
14
15def _validate_int(s):
16 'convert s to int or raise'
17 try: return int(s)
18 except ValueError:
19 raise ValueError('Could not convert "%s" to int' % s)
20
21def _asap_fname():
22 """
23 Return the path to the rc file
24
25 Search order:
26
27 * current working dir
28 * environ var ASAPRC
29 * HOME/.asaprc
30
31 """
32
33 fname = os.path.join( os.getcwd(), '.asaprc')
34 if os.path.exists(fname): return fname
35
36 if os.environ.has_key('ASAPRC'):
37 path = os.environ['ASAPRC']
38 if os.path.exists(path):
39 fname = os.path.join(path, '.asaprc')
40 if os.path.exists(fname):
41 return fname
42
43 if os.environ.has_key('HOME'):
44 home = os.environ['HOME']
45 fname = os.path.join(home, '.asaprc')
46 if os.path.exists(fname):
47 return fname
48 return None
49
50
51defaultParams = {
52 # general
53 'verbose' : [True, _validate_bool],
54 'useplotter' : [True, _validate_bool],
55 'insitu' : [True, _validate_bool],
56
57 # plotting
58 'plotter.gui' : [True, _validate_bool],
59 'plotter.stacking' : ['p', str],
60 'plotter.panelling' : ['s', str],
61 'plotter.colours' : ['', str],
62 'plotter.linestyles' : ['', str],
63 'plotter.decimate' : [False, _validate_bool],
64 'plotter.ganged' : [True, _validate_bool],
65 'plotter.histogram' : [False, _validate_bool],
66
67 # scantable
68 'scantable.save' : ['ASAP', str],
69 'scantable.autoaverage' : [True, _validate_bool],
70 'scantable.freqframe' : ['LSRK', str], #default frequency frame
71 'scantable.verbosesummary' : [False, _validate_bool]
72
73 # fitter
74 }
75
76def list_rcparameters():
77
78 print """
79# general
80# print verbose output
81verbose : True
82
83# preload a default plotter
84useplotter : True
85
86# apply operations on the input scantable or return new one
87insitu : True
88
89# plotting
90
91# do we want a GUI or plot to a file
92plotter.gui : True
93
94# default mode for colour stacking
95plotter.stacking : Pol
96
97# default mode for panelling
98plotter.panelling : scan
99
100# push panels together, to share axislabels
101plotter.ganged : True
102
103# decimate the number of points plotted bya afactor of
104# nchan/1024
105plotter.decimate : False
106
107# default colours/linestyles
108plotter.colours :
109plotter.linestyles :
110
111# enable/disable histogram plotting
112plotter.histogram : False
113
114# scantable
115# default ouput format when saving
116scantable.save : ASAP
117# auto averaging on read
118scantable.autoaverage : True
119
120# default frequency frame to set when function
121# scantable.set_freqfrmae is called
122scantable.freqframe : LSRK
123
124# Control the level of information printed by summary
125scantable.verbosesummary : False
126
127# Fitter
128"""
129
130def rc_params():
131 'Return the default params updated from the values in the rc file'
132
133 fname = _asap_fname()
134
135 if fname is None or not os.path.exists(fname):
136 message = 'could not find rc file; returning defaults'
137 ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
138 #print message
139 return ret
140
141 cnt = 0
142 for line in file(fname):
143 cnt +=1
144 line = line.strip()
145 if not len(line): continue
146 if line.startswith('#'): continue
147 tup = line.split(':',1)
148 if len(tup) !=2:
149 print ('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname))
150 continue
151
152 key, val = tup
153 key = key.strip()
154 if not defaultParams.has_key(key):
155 print ('Bad key "%s" on line %d in %s' % (key, cnt, fname))
156 continue
157
158 default, converter = defaultParams[key]
159
160 ind = val.find('#')
161 if ind>=0: val = val[:ind] # ignore trailing comments
162 val = val.strip()
163 try: cval = converter(val) # try to convert to proper type or raise
164 except Exception, msg:
165 print ('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg))
166 continue
167 else:
168 # Alles Klar, update dict
169 defaultParams[key][0] = cval
170
171 # strip the conveter funcs and return
172 ret = dict([ (key, tup[0]) for key, tup in defaultParams.items()])
173 print ('loaded rc file %s'%fname)
174
175 return ret
176
177
178# this is the instance used by the asap classes
179rcParams = rc_params()
180
181rcParamsDefault = dict(rcParams.items()) # a copy
182
183def rc(group, **kwargs):
184 """
185 Set the current rc params. Group is the grouping for the rc, eg
186 for scantable.save the group is 'scantable', for plotter.stacking, the
187 group is 'plotter', and so on. kwargs is a list of attribute
188 name/value pairs, eg
189
190 rc('scantable', save='SDFITS')
191
192 sets the current rc params and is equivalent to
193
194 rcParams['scantable.save'] = 'SDFITS'
195
196 Use rcdefaults to restore the default rc params after changes.
197 """
198
199 aliases = {}
200
201 for k,v in kwargs.items():
202 name = aliases.get(k) or k
203 key = '%s.%s' % (group, name)
204 if not rcParams.has_key(key):
205 raise KeyError('Unrecognized key "%s" for group "%s" and name "%s"' % (key, group, name))
206
207 rcParams[key] = v
208
209
210def rcdefaults():
211 """
212 Restore the default rc params - the ones that were created at
213 asap load time
214 """
215 rcParams.update(rcParamsDefault)
216
217
218def _is_sequence_or_number(param, ptype=int):
219 if isinstance(param,tuple) or isinstance(param,list):
220 if len(param) == 0: return True # empty list
221 out = True
222 for p in param:
223 out &= isinstance(p,ptype)
224 return out
225 elif isinstance(param, ptype):
226 return True
227 return False
228
229def _to_list(param, ptype=int):
230 if isinstance(param, ptype):
231 if ptype is str: return param.split()
232 else: return [param]
233 if _is_sequence_or_number(param, ptype):
234 return param
235 return None
236
237def unique(x):
238 """
239 Return the unique values in a list
240 Parameters:
241 x: the list to reduce
242 Examples:
243 x = [1,2,3,3,4]
244 print unique(x)
245 [1,2,3,4]
246 """
247 return dict([ (val, 1) for val in x]).keys()
248
249def list_files(path=".",suffix="rpf"):
250 """
251 Return a list files readable by asap, such as rpf, sdfits, mbf, asap
252 Parameters:
253 path: The directory to list (default '.')
254 suffix: The file extension (default rpf)
255 Example:
256 files = list_files("data/","sdfits")
257 print files
258 ['data/2001-09-01_0332_P363.sdfits',
259 'data/2003-04-04_131152_t0002.sdfits',
260 'data/Sgr_86p262_best_SPC.sdfits']
261 """
262 import os
263 if not os.path.isdir(path):
264 return None
265 valid = "rpf sdf sdfits mbf asap".split()
266 if not suffix in valid:
267 return None
268 files = [os.path.expanduser(os.path.expandvars(path+"/"+f)) for f in os.listdir(path)]
269 return filter(lambda x: x.endswith(suffix),files)
270
271# workaround for ipython, which redirects this if banner=0 in ipythonrc
272sys.stdout = sys.__stdout__
273sys.stderr = sys.__stderr__
274
275# Logging
276from asap._asap import Log as _asaplog
277global asaplog
278asaplog=_asaplog()
279if rcParams['verbose']:
280 asaplog.enable()
281else:
282 asaplog.disable()
283
284def print_log():
285 log = asaplog.pop()
286 if len(log) and rcParams['verbose']: print log
287 return
288
289from asapfitter import *
290from asapreader import reader
291from selector import selector
292
293from asapmath import *
294from scantable import *
295from asaplinefind import *
296#from asapfit import *
297
298from numarray import logical_and as mask_and
299from numarray import logical_or as mask_or
300from numarray import logical_not as mask_not
301
302if rcParams['useplotter']:
303 from asapplotter import *
304 gui = os.environ.has_key('DISPLAY') and rcParams['plotter.gui']
305 plotter = asapplotter(gui)
306 del gui
307
308__date__ = '$Date:$'.split()[1]
309__version__ = '2.0.1'
310
311if rcParams['verbose']:
312 def version(): print "ASAP %s(%s)"% (__version__, __date__)
313 def list_scans(t = scantable):
314 import sys, types
315 globs = sys.modules['__main__'].__dict__.iteritems()
316 print "The user created scantables are:"
317 sts = map(lambda x: x[0], filter(lambda x: isinstance(x[1], t), globs))
318 print filter(lambda x: not x.startswith('_'), sts)
319 return
320
321 def commands():
322 x = """
323 [The scan container]
324 scantable - a container for integrations/scans
325 (can open asap/rpfits/sdfits and ms files)
326 copy - returns a copy of a scan
327 get_scan - gets a specific scan out of a scantable
328 (by name or number)
329 set_selection - set a new subselection of the data
330 get_selection - get the current selection object
331 summary - print info about the scantable contents
332 stats - get specified statistic of the spectra in
333 the scantable
334 stddev - get the standard deviation of the spectra
335 in the scantable
336 get_tsys - get the TSys
337 get_time - get the timestamps of the integrations
338 get_sourcename - get the source names of the scans
339 get_azimuth - get the azimuth of the scans
340 get_elevation - get the elevation of the scans
341 get_parangle - get the parallactic angle of the scans
342 get_unit - get the current unit
343 set_unit - set the abcissa unit to be used from this
344 point on
345 get_abcissa - get the abcissa values and name for a given
346 row (time)
347 set_freqframe - set the frame info for the Spectral Axis
348 (e.g. 'LSRK')
349 set_doppler - set the doppler to be used from this point on
350 set_dirframe - set the frame for the direction on the sky
351 set_instrument - set the instrument name
352 get_fluxunit - get the brightness flux unit
353 set_fluxunit - set the brightness flux unit
354 create_mask - return an mask in the current unit
355 for the given region. The specified regions
356 are NOT masked
357 get_restfreqs - get the current list of rest frequencies
358 set_restfreqs - set a list of rest frequencies
359 flag - flag selected channels in the data
360 save - save the scantable to disk as either 'ASAP'
361 or 'SDFITS'
362 nbeam,nif,nchan,npol - the number of beams/IFs/Pols/Chans
363 nscan - the number of scans in the scantable
364 nrow - te number of spectra in the scantable
365 history - print the history of the scantable
366 get_fit - get a fit which has been stored witnh the data
367 average_time - return the (weighted) time average of a scan
368 or a list of scans
369 average_pol - average the polarisations together.
370 The dimension won't be reduced and
371 all polarisations will contain the
372 averaged spectrum.
373 convert_pol - convert to a different polarisation type
374 auto_quotient - return the on/off quotient with
375 automatic detection of the on/off scans
376 (matched pairs and 1 off - n on)
377 scale, *, / - return a scan scaled by a given factor
378 add, +, - - return a scan with given value added
379 bin - return a scan with binned channels
380 resample - return a scan with resampled channels
381 smooth - return the spectrally smoothed scan
382 poly_baseline - fit a polynomial baseline to all Beams/IFs/Pols
383 auto_poly_baseline - automatically fit a polynomial baseline
384 recalc_azel - recalculate azimuth and elevation based on
385 the pointing
386 gain_el - apply gain-elevation correction
387 opacity - apply opacity correction
388 convert_flux - convert to and from Jy and Kelvin brightness
389 units
390 freq_align - align spectra in frequency frame
391 invert_phase - Invert the phase of the cross-correlation
392 swap_linears - Swap XX and YY
393 rotate_xyphase - rotate XY phase of cross correlation
394 rotate_linpolphase - rotate the phase of the complex
395 polarization O=Q+iU correlation
396 freq_switch - perform frequency switching on the data
397 stats - Determine the specified statistic, e.g. 'min'
398 'max', 'rms' etc.
399 stddev - Determine the standard deviation of the current
400 beam/if/pol
401 [Selection]
402 selector - a selection object to set a subset of a scantable
403 set_scans - set (a list of) scans by index
404 set_cycles - set (a list of) cycles by index
405 set_beams - set (a list of) beamss by index
406 set_ifs - set (a list of) ifs by index
407 set_polarisations - set (a list of) polarisations by name
408 or by index
409 set_names - set a selection by name (wildcards allowed)
410 set_tsys - set a selection by tsys thresholds
411 reset - unset all selections
412 + - merge to selections
413
414 [Math] Mainly functions which operate on more than one scantable
415
416 average_time - return the (weighted) time average
417 of a list of scans
418 quotient - return the on/off quotient
419 simple_math - simple mathematical operations on two scantables,
420 'add', 'sub', 'mul', 'div'
421 [Fitting]
422 fitter
423 auto_fit - return a scan where the function is
424 applied to all Beams/IFs/Pols.
425 commit - return a new scan where the fits have been
426 commited.
427 fit - execute the actual fitting process
428 store_fit - store the fit parameters in the data (scantable)
429 get_chi2 - get the Chi^2
430 set_scan - set the scantable to be fit
431 set_function - set the fitting function
432 set_parameters - set the parameters for the function(s), and
433 set if they should be held fixed during fitting
434 set_gauss_parameters - same as above but specialised for individual
435 gaussian components
436 get_parameters - get the fitted parameters
437 plot - plot the resulting fit and/or components and
438 residual
439 [Plotter]
440 asapplotter - a plotter for asap, default plotter is
441 called 'plotter'
442 plot - plot a scantable
443 save - save the plot to a file ('png' ,'ps' or 'eps')
444 set_mode - set the state of the plotter, i.e.
445 what is to be plotted 'colour stacked'
446 and what 'panelled'
447 set_selection - only plot a selected part of the data
448 set_range - set a 'zoom' window [xmin,xmax,ymin,ymax]
449 set_legend - specify user labels for the legend indeces
450 set_title - specify user labels for the panel indeces
451 set_abcissa - specify a user label for the abcissa
452 set_ordinate - specify a user label for the ordinate
453 set_layout - specify the multi-panel layout (rows,cols)
454 set_colors - specify a set of colours to use
455 set_linestyles - specify a set of linestyles to use if only
456 using one color
457 set_histogram - plot in historam style
458 set_mask - set a plotting mask for a specific polarization
459
460 [Reading files]
461 reader - access rpfits/sdfits files
462 open - attach reader to a file
463 close - detach reader from file
464 read - read in integrations
465 summary - list info about all integrations
466
467 [General]
468 commands - this command
469 print - print details about a variable
470 list_scans - list all scantables created bt the user
471 list_files - list all files readable by asap (default rpf)
472 del - delete the given variable from memory
473 range - create a list of values, e.g.
474 range(3) = [0,1,2], range(2,5) = [2,3,4]
475 help - print help for one of the listed functions
476 execfile - execute an asap script, e.g. execfile('myscript')
477 list_rcparameters - print out a list of possible values to be
478 put into $HOME/.asaprc
479 mask_and,mask_or,
480 mask_not - boolean operations on masks created with
481 scantable.create_mask
482
483 Note:
484 How to use this with help:
485 # function 'summary'
486 [xxx] is just a category
487 Every 'sub-level' in this list should be replaces by a '.' Period when
488 using help
489 Example:
490 ASAP> help scantable # to get info on ths scantable
491 ASAP> help scantable.summary # to get help on the scantable's
492 ASAP> help average_time
493
494 """
495 print x
496 return
497
498def welcome():
499 return """Welcome to ASAP v%s (%s) - the ATNF Spectral Analysis Package
500
501Please report any bugs via:
502http://sourcecode.atnf.csiro.au/cgi-bin/trac_asap.cgi/newticket
503
504[IMPORTANT: ASAP is 0-based]
505Type commands() to get a list of all available ASAP commands.""" % (__version__, __date__)
Note: See TracBrowser for help on using the repository browser.