source: branches/Release2.0/python/__init__.py@ 3065

Last change on this file since 3065 was 1057, checked in by mar637, 18 years ago

Version bump for minor bug release

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