source: trunk/python/asapmath.py@ 245

Last change on this file since 245 was 245, checked in by kil064, 20 years ago

add arg. 'preserve' to quotient. also document algorithm

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1from scantable import scantable
2
3def average_time(*args, **kwargs):
4 """
5 Return the (time) average of a scan or list of scans. [in channels only]
6 Parameters:
7 one scan or comma separated scans
8 mask: an optional mask (only used for 'var' and 'tsys' weighting)
9 scanav: False (default) averages all scans together,
10 True averages each scan separately
11 weight: Weighting scheme. 'none' (default), 'var' (variance
12 weighted), 'tsys'
13 Example:
14 # return a time averaged scan from scana and scanb
15 # without using a mask
16 scanav = average_time(scana,scanb)
17 # return the (time) averaged scan, i.e. the average of
18 # all correlator cycles
19 scanav = average_time(scan)
20
21 """
22 scanAv = False
23 if kwargs.has_key('scanav'):
24 scanAv = kwargs.get('scanav')
25#
26 weight = 'none'
27 if kwargs.has_key('weight'):
28 weight = kwargs.get('weight')
29#
30 mask = ()
31 if kwargs.has_key('mask'):
32 mask = kwargs.get('mask')
33#
34 lst = tuple(args)
35 from asap._asap import average as _av
36 for s in lst:
37 if not isinstance(s,scantable):
38 print "Please give a list of scantables"
39 return
40 return scantable(_av(lst, mask, scanAv, weight))
41
42def quotient(source, reference, preserve=True):
43 """
44 Return the quotient of a 'source' (signal) scan and a 'reference' scan
45 Parameters:
46 source: the 'on' scan
47 reference: the 'off' scan
48 preserve: you can preserve (default) the continuum or
49 remove it. The equations used are
50 preserve - Output = Tref * (sig/ref) - Tref
51 remove - Output = Tref * (sig/ref) - Tsig
52 """
53 from asap._asap import quotient as _quot
54 return scantable(_quot(source, reference, preserve))
55
56def b_operate(left, right, op='add'):
57 """
58 Apply simple mathematical binary operations to two
59 scan tables, returning the result in a new scan table.
60 The operation is applied to both the correlations and the TSys data
61 Parameters:
62 left: the 'left' scan
63 right: the 'right' scan
64 op: the operation: 'add' (default), 'sub', 'mul', 'div'
65 """
66 from asap._asap import b_operate as _bop
67 return scantable(_bop(left, right, op))
68
69def scale(scan, factor, insitu=False, all=True):
70 """
71 Return a scan where all spectra are scaled by the give 'factor'
72 Parameters:
73 scan: a scantable
74 factor: the scaling factor
75 insitu: if False (default) a new scantable is returned.
76 Otherwise, the scaling is done in-situ
77 all: if True (default) apply to all spectra. Otherwise
78 apply only to the selected (beam/pol/if)spectra only
79 """
80 if not insitu:
81 from asap._asap import scale as _scale
82 return scantable(_scale(scan, factor, all))
83 else:
84 from asap._asap import scale_insitu as _scale
85 _scale(scan, factor, all)
86 return
87
88
89def add(scan, offset, insitu=False, all=True):
90 """
91 Return a scan where all spectra have the offset added
92 Parameters:
93 scan: a scantable
94 offset: the offset
95 insitu: if False (default) a new scantable is returned.
96 Otherwise, the addition is done in-situ
97 all: if True (default) apply to all spectra. Otherwise
98 apply only to the selected (beam/pol/if)spectra only
99 """
100 if not insitu:
101 from asap._asap import add as _add
102 return scantable(_add(scan, offset, all))
103 else:
104 from asap._asap import add_insitu as _add
105 _add(scan, offset, all)
106 return
107
108def convert_flux(scan, area, eta=1.0, insitu=False, all=True):
109 """
110 Return a scan where all spectra are converted to either Jansky or Kelvin
111 depending upon the flux units of the scan table.
112 Parameters:
113 scan: a scantable
114 area: the illuminated area of the telescope (m**2)
115 eta: The efficiency of the telescope (default 1.0)
116 insitu: if False (default) a new scantable is returned.
117 Otherwise, the conversion is done in-situ
118 all: if True (default) apply to all spectra. Otherwise
119 apply only to the selected (beam/pol/if)spectra only
120 """
121 if not insitu:
122 from asap._asap import convertflux as _convert
123 return scantable(_convert(scan, area, eta, all))
124 else:
125 from asap._asap import convertflux_insitu as _convert
126 _convert(scan, area, eta, all)
127 return
128
129def gain_el(scan, poly=None, filename="", method="linear", insitu=False, all=True):
130 """
131 Return a scan after applying a gain-elevation correction. The correction
132 can be made via either a polynomial or a table-based interpolation
133 (and extrapolation if necessary).
134 You specify polynomial coefficients, an ascii table or neither.
135 If you specify neither, then a polynomial correction will be made
136 with built in coefficients known for certain telescopes (an error will
137 occur if the instrument is not known).
138 Parameters:
139 scan: a scantable
140 poly: Polynomial coefficients (default None) to compute a gain-elevation
141 correction as a function of elevation (in degrees).
142 filename: The name of an ascii file holding correction factors.
143 The first row of the ascii file must give the column
144 names and these MUST include columns
145 "ELEVATION" (degrees) and "FACTOR" (multiply data by this) somewhere.
146 The second row must give the data type of the column. Use 'R' for
147 Real and 'I' for Integer. An example file would be:
148
149 TIME ELEVATION FACTOR
150 R R R
151 0.1 0 1.5
152 0.2 20 1.4
153 0.3 40 1.3
154 0.4 60 1.2
155 0.5 80 1.1
156 0.6 90 1.0
157 method: Interpolation method when correcting from a table. Values
158 are "nearest", "linear" (default), "cubic" and "spline"
159 insitu: if False (default) a new scantable is returned.
160 Otherwise, the conversion is done in-situ
161 all: if True (default) apply to all spectra. Otherwise
162 apply only to the selected (beam/pol/if)spectra only
163 """
164 if poly is None:
165 poly = ()
166 if not insitu:
167 from asap._asap import gainel as _gainEl
168 return scantable(_gainEl(scan, poly, filename, method, all))
169 else:
170 from asap._asap import gainel_insitu as _gainEl
171 _gainEl(scan, poly, filename, method, all)
172 return
173
174def opacity(scan, tau, insitu=False, all=True):
175 """
176 Return a scan after applying an opacity correction.
177 Parameters:
178 scan: a scantable
179 tau: Opacity from which the correction factor is exp(tau*ZD)
180 where ZD is the zenith-distance
181 insitu: if False (default) a new scantable is returned.
182 Otherwise, the conversion is done in-situ
183 all: if True (default) apply to all spectra. Otherwise
184 apply only to the selected (beam/pol/if)spectra only
185 """
186 if not insitu:
187 from asap._asap import opacity as _opacity
188 return scantable(_opacity(scan, tau, all))
189 else:
190 from asap._asap import opacity_insitu as _opacity
191 _opacity(scan, tau, all)
192 return
193
194def bin(scan, width=5, insitu=False):
195 """
196 Return a scan where all spectra have been binned up
197 width: The bin width (default=5) in pixels
198 insitu: if False (default) a new scantable is returned.
199 Otherwise, the addition is done in-situ
200 """
201 if not insitu:
202 from asap._asap import bin as _bin
203 return scantable(_bin(scan, width))
204 else:
205 from asap._asap import bin_insitu as _bin
206 _bin(scan, width)
207 return
208
209def average_pol(scan, mask=None, insitu=False):
210 """
211 Average the Polarisations together.
212 Parameters:
213 scan: The scantable
214 mask: An optional mask defining the region, where the
215 averaging will be applied. The output will have all
216 specified points masked.
217 insitu: If False (default) a new scantable is returned.
218 Otherwise, the averaging is done in-situ
219 Example:
220 polav = average_pols(myscan)
221 """
222 if mask is None:
223 mask = ()
224 if not insitu:
225 from asap._asap import averagepol as _avpol
226 return scantable(_avpol(scan, mask))
227 else:
228 from asap._asap import averagepol_insitu as _avpol
229 _avpol(scan, mask)
230 return
231
232def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
233 """
234 Smooth the spectrum by the specified kernel (conserving flux).
235 Parameters:
236 scan: The input scan
237 kernel: The type of smoothing kernel. Select from
238 'hanning' (default), 'gaussian' and 'boxcar'.
239 The first three characters are sufficient.
240 width: The width of the kernel in pixels. For hanning this is
241 ignored otherwise it defauls to 5 pixels.
242 For 'gaussian' it is the Full Width Half
243 Maximum. For 'boxcar' it is the full width.
244 insitu: If False (default) a new scantable is returned.
245 Otherwise, the scaling is done in-situ
246 all: If True (default) apply to all spectra. Otherwise
247 apply only to the selected (beam/pol/if)spectra only
248 Example:
249 none
250 """
251 if not insitu:
252 from asap._asap import smooth as _smooth
253 return scantable(_smooth(scan,kernel,width,all))
254 else:
255 from asap._asap import smooth_insitu as _smooth
256 _smooth(scan,kernel,width,all)
257 return
258
259def poly_baseline(scan, mask=None, order=0):
260 """
261 Return a scan which has been baselined (all rows) by a polynomial.
262 Parameters:
263 scan: a scantable
264 mask: an optional mask
265 order: the order of the polynomial (default is 0)
266 Example:
267 # return a scan baselined by a third order polynomial,
268 # not using a mask
269 bscan = poly_baseline(scan, order=3)
270 """
271 from asap.asapfitter import fitter
272 if mask is None:
273 from numarray import ones
274 mask = tuple(ones(scan.nchan()))
275 f = fitter()
276 f._verbose(True)
277 f.set_scan(scan, mask)
278 f.set_function(poly=order)
279 sf = f.auto_fit()
280 return sf
Note: See TracBrowser for help on using the repository browser.