source: trunk/python/asapmath.py@ 408

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

add perif arg., to freq_align

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1from scantable import scantable
2from asap import rcParams
3
4def average_time(*args, **kwargs):
5 """
6 Return the (time) average of a scan or list of scans. [in channels only]
7 The cursor of the output scan is set to 0
8 Parameters:
9 one scan or comma separated scans
10 mask: an optional mask (only used for 'var' and 'tsys' weighting)
11 scanav: False (default) averages all scans together,
12 True averages each scan separately
13 weight: Weighting scheme. 'none' (default), 'var' (variance
14 weighted), 'tsys'
15 Example:
16 # return a time averaged scan from scana and scanb
17 # without using a mask
18 scanav = average_time(scana,scanb)
19 # return the (time) averaged scan, i.e. the average of
20 # all correlator cycles
21 scanav = average_time(scan)
22
23 """
24 scanAv = False
25 if kwargs.has_key('scanav'):
26 scanAv = kwargs.get('scanav')
27
28 weight = 'none'
29 if kwargs.has_key('weight'):
30 weight = kwargs.get('weight')
31
32 mask = ()
33 if kwargs.has_key('mask'):
34 mask = kwargs.get('mask')
35
36 lst = tuple(args)
37 from asap._asap import average as _av
38 for s in lst:
39 if not isinstance(s,scantable):
40 print "Please give a list of scantables"
41 return
42 return scantable(_av(lst, mask, scanAv, weight))
43
44def quotient(source, reference, preserve=True):
45 """
46 Return the quotient of a 'source' (signal) scan and a 'reference' scan.
47 The reference can have just one row, even if the signal has many. Otherwise
48 they must have the same number of rows.
49 The cursor of the output scan is set to 0
50 Parameters:
51 source: the 'on' scan
52 reference: the 'off' scan
53 preserve: you can preserve (default) the continuum or
54 remove it. The equations used are
55 preserve - Output = Tref * (sig/ref) - Tref
56 remove - Output = Tref * (sig/ref) - Tsig
57 """
58 from asap._asap import quotient as _quot
59 return scantable(_quot(source, reference, preserve))
60
61def simple_math(left, right, op='add', tsys=True):
62 """
63 Apply simple mathematical binary operations to two
64 scan tables, returning the result in a new scan table.
65 The operation is applied to both the correlations and the TSys data
66 The cursor of the output scan is set to 0
67 Parameters:
68 left: the 'left' scan
69 right: the 'right' scan
70 op: the operation: 'add' (default), 'sub', 'mul', 'div'
71 tsys: if True (default) then apply the operation to Tsys
72 as well as the data
73 """
74 if not isinstance(left,scantable) and not isinstance(right,scantable):
75 print "Please provide two scantables as input"
76 return
77 from asap._asap import b_operate as _bop
78 return scantable(_bop(left, right, op, tsys))
79
80def scale(scan, factor, insitu=None, allaxes=None, tsys=True):
81 """
82 Return a scan where all spectra are scaled by the give 'factor'
83 Parameters:
84 scan: a scantable
85 factor: the scaling factor
86 insitu: if False a new scantable is returned.
87 Otherwise, the scaling is done in-situ
88 The default is taken from .asaprc (False)
89 allaxes: if True apply to all spectra. Otherwise
90 apply only to the selected (beam/pol/if)spectra only.
91 The default is taken from .asaprc (True)
92 tsys: if True (default) then apply the operation to Tsys
93 as well as the data
94 """
95 if allaxes is None: allaxes = rcParams['scantable.allaxes']
96 if insitu is None: insitu = rcParams['insitu']
97 if not insitu:
98 from asap._asap import scale as _scale
99 return scantable(_scale(scan, factor, allaxes, tsys))
100 else:
101 from asap._asap import scale_insitu as _scale
102 _scale(scan, factor, allaxes, tsys)
103 return
104
105
106def add(scan, offset, insitu=None, allaxes=None):
107 """
108 Return a scan where all spectra have the offset added
109 Parameters:
110 scan: a scantable
111 offset: the offset
112 insitu: if False a new scantable is returned.
113 Otherwise, the scaling is done in-situ
114 The default is taken from .asaprc (False)
115 allaxes: if True apply to all spectra. Otherwise
116 apply only to the selected (beam/pol/if)spectra only
117 The default is taken from .asaprc (True)
118 """
119 if allaxes is None: allaxes = rcParams['scantable.allaxes']
120 if insitu is None: insitu = rcParams['insitu']
121 if not insitu:
122 from asap._asap import add as _add
123 return scantable(_add(scan, offset, allaxes))
124 else:
125 from asap._asap import add_insitu as _add
126 _add(scan, offset, allaxes)
127 return
128
129def convert_flux(scan, jyperk=None, eta=None, d=None, insitu=None, allaxes=None):
130 """
131 Return a scan where all spectra are converted to either Jansky or Kelvin
132 depending upon the flux units of the scan table. By default the
133 function tries to look the values up internally. If it can't find
134 them (or if you want to over-ride), you must specify EITHER jyperk
135 OR eta (and D which it will try to look up also if you don't
136 set it). jyperk takes precedence if you set both.
137 Parameters:
138 scan: a scantable
139 jyperk: the Jy / K conversion factor
140 eta: the aperture efficiency
141 d: the geomtric diameter (metres)
142 insitu: if False a new scantable is returned.
143 Otherwise, the scaling is done in-situ
144 The default is taken from .asaprc (False)
145 allaxes: if True apply to all spectra. Otherwise
146 apply only to the selected (beam/pol/if)spectra only
147 The default is taken from .asaprc (True)
148 """
149 if allaxes is None: allaxes = rcParams['scantable.allaxes']
150 if insitu is None: insitu = rcParams['insitu']
151 if jyperk is None: jyperk = -1.0
152 if d is None: d = -1.0
153 if eta is None: eta = -1.0
154 if not insitu:
155 from asap._asap import convertflux as _convert
156 return scantable(_convert(scan, d, eta, jyperk, allaxes))
157 else:
158 from asap._asap import convertflux_insitu as _convert
159 _convert(scan, d, eta, jyperk, allaxes)
160 return
161
162def gain_el(scan, poly=None, filename="", method="linear", insitu=None, allaxes=None):
163 """
164 Return a scan after applying a gain-elevation correction. The correction
165 can be made via either a polynomial or a table-based interpolation
166 (and extrapolation if necessary).
167 You specify polynomial coefficients, an ascii table or neither.
168 If you specify neither, then a polynomial correction will be made
169 with built in coefficients known for certain telescopes (an error will
170 occur if the instrument is not known).
171 Parameters:
172 scan: a scantable
173 poly: Polynomial coefficients (default None) to compute a
174 gain-elevation correction as a function of
175 elevation (in degrees).
176 filename: The name of an ascii file holding correction factors.
177 The first row of the ascii file must give the column
178 names and these MUST include columns
179 "ELEVATION" (degrees) and "FACTOR" (multiply data by this)
180 somewhere.
181 The second row must give the data type of the column. Use
182 'R' for Real and 'I' for Integer. An example file
183 would be:
184
185 TIME ELEVATION FACTOR
186 R R R
187 0.1 0 1.5
188 0.2 20 1.4
189 0.3 40 1.3
190 0.4 60 1.2
191 0.5 80 1.1
192 0.6 90 1.0
193 method: Interpolation method when correcting from a table. Values
194 are "nearest", "linear" (default), "cubic" and "spline"
195 insitu: if False a new scantable is returned.
196 Otherwise, the scaling is done in-situ
197 The default is taken from .asaprc (False)
198 allaxes: if True apply to all spectra. Otherwise
199 apply only to the selected (beam/pol/if) spectra only
200 The default is taken from .asaprc (True)
201 """
202 if allaxes is None: allaxes = rcParams['scantable.allaxes']
203 if poly is None:
204 poly = ()
205 if insitu is None: insitu = rcParams['insitu']
206 if not insitu:
207 from asap._asap import gainel as _gainEl
208 return scantable(_gainEl(scan, poly, filename, method, allaxes))
209 else:
210 from asap._asap import gainel_insitu as _gainEl
211 _gainEl(scan, poly, filename, method, allaxes)
212 return
213
214def freq_align(scan, reftime=None, method='cubic', perif=False, insitu=None):
215 """
216 Return a scan where all rows have been aligned in frequency. The
217 alignment frequency frame (e.g. LSRK) is that set by function
218 set_freqframe.
219 scan: a scantable
220 reftime: reference time to align at. By default, the time of
221 the first row of data is used.
222 method: Interpolation method for regridding the spectra. Choose
223 from "nearest", "linear", "cubic" (default) and "spline"
224 perif: Generate aligners per freqID (no doppler tracking) or
225 per IF (scan-based doppler tracking)
226 insitu: if False a new scantable is returned.
227 Otherwise, the scaling is done in-situ
228 The default is taken from .asaprc (False)
229 """
230 if reftime is None: reftime = ''
231 if insitu is None: insitu = rcParams['insitu']
232 perfreqid = not perif
233 if not insitu:
234 from asap._asap import freq_align as _align
235 return scantable(_align(scan, reftime, method, perfreqid))
236 else:
237 from asap._asap import freq_align_insitu as _align
238 _align(scan, reftime, method, perfreqid)
239 return
240
241def opacity(scan, tau, insitu=None, allaxes=None):
242 """
243 Return a scan after applying an opacity correction.
244 Parameters:
245 scan: a scantable
246 tau: Opacity from which the correction factor is exp(tau*ZD)
247 where ZD is the zenith-distance
248 insitu: if False a new scantable is returned.
249 Otherwise, the scaling is done in-situ
250 The default is taken from .asaprc (False)
251 allaxes: if True apply to all spectra. Otherwise
252 apply only to the selected (beam/pol/if)spectra only
253 The default is taken from .asaprc (True)
254 """
255 if allaxes is None: allaxes = rcParams['scantable.allaxes']
256 if insitu is None: insitu = rcParams['insitu']
257 if not insitu:
258 from asap._asap import opacity as _opacity
259 return scantable(_opacity(scan, tau, allaxes))
260 else:
261 from asap._asap import opacity_insitu as _opacity
262 _opacity(scan, tau, allaxes)
263 return
264
265def bin(scan, width=5, insitu=None):
266 """
267 Return a scan where all spectra have been binned up
268 width: The bin width (default=5) in pixels
269 insitu: if False a new scantable is returned.
270 Otherwise, the scaling is done in-situ
271 The default is taken from .asaprc (False)
272 """
273 if insitu is None: insitu = rcParams['insitu']
274 if not insitu:
275 from asap._asap import bin as _bin
276 return scantable(_bin(scan, width))
277 else:
278 from asap._asap import bin_insitu as _bin
279 _bin(scan, width)
280 return
281
282def resample(scan, width=5, method='cubic', insitu=None):
283 """
284 Return a scan where all spectra have been binned up
285 width: The bin width (default=5) in pixels
286 method: Interpolation method when correcting from a table. Values
287 are "nearest", "linear", "cubic" (default) and "spline"
288 insitu: if False a new scantable is returned.
289 Otherwise, the scaling is done in-situ
290 The default is taken from .asaprc (False)
291 """
292 if insitu is None: insitu = rcParams['insitu']
293 if not insitu:
294 from asap._asap import resample as _resample
295 return scantable(_resample(scan, method, width))
296 else:
297 from asap._asap import resample_insitu as _resample
298 _resample(scan, method, width)
299 return
300
301def average_pol(scan, mask=None, weight='none', insitu=None):
302 """
303 Average the Polarisations together.
304 The polarisation cursor of the output scan is set to 0
305 Parameters:
306 scan: The scantable
307 mask: An optional mask defining the region, where the
308 averaging will be applied. The output will have all
309 specified points masked.
310 weight: Weighting scheme. 'none' (default), or 'var' (variance
311 weighted)
312 insitu: if False a new scantable is returned.
313 Otherwise, the scaling is done in-situ
314 The default is taken from .asaprc (False)
315 Example:
316 polav = average_pols(myscan)
317 """
318 if mask is None:
319 mask = ()
320 if insitu is None: insitu = rcParams['insitu']
321 if not insitu:
322 from asap._asap import averagepol as _avpol
323 return scantable(_avpol(scan, mask, weight))
324 else:
325 from asap._asap import averagepol_insitu as _avpol
326 _avpol(scan, mask, weight)
327 return
328
329def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
330 """
331 Smooth the spectrum by the specified kernel (conserving flux).
332 Parameters:
333 scan: The input scan
334 kernel: The type of smoothing kernel. Select from
335 'hanning' (default), 'gaussian' and 'boxcar'.
336 The first three characters are sufficient.
337 width: The width of the kernel in pixels. For hanning this is
338 ignored otherwise it defauls to 5 pixels.
339 For 'gaussian' it is the Full Width Half
340 Maximum. For 'boxcar' it is the full width.
341 insitu: if False a new scantable is returned.
342 Otherwise, the scaling is done in-situ
343 The default is taken from .asaprc (False)
344 allaxes: If True (default) apply to all spectra. Otherwise
345 apply only to the selected (beam/pol/if)spectra only
346 The default is taken from .asaprc (True)
347 Example:
348 none
349 """
350 if allaxes is None: allaxes = rcParams['scantable.allaxes']
351 if insitu is None: insitu = rcParams['insitu']
352 if not insitu:
353 from asap._asap import smooth as _smooth
354 return scantable(_smooth(scan,kernel,width,allaxes))
355 else:
356 from asap._asap import smooth_insitu as _smooth
357 _smooth(scan,kernel,width,allaxes)
358 return
359
360def poly_baseline(scan, mask=None, order=0, insitu=None):
361 """
362 Return a scan which has been baselined (all rows) by a polynomial.
363 Parameters:
364 scan: a scantable
365 mask: an optional mask
366 order: the order of the polynomial (default is 0)
367 insitu: if False a new scantable is returned.
368 Otherwise, the scaling is done in-situ
369 The default is taken from .asaprc (False)
370 Example:
371 # return a scan baselined by a third order polynomial,
372 # not using a mask
373 bscan = poly_baseline(scan, order=3)
374 """
375 from asap.asapfitter import fitter
376 if mask is None:
377 from numarray import ones
378 mask = tuple(ones(scan.nchan()))
379 f = fitter()
380 f._verbose(True)
381 f.set_scan(scan, mask)
382 f.set_function(poly=order)
383 sf = f.auto_fit(insitu)
384 return sf
Note: See TracBrowser for help on using the repository browser.