source: trunk/python/__init__.py@ 1075

Last change on this file since 1075 was 1069, checked in by mar637, 18 years ago

enhancement ticket #35. median scantable

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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
309__date__ = '$Date: 2006-07-04 04:44:44 +0000 (Tue, 04 Jul 2006) $'.split()[1]
310__version__ = '2.1a'
311
312if rcParams['verbose']:
313 def version(): print "ASAP %s(%s)"% (__version__, __date__)
314 def list_scans(t = scantable):
315 import sys, types
316 globs = sys.modules['__main__'].__dict__.iteritems()
317 print "The user created scantables are:"
318 sts = map(lambda x: x[0], filter(lambda x: isinstance(x[1], t), globs))
319 print filter(lambda x: not x.startswith('_'), sts)
320 return
321
322 def commands():
323 x = """
324 [The scan container]
325 scantable - a container for integrations/scans
326 (can open asap/rpfits/sdfits and ms files)
327 copy - returns a copy of a scan
328 get_scan - gets a specific scan out of a scantable
329 (by name or number)
330 set_selection - set a new subselection of the data
331 get_selection - get the current selection object
332 summary - print info about the scantable contents
333 stats - get specified statistic of the spectra in
334 the scantable
335 stddev - get the standard deviation of the spectra
336 in the scantable
337 get_tsys - get the TSys
338 get_time - get the timestamps of the integrations
339 get_sourcename - get the source names of the scans
340 get_azimuth - get the azimuth of the scans
341 get_elevation - get the elevation of the scans
342 get_parangle - get the parallactic angle of the scans
343 get_unit - get the current unit
344 set_unit - set the abcissa unit to be used from this
345 point on
346 get_abcissa - get the abcissa values and name for a given
347 row (time)
348 set_freqframe - set the frame info for the Spectral Axis
349 (e.g. 'LSRK')
350 set_doppler - set the doppler to be used from this point on
351 set_dirframe - set the frame for the direction on the sky
352 set_instrument - set the instrument name
353 get_fluxunit - get the brightness flux unit
354 set_fluxunit - set the brightness flux unit
355 create_mask - return an mask in the current unit
356 for the given region. The specified regions
357 are NOT masked
358 get_restfreqs - get the current list of rest frequencies
359 set_restfreqs - set a list of rest frequencies
360 flag - flag selected channels in the data
361 save - save the scantable to disk as either 'ASAP'
362 or 'SDFITS'
363 nbeam,nif,nchan,npol - the number of beams/IFs/Pols/Chans
364 nscan - the number of scans in the scantable
365 nrow - te number of spectra in the scantable
366 history - print the history of the scantable
367 get_fit - get a fit which has been stored witnh the data
368 average_time - return the (weighted) time average of a scan
369 or a list of scans
370 average_channel - return the (median) average of a scantable
371 average_pol - average the polarisations together.
372 The dimension won't be reduced and
373 all polarisations will contain the
374 averaged spectrum.
375 convert_pol - convert to a different polarisation type
376 auto_quotient - return the on/off quotient with
377 automatic detection of the on/off scans (closest
378 in time off is selected)
379 scale, *, / - return a scan scaled by a given factor
380 add, +, - - return a scan with given value added
381 bin - return a scan with binned channels
382 resample - return a scan with resampled channels
383 smooth - return the spectrally smoothed scan
384 poly_baseline - fit a polynomial baseline to all Beams/IFs/Pols
385 auto_poly_baseline - automatically fit a polynomial baseline
386 recalc_azel - recalculate azimuth and elevation based on
387 the pointing
388 gain_el - apply gain-elevation correction
389 opacity - apply opacity correction
390 convert_flux - convert to and from Jy and Kelvin brightness
391 units
392 freq_align - align spectra in frequency frame
393 invert_phase - Invert the phase of the cross-correlation
394 swap_linears - Swap XX and YY
395 rotate_xyphase - rotate XY phase of cross correlation
396 rotate_linpolphase - rotate the phase of the complex
397 polarization O=Q+iU correlation
398 freq_switch - perform frequency switching on the data
399 stats - Determine the specified statistic, e.g. 'min'
400 'max', 'rms' etc.
401 stddev - Determine the standard deviation of the current
402 beam/if/pol
403 [Selection]
404 selector - a selection object to set a subset of a scantable
405 set_scans - set (a list of) scans by index
406 set_cycles - set (a list of) cycles by index
407 set_beams - set (a list of) beamss by index
408 set_ifs - set (a list of) ifs by index
409 set_polarisations - set (a list of) polarisations by name
410 or by index
411 set_names - set a selection by name (wildcards allowed)
412 set_tsys - set a selection by tsys thresholds
413 reset - unset all selections
414 + - merge to selections
415
416 [Math] Mainly functions which operate on more than one scantable
417
418 average_time - return the (weighted) time average
419 of a list of scans
420 quotient - return the on/off quotient
421 simple_math - simple mathematical operations on two scantables,
422 'add', 'sub', 'mul', 'div'
423 quotient - build quotient of the given on and off scans
424 (matched pairs and 1 off/n on are valid)
425
426 [Fitting]
427 fitter
428 auto_fit - return a scan where the function is
429 applied to all Beams/IFs/Pols.
430 commit - return a new scan where the fits have been
431 commited.
432 fit - execute the actual fitting process
433 store_fit - store the fit parameters in the data (scantable)
434 get_chi2 - get the Chi^2
435 set_scan - set the scantable to be fit
436 set_function - set the fitting function
437 set_parameters - set the parameters for the function(s), and
438 set if they should be held fixed during fitting
439 set_gauss_parameters - same as above but specialised for individual
440 gaussian components
441 get_parameters - get the fitted parameters
442 plot - plot the resulting fit and/or components and
443 residual
444 [Plotter]
445 asapplotter - a plotter for asap, default plotter is
446 called 'plotter'
447 plot - plot a scantable
448 save - save the plot to a file ('png' ,'ps' or 'eps')
449 set_mode - set the state of the plotter, i.e.
450 what is to be plotted 'colour stacked'
451 and what 'panelled'
452 set_selection - only plot a selected part of the data
453 set_range - set a 'zoom' window [xmin,xmax,ymin,ymax]
454 set_legend - specify user labels for the legend indeces
455 set_title - specify user labels for the panel indeces
456 set_abcissa - specify a user label for the abcissa
457 set_ordinate - specify a user label for the ordinate
458 set_layout - specify the multi-panel layout (rows,cols)
459 set_colors - specify a set of colours to use
460 set_linestyles - specify a set of linestyles to use if only
461 using one color
462 set_histogram - plot in historam style
463 set_mask - set a plotting mask for a specific polarization
464
465 [Reading files]
466 reader - access rpfits/sdfits files
467 open - attach reader to a file
468 close - detach reader from file
469 read - read in integrations
470 summary - list info about all integrations
471
472 [General]
473 commands - this command
474 print - print details about a variable
475 list_scans - list all scantables created bt the user
476 list_files - list all files readable by asap (default rpf)
477 del - delete the given variable from memory
478 range - create a list of values, e.g.
479 range(3) = [0,1,2], range(2,5) = [2,3,4]
480 help - print help for one of the listed functions
481 execfile - execute an asap script, e.g. execfile('myscript')
482 list_rcparameters - print out a list of possible values to be
483 put into $HOME/.asaprc
484 mask_and,mask_or,
485 mask_not - boolean operations on masks created with
486 scantable.create_mask
487
488 Note:
489 How to use this with help:
490 # function 'summary'
491 [xxx] is just a category
492 Every 'sub-level' in this list should be replaces by a '.' Period when
493 using help
494 Example:
495 ASAP> help scantable # to get info on ths scantable
496 ASAP> help scantable.summary # to get help on the scantable's
497 ASAP> help average_time
498
499 """
500 print x
501 return
502
503def welcome():
504 return """Welcome to ASAP v%s (%s) - the ATNF Spectral Analysis Package
505
506Please report any bugs via:
507http://sourcecode.atnf.csiro.au/cgi-bin/trac_asap.cgi/newticket
508
509[IMPORTANT: ASAP is 0-based]
510Type commands() to get a list of all available ASAP commands.""" % (__version__, __date__)
Note: See TracBrowser for help on using the repository browser.