source: trunk/python/__init__.py @ 1069

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

enhancement ticket #35. median scantable

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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
[1069]308
[574]309__date__ = '$Date: 2006-07-04 04:44:44 +0000 (Tue, 04 Jul 2006) $'.split()[1]
[1069]310__version__  = '2.1a'
[100]311
[706]312if rcParams['verbose']:
[1014]313    def version(): print  "ASAP %s(%s)"% (__version__, __date__)
[706]314    def list_scans(t = scantable):
315        import sys, types
316        globs = sys.modules['__main__'].__dict__.iteritems()
317        print "The user created scantables are:"
318        sts = map(lambda x: x[0], filter(lambda x: isinstance(x[1], t), globs))
319        print filter(lambda x: not x.startswith('_'), sts)
320        return
[100]321
[715]322    def commands():
323        x = """
[113]324    [The scan container]
325        scantable           - a container for integrations/scans
[182]326                              (can open asap/rpfits/sdfits and ms files)
[113]327            copy            - returns a copy of a scan
328            get_scan        - gets a specific scan out of a scantable
[984]329                              (by name or number)
330            set_selection   - set a new subselection of the data
331            get_selection   - get the current selection object
[113]332            summary         - print info about the scantable contents
[182]333            stats           - get specified statistic of the spectra in
334                              the scantable
335            stddev          - get the standard deviation of the spectra
336                              in the scantable
[113]337            get_tsys        - get the TSys
338            get_time        - get the timestamps of the integrations
[733]339            get_sourcename  - get the source names of the scans
[794]340            get_azimuth     - get the azimuth of the scans
341            get_elevation   - get the elevation of the scans
342            get_parangle    - get the parallactic angle of the scans
[876]343            get_unit        - get the current unit
[513]344            set_unit        - set the abcissa unit to be used from this
345                              point on
[255]346            get_abcissa     - get the abcissa values and name for a given
347                              row (time)
[113]348            set_freqframe   - set the frame info for the Spectral Axis
349                              (e.g. 'LSRK')
[276]350            set_doppler     - set the doppler to be used from this point on
[984]351            set_dirframe    - set the frame for the direction on the sky
[240]352            set_instrument  - set the instrument name
[255]353            get_fluxunit    - get the brightness flux unit
[240]354            set_fluxunit    - set the brightness flux unit
[188]355            create_mask     - return an mask in the current unit
356                              for the given region. The specified regions
357                              are NOT masked
[255]358            get_restfreqs   - get the current list of rest frequencies
359            set_restfreqs   - set a list of rest frequencies
[1012]360            flag            - flag selected channels in the data
[116]361            save            - save the scantable to disk as either 'ASAP'
362                              or 'SDFITS'
[486]363            nbeam,nif,nchan,npol - the number of beams/IFs/Pols/Chans
[733]364            nscan           - the number of scans in the scantable
[984]365            nrow            - te number of spectra in the scantable
[486]366            history         - print the history of the scantable
[530]367            get_fit         - get a fit which has been stored witnh the data
[706]368            average_time    - return the (weighted) time average of a scan
[513]369                              or a list of scans
[1069]370            average_channel - return the (median) average of a scantable
[513]371            average_pol     - average the polarisations together.
[113]372                              The dimension won't be reduced and
373                              all polarisations will contain the
374                              averaged spectrum.
[992]375            convert_pol     - convert to a different polarisation type
[690]376            auto_quotient   - return the on/off quotient with
[1069]377                              automatic detection of the on/off scans (closest
378                              in time off is selected)
[1014]379            scale, *, /     - return a scan scaled by a given factor
380            add, +, -       - return a scan with given value added
[513]381            bin             - return a scan with binned channels
382            resample        - return a scan with resampled channels
383            smooth          - return the spectrally smoothed scan
384            poly_baseline   - fit a polynomial baseline to all Beams/IFs/Pols
[706]385            auto_poly_baseline - automatically fit a polynomial baseline
[780]386            recalc_azel     - recalculate azimuth and elevation based on
387                              the pointing
[513]388            gain_el         - apply gain-elevation correction
389            opacity         - apply opacity correction
390            convert_flux    - convert to and from Jy and Kelvin brightness
[255]391                              units
[513]392            freq_align      - align spectra in frequency frame
[1014]393            invert_phase    - Invert the phase of the cross-correlation
394            swap_linears    - Swap XX and YY
[513]395            rotate_xyphase  - rotate XY phase of cross correlation
396            rotate_linpolphase - rotate the phase of the complex
397                                 polarization O=Q+iU correlation
[733]398            freq_switch     - perform frequency switching on the data
399            stats           - Determine the specified statistic, e.g. 'min'
400                              'max', 'rms' etc.
401            stddev          - Determine the standard deviation of the current
402                              beam/if/pol
[1014]403     [Selection]
404         selector              - a selection object to set a subset of a scantable
405            set_scans          - set (a list of) scans by index
406            set_cycles         - set (a list of) cycles by index
407            set_beams          - set (a list of) beamss by index
408            set_ifs            - set (a list of) ifs by index
409            set_polarisations  - set (a list of) polarisations by name
410                                 or by index
411            set_names          - set a selection by name (wildcards allowed)
412            set_tsys           - set a selection by tsys thresholds
413            reset              - unset all selections
414            +                  - merge to selections
[733]415
[513]416     [Math] Mainly functions which operate on more than one scantable
[100]417
[706]418            average_time    - return the (weighted) time average
[513]419                              of a list of scans
420            quotient        - return the on/off quotient
421            simple_math     - simple mathematical operations on two scantables,
422                              'add', 'sub', 'mul', 'div'
[1069]423            quotient        - build quotient of the given on and off scans
424                              (matched pairs and 1 off/n on are valid)
425
[513]426     [Fitting]
[113]427        fitter
428            auto_fit        - return a scan where the function is
429                              applied to all Beams/IFs/Pols.
430            commit          - return a new scan where the fits have been
431                              commited.
432            fit             - execute the actual fitting process
[984]433            store_fit       - store the fit parameters in the data (scantable)
[113]434            get_chi2        - get the Chi^2
435            set_scan        - set the scantable to be fit
436            set_function    - set the fitting function
437            set_parameters  - set the parameters for the function(s), and
438                              set if they should be held fixed during fitting
[513]439            set_gauss_parameters - same as above but specialised for individual
440                                   gaussian components
[113]441            get_parameters  - get the fitted parameters
[513]442            plot            - plot the resulting fit and/or components and
443                              residual
[210]444    [Plotter]
445        asapplotter         - a plotter for asap, default plotter is
446                              called 'plotter'
[984]447            plot            - plot a scantable
[378]448            save            - save the plot to a file ('png' ,'ps' or 'eps')
[210]449            set_mode        - set the state of the plotter, i.e.
450                              what is to be plotted 'colour stacked'
451                              and what 'panelled'
[984]452            set_selection   - only plot a selected part of the data
[733]453            set_range       - set a 'zoom' window [xmin,xmax,ymin,ymax]
[255]454            set_legend      - specify user labels for the legend indeces
455            set_title       - specify user labels for the panel indeces
[733]456            set_abcissa     - specify a user label for the abcissa
[255]457            set_ordinate    - specify a user label for the ordinate
[378]458            set_layout      - specify the multi-panel layout (rows,cols)
[733]459            set_colors      - specify a set of colours to use
460            set_linestyles  - specify a set of linestyles to use if only
461                              using one color
[1054]462            set_histogram   - plot in historam style
[733]463            set_mask        - set a plotting mask for a specific polarization
[706]464
[182]465    [Reading files]
466        reader              - access rpfits/sdfits files
[984]467            open            - attach reader to a file
468            close           - detach reader from file
[182]469            read            - read in integrations
470            summary         - list info about all integrations
471
[113]472    [General]
473        commands            - this command
474        print               - print details about a variable
475        list_scans          - list all scantables created bt the user
[992]476        list_files          - list all files readable by asap (default rpf)
[113]477        del                 - delete the given variable from memory
478        range               - create a list of values, e.g.
479                              range(3) = [0,1,2], range(2,5) = [2,3,4]
480        help                - print help for one of the listed functions
481        execfile            - execute an asap script, e.g. execfile('myscript')
[255]482        list_rcparameters   - print out a list of possible values to be
[274]483                              put into $HOME/.asaprc
[466]484        mask_and,mask_or,
485        mask_not            - boolean operations on masks created with
486                              scantable.create_mask
[706]487
[210]488    Note:
489        How to use this with help:
490                                         # function 'summary'
491        [xxx] is just a category
492        Every 'sub-level' in this list should be replaces by a '.' Period when
[706]493        using help
[210]494        Example:
495            ASAP> help scantable # to get info on ths scantable
496            ASAP> help scantable.summary # to get help on the scantable's
497            ASAP> help average_time
498
[715]499            """
500        print x
501        return
[113]502
[706]503def welcome():
504    return """Welcome to ASAP v%s (%s) - the ATNF Spectral Analysis Package
[100]505
[1035]506Please report any bugs via:
507http://sourcecode.atnf.csiro.au/cgi-bin/trac_asap.cgi/newticket
[100]508
[378]509[IMPORTANT: ASAP is 0-based]
[706]510Type commands() to get a list of all available ASAP commands.""" % (__version__, __date__)
Note: See TracBrowser for help on using the repository browser.