source: trunk/python/__init__.py@ 1077

Last change on this file since 1077 was 1076, checked in by mar637, 18 years ago

added rc parameter scanatble.storage

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