source: trunk/python/asapmath.py@ 1364

Last change on this file since 1364 was 1362, checked in by mar637, 18 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
RevLine 
[1085]1from asap.scantable import scantable
[258]2from asap import rcParams
[720]3from asap import print_log
[101]4
[143]5def average_time(*args, **kwargs):
[101]6 """
[113]7 Return the (time) average of a scan or list of scans. [in channels only]
[305]8 The cursor of the output scan is set to 0
[113]9 Parameters:
[1361]10 one scan or comma separated scans or a list of scans
[143]11 mask: an optional mask (only used for 'var' and 'tsys' weighting)
[558]12 scanav: True averages each scan separately.
13 False (default) averages all scans together,
[1232]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)
[930]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.
[113]24 Example:
25 # return a time averaged scan from scana and scanb
26 # without using a mask
[129]27 scanav = average_time(scana,scanb)
[1361]28 # or equivalent
29 # scanav = average_time([scana, scanb])
[113]30 # return the (time) averaged scan, i.e. the average of
31 # all correlator cycles
[558]32 scanav = average_time(scan, scanav=True)
[101]33 """
[930]34 scanav = False
[143]35 if kwargs.has_key('scanav'):
[930]36 scanav = kwargs.get('scanav')
[524]37 weight = 'tint'
[143]38 if kwargs.has_key('weight'):
39 weight = kwargs.get('weight')
40 mask = ()
41 if kwargs.has_key('mask'):
42 mask = kwargs.get('mask')
[930]43 align = False
44 if kwargs.has_key('align'):
45 align = kwargs.get('align')
[489]46 varlist = vars()
[665]47 if isinstance(args[0],list):
[981]48 lst = args[0]
[665]49 elif isinstance(args[0],tuple):
[981]50 lst = list(args[0])
[665]51 else:
[981]52 lst = list(args)
[720]53
[489]54 del varlist["kwargs"]
55 varlist["args"] = "%d scantables" % len(lst)
[981]56 # need special formatting here for history...
[720]57
[876]58 from asap._asap import stmath
59 stm = stmath()
[113]60 for s in lst:
[101]61 if not isinstance(s,scantable):
[720]62 msg = "Please give a list of scantables"
63 if rcParams['verbose']:
64 print msg
65 return
66 else:
67 raise TypeError(msg)
[945]68 if scanav: scanav = "SCAN"
69 else: scanav = "NONE"
[981]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:
[1059]76 alignedlst = lst
[1232]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))
[489]88 s._add_history("average_time",varlist)
[720]89 print_log()
[489]90 return s
[101]91
[1074]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
[101]114
[296]115def simple_math(left, right, op='add', tsys=True):
[242]116 """
[720]117 Apply simple mathematical binary operations to two
[242]118 scan tables, returning the result in a new scan table.
119 The operation is applied to both the correlations and the TSys data
[305]120 The cursor of the output scan is set to 0
[242]121 Parameters:
122 left: the 'left' scan
123 right: the 'right' scan
124 op: the operation: 'add' (default), 'sub', 'mul', 'div'
[296]125 tsys: if True (default) then apply the operation to Tsys
126 as well as the data
[242]127 """
[1357]128 print "simple_math is deprecated use +=/* instead."
[918]129
130def merge(*args):
[945]131 """
[1362]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)
[945]141 """
[918]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
[1074]165
Note: See TracBrowser for help on using the repository browser.