source: trunk/python/__init__.py@ 1145

Last change on this file since 1145 was 1144, checked in by mar637, 18 years ago

added new functions to commands

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