source: trunk/python/__init__.py@ 1358

Last change on this file since 1358 was 1351, checked in by mar637, 18 years ago

several documentation updates for the upcoming release

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