source: tags/asap2.2.0/python/asapmath.py

Last change on this file was 1362, checked in by mar637, 17 years ago

added missing docstring for merge

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1from asap.scantable import scantable
2from asap import rcParams
3from asap import print_log
4
5def average_time(*args, **kwargs):
6    """
7    Return the (time) average of a scan or list of scans. [in channels only]
8    The cursor of the output scan is set to 0
9    Parameters:
10        one scan or comma separated  scans or a list of scans
11        mask:     an optional mask (only used for 'var' and 'tsys' weighting)
12        scanav:   True averages each scan separately.
13                  False (default) averages all scans together,
14        weight:   Weighting scheme.
15                    'none'     (mean no weight)
16                    'var'      (1/var(spec) weighted)
17                    'tsys'     (1/Tsys**2 weighted)
18                    'tint'     (integration time weighted)
19                    'tintsys'  (Tint/Tsys**2)
20                    'median'   ( median averaging)
21        align:    align the spectra in velocity before averaging. It takes
22                  the time of the first spectrum in the first scantable
23                  as reference time.
24    Example:
25        # return a time averaged scan from scana and scanb
26        # without using a mask
27        scanav = average_time(scana,scanb)
28        # or equivalent
29        # scanav = average_time([scana, scanb])
30        # return the (time) averaged scan, i.e. the average of
31        # all correlator cycles
32        scanav = average_time(scan, scanav=True)
33    """
34    scanav = False
35    if kwargs.has_key('scanav'):
36       scanav = kwargs.get('scanav')
37    weight = 'tint'
38    if kwargs.has_key('weight'):
39       weight = kwargs.get('weight')
40    mask = ()
41    if kwargs.has_key('mask'):
42        mask = kwargs.get('mask')
43    align = False
44    if kwargs.has_key('align'):
45        align = kwargs.get('align')
46    varlist = vars()
47    if isinstance(args[0],list):
48        lst = args[0]
49    elif isinstance(args[0],tuple):
50        lst = list(args[0])
51    else:
52        lst = list(args)
53
54    del varlist["kwargs"]
55    varlist["args"] = "%d scantables" % len(lst)
56    # need special formatting here for history...
57
58    from asap._asap import stmath
59    stm = stmath()
60    for s in lst:
61        if not isinstance(s,scantable):
62            msg = "Please give a list of scantables"
63            if rcParams['verbose']:
64                print msg
65                return
66            else:
67                raise TypeError(msg)
68    if scanav: scanav = "SCAN"
69    else: scanav = "NONE"
70    alignedlst = []
71    if align:
72        refepoch = lst[0].get_time(0)
73        for scan in lst:
74            alignedlst.append(scan.freq_align(refepoch,insitu=False))
75    else:
76        alignedlst = lst
77    if weight.upper() == 'MEDIAN':
78        # median doesn't support list of scantables - merge first
79        merged = None
80        if len(alignedlst) > 1:
81            merged = merge(alignedlst)
82        else:
83            merged = alignedlst[0]
84        s = scantable(stm._averagechannel(merged, 'MEDIAN', scanav))
85        del merged
86    else:
87        s = scantable(stm._average(alignedlst, mask, weight.upper(), scanav))
88    s._add_history("average_time",varlist)
89    print_log()
90    return s
91
92def quotient(source, reference, preserve=True):
93    """
94    Return the quotient of a 'source' (signal) scan and a 'reference' scan.
95    The reference can have just one scan, even if the signal has many. Otherwise
96    they must have the same number of scans.
97    The cursor of the output scan is set to 0
98    Parameters:
99        source:        the 'on' scan
100        reference:     the 'off' scan
101        preserve:      you can preserve (default) the continuum or
102                       remove it.  The equations used are
103                       preserve:  Output = Toff * (on/off) - Toff
104                       remove:    Output = Toff * (on/off) - Ton
105    """
106    varlist = vars()
107    from asap._asap import stmath
108    stm = stmath()
109    stm._setinsitu(False)
110    s = scantable(stm._quotient(source, reference, preserve))
111    s._add_history("quotient",varlist)
112    print_log()
113    return s
114
115def simple_math(left, right, op='add', tsys=True):
116    """
117    Apply simple mathematical binary operations to two
118    scan tables,  returning the result in a new scan table.
119    The operation is applied to both the correlations and the TSys data
120    The cursor of the output scan is set to 0
121    Parameters:
122        left:          the 'left' scan
123        right:         the 'right' scan
124        op:            the operation: 'add' (default), 'sub', 'mul', 'div'
125        tsys:          if True (default) then apply the operation to Tsys
126                       as well as the data
127    """
128    print "simple_math is deprecated use +=/* instead."
129
130def merge(*args):
131    """
132    Merge a list of scanatables, or comma-sperated scantables into one
133    scnatble.
134    Parameters:
135        A list [scan1, scan2] or scan1, scan2.
136    Example:
137        myscans = [scan1, scan2]
138        allscans = merge(myscans)
139        # or equivalent
140        sameallscans = merge(scan1, scan2)
141    """
142    varlist = vars()
143    if isinstance(args[0],list):
144        lst = tuple(args[0])
145    elif isinstance(args[0],tuple):
146        lst = args[0]
147    else:
148        lst = tuple(args)
149    varlist["args"] = "%d scantables" % len(lst)
150    # need special formatting her for history...
151    from asap._asap import stmath
152    stm = stmath()
153    for s in lst:
154        if not isinstance(s,scantable):
155            msg = "Please give a list of scantables"
156            if rcParams['verbose']:
157                print msg
158                return
159            else:
160                raise TypeError(msg)
161    s = scantable(stm._merge(lst))
162    s._add_history("merge", varlist)
163    print_log()
164    return s
165
Note: See TracBrowser for help on using the repository browser.