source: trunk/python/__init__.py@ 1086

Last change on this file since 1086 was 1080, checked in by mar637, 18 years ago

move all the environment set-up into python asap module. only detect ipython and run with profile in shell script

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