source: trunk/python/__init__.py@ 1457

Last change on this file since 1457 was 1434, checked in by Malte Marquarding, 16 years ago

work with ipython >= 0.8

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