source: branches/Release2.1.1/python/__init__.py@ 1491

Last change on this file since 1491 was 1301, checked in by mar637, 18 years ago

version -> 2.1.1

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