source: branches/alma/python/__init__.py@ 1462

Last change on this file since 1462 was 1460, checked in by Kana Sugimoto, 16 years ago

New Development: Yes

JIRA Issue: Yes (CAS-1080)

Ready to Release: Yes

Interface Changes: Yes

What Interface Changed: This is a brand new module.

Test Programs:

#from ASAP
scan=scantable('A_FILE_NAME') # Read spectral data from 'A_FILE_NAME'.
new_mask=interactivemask() # Create a interactive masking object
new_mask.select_mask(scan) # Do interactive mask selection
mask=new_mask.get_mask() # Get a final channel mask

Put in Release Notes: No

Description:

The class for interactive mask selection, 'interactivemask', is used
to hard coded in CASA tasks 'sdbaseline' and 'sdstat'.
I moved the class from the tasks and defined as a new ASAP module.


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