[1085] | 1 | from asap.scantable import scantable
|
---|
[258] | 2 | from asap import rcParams
|
---|
[720] | 3 | from asap import print_log
|
---|
[101] | 4 |
|
---|
[143] | 5 | def 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:
|
---|
| 10 | one scan or comma separated 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)
|
---|
[113] | 28 | # return the (time) averaged scan, i.e. the average of
|
---|
| 29 | # all correlator cycles
|
---|
[558] | 30 | scanav = average_time(scan, scanav=True)
|
---|
[101] | 31 | """
|
---|
[930] | 32 | scanav = False
|
---|
[143] | 33 | if kwargs.has_key('scanav'):
|
---|
[930] | 34 | scanav = kwargs.get('scanav')
|
---|
[524] | 35 | weight = 'tint'
|
---|
[143] | 36 | if kwargs.has_key('weight'):
|
---|
| 37 | weight = kwargs.get('weight')
|
---|
| 38 | mask = ()
|
---|
| 39 | if kwargs.has_key('mask'):
|
---|
| 40 | mask = kwargs.get('mask')
|
---|
[930] | 41 | align = False
|
---|
| 42 | if kwargs.has_key('align'):
|
---|
| 43 | align = kwargs.get('align')
|
---|
[489] | 44 | varlist = vars()
|
---|
[665] | 45 | if isinstance(args[0],list):
|
---|
[981] | 46 | lst = args[0]
|
---|
[665] | 47 | elif isinstance(args[0],tuple):
|
---|
[981] | 48 | lst = list(args[0])
|
---|
[665] | 49 | else:
|
---|
[981] | 50 | lst = list(args)
|
---|
[720] | 51 |
|
---|
[489] | 52 | del varlist["kwargs"]
|
---|
| 53 | varlist["args"] = "%d scantables" % len(lst)
|
---|
[981] | 54 | # need special formatting here for history...
|
---|
[720] | 55 |
|
---|
[876] | 56 | from asap._asap import stmath
|
---|
| 57 | stm = stmath()
|
---|
[113] | 58 | for s in lst:
|
---|
[101] | 59 | if not isinstance(s,scantable):
|
---|
[720] | 60 | msg = "Please give a list of scantables"
|
---|
| 61 | if rcParams['verbose']:
|
---|
| 62 | print msg
|
---|
| 63 | return
|
---|
| 64 | else:
|
---|
| 65 | raise TypeError(msg)
|
---|
[945] | 66 | if scanav: scanav = "SCAN"
|
---|
| 67 | else: scanav = "NONE"
|
---|
[981] | 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:
|
---|
[1059] | 74 | alignedlst = lst
|
---|
[1232] | 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))
|
---|
[489] | 86 | s._add_history("average_time",varlist)
|
---|
[720] | 87 | print_log()
|
---|
[489] | 88 | return s
|
---|
[101] | 89 |
|
---|
[1074] | 90 | def 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
|
---|
[101] | 112 |
|
---|
[296] | 113 | def simple_math(left, right, op='add', tsys=True):
|
---|
[242] | 114 | """
|
---|
[720] | 115 | Apply simple mathematical binary operations to two
|
---|
[242] | 116 | scan tables, returning the result in a new scan table.
|
---|
| 117 | The operation is applied to both the correlations and the TSys data
|
---|
[305] | 118 | The cursor of the output scan is set to 0
|
---|
[242] | 119 | Parameters:
|
---|
| 120 | left: the 'left' scan
|
---|
| 121 | right: the 'right' scan
|
---|
| 122 | op: the operation: 'add' (default), 'sub', 'mul', 'div'
|
---|
[296] | 123 | tsys: if True (default) then apply the operation to Tsys
|
---|
| 124 | as well as the data
|
---|
[242] | 125 | """
|
---|
[489] | 126 | varlist = vars()
|
---|
[876] | 127 | print "Not yet available in asap"
|
---|
| 128 | return
|
---|
[258] | 129 | if not isinstance(left,scantable) and not isinstance(right,scantable):
|
---|
[720] | 130 | msg = "Please provide two scantables as input"
|
---|
| 131 | if rcParams['verbose']:
|
---|
| 132 | print msg
|
---|
| 133 | return
|
---|
| 134 | else:
|
---|
| 135 | raise TypeError(msg)
|
---|
[918] | 136 | s = scantable(stm._bop(left, right, op, tsys))
|
---|
[489] | 137 | s._add_history("simple_math", varlist)
|
---|
[720] | 138 | print_log()
|
---|
[489] | 139 | return s
|
---|
[918] | 140 |
|
---|
| 141 | def merge(*args):
|
---|
[945] | 142 | """
|
---|
| 143 | """
|
---|
[918] | 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
|
---|
[1074] | 167 |
|
---|