source: trunk/python/__init__.py@ 1078

Last change on this file since 1078 was 1076, checked in by mar637, 18 years ago

added rc parameter scanatble.storage

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