source: trunk/python/__init__.py@ 987

Last change on this file since 987 was 984, checked in by mar637, 19 years ago

added set_dirframe
asap2 tidy up

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