source: branches/Release2.1/python/asapmath.py@ 3105

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

Fix for Ticket #68, added 'median' to average_time

  • 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
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 # return the (time) averaged scan, i.e. the average of
29 # all correlator cycles
30 scanav = average_time(scan, scanav=True)
31 """
32 scanav = False
33 if kwargs.has_key('scanav'):
34 scanav = kwargs.get('scanav')
35 weight = 'tint'
36 if kwargs.has_key('weight'):
37 weight = kwargs.get('weight')
38 mask = ()
39 if kwargs.has_key('mask'):
40 mask = kwargs.get('mask')
41 align = False
42 if kwargs.has_key('align'):
43 align = kwargs.get('align')
44 varlist = vars()
45 if isinstance(args[0],list):
46 lst = args[0]
47 elif isinstance(args[0],tuple):
48 lst = list(args[0])
49 else:
50 lst = list(args)
51
52 del varlist["kwargs"]
53 varlist["args"] = "%d scantables" % len(lst)
54 # need special formatting here for history...
55
56 from asap._asap import stmath
57 stm = stmath()
58 for s in lst:
59 if not isinstance(s,scantable):
60 msg = "Please give a list of scantables"
61 if rcParams['verbose']:
62 print msg
63 return
64 else:
65 raise TypeError(msg)
66 if scanav: scanav = "SCAN"
67 else: scanav = "NONE"
68 alignedlst = []
69 if align:
70 refepoch = lst[0].get_time(0)
71 for scan in lst:
72 alignedlst.append(scan.freq_align(refepoch,insitu=False))
73 else:
74 alignedlst = lst
75 if weight.upper() == 'MEDIAN':
76 # median doesn't support list of scantables - merge first
77 merged = None
78 if len(alignedlst) > 1:
79 merged = merge(alignedlst)
80 else:
81 merged = alignedlst[0]
82 s = scantable(stm._averagechannel(merged, 'MEDIAN', scanav))
83 del merged
84 else:
85 s = scantable(stm._average(alignedlst, mask, weight.upper(), scanav))
86 s._add_history("average_time",varlist)
87 print_log()
88 return s
89
90def quotient(source, reference, preserve=True):
91 """
92 Return the quotient of a 'source' (signal) scan and a 'reference' scan.
93 The reference can have just one scan, even if the signal has many. Otherwise
94 they must have the same number of scans.
95 The cursor of the output scan is set to 0
96 Parameters:
97 source: the 'on' scan
98 reference: the 'off' scan
99 preserve: you can preserve (default) the continuum or
100 remove it. The equations used are
101 preserve: Output = Toff * (on/off) - Toff
102 remove: Output = Toff * (on/off) - Ton
103 """
104 varlist = vars()
105 from asap._asap import stmath
106 stm = stmath()
107 stm._setinsitu(False)
108 s = scantable(stm._quotient(source, reference, preserve))
109 s._add_history("quotient",varlist)
110 print_log()
111 return s
112
113def simple_math(left, right, op='add', tsys=True):
114 """
115 Apply simple mathematical binary operations to two
116 scan tables, returning the result in a new scan table.
117 The operation is applied to both the correlations and the TSys data
118 The cursor of the output scan is set to 0
119 Parameters:
120 left: the 'left' scan
121 right: the 'right' scan
122 op: the operation: 'add' (default), 'sub', 'mul', 'div'
123 tsys: if True (default) then apply the operation to Tsys
124 as well as the data
125 """
126 varlist = vars()
127 print "Not yet available in asap"
128 return
129 if not isinstance(left,scantable) and not isinstance(right,scantable):
130 msg = "Please provide two scantables as input"
131 if rcParams['verbose']:
132 print msg
133 return
134 else:
135 raise TypeError(msg)
136 s = scantable(stm._bop(left, right, op, tsys))
137 s._add_history("simple_math", varlist)
138 print_log()
139 return s
140
141def merge(*args):
142 """
143 """
144 varlist = vars()
145 if isinstance(args[0],list):
146 lst = tuple(args[0])
147 elif isinstance(args[0],tuple):
148 lst = args[0]
149 else:
150 lst = tuple(args)
151 varlist["args"] = "%d scantables" % len(lst)
152 # need special formatting her for history...
153 from asap._asap import stmath
154 stm = stmath()
155 for s in lst:
156 if not isinstance(s,scantable):
157 msg = "Please give a list of scantables"
158 if rcParams['verbose']:
159 print msg
160 return
161 else:
162 raise TypeError(msg)
163 s = scantable(stm._merge(lst))
164 s._add_history("merge", varlist)
165 print_log()
166 return s
167
Note: See TracBrowser for help on using the repository browser.