source: trunk/python/asapmath.py@ 1358

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

deprecated simple_math

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 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 print "simple_math is deprecated use +=/* instead."
127
128def merge(*args):
129 """
130 """
131 varlist = vars()
132 if isinstance(args[0],list):
133 lst = tuple(args[0])
134 elif isinstance(args[0],tuple):
135 lst = args[0]
136 else:
137 lst = tuple(args)
138 varlist["args"] = "%d scantables" % len(lst)
139 # need special formatting her for history...
140 from asap._asap import stmath
141 stm = stmath()
142 for s in lst:
143 if not isinstance(s,scantable):
144 msg = "Please give a list of scantables"
145 if rcParams['verbose']:
146 print msg
147 return
148 else:
149 raise TypeError(msg)
150 s = scantable(stm._merge(lst))
151 s._add_history("merge", varlist)
152 print_log()
153 return s
154
Note: See TracBrowser for help on using the repository browser.